home *** CD-ROM | disk | FTP | other *** search
- /*
- * newmemory.c
- *
- * Copyright (C) 1991 Adam Lock
- * All rights reserved.
- *
- * This software may be freely copied, modified, and redistributed
- * provided that this copyright notice is preserved on all copies.
- *
- * You may not distribute this software, in whole or in part, as part of
- * any commercial product without the express consent of the authors.
- *
- * There is no warranty or other guarantee of fitness of this software
- * for any purpose. It is provided solely "as is".
- *
- */
- #include "common.h"
-
- #define MAX_ALLOC_LIST 50000
-
- typedef struct alloc_item {
- char *location ;
- int size ;
- } alloc_item ;
-
- typedef struct alloc_list {
- alloc_item list[MAX_ALLOC_LIST] ;
- int list_length ;
- int total_used ;
- } alloc_list ;
-
- alloc_list *new_alloc_list() ;
- void dispose_alloc_list(alloc_list *list) ;
- voidstar malloc_list(int size, alloc_list *list) ;
- void free_list( voidstar item, alloc_list *list) ;
- void empty_list(alloc_list *list) ;
- void CleanUpMemory() ;
-
- unsigned long TotalAllocated;
- alloc_list *general = NULL;
-
- void CleanUpMemory()
- {
- dispose_alloc_list(general) ;
- general = NULL ;
- }
-
- voidstar
- Malloc(bytes)
- unsigned bytes;
- {
- voidstar res;
-
- TotalAllocated += bytes;
-
- if(general == NULL)
- general = new_alloc_list() ;
- if(general) {
- res = (voidstar)malloc_list(bytes,general);
- if (res == (voidstar)NULL)
- RLerror(RL_PANIC,
- "Out of memory trying to allocate %d bytes.\n",(char *)bytes,0,0);
- return res;
- }
- }
-
- void Free(voidstar memory)
- {
- free_list(memory, general) ;
- }
-
- void dispose_alloc_list(alloc_list *list)
- {
- if(list) {
- empty_list(list) ;
- free(list) ;
- }
- }
-
- void empty_list(alloc_list *list)
- {
- int i ;
-
- if(list && list->list_length) {
- for(i = 0 ; i < list->list_length ; i++) {
- if(list->list[i].location) {
- free(list->list[i].location) ;
- list->list[i].location = NULL ;
- }
- }
- list->list_length = 0 ;
- list->total_used = 0 ;
- }
- }
-
- void free_list(voidstar item, alloc_list *list)
- {
- int i, j ;
-
- if(list && item) {
- i = 0 ;
- while((list->list[i].location != item) && (i < list->list_length)) i++ ;
- if(i < list->list_length) {
- list->total_used -= list->list[i].size ;
- free(list->list[i].location) ;
- list->list[i].location = NULL ;
- for(j = i ; j < list->list_length ; j++)
- list->list[j] = list->list[j+1] ;
- list->list_length-- ;
- }
- }
- }
-
- alloc_list *new_alloc_list()
- {
- alloc_list *l ;
-
- l = malloc(sizeof(alloc_list)) ;
- if(l) {
- l->list_length = 0 ;
- l->total_used = 0 ;
- }
- }
-
- voidstar malloc_list(int size, alloc_list *list)
- {
- voidstar mem ;
-
- if(list && (list->list_length < MAX_ALLOC_LIST)) {
- mem = malloc(size) ;
- if(mem) {
- list->list[list->list_length].location = mem ;
- list->list[list->list_length].size = size ;
- list->list_length++ ;
- list->total_used += size ;
- return mem ;
- }
- }
- return NULL ;
- }
-
- voidstar
- Calloc(nelem, elen)
- unsigned nelem, elen;
- {
- voidstar res;
-
- res = Malloc(nelem*elen);
- bzero(res, (int)nelem*elen);
- return res;
- }
-
- void PrintMemoryStats()
- {
- printf("General memory allocated:\t\t%lu bytes\n",general->total_used);
- printf("Memory slots filled:\t\t%lu\n",general->list_length ) ;
-
- printf("Total memory allocated:\t\t%lu bytes\n",TotalAllocated);
-
- }
-
- /*
- * Allocate space for a string, copy string into space.
- */
- char *
- strsave(s)
- char *s;
- {
- char *tmp;
-
- if (s == (char *)NULL) return (char *)NULL;
- tmp = (char *)Malloc((unsigned)strlen(s) + 1);
- (void)strcpy(tmp, s);
- return tmp;
- }