home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / FED_PLUS.EXE / MEMORY.C < prev    next >
C/C++ Source or Header  |  1994-07-23  |  5KB  |  217 lines

  1. #define MEMORY_C
  2.  
  3. /* mem.c - memory allocator for fixed size blocks */
  4.  
  5. /*
  6.  
  7. This code is replacement for malloc() and free().  It takes advantage
  8. of the fact that all of my memory allocation is of known fixed-size
  9. blocks.  This particular implementation, however, is extremely general
  10. and will do allocation of any number of different fixed-size blocks.
  11.  
  12. I will just give a simple example here.  To allocate struct foo's, declare a handle to the foo space as:
  13.  
  14.     struct freelist_head foo_freelist;
  15.  
  16. Initialize it:
  17.  
  18.     memory_init(&foo_freelist,sizeof(struct foo),1000,200);
  19.  
  20. Here we have asked for an initial allocation of 1000 foo's.  When that
  21. runs out, further allocations will automatically be performed 200
  22. foo's at a time.  (Each allocation calls sbrk() so you want to
  23. minimize them.)
  24.  
  25. To actually allocate and deallocate foo's, it helps to define two
  26. macros as follow:
  27.  
  28.     #define foo_new()    (struct foo *)new(&foo_freelist)
  29.     #define foo_destroy(x)    destroy(&oct_freelist_head,(Freelist *)(char *)x)
  30.  
  31. Now you can say things like:
  32.  
  33.     foo1 = foo_new();
  34.     foo_destroy(foo1);
  35.  
  36. */
  37.  
  38. #ifdef REAL_MALLOC
  39. #include <stdlib.h>
  40. #endif
  41.  
  42. #include <string.h>
  43. #include "error.h"
  44. #include <stdlib.h>
  45. /* just in case we are compiling by hand */
  46. #ifndef ALLOC
  47. #define ALLOC
  48. #endif /*ALLOC*/
  49.  
  50. print_freelist(flh)
  51. struct freelist_head *flh;
  52. {
  53.     Freelist *current;
  54.  
  55.     current = flh->freelist;
  56.     while (current) {
  57.         printf("-> %x",current);
  58.         current = current->next;
  59.     }
  60.     putchar('\n');
  61. }
  62.  
  63. /* chop up big block into linked list of small blocks */
  64. Freelist * /* return 0 for failure */
  65. create_freelist(flh,bytes)
  66. struct freelist_head *flh;    /* freelist head */
  67. int bytes;            /* new memory size */
  68. {
  69.     Freelist *current = (Freelist *)malloc(bytes);
  70.     if (current == 0) return(0);
  71.  
  72.     flh->freelist = current;
  73.  
  74. #ifndef NOSTAT
  75.     flh->create++;
  76.  
  77.     /* set max to point to end of freelist */
  78.     if ((char *)flh->freelist + bytes > (char *)flh->max)
  79.         flh->max = (char *)flh->freelist + bytes;
  80. #endif
  81.  
  82.     while ((char *)current + flh->size <
  83.             ((char *)flh->freelist + bytes)) {
  84.         current->next = (Freelist *)(¤t->memory + flh->size);
  85.         current = current->next;
  86.     }
  87.     current->next = NULL;
  88.     return(current);
  89. }
  90.  
  91. void
  92. _MEMinitialize()
  93. {
  94. #if DEBUG_MALLOC
  95.     malloc_debug(2);
  96. #endif
  97. }
  98.  
  99. void
  100. MEMinitialize(flh,size,alloc1,alloc2)
  101. struct freelist_head *flh;
  102. int size;            /* size of a single element */
  103. int alloc1;            /* number to allocate initially */
  104. int alloc2;            /* number to allocate if we run out */
  105. {
  106.     flh->size_elt = size;    /* kludge for calloc-like behavior */
  107. #ifndef NOSTAT
  108.     flh->alloc = flh->dealloc = flh->create = 0;
  109.     flh->max = 0;
  110. #endif
  111.  
  112.         /* make block large enough to hold the linked list pointer */
  113.     flh->size = (size>sizeof(Freelist *)?size:sizeof(Freelist *));
  114.     /* set up for future allocations */
  115.     flh->bytes = flh->size * alloc2;
  116.  
  117. #ifdef REAL_MALLOC
  118.     return;
  119.     /*NOTREACHED*/
  120. #endif
  121.         if (0 == create_freelist(flh,flh->size*alloc1)) ERRORnospace();
  122.  
  123. #if SPACE_PROFILE
  124.     flh->count = 0;
  125. #endif /*SPACE_PROFILE*/
  126. }
  127.  
  128. Generic
  129. MEM_new(flh)
  130. struct freelist_head *flh;
  131. {
  132.     Generic obj;
  133.  
  134. #ifndef NOSTAT
  135.     flh->alloc++;
  136. #endif
  137.  
  138. #ifdef REAL_MALLOC
  139.     return(calloc(1,flh->size_elt));
  140.     /*NOTREACHED*/
  141. #endif
  142.     if (flh->freelist == NULL && 0 == create_freelist(flh,flh->bytes)) {
  143.         ERRORnospace();
  144.     }
  145.  
  146.     obj = &flh->freelist->memory;
  147.     flh->freelist = flh->freelist->next;
  148.  
  149. #ifndef NOSTAT
  150.     if (obj > flh->max) abort();
  151. #endif
  152.  
  153. #if SPACE_PROFILE
  154.     flh->count++;
  155. #endif /*SPACE_PROFILE*/
  156.  
  157.     /* calloc-like */
  158.     memset(obj,0,flh->size_elt);
  159.  
  160.     return(obj);
  161. }
  162.  
  163. void
  164. MEM_destroy(flh,link)
  165. struct freelist_head *flh;
  166. Freelist *link;
  167. {
  168. #ifndef NOSTAT
  169.     flh->dealloc++;
  170. #endif
  171.  
  172. #ifdef REAL_MALLOC
  173.     free(link);
  174.     return;
  175.     /*NOTREACHED*/
  176. #endif
  177.  
  178.     link->next = flh->freelist;
  179.     flh->freelist = link;
  180.  
  181. #ifdef SPACE_PROFILE
  182.     flh->count--;
  183. #endif /*SPACE_PROFILE*/
  184. }
  185.  
  186. #ifdef ALLOC_MAIN
  187. struct freelist_head oct_freelist;
  188.  
  189. #define new_oct()     (struct oct *)new(&oct_freelist)
  190. #define destroy_oct(oct) (destroy(&oct_freelist,(Freelist *)(char *)oct))
  191.  
  192. struct oct {
  193.     char a[16];
  194. };
  195.  
  196. main()
  197. {
  198.     struct oct *o1,*o2,*o3,*o4,*o5,*o6;
  199.  
  200.     memory_init(&oct_freelist, sizeof(struct oct), 5,2);
  201.  
  202.     o1 = new_oct(); printf("o1 = %x\n",o1);
  203.     o2 = new_oct(); printf("o2 = %x\n",o2);
  204.     o3 = new_oct(); printf("o3 = %x\n",o3);
  205.     o4 = new_oct(); printf("o4 = %x\n",o4);
  206.     o5 = new_oct(); printf("o5 = %x\n",o5);
  207.     o6 = new_oct(); printf("o6 = %x\n",o6);
  208.     destroy_oct(o1);
  209.     destroy_oct(o2);
  210.     o1 = new_oct(); printf("o1 = %x\n",o1);
  211.     o2 = new_oct(); printf("o2 = %x\n",o2);
  212.     o3 = new_oct(); printf("o3 = %x\n",o3);
  213.     o4 = new_oct(); printf("o4 = %x\n",o4);
  214.     o5 = new_oct(); printf("o5 = %x\n",o5);
  215. }
  216. #endif /*ALLOC_MAIN*/
  217.