Skip to content

Commit

Permalink
Make zfree_with_size work correctly on systems without malloc_size
Browse files Browse the repository at this point in the history
  • Loading branch information
poiuj committed Jun 13, 2024
1 parent 72e6b28 commit d20f301
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,13 @@ size_t zmalloc_usable_size(void *ptr) {
#endif

/* Frees the memory buffer pointed by ptr and updates statistics. When using
* jemalloc this function uses the fast track by specifying the buffer size.
* jemalloc it uses the fast track by specifying the buffer size.
*
* ptr must point to the start of the buffer. On systems where we have
* the additional header with size, the caller must do the necessary adjustments
* to ptr. ptr must not be NULL.
* The caller is responsible to provide the real allocaction size.
*
* If the caller can't satisfy any of the conditions above, it should call
* zfree() instead. */
void zfree_with_size(void *ptr, size_t size) {
* ptr must point to the start of the buffer. On systems where we have the
* additional header with size, the caller must do the necessary adjustments to
* ptr. ptr must not be NULL. The caller is responsible to provide the real
* allocaction size. */
static inline void zfree_internal(void *ptr, size_t size) {
update_zmalloc_stat_free(size);

#ifdef USE_JEMALLOC
Expand All @@ -392,7 +389,19 @@ void zfree(void *ptr) {
size_t size = data_size + PREFIX_SIZE;
#endif

zfree_with_size(ptr, size);
zfree_internal(ptr, size);
}

/* Like zfree(), but doesn't call zmalloc_size(). */
void zfree_with_size(void *ptr, size_t size) {
if (ptr == NULL) return;

#ifndef HAVE_MALLOC_SIZE
ptr = (char *)ptr - PREFIX_SIZE;
size += PREFIX_SIZE;
#endif

zfree_internal(ptr, size);
}

char *zstrdup(const char *s) {
Expand Down

0 comments on commit d20f301

Please sign in to comment.