system.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * system.c: A grab bag of system-level abstractions
  43. *
  44. * Many authors
  45. */
  46. #include "netsite.h"
  47. #include "prlog.h"
  48. #include "base/ereport.h"
  49. #ifdef XP_WIN32
  50. #include <windows.h>
  51. #endif
  52. #include "base/systems.h" /* find out if we have malloc pools */
  53. static int thread_malloc_key = -1;
  54. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  55. #include "base/pool.h"
  56. #include "base/systhr.h"
  57. #define MALLOC_KEY \
  58. ((pool_handle_t *)(thread_malloc_key != -1 ? systhread_getdata(thread_malloc_key) : NULL))
  59. #endif
  60. #ifdef MCC_DEBUG
  61. #define DEBUG_MALLOC
  62. #endif
  63. #ifdef DEBUG_MALLOC
  64. /* The debug malloc routines provide several functions:
  65. *
  66. * - detect allocated memory overflow/underflow
  67. * - detect multiple frees
  68. * - intentionally clobbers malloc'd buffers
  69. * - intentionally clobbers freed buffers
  70. */
  71. #define DEBUG_MAGIC 0x12345678
  72. #define DEBUG_MARGIN 32
  73. #define DEBUG_MARGIN_CHAR '*'
  74. #define DEBUG_MALLOC_CHAR '.'
  75. #define DEBUG_FREE_CHAR 'X'
  76. #endif /* DEBUG_MALLOC */
  77. NSAPI_PUBLIC void *system_malloc(int size)
  78. {
  79. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  80. return pool_malloc(MALLOC_KEY, size);
  81. #else
  82. return malloc(size);
  83. #endif
  84. }
  85. NSAPI_PUBLIC void *system_calloc(int size)
  86. {
  87. void *ret;
  88. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  89. ret = pool_malloc(MALLOC_KEY, size);
  90. #else
  91. ret = malloc(size);
  92. #endif
  93. if(ret)
  94. ZERO(ret, size);
  95. return ret;
  96. }
  97. NSAPI_PUBLIC void *system_realloc(void *ptr, int size)
  98. {
  99. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  100. return pool_realloc(MALLOC_KEY, ptr, size);
  101. #else
  102. return realloc(ptr, size);
  103. #endif
  104. }
  105. NSAPI_PUBLIC void system_free(void *ptr)
  106. {
  107. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  108. pool_free(MALLOC_KEY, ptr);
  109. #else
  110. PR_ASSERT(ptr);
  111. free(ptr);
  112. #endif
  113. }
  114. NSAPI_PUBLIC char *system_strdup(const char *ptr)
  115. {
  116. PR_ASSERT(ptr);
  117. #if defined(MALLOC_POOLS) && defined(THREAD_ANY)
  118. return pool_strdup(MALLOC_KEY, ptr);
  119. #else
  120. return strdup(ptr);
  121. #endif
  122. }
  123. NSAPI_PUBLIC void *system_malloc_perm(int size)
  124. {
  125. #ifndef DEBUG_MALLOC
  126. return malloc(size);
  127. #else
  128. char *ptr = (char *)malloc(size + 2*DEBUG_MARGIN+2*sizeof(int));
  129. char *real_ptr;
  130. int *magic;
  131. int *length;
  132. magic = (int *)ptr;
  133. *magic = DEBUG_MAGIC;
  134. ptr += sizeof(int);
  135. length = (int *)ptr;
  136. *length = size;
  137. ptr += sizeof(int);
  138. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  139. ptr += DEBUG_MARGIN;
  140. memset(ptr, DEBUG_MALLOC_CHAR, size);
  141. real_ptr = ptr;
  142. ptr += size;
  143. memset(ptr, DEBUG_MARGIN_CHAR, DEBUG_MARGIN);
  144. return real_ptr;
  145. #endif
  146. }
  147. NSAPI_PUBLIC void *system_calloc_perm(int size)
  148. {
  149. void *ret = system_malloc_perm(size);
  150. if(ret)
  151. ZERO(ret, size);
  152. return ret;
  153. }
  154. NSAPI_PUBLIC void *system_realloc_perm(void *ptr, int size)
  155. {
  156. #ifndef DEBUG_MALLOC
  157. return realloc(ptr, size);
  158. #else
  159. int *magic, *length;
  160. char *cptr;
  161. cptr = (char *)ptr - DEBUG_MARGIN - 2 * sizeof(int);
  162. magic = (int *)cptr;
  163. if (*magic == DEBUG_MAGIC) {
  164. cptr += sizeof(int);
  165. length = (int *)cptr;
  166. if (*length < size) {
  167. char *newptr = (char *)system_malloc_perm(size);
  168. memcpy(newptr, ptr, *length);
  169. system_free_perm(ptr);
  170. return newptr;
  171. }else {
  172. return ptr;
  173. }
  174. } else {
  175. ereport(LOG_WARN, "realloc: attempt to realloc to smaller size");
  176. return realloc(ptr, size);
  177. }
  178. #endif
  179. }
  180. NSAPI_PUBLIC void system_free_perm(void *ptr)
  181. {
  182. #ifdef DEBUG_MALLOC
  183. int *length, *magic;
  184. char *baseptr, *cptr;
  185. int index;
  186. PR_ASSERT(ptr);
  187. cptr = baseptr = ((char *)ptr) - DEBUG_MARGIN - 2*sizeof(int);
  188. magic = (int *)cptr;
  189. if (*magic == DEBUG_MAGIC) {
  190. cptr += sizeof(int);
  191. length = (int *)cptr;
  192. cptr += sizeof(int);
  193. for (index=0; index<DEBUG_MARGIN; index++)
  194. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  195. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  196. break;
  197. }
  198. cptr += DEBUG_MARGIN + *length;
  199. for (index=0; index<DEBUG_MARGIN; index++)
  200. if (cptr[index] != DEBUG_MARGIN_CHAR) {
  201. ereport(LOG_CATASTROPHE, "free: corrupt memory (prebounds overwrite)");
  202. break;
  203. }
  204. memset(baseptr, DEBUG_FREE_CHAR, *length + 2*DEBUG_MARGIN+sizeof(int));
  205. } else {
  206. ereport(LOG_CATASTROPHE, "free: freeing unallocated memory");
  207. }
  208. free(baseptr);
  209. #else
  210. free(ptr);
  211. #endif
  212. }
  213. NSAPI_PUBLIC char *system_strdup_perm(const char *ptr)
  214. {
  215. #ifndef DEBUG_MALLOC
  216. PR_ASSERT(ptr);
  217. return strdup(ptr);
  218. #else
  219. int len = strlen(ptr);
  220. char *nptr = (char *)system_malloc_perm(len+1);
  221. memcpy(nptr, ptr, len);
  222. nptr[len] = '\0';
  223. return nptr;
  224. #endif
  225. }
  226. NSAPI_PUBLIC int
  227. getThreadMallocKey(void)
  228. {
  229. return thread_malloc_key;
  230. }
  231. void
  232. setThreadMallocKey(int key)
  233. {
  234. thread_malloc_key = key;
  235. }