home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / newmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-07  |  3.4 KB  |  175 lines

  1. /*
  2.  * newmemory.c
  3.  *
  4.  * Copyright (C) 1991 Adam Lock
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17. #include "common.h"
  18.  
  19. #define MAX_ALLOC_LIST 50000
  20.  
  21. typedef struct alloc_item {
  22.   char *location ;
  23.   int size ;
  24. } alloc_item ;
  25.  
  26. typedef struct alloc_list {
  27.   alloc_item list[MAX_ALLOC_LIST] ;
  28.   int list_length ;
  29.   int total_used ;
  30. } alloc_list ;
  31.  
  32. alloc_list *new_alloc_list() ;
  33. void dispose_alloc_list(alloc_list *list) ;
  34. voidstar malloc_list(int size, alloc_list *list) ;
  35. void free_list( voidstar item, alloc_list *list) ;
  36. void empty_list(alloc_list *list) ;
  37. void CleanUpMemory() ;
  38.  
  39. unsigned long TotalAllocated;
  40. alloc_list *general = NULL;
  41.  
  42. void CleanUpMemory()
  43. {
  44.     dispose_alloc_list(general) ;
  45.     general = NULL ;
  46. }
  47.  
  48. voidstar
  49. Malloc(bytes)
  50. unsigned bytes;
  51. {
  52.   voidstar res;
  53.  
  54.   TotalAllocated += bytes;
  55.  
  56.   if(general == NULL)
  57.     general = new_alloc_list() ;
  58.   if(general) {
  59.     res = (voidstar)malloc_list(bytes,general);
  60.     if (res == (voidstar)NULL)
  61.       RLerror(RL_PANIC,
  62.           "Out of memory trying to allocate %d bytes.\n",(char *)bytes,0,0);
  63.     return res;
  64.   }
  65. }
  66.  
  67. void Free(voidstar memory)
  68. {
  69.     free_list(memory, general) ;
  70. }
  71.  
  72. void dispose_alloc_list(alloc_list *list) 
  73. {
  74.   if(list) {
  75.     empty_list(list) ;
  76.     free(list) ;
  77.   }
  78. }
  79.  
  80. void empty_list(alloc_list *list)
  81. {
  82.   int i ;
  83.  
  84.   if(list && list->list_length) {
  85.     for(i = 0 ; i < list->list_length ; i++) {
  86.         if(list->list[i].location) {
  87.             free(list->list[i].location) ;
  88.             list->list[i].location = NULL ;
  89.         }
  90.     }    
  91.     list->list_length = 0 ;
  92.     list->total_used = 0 ;
  93.   }
  94. }
  95.  
  96. void free_list(voidstar item, alloc_list *list) 
  97. {
  98.   int i, j ;
  99.  
  100.   if(list && item) {
  101.     i = 0 ;
  102.     while((list->list[i].location != item) && (i < list->list_length)) i++ ;
  103.     if(i < list->list_length) {
  104.       list->total_used -= list->list[i].size ;
  105.       free(list->list[i].location) ;
  106.       list->list[i].location = NULL ;
  107.       for(j =  i ; j < list->list_length ; j++) 
  108.         list->list[j] = list->list[j+1] ;
  109.       list->list_length-- ;
  110.     }
  111.   }
  112. }
  113.  
  114. alloc_list *new_alloc_list()
  115. {
  116.   alloc_list *l ;
  117.  
  118.   l = malloc(sizeof(alloc_list)) ;
  119.   if(l) {
  120.     l->list_length = 0 ;
  121.     l->total_used = 0 ;
  122.   }
  123. }
  124.  
  125. voidstar malloc_list(int size, alloc_list *list)
  126. {
  127.   voidstar mem ;
  128.  
  129.   if(list && (list->list_length < MAX_ALLOC_LIST)) {
  130.     mem = malloc(size) ;
  131.     if(mem) {
  132.       list->list[list->list_length].location = mem ;
  133.       list->list[list->list_length].size = size ;
  134.       list->list_length++ ;
  135.       list->total_used += size ;
  136.       return mem ;
  137.     }
  138.   }
  139.   return NULL ;
  140. }
  141.  
  142. voidstar
  143. Calloc(nelem, elen)
  144. unsigned nelem, elen;
  145. {
  146.     voidstar res;
  147.  
  148.     res = Malloc(nelem*elen);
  149.     bzero(res, (int)nelem*elen);
  150.     return res;
  151. }
  152.  
  153. void PrintMemoryStats()
  154. {
  155.     printf("General memory allocated:\t\t%lu bytes\n",general->total_used);
  156.     printf("Memory slots filled:\t\t%lu\n",general->list_length ) ;
  157.  
  158.     printf("Total memory allocated:\t\t%lu bytes\n",TotalAllocated);
  159.  
  160. }
  161.  
  162. /*
  163.  * Allocate space for a string, copy string into space.
  164.  */
  165. char *
  166. strsave(s)
  167. char *s;
  168. {
  169.     char *tmp;
  170.  
  171.     if (s == (char *)NULL) return (char *)NULL;
  172.     tmp = (char *)Malloc((unsigned)strlen(s) + 1);
  173.     (void)strcpy(tmp, s);
  174.     return tmp;
  175. }