Home | History | Annotate | Line # | Download | only in raidframe
rf_alloclist.c revision 1.2
      1 /*	$NetBSD: rf_alloclist.c,v 1.2 1999/01/26 02:33:49 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 "rf_types.h"
     40 #include "rf_threadstuff.h"
     41 #include "rf_alloclist.h"
     42 #include "rf_debugMem.h"
     43 #include "rf_etimer.h"
     44 #include "rf_general.h"
     45 #include "rf_shutdown.h"
     46 #include "rf_sys.h"
     47 
     48 RF_DECLARE_STATIC_MUTEX(alist_mutex)
     49 static unsigned int fl_hit_count, fl_miss_count;
     50 
     51 static RF_AllocListElem_t *al_free_list=NULL;
     52 static int al_free_list_count;
     53 
     54 #define RF_AL_FREELIST_MAX 256
     55 
     56 #define DO_FREE(_p,_sz) RF_Free((_p),(_sz))
     57 
     58 static void rf_ShutdownAllocList(void *);
     59 
     60 static void rf_ShutdownAllocList(ignored)
     61   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     DO_FREE(pt, sizeof(*pt));
     69   }
     70   rf_mutex_destroy(&alist_mutex);
     71   /*
     72   printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu (%lu %%)\n",
     73 	 fl_hit_count, (100*fl_hit_count)/(fl_hit_count+fl_miss_count),
     74 	 fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count));
     75   */
     76 }
     77 
     78 int rf_ConfigureAllocList(listp)
     79   RF_ShutdownList_t  **listp;
     80 {
     81   int rc;
     82 
     83   rc = rf_mutex_init(&alist_mutex);
     84   if (rc) {
     85     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
     86       __LINE__, rc);
     87     return(rc);
     88   }
     89   al_free_list = NULL;
     90   fl_hit_count = fl_miss_count = al_free_list_count = 0;
     91   rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
     92   if (rc) {
     93     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
     94       __FILE__, __LINE__, rc);
     95     rf_mutex_destroy(&alist_mutex);
     96     return(rc);
     97   }
     98   return(0);
     99 }
    100 
    101 
    102 /* we expect the lists to have at most one or two elements, so we're willing
    103  * to search for the end.  If you ever observe the lists growing longer,
    104  * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
    105  */
    106 void rf_real_AddToAllocList(l, p, size, lockflag)
    107   RF_AllocListElem_t  *l;
    108   void                *p;
    109   int                  size;
    110   int                  lockflag;
    111 {
    112   RF_AllocListElem_t *newelem;
    113 
    114   for ( ; l->next; l=l->next)
    115 	  RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT);  /* find end of list */
    116 
    117   RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    118   if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
    119     newelem = rf_real_MakeAllocList(lockflag);
    120     l->next = newelem;
    121     l = newelem;
    122   }
    123   l->pointers[ l->numPointers ] = p;
    124   l->sizes   [ l->numPointers ] = size;
    125   l->numPointers++;
    126 
    127 }
    128 
    129 
    130 /* we use the debug_mem_mutex here because we need to lock it anyway to call free.
    131  * this is probably a bug somewhere else in the code, but when I call malloc/free
    132  * outside of any lock I have endless trouble with malloc appearing to return the
    133  * same pointer twice.  Since we have to lock it anyway, we might as well use it
    134  * as the lock around the al_free_list.  Note that we can't call Free with the
    135  * debug_mem_mutex locked.
    136  */
    137 void rf_FreeAllocList(l)
    138   RF_AllocListElem_t  *l;
    139 {
    140   int i;
    141   RF_AllocListElem_t *temp, *p;
    142 
    143   for (p=l; p; p=p->next) {
    144     RF_ASSERT(p->numPointers >= 0 && p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    145     for (i=0; i<p->numPointers; i++) {
    146       RF_ASSERT(p->pointers[i]);
    147       RF_Free(p->pointers[i], p->sizes[i]);
    148     }
    149   }
    150   while (l) {
    151     temp = l;
    152     l = l->next;
    153     if (al_free_list_count > RF_AL_FREELIST_MAX) {DO_FREE(temp, sizeof(*temp));}
    154     else {temp->next = al_free_list; al_free_list = temp; al_free_list_count++;}
    155   }
    156 }
    157 
    158 RF_AllocListElem_t *rf_real_MakeAllocList(lockflag)
    159   int  lockflag;
    160 {
    161   RF_AllocListElem_t *p;
    162 
    163   if (al_free_list) {fl_hit_count++; p = al_free_list; al_free_list = p->next; al_free_list_count--;}
    164   else {
    165     fl_miss_count++;
    166     RF_Malloc(p, sizeof(RF_AllocListElem_t), (RF_AllocListElem_t *));  /* no allocation locking in kernel, so this is fine */
    167   }
    168   if (p == NULL) {
    169     return(NULL);
    170   }
    171   bzero((char *)p, sizeof(RF_AllocListElem_t));
    172   return(p);
    173 }
    174