Home | History | Annotate | Line # | Download | only in raidframe
rf_alloclist.c revision 1.13
      1 /*	$NetBSD: rf_alloclist.c,v 1.13 2003/07/01 23:50:54 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 /****************************************************************************
     30  *
     31  * Alloclist.c -- code to manipulate allocation lists
     32  *
     33  * an allocation list is just a list of AllocListElem structures.  Each
     34  * such structure contains a fixed-size array of pointers.  Calling
     35  * FreeAList() causes each pointer to be freed.
     36  *
     37  ***************************************************************************/
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: rf_alloclist.c,v 1.13 2003/07/01 23:50:54 oster Exp $");
     41 
     42 #include <dev/raidframe/raidframevar.h>
     43 
     44 #include "rf_options.h"
     45 #include "rf_threadstuff.h"
     46 #include "rf_alloclist.h"
     47 #include "rf_debugMem.h"
     48 #include "rf_etimer.h"
     49 #include "rf_general.h"
     50 #include "rf_shutdown.h"
     51 
     52 static unsigned int fl_hit_count, fl_miss_count;
     53 
     54 static RF_AllocListElem_t *al_free_list = NULL;
     55 static int al_free_list_count;
     56 
     57 #define RF_AL_FREELIST_MAX 256
     58 
     59 #define DO_FREE(_p,_sz) RF_Free((_p),(_sz))
     60 
     61 static void rf_ShutdownAllocList(void *);
     62 
     63 static void rf_ShutdownAllocList(ignored)
     64 	void   *ignored;
     65 {
     66 	RF_AllocListElem_t *p, *pt;
     67 
     68 	for (p = al_free_list; p;) {
     69 		pt = p;
     70 		p = p->next;
     71 		DO_FREE(pt, sizeof(*pt));
     72 	}
     73 	/*
     74         printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu (%lu %%)\n",
     75 	       fl_hit_count, (100*fl_hit_count)/(fl_hit_count+fl_miss_count),
     76 	       fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count));
     77         */
     78 }
     79 
     80 int
     81 rf_ConfigureAllocList(listp)
     82 	RF_ShutdownList_t **listp;
     83 {
     84 	int     rc;
     85 
     86 	al_free_list = NULL;
     87 	fl_hit_count = fl_miss_count = al_free_list_count = 0;
     88 	rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
     89 	if (rc) {
     90 		rf_print_unable_to_add_shutdown( __FILE__, __LINE__, rc);
     91 		return (rc);
     92 	}
     93 	return (0);
     94 }
     95 
     96 
     97 /* we expect the lists to have at most one or two elements, so we're willing
     98  * to search for the end.  If you ever observe the lists growing longer,
     99  * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
    100  */
    101 void
    102 rf_real_AddToAllocList(l, p, size)
    103 	RF_AllocListElem_t *l;
    104 	void   *p;
    105 	int     size;
    106 {
    107 	RF_AllocListElem_t *newelem;
    108 
    109 	for (; l->next; l = l->next)
    110 		RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT);	/* find end of list */
    111 
    112 	RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    113 	if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
    114 		newelem = rf_real_MakeAllocList();
    115 		l->next = newelem;
    116 		l = newelem;
    117 	}
    118 	l->pointers[l->numPointers] = p;
    119 	l->sizes[l->numPointers] = size;
    120 	l->numPointers++;
    121 
    122 }
    123 
    124 
    125 /* we use the debug_mem_mutex here because we need to lock it anyway
    126  * to call free.  this is probably a bug somewhere else in the code,
    127  * but when I call malloc/free outside of any lock I have endless
    128  * trouble with malloc appearing to return the same pointer twice.
    129  * Since we have to lock it anyway, we might as well use it as the
    130  * lock around the al_free_list.  Note that we can't call Free with
    131  * the debug_mem_mutex locked.
    132  */
    133 void
    134 rf_FreeAllocList(l)
    135 	RF_AllocListElem_t *l;
    136 {
    137 	int     i;
    138 	RF_AllocListElem_t *temp, *p;
    139 
    140 	for (p = l; p; p = p->next) {
    141 		RF_ASSERT(p->numPointers >= 0 &&
    142 			  p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    143 		for (i = 0; i < p->numPointers; i++) {
    144 			RF_ASSERT(p->pointers[i]);
    145 			RF_Free(p->pointers[i], p->sizes[i]);
    146 		}
    147 	}
    148 	while (l) {
    149 		temp = l;
    150 		l = l->next;
    151 		if (al_free_list_count > RF_AL_FREELIST_MAX) {
    152 			DO_FREE(temp, sizeof(*temp));
    153 		} else {
    154 			temp->next = al_free_list;
    155 			al_free_list = temp;
    156 			al_free_list_count++;
    157 		}
    158 	}
    159 }
    160 
    161 RF_AllocListElem_t *
    162 rf_real_MakeAllocList()
    163 {
    164 	RF_AllocListElem_t *p;
    165 
    166 	if (al_free_list) {
    167 		fl_hit_count++;
    168 		p = al_free_list;
    169 		al_free_list = p->next;
    170 		al_free_list_count--;
    171 	} else {
    172 		fl_miss_count++;
    173 		RF_Malloc(p, sizeof(RF_AllocListElem_t),
    174 			  (RF_AllocListElem_t *));
    175 		/* no allocation locking in kernel, so this is fine */
    176 	}
    177 	if (p == NULL) {
    178 		return (NULL);
    179 	}
    180 	memset((char *) p, 0, sizeof(RF_AllocListElem_t));
    181 	return (p);
    182 }
    183