system.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * system.c: A grab bag of system-level abstractions
  8. *
  9. * Many authors
  10. */
  11. #include "netsite.h"
  12. #include "prlog.h"
  13. #include "base/ereport.h"
  14. #ifdef XP_WIN32
  15. #include <windows.h>
  16. #endif
  17. #include "base/systems.h" /* find out if we have malloc pools */
  18. static int thread_malloc_key = -1;
  19. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  20. #include "base/pool.h"
  21. #include "base/systhr.h"
  22. #define MALLOC_KEY \
  23. ((pool_handle_t *)(thread_malloc_key != -1 ? systhread_getdata(thread_malloc_key) : NULL))
  24. #endif
  25. #ifdef MCC_DEBUG
  26. #define DEBUG_MALLOC
  27. #endif
  28. #ifdef DEBUG_MALLOC
  29. /* The debug malloc routines provide several functions:
  30. *
  31. * - detect allocated memory overflow/underflow
  32. * - detect multiple frees
  33. * - intentionally clobbers malloc'd buffers
  34. * - intentionally clobbers freed buffers
  35. */
  36. #define DEBUG_MAGIC 0x12345678
  37. #define DEBUG_MARGIN 32
  38. #define DEBUG_MARGIN_CHAR '*'
  39. #define DEBUG_MALLOC_CHAR '.'
  40. #define DEBUG_FREE_CHAR 'X'
  41. #endif /* DEBUG_MALLOC */
  42. NSAPI_PUBLIC void *system_malloc(int size)
  43. {
  44. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  45. return pool_malloc(MALLOC_KEY, size);
  46. #else
  47. return malloc(size);
  48. #endif
  49. }
  50. NSAPI_PUBLIC void *system_calloc(int size)
  51. {
  52. void *ret;
  53. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  54. ret = pool_malloc(MALLOC_KEY, size);
  55. #else
  56. ret = malloc(size);
  57. #endif
  58. if(ret)
  59. ZERO(ret, size);
  60. return ret;
  61. }
  62. NSAPI_PUBLIC void *system_realloc(void *ptr, int size)
  63. {
  64. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  65. return pool_realloc(MALLOC_KEY, ptr, size);
  66. #else
  67. return realloc(ptr, size);
  68. #endif
  69. }
  70. NSAPI_PUBLIC void system_free(void *ptr)
  71. {
  72. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  73. pool_free(MALLOC_KEY, ptr);
  74. #else
  75. PR_ASSERT(ptr);
  76. free(ptr);
  77. #endif
  78. }
  79. NSAPI_PUBLIC char *system_strdup(const char *ptr)
  80. {
  81. PR_ASSERT(ptr);
  82. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  83. return pool_strdup(MALLOC_KEY, ptr);
  84. #else
  85. return strdup(ptr);
  86. #endif
  87. }
  88. NSAPI_PUBLIC void *system_malloc_perm(int size)
  89. {
  90. #ifndef DEBUG_MALLOC
  91. return malloc(size);
  92. #else
  93. char *ptr = (char *)malloc(size + 2*DEBUG_MARGIN+2*sizeof(int));
  94. char *real_ptr;
  95. int *magic;
  96. int *length;
  97. magic = (int *)ptr;
  98. *magic = DEBUG_MAGIC;
  99. ptr += sizeof(int);
  100. length = (int *)ptr;
  101. *length = size;
  102. ptr += sizeof(int);
  103. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  104. ptr += DEBUG_MARGIN;
  105. memset(ptr, DEBUG_MALLOC_CHAR, size);
  106. real_ptr = ptr;
  107. ptr += size;
  108. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  109. return real_ptr;
  110. #endif
  111. }
  112. NSAPI_PUBLIC void *system_calloc_perm(int size)
  113. {
  114. void *ret = system_malloc_perm(size);
  115. if(ret)
  116. ZERO(ret, size);
  117. return ret;
  118. }
  119. NSAPI_PUBLIC void *system_realloc_perm(void *ptr, int size)
  120. {
  121. #ifndef DEBUG_MALLOC
  122. return realloc(ptr, size);
  123. #else
  124. int *magic, *length;
  125. char *cptr;
  126. cptr = (char *)ptr - DEBUG_MARGIN - 2 * sizeof(int);
  127. magic = (int *)cptr;
  128. if (*magic == DEBUG_MAGIC) {
  129. cptr += sizeof(int);
  130. length = (int *)cptr;
  131. if (*length < size) {
  132. char *newptr = (char *)system_malloc_perm(size);
  133. memcpy(newptr, ptr, *length);
  134. system_free_perm(ptr);
  135. return newptr;
  136. }else {
  137. return ptr;
  138. }
  139. } else {
  140. ereport(LOG_WARN, "realloc: attempt to realloc to smaller size");
  141. return realloc(ptr, size);
  142. }
  143. #endif
  144. }
  145. NSAPI_PUBLIC void system_free_perm(void *ptr)
  146. {
  147. #ifdef DEBUG_MALLOC
  148. int *length, *magic;
  149. char *baseptr, *cptr;
  150. int index;
  151. PR_ASSERT(ptr);
  152. cptr = baseptr = ((char *)ptr) - DEBUG_MARGIN - 2*sizeof(int);
  153. magic = (int *)cptr;
  154. if (*magic == DEBUG_MAGIC) {
  155. cptr += sizeof(int);
  156. length = (int *)cptr;
  157. cptr += sizeof(int);
  158. for (index=0; index<DEBUG_MARGIN; index++)
  159. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  160. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  161. break;
  162. }
  163. cptr += DEBUG_MARGIN + *length;
  164. for (index=0; index<DEBUG_MARGIN; index++)
  165. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  166. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  167. break;
  168. }
  169. memset(baseptr, DEBUG_FREE_CHAR, *length + 2*DEBUG_MARGIN+sizeof(int));
  170. } else {
  171. ereport(LOG_CATASTROPHE, "free: freeing unallocated memory");
  172. }
  173. free(baseptr);
  174. #else
  175. free(ptr);
  176. #endif
  177. }
  178. NSAPI_PUBLIC char *system_strdup_perm(const char *ptr)
  179. {
  180. #ifndef DEBUG_MALLOC
  181. PR_ASSERT(ptr);
  182. return strdup(ptr);
  183. #else
  184. int len = strlen(ptr);
  185. char *nptr = (char *)system_malloc_perm(len+1);
  186. memcpy(nptr, ptr, len);
  187. nptr[len] = '\0';
  188. return nptr;
  189. #endif
  190. }
  191. NSAPI_PUBLIC int
  192. getThreadMallocKey(void)
  193. {
  194. return thread_malloc_key;
  195. }
  196. void
  197. setThreadMallocKey(int key)
  198. {
  199. thread_malloc_key = key;
  200. }