Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ conf.set(
),
description: 'Does struct tm have a tm_gmtoff field?'
)
conf.set(
'HAVE_WEAK_MALLOC',
cc.links(
'''#include <malloc.h>
void *malloc(size_t size) {
return NULL;
}
int main(void) {
malloc(1);
}
''',
name: 'weak_malloc'
),
description: 'Is malloc() weak function?'
)

if cc.has_function_attribute('fallthrough')
conf.set('fallthrough', '__attribute__((__fallthrough__))')
Expand Down
55 changes: 55 additions & 0 deletions util/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/mman.h>
#include <ccan/likely/likely.h>

#include "mem.h"

Expand Down Expand Up @@ -107,3 +109,56 @@ void nvme_free_huge(struct nvme_mem_huge *mh)
mh->len = 0;
mh->p = NULL;
}

#ifdef HAVE_WEAK_MALLOC
void *malloc(size_t size)
{
static void *(*malloc_sym)(size_t size);
void *result = NULL;

if (!malloc_sym)
malloc_sym = dlsym(RTLD_NEXT, "malloc");

if (malloc_sym)
result = malloc_sym(size);

if (unlikely(!result))
abort();

return result;
}

void *calloc(size_t number, size_t size)
{
static void *(*calloc_sym)(size_t number, size_t size);
void *result = NULL;

if (!calloc_sym)
calloc_sym = dlsym(RTLD_NEXT, "calloc");

if (calloc_sym)
result = calloc_sym(number, size);

if (unlikely(!result))
abort();

return result;
}

void *realloc(void *ptr, size_t size)
{
static void *(*realloc_sym)(void *ptr, size_t size);
void *result = NULL;

if (!realloc_sym)
realloc_sym = dlsym(RTLD_NEXT, "realloc");

if (realloc_sym)
result = realloc_sym(ptr, size);

if (unlikely(!result))
abort();

return result;
}
#endif /* HAVE_WEAK_MALLOC */
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need the same thing at least for calloc and realloc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. After looking at this again with fresh eyes, I think we don't gain alot with this approach. Because we can't really remove the p = malloc(...); if (p) { printf(...); return -1; } code. We still need it when the allocation fails.

That means it would make more sense to introduce nvme_malloc & Co which would check the allocation and update all call sites and remove the memory allocation checks. Basically like the idea from above but with function instead ugly macros.

Since the nvme-cli 2.x is entering the maintenance mode today this is something we could consider for nvme-cli3

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. I will do for nvme-cli3.