sentry-native icon indicating copy to clipboard operation
sentry-native copied to clipboard

add custom memory allocator

Open irov opened this issue 2 years ago • 5 comments

add custom memory allocator for sentry_malloc and sentry_free

thanks!

irov avatar Nov 10 '21 16:11 irov

That is a good idea! In which form would you prefer that?

Swatinem avatar Nov 11 '21 12:11 Swatinem

I think in the simplest way, to make a define SENTRY_EXTRA_STATIC_ALLOCATION, and give the opportunity to implement these functions yourself (this is a static version).

like extern sentry_malloc & sentry_free

Dynamic - is also a definition SENTRY_EXTRA_DYNAMIC_ALLOCATION (optional, so as not to do an extra IF) and a function that sets two pointers to a function *sentry_malloc & *sentry_free.

irov avatar Nov 11 '21 23:11 irov

I’m not sure making this dynamic (as in: changable at runtime) is a good idea in general.

The other thing we have to consider is that sentry-native has its own signal safe allocator which is very barebones and intentionally leaks. We want to keep that for sure.

I think it might be possible to expose two compile time options: SENTRY_SYSTEM_MALLOC / SENTRY_SYSTEM_FREE, and those two are being used instead of libc malloc/free when defined.

Swatinem avatar Nov 12 '21 13:11 Swatinem

I think it's easiest with just one define ^^

#ifndef SENTRY_EXTRA_ALLOCATOR
void *
sentry_malloc(size_t size)
{
#ifdef WITH_PAGE_ALLOCATOR
    if (sentry__page_allocator_enabled()) {
        return sentry__page_allocator_alloc(size);
    }
#endif
    return malloc(size);
}

void
sentry_free(void *ptr)
{
#ifdef WITH_PAGE_ALLOCATOR
    /* page allocator can't free */
    if (sentry__page_allocator_enabled()) {
        return;
    }
#endif
    free(ptr);
}
#endif

irov avatar Nov 12 '21 14:11 irov

I’m not sure making this dynamic (as in: changable at runtime) is a good idea in general.

Many SDKs do this with a DLL. For example Epic Online SDK (EOS). But not really "changable", just passing the pointers to functions during initialization call.

p0358 avatar Mar 11 '22 21:03 p0358