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