libobs: Deprecate base_set_allocator and make it no-op

Hopefully nothing is actually using this in the first place. As a
library libobs has alignment requirements so we should probably not
allow an application to replace our aligned mallocs with something else.
This also allows more aggressive optimizations inside libobs as the call
can be completely inlined into a libc malloc.
This commit is contained in:
Richard Stanway 2022-07-13 00:05:37 +02:00 committed by Jim
parent 05c245532f
commit dbdbfe79d2
2 changed files with 8 additions and 12 deletions

View file

@ -87,19 +87,13 @@ static void a_free(void *ptr)
#endif
}
static struct base_allocator alloc = {a_malloc, a_realloc, a_free};
static long num_allocs = 0;
void base_set_allocator(struct base_allocator *defs)
{
memcpy(&alloc, defs, sizeof(struct base_allocator));
}
void *bmalloc(size_t size)
{
void *ptr = alloc.malloc(size);
void *ptr = a_malloc(size);
if (!ptr && !size)
ptr = alloc.malloc(1);
ptr = a_malloc(1);
if (!ptr) {
os_breakpoint();
bcrash("Out of memory while trying to allocate %lu bytes",
@ -115,9 +109,9 @@ void *brealloc(void *ptr, size_t size)
if (!ptr)
os_atomic_inc_long(&num_allocs);
ptr = alloc.realloc(ptr, size);
ptr = a_realloc(ptr, size);
if (!ptr && !size)
ptr = alloc.realloc(ptr, 1);
ptr = a_realloc(ptr, 1);
if (!ptr) {
os_breakpoint();
bcrash("Out of memory while trying to allocate %lu bytes",
@ -131,7 +125,7 @@ void bfree(void *ptr)
{
if (ptr) {
os_atomic_dec_long(&num_allocs);
alloc.free(ptr);
a_free(ptr);
}
}
@ -153,3 +147,5 @@ void *bmemdup(const void *ptr, size_t size)
return out;
}
OBS_DEPRECATED void base_set_allocator(struct base_allocator *defs) {}

View file

@ -31,7 +31,7 @@ struct base_allocator {
void (*free)(void *);
};
EXPORT void base_set_allocator(struct base_allocator *defs);
OBS_DEPRECATED EXPORT void base_set_allocator(struct base_allocator *defs);
EXPORT void *bmalloc(size_t size);
EXPORT void *brealloc(void *ptr, size_t size);