ch_malloc.c 11 KB

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