ch_malloc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /* slapi_ch_malloc.c - malloc routines that test returns from malloc and friends */
  13. #include <errno.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h> /* strdup */
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #undef DEBUG /* disable counters */
  20. #include <prcountr.h>
  21. #include "slap.h"
  22. static int counters_created= 0;
  23. PR_DEFINE_COUNTER(slapi_ch_counter_malloc);
  24. PR_DEFINE_COUNTER(slapi_ch_counter_calloc);
  25. PR_DEFINE_COUNTER(slapi_ch_counter_realloc);
  26. PR_DEFINE_COUNTER(slapi_ch_counter_strdup);
  27. PR_DEFINE_COUNTER(slapi_ch_counter_free);
  28. PR_DEFINE_COUNTER(slapi_ch_counter_created);
  29. PR_DEFINE_COUNTER(slapi_ch_counter_exist);
  30. #define OOM_PREALLOC_SIZE 65536
  31. static void *oom_emergency_area = NULL;
  32. static PRLock *oom_emergency_lock = NULL;
  33. #define SLAPD_MODULE "memory allocator"
  34. static const char* const oom_advice =
  35. "\nThe server has probably allocated all available virtual memory. To solve\n"
  36. "this problem, make more virtual memory available to your server, or reduce\n"
  37. "one or more of the following server configuration settings:\n"
  38. " nsslapd-cachesize (Database Settings - Maximum entries in cache)\n"
  39. " nsslapd-cachememsize (Database Settings - Memory available for cache)\n"
  40. " nsslapd-dbcachesize (LDBM Plug-in Settings - Maximum cache size)\n"
  41. " nsslapd-import-cachesize (LDBM Plug-in Settings - Import cache size).\n"
  42. "Can't recover; calling exit(1).\n";
  43. static void
  44. create_counters()
  45. {
  46. PR_CREATE_COUNTER(slapi_ch_counter_malloc,"slapi_ch","malloc","");
  47. PR_CREATE_COUNTER(slapi_ch_counter_calloc,"slapi_ch","calloc","");
  48. PR_CREATE_COUNTER(slapi_ch_counter_realloc,"slapi_ch","realloc","");
  49. PR_CREATE_COUNTER(slapi_ch_counter_strdup,"slapi_ch","strdup","");
  50. PR_CREATE_COUNTER(slapi_ch_counter_free,"slapi_ch","free","");
  51. PR_CREATE_COUNTER(slapi_ch_counter_created,"slapi_ch","created","");
  52. PR_CREATE_COUNTER(slapi_ch_counter_exist,"slapi_ch","exist","");
  53. /* ensure that we have space to allow for shutdown calls to malloc()
  54. * from should we run out of memory.
  55. */
  56. if (oom_emergency_area == NULL) {
  57. oom_emergency_area = malloc(OOM_PREALLOC_SIZE);
  58. }
  59. oom_emergency_lock = PR_NewLock();
  60. }
  61. /* called when we have just detected an out of memory condition, before
  62. * we make any other library calls. Note that LDAPDebug() calls malloc,
  63. * indirectly. By making 64KB free, we should be able to have a few
  64. * mallocs' succeed before we shut down.
  65. */
  66. void oom_occurred(void)
  67. {
  68. int tmp_errno = errno; /* callers will need the error from malloc */
  69. if (oom_emergency_lock == NULL) return;
  70. PR_Lock(oom_emergency_lock);
  71. if (oom_emergency_area) {
  72. free(oom_emergency_area);
  73. oom_emergency_area = NULL;
  74. }
  75. PR_Unlock(oom_emergency_lock);
  76. errno = tmp_errno;
  77. }
  78. static void
  79. log_negative_alloc_msg( const char *op, const char *units, unsigned long size )
  80. {
  81. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  82. "cannot %s %lu %s;\n"
  83. "trying to allocate 0 or a negative number of %s is not portable and\n"
  84. "gives different results on different platforms.\n",
  85. op, size, units, units );
  86. }
  87. #if !defined(MEMPOOL_EXPERIMENTAL)
  88. char *
  89. slapi_ch_malloc(
  90. unsigned long size
  91. )
  92. {
  93. char *newmem;
  94. if (size <= 0) {
  95. log_negative_alloc_msg( "malloc", "bytes", size );
  96. return 0;
  97. }
  98. if ( (newmem = (char *) malloc( size )) == NULL ) {
  99. int oserr = errno;
  100. oom_occurred();
  101. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  102. "malloc of %lu bytes failed; OS error %d (%s)%s\n",
  103. size, oserr, slapd_system_strerror( oserr ), oom_advice );
  104. exit( 1 );
  105. }
  106. if(!counters_created)
  107. {
  108. create_counters();
  109. counters_created= 1;
  110. }
  111. PR_INCREMENT_COUNTER(slapi_ch_counter_malloc);
  112. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  113. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  114. return( newmem );
  115. }
  116. char *
  117. slapi_ch_realloc(
  118. char *block,
  119. unsigned long size
  120. )
  121. {
  122. char *newmem;
  123. if ( block == NULL ) {
  124. return( slapi_ch_malloc( size ) );
  125. }
  126. if (size <= 0) {
  127. log_negative_alloc_msg( "realloc", "bytes", size );
  128. return block;
  129. }
  130. if ( (newmem = (char *) realloc( block, size )) == NULL ) {
  131. int oserr = errno;
  132. oom_occurred();
  133. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  134. "realloc of %lu bytes failed; OS error %d (%s)%s\n",
  135. size, oserr, slapd_system_strerror( oserr ), oom_advice );
  136. exit( 1 );
  137. }
  138. if(!counters_created)
  139. {
  140. create_counters();
  141. counters_created= 1;
  142. }
  143. PR_INCREMENT_COUNTER(slapi_ch_counter_realloc);
  144. return( newmem );
  145. }
  146. char *
  147. slapi_ch_calloc(
  148. unsigned long nelem,
  149. unsigned long size
  150. )
  151. {
  152. char *newmem;
  153. if (size <= 0) {
  154. log_negative_alloc_msg( "calloc", "bytes", size );
  155. return 0;
  156. }
  157. if (nelem <= 0) {
  158. log_negative_alloc_msg( "calloc", "elements", nelem );
  159. return 0;
  160. }
  161. if ( (newmem = (char *) calloc( nelem, size )) == NULL ) {
  162. int oserr = errno;
  163. oom_occurred();
  164. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  165. "calloc of %lu elems of %lu bytes failed; OS error %d (%s)%s\n",
  166. nelem, size, oserr, slapd_system_strerror( oserr ), oom_advice );
  167. exit( 1 );
  168. }
  169. if(!counters_created)
  170. {
  171. create_counters();
  172. counters_created= 1;
  173. }
  174. PR_INCREMENT_COUNTER(slapi_ch_counter_calloc);
  175. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  176. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  177. return( newmem );
  178. }
  179. char*
  180. slapi_ch_strdup ( const char* s1)
  181. {
  182. char* newmem;
  183. /* strdup pukes on NULL strings...bail out now */
  184. if(NULL == s1)
  185. return NULL;
  186. newmem = strdup (s1);
  187. if (newmem == NULL) {
  188. int oserr = errno;
  189. oom_occurred();
  190. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  191. "strdup of %lu characters failed; OS error %d (%s)%s\n",
  192. (unsigned long)strlen(s1), oserr, slapd_system_strerror( oserr ),
  193. oom_advice );
  194. exit (1);
  195. }
  196. if(!counters_created)
  197. {
  198. create_counters();
  199. counters_created= 1;
  200. }
  201. PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
  202. PR_INCREMENT_COUNTER(slapi_ch_counter_created);
  203. PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
  204. return newmem;
  205. }
  206. #endif /* !MEMPOOL_EXPERIMENTAL */
  207. struct berval*
  208. slapi_ch_bvdup (const struct berval* v)
  209. {
  210. struct berval* newberval = ber_bvdup ((struct berval *)v);
  211. if (newberval == NULL) {
  212. int oserr = errno;
  213. oom_occurred();
  214. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
  215. "ber_bvdup of %lu bytes failed; OS error %d (%s)%s\n",
  216. (unsigned long)v->bv_len, oserr, slapd_system_strerror( oserr ),
  217. oom_advice );
  218. exit( 1 );
  219. }
  220. return newberval;
  221. }
  222. struct berval**
  223. slapi_ch_bvecdup (struct berval** v)
  224. {
  225. struct berval** newberval = NULL;
  226. if (v != NULL) {
  227. size_t i = 0;
  228. while (v[i] != NULL) ++i;
  229. newberval = (struct berval**) slapi_ch_malloc ((i + 1) * sizeof (struct berval*));
  230. newberval[i] = NULL;
  231. while (i-- > 0) {
  232. newberval[i] = slapi_ch_bvdup (v[i]);
  233. }
  234. }
  235. return newberval;
  236. }
  237. #if !defined(MEMPOOL_EXPERIMENTAL)
  238. /*
  239. * Function: slapi_ch_free
  240. *
  241. * Returns: nothing
  242. *
  243. * Description: frees the pointer, and then sets it to NULL to
  244. * prevent free-memory writes.
  245. * Note: pass in the address of the pointer you want to free.
  246. * Note: you can pass in null pointers, it's cool.
  247. */
  248. void
  249. slapi_ch_free(void **ptr)
  250. {
  251. if (ptr==NULL || *ptr == NULL){
  252. return;
  253. }
  254. free (*ptr);
  255. *ptr = NULL;
  256. if(!counters_created)
  257. {
  258. create_counters();
  259. counters_created= 1;
  260. }
  261. PR_INCREMENT_COUNTER(slapi_ch_counter_free);
  262. PR_DECREMENT_COUNTER(slapi_ch_counter_exist);
  263. return;
  264. }
  265. #endif /* !MEMPOOL_EXPERIMENTAL */
  266. /* just like slapi_ch_free, takes the address of the struct berval pointer */
  267. void
  268. slapi_ch_bvfree(struct berval** v)
  269. {
  270. if (v == NULL || *v == NULL)
  271. return;
  272. slapi_ch_free((void **)&((*v)->bv_val));
  273. slapi_ch_free((void **)v);
  274. return;
  275. }
  276. /* just like slapi_ch_free, but the argument is the address of a string
  277. This helps with compile time error checking
  278. */
  279. void
  280. slapi_ch_free_string(char **s)
  281. {
  282. slapi_ch_free((void **)s);
  283. }
  284. /*
  285. This function is just like PR_smprintf. It works like sprintf
  286. except that it allocates enough memory to hold the result
  287. string and returns that allocated memory to the caller. The
  288. caller must use slapi_ch_free_string to free the memory.
  289. It should only be used in those situations that will eventually free
  290. the memory using slapi_ch_free_string e.g. allocating a string
  291. that will be freed as part of pblock cleanup, or passed in to create
  292. a Slapi_DN, or things of that nature. If you have control of the
  293. flow such that the memory will be allocated and freed in the same
  294. scope, better to just use PR_smprintf and PR_smprintf_free instead
  295. because it is likely faster.
  296. */
  297. /*
  298. This implementation is the same as PR_smprintf.
  299. The above comment does not apply to this function for now.
  300. see [150809] for more details.
  301. WARNING - with this fix, this means we are now mixing PR_Malloc with
  302. slapi_ch_free. Which is ok for now - they both use malloc/free from
  303. the operating system. But if this changes in the future, this
  304. function will have to change as well.
  305. */
  306. #if !defined(MEMPOOL_EXPERIMENTAL)
  307. char *
  308. slapi_ch_smprintf(const char *fmt, ...)
  309. {
  310. char *p = NULL;
  311. va_list ap;
  312. if (NULL == fmt) {
  313. return NULL;
  314. }
  315. va_start(ap, fmt);
  316. p = PR_vsmprintf(fmt, ap);
  317. va_end(ap);
  318. return p;
  319. }
  320. #endif