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