diff --git a/src/H5.c b/src/H5.c index 538925ca963..25691276c18 100644 --- a/src/H5.c +++ b/src/H5.c @@ -1222,10 +1222,12 @@ H5allocate_memory(size_t size, hbool_t clear) FUNC_ENTER_API_NOINIT H5TRACE2("*x", "zb", size, clear); - if (clear) - ret_value = H5MM_calloc(size); - else - ret_value = H5MM_malloc(size); + if (size != 0) { + if (clear) + ret_value = H5MM_calloc(size); + else + ret_value = H5MM_malloc(size); + } FUNC_LEAVE_API_NOINIT(ret_value) } /* end H5allocate_memory() */ @@ -1262,7 +1264,10 @@ H5resize_memory(void *mem, size_t size) FUNC_ENTER_API_NOINIT H5TRACE2("*x", "*xz", mem, size); - ret_value = H5MM_realloc(mem, size); + if (size != 0) + ret_value = H5MM_realloc(mem, size); + else if (mem) + ret_value = H5MM_xfree(mem); FUNC_LEAVE_API_NOINIT(ret_value) } /* end H5resize_memory() */ diff --git a/src/H5MM.c b/src/H5MM.c index 9c03ceb338d..40634e2d35a 100644 --- a/src/H5MM.c +++ b/src/H5MM.c @@ -232,7 +232,6 @@ H5MM_final_sanity_check(void) HDfprintf(stderr, "%s: H5MM_peak_alloc_blocks_count_s = %zu\n", __func__, H5MM_peak_alloc_blocks_count_s); #endif /* H5MM_PRINT_MEMORY_STATS */ } -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ /*------------------------------------------------------------------------- * Function: H5MM_malloc @@ -261,7 +260,6 @@ H5MM_malloc(size_t size) /* Use FUNC_ENTER_NOAPI_NOINIT_NOERR here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOERR -#if defined H5_MEMORY_ALLOC_SANITY_CHECK /* Initialize block list head singleton */ if (!H5MM_init_s) { H5MM_memcpy(H5MM_block_head_s.sig, H5MM_block_signature_s, H5MM_SIG_SIZE); @@ -271,11 +269,9 @@ H5MM_malloc(size_t size) H5MM_block_head_s.u.info.in_use = TRUE; H5MM_init_s = TRUE; - } /* end if */ -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ + } /* end if */ if (size) { -#if defined H5_MEMORY_ALLOC_SANITY_CHECK H5MM_block_t *block; size_t alloc_size = sizeof(H5MM_block_t) + size + H5MM_HEAD_GUARD_SIZE + H5MM_TAIL_GUARD_SIZE; @@ -309,10 +305,7 @@ H5MM_malloc(size_t size) } /* end if */ else ret_value = NULL; -#else /* H5_MEMORY_ALLOC_SANITY_CHECK */ - ret_value = HDmalloc(size); -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ - } /* end if */ + } /* end if */ else ret_value = NULL; @@ -349,13 +342,9 @@ H5MM_calloc(size_t size) FUNC_ENTER_NOAPI_NOINIT_NOERR if (size) { -#if defined H5_MEMORY_ALLOC_SANITY_CHECK if (NULL != (ret_value = H5MM_malloc(size))) HDmemset(ret_value, 0, size); -#else /* H5_MEMORY_ALLOC_SANITY_CHECK */ - ret_value = HDcalloc((size_t)1, size); -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ - } /* end if */ + } /* end if */ else ret_value = NULL; @@ -396,7 +385,6 @@ H5MM_realloc(void *mem, size_t size) /* Not defined in the standard, return NULL */ ret_value = NULL; else { -#if defined H5_MEMORY_ALLOC_SANITY_CHECK if (size > 0) { if (mem) { if (H5MM__is_our_block(mem)) { @@ -417,17 +405,11 @@ H5MM_realloc(void *mem, size_t size) } else ret_value = H5MM_xfree(mem); -#else /* H5_MEMORY_ALLOC_SANITY_CHECK */ - ret_value = HDrealloc(mem, size); - - /* Some platforms do not return NULL if size is zero. */ - if (0 == size) - ret_value = NULL; -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ - } /* end else */ + } /* end else */ FUNC_LEAVE_NOAPI(ret_value) } /* end H5MM_realloc() */ +#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ /*------------------------------------------------------------------------- * Function: H5MM_xstrdup @@ -465,6 +447,7 @@ H5MM_xstrdup(const char *s) FUNC_LEAVE_NOAPI(ret_value) } /* end H5MM_xstrdup() */ +#if defined H5_MEMORY_ALLOC_SANITY_CHECK /*------------------------------------------------------------------------- * Function: H5MM_strdup * @@ -490,14 +473,9 @@ H5MM_strdup(const char *s) if (!s) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "NULL string not allowed") -#if defined H5_MEMORY_ALLOC_SANITY_CHECK if (NULL == (ret_value = (char *)H5MM_malloc(HDstrlen(s) + 1))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") HDstrcpy(ret_value, s); -#else - if (NULL == (ret_value = HDstrdup(s))) - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "string duplication failed") -#endif done: FUNC_LEAVE_NOAPI(ret_value) @@ -522,17 +500,14 @@ H5MM_strdup(const char *s) char * H5MM_strndup(const char *s, size_t n) { -#if defined H5_MEMORY_ALLOC_SANITY_CHECK size_t len; -#endif - char *ret_value = NULL; + char *ret_value = NULL; FUNC_ENTER_NOAPI(NULL) if (!s) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "NULL string not allowed") -#if defined H5_MEMORY_ALLOC_SANITY_CHECK for (len = 0; len < n && s[len] != '\0'; len++) ; @@ -541,14 +516,11 @@ H5MM_strndup(const char *s, size_t n) H5MM_memcpy(ret_value, s, len); ret_value[len] = '\0'; -#else - if (NULL == (ret_value = HDstrndup(s, n))) - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "string duplication failed") -#endif done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5MM_strndup() */ +#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ /*------------------------------------------------------------------------- * Function: H5MM_xfree @@ -632,6 +604,7 @@ H5MM_xfree_const(const void *mem) FUNC_LEAVE_NOAPI(NULL) } /* end H5MM_xfree_const() */ +#if defined H5_MEMORY_ALLOC_SANITY_CHECK /*------------------------------------------------------------------------- * Function: H5MM_memcpy * @@ -666,6 +639,7 @@ H5MM_memcpy(void *dest, const void *src, size_t n) FUNC_LEAVE_NOAPI(ret) } /* end H5MM_memcpy() */ +#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ /*------------------------------------------------------------------------- * Function: H5MM_get_alloc_stats diff --git a/src/H5MMprivate.h b/src/H5MMprivate.h index 0a5a0111348..2a316c02918 100644 --- a/src/H5MMprivate.h +++ b/src/H5MMprivate.h @@ -30,28 +30,45 @@ #include "H5private.h" #if defined H5_MEMORY_ALLOC_SANITY_CHECK -/*#define H5MM_PRINT_MEMORY_STATS */ -#define H5MM_free(Z) H5MM_xfree(Z) -#else /* H5_MEMORY_ALLOC_SANITY_CHECK */ -#define H5MM_free(Z) HDfree(Z) -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ + +/* + * Define this to have the library print out memory + * statistics during its final memory sanity checking + */ +/*#define H5MM_PRINT_MEMORY_STATS*/ + +#define H5MM_free(ptr) H5MM_xfree(ptr) + +/* + * Library prototypes... + */ +H5_DLL void *H5MM_malloc(size_t size) H5_ATTR_MALLOC; +H5_DLL void *H5MM_calloc(size_t size) H5_ATTR_MALLOC; +H5_DLL void *H5MM_realloc(void *mem, size_t size); +H5_DLL char *H5MM_strdup(const char *s); +H5_DLL char *H5MM_strndup(const char *s, size_t n); +H5_DLL void *H5MM_memcpy(void *dest, const void *src, size_t n); +H5_DLL void H5MM_sanity_check_all(void); +H5_DLL void H5MM_final_sanity_check(void); + +#else + +#define H5MM_malloc(size) HDmalloc(size) +#define H5MM_calloc(size) HDcalloc(1, size) +#define H5MM_realloc(ptr, size) HDrealloc(ptr, size) +#define H5MM_free(ptr) HDfree(ptr) +#define H5MM_strdup(s) HDstrdup(s) +#define H5MM_strndup(s, n) HDstrndup(s, n) +#define H5MM_memcpy(dst, src, n) HDmemcpy(dst, src, n) + +#endif /* * Library prototypes... */ -H5_DLL void *H5MM_malloc(size_t size) H5_ATTR_MALLOC; -H5_DLL void *H5MM_calloc(size_t size) H5_ATTR_MALLOC; -H5_DLL void *H5MM_realloc(void *mem, size_t size); -H5_DLL char *H5MM_xstrdup(const char *s); -H5_DLL char *H5MM_strdup(const char *s); -H5_DLL char *H5MM_strndup(const char *s, size_t n); H5_DLL void *H5MM_xfree(void *mem); H5_DLL void *H5MM_xfree_const(const void *mem); -H5_DLL void *H5MM_memcpy(void *dest, const void *src, size_t n); +H5_DLL char *H5MM_xstrdup(const char *s); H5_DLL herr_t H5MM_get_alloc_stats(H5_alloc_stats_t *stats); -#if defined H5_MEMORY_ALLOC_SANITY_CHECK -H5_DLL void H5MM_sanity_check_all(void); -H5_DLL void H5MM_final_sanity_check(void); -#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */ #endif /* H5MMprivate_H */ diff --git a/test/tmisc.c b/test/tmisc.c index 429b275f576..3bb9192d280 100644 --- a/test/tmisc.c +++ b/test/tmisc.c @@ -5826,6 +5826,7 @@ test_misc34(void) mem = H5MM_xfree(mem); CHECK_PTR_NULL(mem, "H5MM_xfree"); +#if defined H5_MEMORY_ALLOC_SANITY_CHECK /* H5MM_malloc(): Ensure that size 0 returns NULL */ mem = H5MM_malloc(sz); CHECK_PTR_NULL(mem, "H5MM_malloc"); @@ -5835,6 +5836,7 @@ test_misc34(void) mem = H5MM_calloc(sz); CHECK_PTR_NULL(mem, "H5MM_calloc"); mem = H5MM_xfree(mem); +#endif /* H5MM_realloc(): Check behavior: * @@ -5849,6 +5851,7 @@ test_misc34(void) CHECK_PTR(mem, "H5MM_realloc (case 1)"); /* Don't free mem here! */ +#if defined H5_MEMORY_ALLOC_SANITY_CHECK sz = 0; mem = H5MM_realloc(mem, sz); CHECK_PTR_NULL(mem, "H5MM_realloc (case 2)"); @@ -5856,6 +5859,8 @@ test_misc34(void) mem = H5MM_realloc(mem, sz); CHECK_PTR_NULL(mem, "H5MM_realloc (case 3)"); +#endif + mem = H5MM_xfree(mem); /* H5MM_xstrdup(): Ensure NULL returns NULL */