Home | History | Annotate | Line # | Download | only in raidframe
rf_alloclist.c revision 1.17
      1 /*	$NetBSD: rf_alloclist.c,v 1.17 2004/02/25 02:15:52 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.17 2004/02/25 02:15:52 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 #include <sys/pool.h>
     53 
     54 static struct pool rf_alloclist_pool;
     55 #define RF_AL_FREELIST_MAX 256
     56 
     57 static void rf_ShutdownAllocList(void *);
     58 
     59 static void rf_ShutdownAllocList(void *ignored)
     60 {
     61 	pool_destroy(&rf_alloclist_pool);
     62 }
     63 
     64 int
     65 rf_ConfigureAllocList(RF_ShutdownList_t **listp)
     66 {
     67 	int     rc;
     68 
     69 	pool_init(&rf_alloclist_pool, sizeof(RF_AllocListElem_t),
     70 		  0, 0, 0, "rf_alloclist_pl", NULL);
     71 	pool_sethiwat(&rf_alloclist_pool, RF_AL_FREELIST_MAX);
     72 	pool_prime(&rf_alloclist_pool, 16);  /* need something.. */
     73 
     74 	rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
     75 	if (rc) {
     76 		rf_print_unable_to_add_shutdown( __FILE__, __LINE__, rc);
     77 		return (rc);
     78 	}
     79 
     80 	return (0);
     81 }
     82 
     83 
     84 /* we expect the lists to have at most one or two elements, so we're willing
     85  * to search for the end.  If you ever observe the lists growing longer,
     86  * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
     87  */
     88 void
     89 rf_real_AddToAllocList(RF_AllocListElem_t *l, void *p, int size)
     90 {
     91 	RF_AllocListElem_t *newelem;
     92 
     93 	for (; l->next; l = l->next)
     94 		RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT);	/* find end of list */
     95 
     96 	RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
     97 	if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
     98 		newelem = rf_real_MakeAllocList();
     99 		l->next = newelem;
    100 		l = newelem;
    101 	}
    102 	l->pointers[l->numPointers] = p;
    103 	l->sizes[l->numPointers] = size;
    104 	l->numPointers++;
    105 
    106 }
    107 
    108 void
    109 rf_FreeAllocList(RF_AllocListElem_t *l)
    110 {
    111 	int     i;
    112 	RF_AllocListElem_t *temp, *p;
    113 
    114 	for (p = l; p; p = p->next) {
    115 		RF_ASSERT(p->numPointers >= 0 &&
    116 			  p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    117 		for (i = 0; i < p->numPointers; i++) {
    118 			RF_ASSERT(p->pointers[i]);
    119 			RF_Free(p->pointers[i], p->sizes[i]);
    120 		}
    121 	}
    122 	while (l) {
    123 		temp = l;
    124 		l = l->next;
    125 		pool_put(&rf_alloclist_pool, temp);
    126 	}
    127 }
    128 
    129 RF_AllocListElem_t *
    130 rf_real_MakeAllocList()
    131 {
    132 	RF_AllocListElem_t *p;
    133 
    134 	p = pool_get(&rf_alloclist_pool, PR_WAITOK);
    135 	memset((char *) p, 0, sizeof(RF_AllocListElem_t));
    136 	return (p);
    137 }
    138