xref: /netbsd-src/sys/dev/raidframe/rf_alloclist.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: rf_alloclist.c,v 1.14 2003/07/01 23:53:48 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.14 2003/07/01 23:53:48 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 void
125 rf_FreeAllocList(l)
126 	RF_AllocListElem_t *l;
127 {
128 	int     i;
129 	RF_AllocListElem_t *temp, *p;
130 
131 	for (p = l; p; p = p->next) {
132 		RF_ASSERT(p->numPointers >= 0 &&
133 			  p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
134 		for (i = 0; i < p->numPointers; i++) {
135 			RF_ASSERT(p->pointers[i]);
136 			RF_Free(p->pointers[i], p->sizes[i]);
137 		}
138 	}
139 	while (l) {
140 		temp = l;
141 		l = l->next;
142 		if (al_free_list_count > RF_AL_FREELIST_MAX) {
143 			DO_FREE(temp, sizeof(*temp));
144 		} else {
145 			temp->next = al_free_list;
146 			al_free_list = temp;
147 			al_free_list_count++;
148 		}
149 	}
150 }
151 
152 RF_AllocListElem_t *
153 rf_real_MakeAllocList()
154 {
155 	RF_AllocListElem_t *p;
156 
157 	if (al_free_list) {
158 		fl_hit_count++;
159 		p = al_free_list;
160 		al_free_list = p->next;
161 		al_free_list_count--;
162 	} else {
163 		fl_miss_count++;
164 		RF_Malloc(p, sizeof(RF_AllocListElem_t),
165 			  (RF_AllocListElem_t *));
166 		/* no allocation locking in kernel, so this is fine */
167 	}
168 	if (p == NULL) {
169 		return (NULL);
170 	}
171 	memset((char *) p, 0, sizeof(RF_AllocListElem_t));
172 	return (p);
173 }
174