system.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  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. static char *version = "Netscape Server/2.0";
  17. #endif
  18. #include "base/systems.h" /* find out if we have malloc pools */
  19. static int thread_malloc_key = -1;
  20. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  21. #include "base/pool.h"
  22. #include "base/systhr.h"
  23. #define MALLOC_KEY \
  24. ((pool_handle_t *)(thread_malloc_key != -1 ? systhread_getdata(thread_malloc_key) : NULL))
  25. #endif
  26. #ifdef MCC_DEBUG
  27. #define DEBUG_MALLOC
  28. #endif
  29. #ifdef DEBUG_MALLOC
  30. /* The debug malloc routines provide several functions:
  31. *
  32. * - detect allocated memory overflow/underflow
  33. * - detect multiple frees
  34. * - intentionally clobbers malloc'd buffers
  35. * - intentionally clobbers freed buffers
  36. */
  37. #define DEBUG_MAGIC 0x12345678
  38. #define DEBUG_MARGIN 32
  39. #define DEBUG_MARGIN_CHAR '*'
  40. #define DEBUG_MALLOC_CHAR '.'
  41. #define DEBUG_FREE_CHAR 'X'
  42. #endif /* DEBUG_MALLOC */
  43. /* On NT, the server version string is not statically encoded based
  44. * upon a product compile define but dynamically set by the server
  45. * exe at startup.
  46. */
  47. NSAPI_PUBLIC char *system_version()
  48. {
  49. #ifdef XP_WIN32
  50. return version;
  51. #else /* XP_UNIX */
  52. return MAGNUS_VERSION_STRING;
  53. #endif /* XP_UNIX */
  54. }
  55. NSAPI_PUBLIC void system_version_set(char *server_version)
  56. {
  57. #ifdef XP_WIN32
  58. version = PERM_STRDUP(server_version);
  59. #endif
  60. }
  61. NSAPI_PUBLIC void *system_malloc(int size)
  62. {
  63. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  64. return pool_malloc(MALLOC_KEY, size);
  65. #else
  66. return malloc(size);
  67. #endif
  68. }
  69. NSAPI_PUBLIC void *system_calloc(int size)
  70. {
  71. void *ret;
  72. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  73. ret = pool_malloc(MALLOC_KEY, size);
  74. #else
  75. ret = malloc(size);
  76. #endif
  77. if(ret)
  78. ZERO(ret, size);
  79. return ret;
  80. }
  81. NSAPI_PUBLIC void *system_realloc(void *ptr, int size)
  82. {
  83. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  84. return pool_realloc(MALLOC_KEY, ptr, size);
  85. #else
  86. return realloc(ptr, size);
  87. #endif
  88. }
  89. NSAPI_PUBLIC void system_free(void *ptr)
  90. {
  91. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  92. pool_free(MALLOC_KEY, ptr);
  93. #else
  94. PR_ASSERT(ptr);
  95. free(ptr);
  96. #endif
  97. }
  98. NSAPI_PUBLIC char *system_strdup(const char *ptr)
  99. {
  100. PR_ASSERT(ptr);
  101. #if defined(MALLOC_POOLS) && defined(MCC_HTTPD) && defined(THREAD_ANY)
  102. return pool_strdup(MALLOC_KEY, ptr);
  103. #else
  104. return strdup(ptr);
  105. #endif
  106. }
  107. NSAPI_PUBLIC void *system_malloc_perm(int size)
  108. {
  109. #ifndef DEBUG_MALLOC
  110. return malloc(size);
  111. #else
  112. char *ptr = (char *)malloc(size + 2*DEBUG_MARGIN+2*sizeof(int));
  113. char *real_ptr;
  114. int *magic;
  115. int *length;
  116. magic = (int *)ptr;
  117. *magic = DEBUG_MAGIC;
  118. ptr += sizeof(int);
  119. length = (int *)ptr;
  120. *length = size;
  121. ptr += sizeof(int);
  122. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  123. ptr += DEBUG_MARGIN;
  124. memset(ptr, DEBUG_MALLOC_CHAR, size);
  125. real_ptr = ptr;
  126. ptr += size;
  127. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  128. return real_ptr;
  129. #endif
  130. }
  131. NSAPI_PUBLIC void *system_calloc_perm(int size)
  132. {
  133. void *ret = system_malloc_perm(size);
  134. if(ret)
  135. ZERO(ret, size);
  136. return ret;
  137. }
  138. NSAPI_PUBLIC void *system_realloc_perm(void *ptr, int size)
  139. {
  140. #ifndef DEBUG_MALLOC
  141. return realloc(ptr, size);
  142. #else
  143. int *magic, *length;
  144. char *baseptr;
  145. char *cptr;
  146. cptr = (char *)ptr - DEBUG_MARGIN - 2 * sizeof(int);
  147. magic = (int *)cptr;
  148. if (*magic == DEBUG_MAGIC) {
  149. cptr += sizeof(int);
  150. length = (int *)cptr;
  151. if (*length < size) {
  152. char *newptr = (char *)system_malloc_perm(size);
  153. memcpy(newptr, ptr, *length);
  154. system_free_perm(ptr);
  155. return newptr;
  156. }else {
  157. return ptr;
  158. }
  159. } else {
  160. ereport(LOG_WARN, "realloc: attempt to realloc to smaller size");
  161. return realloc(ptr, size);
  162. }
  163. #endif
  164. }
  165. NSAPI_PUBLIC void system_free_perm(void *ptr)
  166. {
  167. #ifdef DEBUG_MALLOC
  168. int *length, *magic;
  169. char *baseptr, *cptr;
  170. int index;
  171. PR_ASSERT(ptr);
  172. cptr = baseptr = ((char *)ptr) - DEBUG_MARGIN - 2*sizeof(int);
  173. magic = (int *)cptr;
  174. if (*magic == DEBUG_MAGIC) {
  175. cptr += sizeof(int);
  176. length = (int *)cptr;
  177. cptr += sizeof(int);
  178. for (index=0; index<DEBUG_MARGIN; index++)
  179. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  180. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  181. break;
  182. }
  183. cptr += DEBUG_MARGIN + *length;
  184. for (index=0; index<DEBUG_MARGIN; index++)
  185. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  186. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  187. break;
  188. }
  189. memset(baseptr, DEBUG_FREE_CHAR, *length + 2*DEBUG_MARGIN+sizeof(int));
  190. } else {
  191. ereport(LOG_CATASTROPHE, "free: freeing unallocated memory");
  192. }
  193. free(baseptr);
  194. #else
  195. free(ptr);
  196. #endif
  197. }
  198. NSAPI_PUBLIC char *system_strdup_perm(const char *ptr)
  199. {
  200. #ifndef DEBUG_MALLOC
  201. PR_ASSERT(ptr);
  202. return strdup(ptr);
  203. #else
  204. int len = strlen(ptr);
  205. char *nptr = (char *)system_malloc_perm(len+1);
  206. memcpy(nptr, ptr, len);
  207. nptr[len] = '\0';
  208. return nptr;
  209. #endif
  210. }
  211. NSAPI_PUBLIC int
  212. getThreadMallocKey(void)
  213. {
  214. return thread_malloc_key;
  215. }
  216. void
  217. setThreadMallocKey(int key)
  218. {
  219. thread_malloc_key = key;
  220. }