system.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * system.c: A grab bag of system-level abstractions
  40. *
  41. * Many authors
  42. */
  43. #include "netsite.h"
  44. #include "prlog.h"
  45. #include "base/ereport.h"
  46. #ifdef XP_WIN32
  47. #include <windows.h>
  48. #endif
  49. #include "base/systems.h" /* find out if we have malloc pools */
  50. static int thread_malloc_key = -1;
  51. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  52. #include "base/pool.h"
  53. #include "base/systhr.h"
  54. #define MALLOC_KEY \
  55. ((pool_handle_t *)(thread_malloc_key != -1 ? systhread_getdata(thread_malloc_key) : NULL))
  56. #endif
  57. #ifdef MCC_DEBUG
  58. #define DEBUG_MALLOC
  59. #endif
  60. #ifdef DEBUG_MALLOC
  61. /* The debug malloc routines provide several functions:
  62. *
  63. * - detect allocated memory overflow/underflow
  64. * - detect multiple frees
  65. * - intentionally clobbers malloc'd buffers
  66. * - intentionally clobbers freed buffers
  67. */
  68. #define DEBUG_MAGIC 0x12345678
  69. #define DEBUG_MARGIN 32
  70. #define DEBUG_MARGIN_CHAR '*'
  71. #define DEBUG_MALLOC_CHAR '.'
  72. #define DEBUG_FREE_CHAR 'X'
  73. #endif /* DEBUG_MALLOC */
  74. NSAPI_PUBLIC void *system_malloc(int size)
  75. {
  76. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  77. return pool_malloc(MALLOC_KEY, size);
  78. #else
  79. return malloc(size);
  80. #endif
  81. }
  82. NSAPI_PUBLIC void *system_calloc(int size)
  83. {
  84. void *ret;
  85. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  86. ret = pool_malloc(MALLOC_KEY, size);
  87. #else
  88. ret = malloc(size);
  89. #endif
  90. if(ret)
  91. ZERO(ret, size);
  92. return ret;
  93. }
  94. NSAPI_PUBLIC void *system_realloc(void *ptr, int size)
  95. {
  96. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  97. return pool_realloc(MALLOC_KEY, ptr, size);
  98. #else
  99. return realloc(ptr, size);
  100. #endif
  101. }
  102. NSAPI_PUBLIC void system_free(void *ptr)
  103. {
  104. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  105. pool_free(MALLOC_KEY, ptr);
  106. #else
  107. PR_ASSERT(ptr);
  108. free(ptr);
  109. #endif
  110. }
  111. NSAPI_PUBLIC char *system_strdup(const char *ptr)
  112. {
  113. PR_ASSERT(ptr);
  114. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  115. return pool_strdup(MALLOC_KEY, ptr);
  116. #else
  117. return strdup(ptr);
  118. #endif
  119. }
  120. NSAPI_PUBLIC void *system_malloc_perm(int size)
  121. {
  122. #ifndef DEBUG_MALLOC
  123. return malloc(size);
  124. #else
  125. char *ptr = (char *)malloc(size + 2*DEBUG_MARGIN+2*sizeof(int));
  126. char *real_ptr;
  127. int *magic;
  128. int *length;
  129. magic = (int *)ptr;
  130. *magic = DEBUG_MAGIC;
  131. ptr += sizeof(int);
  132. length = (int *)ptr;
  133. *length = size;
  134. ptr += sizeof(int);
  135. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  136. ptr += DEBUG_MARGIN;
  137. memset(ptr, DEBUG_MALLOC_CHAR, size);
  138. real_ptr = ptr;
  139. ptr += size;
  140. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  141. return real_ptr;
  142. #endif
  143. }
  144. NSAPI_PUBLIC void *system_calloc_perm(int size)
  145. {
  146. void *ret = system_malloc_perm(size);
  147. if(ret)
  148. ZERO(ret, size);
  149. return ret;
  150. }
  151. NSAPI_PUBLIC void *system_realloc_perm(void *ptr, int size)
  152. {
  153. #ifndef DEBUG_MALLOC
  154. return realloc(ptr, size);
  155. #else
  156. int *magic, *length;
  157. char *cptr;
  158. cptr = (char *)ptr - DEBUG_MARGIN - 2 * sizeof(int);
  159. magic = (int *)cptr;
  160. if (*magic == DEBUG_MAGIC) {
  161. cptr += sizeof(int);
  162. length = (int *)cptr;
  163. if (*length < size) {
  164. char *newptr = (char *)system_malloc_perm(size);
  165. memcpy(newptr, ptr, *length);
  166. system_free_perm(ptr);
  167. return newptr;
  168. }else {
  169. return ptr;
  170. }
  171. } else {
  172. ereport(LOG_WARN, "realloc: attempt to realloc to smaller size");
  173. return realloc(ptr, size);
  174. }
  175. #endif
  176. }
  177. NSAPI_PUBLIC void system_free_perm(void *ptr)
  178. {
  179. #ifdef DEBUG_MALLOC
  180. int *length, *magic;
  181. char *baseptr, *cptr;
  182. int index;
  183. PR_ASSERT(ptr);
  184. cptr = baseptr = ((char *)ptr) - DEBUG_MARGIN - 2*sizeof(int);
  185. magic = (int *)cptr;
  186. if (*magic == DEBUG_MAGIC) {
  187. cptr += sizeof(int);
  188. length = (int *)cptr;
  189. cptr += sizeof(int);
  190. for (index=0; index<DEBUG_MARGIN; index++)
  191. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  192. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  193. break;
  194. }
  195. cptr += DEBUG_MARGIN + *length;
  196. for (index=0; index<DEBUG_MARGIN; index++)
  197. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  198. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  199. break;
  200. }
  201. memset(baseptr, DEBUG_FREE_CHAR, *length + 2*DEBUG_MARGIN+sizeof(int));
  202. } else {
  203. ereport(LOG_CATASTROPHE, "free: freeing unallocated memory");
  204. }
  205. free(baseptr);
  206. #else
  207. free(ptr);
  208. #endif
  209. }
  210. NSAPI_PUBLIC char *system_strdup_perm(const char *ptr)
  211. {
  212. #ifndef DEBUG_MALLOC
  213. PR_ASSERT(ptr);
  214. return strdup(ptr);
  215. #else
  216. int len = strlen(ptr);
  217. char *nptr = (char *)system_malloc_perm(len+1);
  218. memcpy(nptr, ptr, len);
  219. nptr[len] = '\0';
  220. return nptr;
  221. #endif
  222. }
  223. NSAPI_PUBLIC int
  224. getThreadMallocKey(void)
  225. {
  226. return thread_malloc_key;
  227. }
  228. void
  229. setThreadMallocKey(int key)
  230. {
  231. thread_malloc_key = key;
  232. }