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