rf_alloclist.c revision 1.15 1 /* $NetBSD: rf_alloclist.c,v 1.15 2003/12/30 21:59:03 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.15 2003/12/30 21:59:03 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(void *ignored)
64 {
65 RF_AllocListElem_t *p, *pt;
66
67 for (p = al_free_list; p;) {
68 pt = p;
69 p = p->next;
70 DO_FREE(pt, sizeof(*pt));
71 }
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(RF_ShutdownList_t **listp)
81 {
82 int rc;
83
84 al_free_list = NULL;
85 fl_hit_count = fl_miss_count = al_free_list_count = 0;
86 rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
87 if (rc) {
88 rf_print_unable_to_add_shutdown( __FILE__, __LINE__, rc);
89 return (rc);
90 }
91 return (0);
92 }
93
94
95 /* we expect the lists to have at most one or two elements, so we're willing
96 * to search for the end. If you ever observe the lists growing longer,
97 * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
98 */
99 void
100 rf_real_AddToAllocList(RF_AllocListElem_t *l, void *p, int size)
101 {
102 RF_AllocListElem_t *newelem;
103
104 for (; l->next; l = l->next)
105 RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT); /* find end of list */
106
107 RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
108 if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
109 newelem = rf_real_MakeAllocList();
110 l->next = newelem;
111 l = newelem;
112 }
113 l->pointers[l->numPointers] = p;
114 l->sizes[l->numPointers] = size;
115 l->numPointers++;
116
117 }
118
119 void
120 rf_FreeAllocList(RF_AllocListElem_t *l)
121 {
122 int i;
123 RF_AllocListElem_t *temp, *p;
124
125 for (p = l; p; p = p->next) {
126 RF_ASSERT(p->numPointers >= 0 &&
127 p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
128 for (i = 0; i < p->numPointers; i++) {
129 RF_ASSERT(p->pointers[i]);
130 RF_Free(p->pointers[i], p->sizes[i]);
131 }
132 }
133 while (l) {
134 temp = l;
135 l = l->next;
136 if (al_free_list_count > RF_AL_FREELIST_MAX) {
137 DO_FREE(temp, sizeof(*temp));
138 } else {
139 temp->next = al_free_list;
140 al_free_list = temp;
141 al_free_list_count++;
142 }
143 }
144 }
145
146 RF_AllocListElem_t *
147 rf_real_MakeAllocList()
148 {
149 RF_AllocListElem_t *p;
150
151 if (al_free_list) {
152 fl_hit_count++;
153 p = al_free_list;
154 al_free_list = p->next;
155 al_free_list_count--;
156 } else {
157 fl_miss_count++;
158 RF_Malloc(p, sizeof(RF_AllocListElem_t),
159 (RF_AllocListElem_t *));
160 /* no allocation locking in kernel, so this is fine */
161 }
162 if (p == NULL) {
163 return (NULL);
164 }
165 memset((char *) p, 0, sizeof(RF_AllocListElem_t));
166 return (p);
167 }
168