Annotation of libwww/Library/src/HTMemory.c, revision 2.10

2.1       frystyk     1: /*                                                                  HTMemory.c
                      2: **     DYNAMIC MEMORY MANAGER
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.10    ! frystyk     6: **     @(#) $Id: HTMemory.c,v 2.9 1998/05/04 19:36:58 frystyk Exp $
2.1       frystyk     7: **
                      8: ** History:
                      9: **      8 Feb 95       Written by Eric and Henrik
                     10: */
                     11: 
                     12: /* Library include files */
2.9       frystyk    13: #include "wwwsys.h"
2.1       frystyk    14: #include "HTUtils.h"
                     15: #include "HTList.h"
2.2       frystyk    16: #include "HTMemory.h"                                   /* Implemented here */
2.1       frystyk    17: 
                     18: PRIVATE HTList * HTMemCall = NULL;                 /* List of memory freers */
2.2       frystyk    19: PRIVATE HTMemory_exitCallback * PExit = NULL;    /* panic and exit function */
2.1       frystyk    20: PRIVATE size_t LastAllocSize = 0;                /* size of last allocation */ 
                     21: 
                     22: /* ------------------------------------------------------------------------- */
                     23: 
                     24: /*     HTMemoryCall_add
                     25: **     ----------------
                     26: **     Register a call back function that is to be called if we need more
                     27: **     memory. Several call back functions can be registered in which case
                     28: **     all of them are called in the order of which they were registered.
                     29: */
                     30: PUBLIC BOOL HTMemoryCall_add (HTMemoryCallback * cbf)
                     31: {
2.10    ! frystyk    32:     HTTRACE(MEM_TRACE, "Mem Add..... Callback %p\n" _ (void *) cbf);
2.1       frystyk    33:     if (!HTMemCall) HTMemCall = HTList_new();
                     34:     return cbf ? HTList_addObject(HTMemCall, (void *) cbf) : NO;
                     35: }
                     36: 
                     37: /*     HTMemoryCall_delete
                     38: **     -------------------
                     39: **     Unregister a call back function from a list
                     40: */
                     41: PUBLIC BOOL HTMemoryCall_delete (HTMemoryCallback * cbf)
                     42: {
2.10    ! frystyk    43:     HTTRACE(MEM_TRACE, "Mem Delete.. Callback %p\n" _ (void *) cbf);
2.1       frystyk    44:     return (HTMemCall && cbf) ? HTList_removeObject(HTMemCall,(void*)cbf) : NO;
                     45: }
                     46: 
                     47: /*     HTMemoryCall_deleteAll
                     48: **     ----------------------
                     49: **     Unregisters all call back functions
                     50: */
                     51: PUBLIC BOOL HTMemoryCall_deleteAll (void)
                     52: {
2.10    ! frystyk    53:     HTTRACE(MEM_TRACE, "Mem Delete.. All Callback functions\n");
2.1       frystyk    54:     if (HTMemCall) {
                     55:        HTList_delete(HTMemCall);
                     56:        HTMemCall = NULL;
                     57:        return YES;
                     58:     }
                     59:     return NO;
                     60: }
                     61: 
                     62: /*
                     63: **     Allocates memory using malloc
                     64: */
                     65: PUBLIC void * HTMemory_malloc (size_t size)
                     66: {
                     67:     void * ptr;
                     68:     ptr = malloc(LastAllocSize = size);
                     69:     if (ptr != NULL) return ptr;
                     70:     if (HTMemCall) {
                     71:        HTMemoryCallback * pres;
                     72:        while ((pres = (HTMemoryCallback *) HTList_nextObject(HTMemCall))) {
2.10    ! frystyk    73:            HTTRACE(MEM_TRACE, "Mem Calling. %p (size %d)\n" _ (void*)pres _ size);
2.1       frystyk    74:            (*pres)(size);
                     75:            if ((ptr = malloc(size)) != NULL) return ptr;
                     76:        }
                     77:     }
2.10    ! frystyk    78:     HTTRACE(MEM_TRACE, "Memory.... Couldn't allocate %d bytes\n" _ size);
2.1       frystyk    79:     return NULL;
                     80: }
                     81: 
                     82: /*
                     83: **     Allocates memory using calloc
                     84: */
                     85: PUBLIC void * HTMemory_calloc (size_t nobj, size_t size)
                     86: {
                     87:     void * ptr;
                     88:     ptr = calloc(nobj, LastAllocSize = size);
                     89:     if (ptr != NULL) return ptr;
                     90:     if (HTMemCall) {
                     91:        HTMemoryCallback * pres;
                     92:        size_t total = size * nobj;
                     93:        while ((pres = (HTMemoryCallback *) HTList_nextObject(HTMemCall))) {
2.10    ! frystyk    94:            HTTRACE(MEM_TRACE, "Mem Calling. %p (size %d)\n" _ 
        !            95:                                   (void *) pres _ total);
2.1       frystyk    96:            (*pres)(total);
                     97:            if ((ptr = calloc(nobj, size)) != NULL) return ptr;
                     98:        }
                     99:     }
2.10    ! frystyk   100:     HTTRACE(MEM_TRACE, "Memory...... Couldn't allocate %d objects of size %d\n" _ 
        !           101:                 nobj _ size);
2.1       frystyk   102:     return NULL;
                    103: }
                    104: 
                    105: /*
                    106: **     Reallocates memory using realloc
                    107: */
                    108: PUBLIC void * HTMemory_realloc (void * p, size_t size)
                    109: {
                    110:     void * ptr;
                    111:     ptr = realloc(p, LastAllocSize = size);
                    112:     if (ptr != NULL) return ptr;
                    113:     if (HTMemCall) {
                    114:        HTMemoryCallback * pres;
                    115:        while ((pres = (HTMemoryCallback *) HTList_nextObject(HTMemCall))) {
2.10    ! frystyk   116:            HTTRACE(MEM_TRACE, "Mem Calling. %p (size %d)\n" _ (void*)pres _ size);
2.1       frystyk   117:            (*pres)(size);
                    118:            if ((ptr = realloc(p, size)) != NULL) return ptr;
                    119:        }
                    120:     }
2.10    ! frystyk   121:     HTTRACE(MEM_TRACE, "Memory...... Couldn't reallocate %d bytes\n" _ size);
2.1       frystyk   122:     return NULL;
                    123: }
                    124: 
                    125: /*
                    126: **     Frees memory
                    127: */
                    128: PUBLIC void HTMemory_free (void * ptr)
                    129: {
                    130:     if (ptr) {
2.10    ! frystyk   131:        HTTRACE(MEM_TRACE, "Memory Free. %p\n" _ ptr);
2.1       frystyk   132:        free(ptr);
                    133:     }
                    134: }
                    135: 
                    136: /*     HTMemory_setExit
                    137: **     ----------------
                    138: **     Register the memory exit function. This function notifies the user that
                    139: **     it is all over. If this function returns or is undefined, 
2.8       frystyk   140: **     HTMemory_outofmem calls abort.
2.1       frystyk   141: */
2.2       frystyk   142: PUBLIC void HTMemory_setExit (HTMemory_exitCallback * pExit)
2.1       frystyk   143: {
                    144:     PExit = pExit;
                    145: }
                    146: 
                    147: /*     HTMemory_exit
                    148: **     -------------
                    149: **     Get the current exit function.
                    150: */
2.2       frystyk   151: PUBLIC HTMemory_exitCallback * HTMemory_exit (void)
2.1       frystyk   152: {
                    153:     return PExit;
                    154: }
                    155: 
                    156: /*     HTMemory_outofmem
                    157: **     -----------------
                    158: **     Call app defined exit function. If that returns, exit anyway.
                    159: */
                    160: PUBLIC void HTMemory_outofmem (char * name, char * file, unsigned long line)
                    161: {
                    162:     if (PExit)
                    163:        (*PExit)(name, file, line);
2.10    ! frystyk   164:     HTTRACE(ALL_TRACE, "%s:%ld failed allocation for \"%s\" (%ld bytes).\nProgram aborted.\n" _
        !           165:            file _ line _ name _ LastAllocSize);
2.8       frystyk   166:     abort();
2.1       frystyk   167: }
                    168: 

Webmaster