Home | History | Annotate | Line # | Download | only in raidframe
rf_alloclist.c revision 1.2
      1  1.2  oster /*	$NetBSD: rf_alloclist.c,v 1.2 1999/01/26 02:33:49 oster Exp $	*/
      2  1.1  oster /*
      3  1.1  oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4  1.1  oster  * All rights reserved.
      5  1.1  oster  *
      6  1.1  oster  * Author: Mark Holland
      7  1.1  oster  *
      8  1.1  oster  * Permission to use, copy, modify and distribute this software and
      9  1.1  oster  * its documentation is hereby granted, provided that both the copyright
     10  1.1  oster  * notice and this permission notice appear in all copies of the
     11  1.1  oster  * software, derivative works or modified versions, and any portions
     12  1.1  oster  * thereof, and that both notices appear in supporting documentation.
     13  1.1  oster  *
     14  1.1  oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.1  oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  1.1  oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.1  oster  *
     18  1.1  oster  * Carnegie Mellon requests users of this software to return to
     19  1.1  oster  *
     20  1.1  oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.1  oster  *  School of Computer Science
     22  1.1  oster  *  Carnegie Mellon University
     23  1.1  oster  *  Pittsburgh PA 15213-3890
     24  1.1  oster  *
     25  1.1  oster  * any improvements or extensions that they make and grant Carnegie the
     26  1.1  oster  * rights to redistribute these changes.
     27  1.1  oster  */
     28  1.1  oster 
     29  1.1  oster /****************************************************************************
     30  1.1  oster  *
     31  1.1  oster  * Alloclist.c -- code to manipulate allocation lists
     32  1.1  oster  *
     33  1.1  oster  * an allocation list is just a list of AllocListElem structures.  Each
     34  1.1  oster  * such structure contains a fixed-size array of pointers.  Calling
     35  1.1  oster  * FreeAList() causes each pointer to be freed.
     36  1.1  oster  *
     37  1.1  oster  ***************************************************************************/
     38  1.1  oster 
     39  1.1  oster #include "rf_types.h"
     40  1.1  oster #include "rf_threadstuff.h"
     41  1.1  oster #include "rf_alloclist.h"
     42  1.1  oster #include "rf_debugMem.h"
     43  1.1  oster #include "rf_etimer.h"
     44  1.1  oster #include "rf_general.h"
     45  1.1  oster #include "rf_shutdown.h"
     46  1.1  oster #include "rf_sys.h"
     47  1.1  oster 
     48  1.1  oster RF_DECLARE_STATIC_MUTEX(alist_mutex)
     49  1.1  oster static unsigned int fl_hit_count, fl_miss_count;
     50  1.1  oster 
     51  1.1  oster static RF_AllocListElem_t *al_free_list=NULL;
     52  1.1  oster static int al_free_list_count;
     53  1.1  oster 
     54  1.1  oster #define RF_AL_FREELIST_MAX 256
     55  1.1  oster 
     56  1.1  oster #define DO_FREE(_p,_sz) RF_Free((_p),(_sz))
     57  1.1  oster 
     58  1.1  oster static void rf_ShutdownAllocList(void *);
     59  1.1  oster 
     60  1.1  oster static void rf_ShutdownAllocList(ignored)
     61  1.1  oster   void  *ignored;
     62  1.1  oster {
     63  1.1  oster   RF_AllocListElem_t *p, *pt;
     64  1.1  oster 
     65  1.1  oster   for (p = al_free_list; p; ) {
     66  1.1  oster     pt = p;
     67  1.1  oster     p = p->next;
     68  1.1  oster     DO_FREE(pt, sizeof(*pt));
     69  1.1  oster   }
     70  1.1  oster   rf_mutex_destroy(&alist_mutex);
     71  1.1  oster   /*
     72  1.1  oster   printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu (%lu %%)\n",
     73  1.1  oster 	 fl_hit_count, (100*fl_hit_count)/(fl_hit_count+fl_miss_count),
     74  1.1  oster 	 fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count));
     75  1.1  oster   */
     76  1.1  oster }
     77  1.1  oster 
     78  1.1  oster int rf_ConfigureAllocList(listp)
     79  1.1  oster   RF_ShutdownList_t  **listp;
     80  1.1  oster {
     81  1.1  oster   int rc;
     82  1.1  oster 
     83  1.1  oster   rc = rf_mutex_init(&alist_mutex);
     84  1.1  oster   if (rc) {
     85  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
     86  1.1  oster       __LINE__, rc);
     87  1.1  oster     return(rc);
     88  1.1  oster   }
     89  1.1  oster   al_free_list = NULL;
     90  1.1  oster   fl_hit_count = fl_miss_count = al_free_list_count = 0;
     91  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
     92  1.1  oster   if (rc) {
     93  1.1  oster     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
     94  1.1  oster       __FILE__, __LINE__, rc);
     95  1.1  oster     rf_mutex_destroy(&alist_mutex);
     96  1.1  oster     return(rc);
     97  1.1  oster   }
     98  1.1  oster   return(0);
     99  1.1  oster }
    100  1.1  oster 
    101  1.1  oster 
    102  1.1  oster /* we expect the lists to have at most one or two elements, so we're willing
    103  1.1  oster  * to search for the end.  If you ever observe the lists growing longer,
    104  1.1  oster  * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
    105  1.1  oster  */
    106  1.1  oster void rf_real_AddToAllocList(l, p, size, lockflag)
    107  1.1  oster   RF_AllocListElem_t  *l;
    108  1.1  oster   void                *p;
    109  1.1  oster   int                  size;
    110  1.1  oster   int                  lockflag;
    111  1.1  oster {
    112  1.1  oster   RF_AllocListElem_t *newelem;
    113  1.1  oster 
    114  1.1  oster   for ( ; l->next; l=l->next)
    115  1.1  oster 	  RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT);  /* find end of list */
    116  1.1  oster 
    117  1.1  oster   RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    118  1.1  oster   if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
    119  1.1  oster     newelem = rf_real_MakeAllocList(lockflag);
    120  1.1  oster     l->next = newelem;
    121  1.1  oster     l = newelem;
    122  1.1  oster   }
    123  1.1  oster   l->pointers[ l->numPointers ] = p;
    124  1.1  oster   l->sizes   [ l->numPointers ] = size;
    125  1.1  oster   l->numPointers++;
    126  1.1  oster 
    127  1.1  oster }
    128  1.1  oster 
    129  1.1  oster 
    130  1.1  oster /* we use the debug_mem_mutex here because we need to lock it anyway to call free.
    131  1.1  oster  * this is probably a bug somewhere else in the code, but when I call malloc/free
    132  1.1  oster  * outside of any lock I have endless trouble with malloc appearing to return the
    133  1.1  oster  * same pointer twice.  Since we have to lock it anyway, we might as well use it
    134  1.1  oster  * as the lock around the al_free_list.  Note that we can't call Free with the
    135  1.1  oster  * debug_mem_mutex locked.
    136  1.1  oster  */
    137  1.1  oster void rf_FreeAllocList(l)
    138  1.1  oster   RF_AllocListElem_t  *l;
    139  1.1  oster {
    140  1.1  oster   int i;
    141  1.1  oster   RF_AllocListElem_t *temp, *p;
    142  1.1  oster 
    143  1.1  oster   for (p=l; p; p=p->next) {
    144  1.1  oster     RF_ASSERT(p->numPointers >= 0 && p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    145  1.1  oster     for (i=0; i<p->numPointers; i++) {
    146  1.1  oster       RF_ASSERT(p->pointers[i]);
    147  1.1  oster       RF_Free(p->pointers[i], p->sizes[i]);
    148  1.1  oster     }
    149  1.1  oster   }
    150  1.1  oster   while (l) {
    151  1.1  oster     temp = l;
    152  1.1  oster     l = l->next;
    153  1.1  oster     if (al_free_list_count > RF_AL_FREELIST_MAX) {DO_FREE(temp, sizeof(*temp));}
    154  1.1  oster     else {temp->next = al_free_list; al_free_list = temp; al_free_list_count++;}
    155  1.1  oster   }
    156  1.1  oster }
    157  1.1  oster 
    158  1.1  oster RF_AllocListElem_t *rf_real_MakeAllocList(lockflag)
    159  1.1  oster   int  lockflag;
    160  1.1  oster {
    161  1.1  oster   RF_AllocListElem_t *p;
    162  1.1  oster 
    163  1.1  oster   if (al_free_list) {fl_hit_count++; p = al_free_list; al_free_list = p->next; al_free_list_count--;}
    164  1.1  oster   else {
    165  1.1  oster     fl_miss_count++;
    166  1.1  oster     RF_Malloc(p, sizeof(RF_AllocListElem_t), (RF_AllocListElem_t *));  /* no allocation locking in kernel, so this is fine */
    167  1.1  oster   }
    168  1.1  oster   if (p == NULL) {
    169  1.1  oster     return(NULL);
    170  1.1  oster   }
    171  1.1  oster   bzero((char *)p, sizeof(RF_AllocListElem_t));
    172  1.1  oster   return(p);
    173  1.1  oster }
    174