Skip to content

Commit

Permalink
Pass H5MM calls to stdlib counterparts by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendersonHDF committed Jul 25, 2022
1 parent 32caa56 commit f38521e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 48 deletions.
15 changes: 10 additions & 5 deletions src/H5.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() */
Expand Down Expand Up @@ -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() */
Expand Down
37 changes: 9 additions & 28 deletions src/H5MM.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -490,18 +473,14 @@ 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)
} /* end H5MM_strdup() */
#endif /* H5_MEMORY_ALLOC_SANITY_CHECK */

/*-------------------------------------------------------------------------
* Function: H5MM_xfree
Expand Down Expand Up @@ -585,6 +564,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
*
Expand Down Expand Up @@ -619,6 +599,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
Expand Down
46 changes: 31 additions & 15 deletions src/H5MMprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,43 @@
#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 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_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 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 */
5 changes: 5 additions & 0 deletions test/tmisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5764,6 +5764,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");
Expand All @@ -5773,6 +5774,7 @@ test_misc34(void)
mem = H5MM_calloc(sz);
CHECK_PTR_NULL(mem, "H5MM_calloc");
mem = H5MM_xfree(mem);
#endif

/* H5MM_realloc(): Check behavior:
*
Expand All @@ -5787,13 +5789,16 @@ 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)");
mem = H5MM_xfree(mem);

mem = H5MM_realloc(mem, sz);
CHECK_PTR_NULL(mem, "H5MM_realloc (case 3)");
#endif

mem = H5MM_xfree(mem);

/* H5MM_xstrdup(): Ensure NULL returns NULL */
Expand Down

0 comments on commit f38521e

Please sign in to comment.