HTMemory.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:4k
源码类别:

浏览器

开发平台:

Unix_Linux

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