system.cpp 5.2 KB

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