1 /* $NetBSD: rf_alloclist.c,v 1.10 2002/09/14 17:53:57 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@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.10 2002/09/14 17:53:57 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 RF_DECLARE_STATIC_MUTEX(alist_mutex) 53 static unsigned int fl_hit_count, fl_miss_count; 54 55 static RF_AllocListElem_t *al_free_list = NULL; 56 static int al_free_list_count; 57 58 #define RF_AL_FREELIST_MAX 256 59 60 #define DO_FREE(_p,_sz) RF_Free((_p),(_sz)) 61 62 static void rf_ShutdownAllocList(void *); 63 64 static void rf_ShutdownAllocList(ignored) 65 void *ignored; 66 { 67 RF_AllocListElem_t *p, *pt; 68 69 for (p = al_free_list; p;) { 70 pt = p; 71 p = p->next; 72 DO_FREE(pt, sizeof(*pt)); 73 } 74 rf_mutex_destroy(&alist_mutex); 75 /* 76 printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu (%lu %%)\n", 77 fl_hit_count, (100*fl_hit_count)/(fl_hit_count+fl_miss_count), 78 fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count)); 79 */ 80 } 81 82 int 83 rf_ConfigureAllocList(listp) 84 RF_ShutdownList_t **listp; 85 { 86 int rc; 87 88 rc = rf_mutex_init(&alist_mutex); 89 if (rc) { 90 rf_print_unable_to_init_mutex( __FILE__, __LINE__, rc); 91 return (rc); 92 } 93 al_free_list = NULL; 94 fl_hit_count = fl_miss_count = al_free_list_count = 0; 95 rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL); 96 if (rc) { 97 rf_print_unable_to_add_shutdown( __FILE__, __LINE__, rc); 98 rf_mutex_destroy(&alist_mutex); 99 return (rc); 100 } 101 return (0); 102 } 103 104 105 /* we expect the lists to have at most one or two elements, so we're willing 106 * to search for the end. If you ever observe the lists growing longer, 107 * increase POINTERS_PER_ALLOC_LIST_ELEMENT. 108 */ 109 void 110 rf_real_AddToAllocList(l, p, size, lockflag) 111 RF_AllocListElem_t *l; 112 void *p; 113 int size; 114 int lockflag; 115 { 116 RF_AllocListElem_t *newelem; 117 118 for (; l->next; l = l->next) 119 RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT); /* find end of list */ 120 121 RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT); 122 if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) { 123 newelem = rf_real_MakeAllocList(lockflag); 124 l->next = newelem; 125 l = newelem; 126 } 127 l->pointers[l->numPointers] = p; 128 l->sizes[l->numPointers] = size; 129 l->numPointers++; 130 131 } 132 133 134 /* we use the debug_mem_mutex here because we need to lock it anyway to call free. 135 * this is probably a bug somewhere else in the code, but when I call malloc/free 136 * outside of any lock I have endless trouble with malloc appearing to return the 137 * same pointer twice. Since we have to lock it anyway, we might as well use it 138 * as the lock around the al_free_list. Note that we can't call Free with the 139 * debug_mem_mutex locked. 140 */ 141 void 142 rf_FreeAllocList(l) 143 RF_AllocListElem_t *l; 144 { 145 int i; 146 RF_AllocListElem_t *temp, *p; 147 148 for (p = l; p; p = p->next) { 149 RF_ASSERT(p->numPointers >= 0 && p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT); 150 for (i = 0; i < p->numPointers; i++) { 151 RF_ASSERT(p->pointers[i]); 152 RF_Free(p->pointers[i], p->sizes[i]); 153 } 154 } 155 while (l) { 156 temp = l; 157 l = l->next; 158 if (al_free_list_count > RF_AL_FREELIST_MAX) { 159 DO_FREE(temp, sizeof(*temp)); 160 } else { 161 temp->next = al_free_list; 162 al_free_list = temp; 163 al_free_list_count++; 164 } 165 } 166 } 167 168 RF_AllocListElem_t * 169 rf_real_MakeAllocList(lockflag) 170 int lockflag; 171 { 172 RF_AllocListElem_t *p; 173 174 if (al_free_list) { 175 fl_hit_count++; 176 p = al_free_list; 177 al_free_list = p->next; 178 al_free_list_count--; 179 } else { 180 fl_miss_count++; 181 RF_Malloc(p, sizeof(RF_AllocListElem_t), (RF_AllocListElem_t *)); /* no allocation locking 182 * in kernel, so this is 183 * fine */ 184 } 185 if (p == NULL) { 186 return (NULL); 187 } 188 memset((char *) p, 0, sizeof(RF_AllocListElem_t)); 189 return (p); 190 } 191