Skip to content

Commit

Permalink
Merge pull request #908 from floooh/issue903-allocator_names
Browse files Browse the repository at this point in the history
Rename custom allocator callbacks to alloc_fn/free_fn, fixes #903
  • Loading branch information
floooh committed Sep 25, 2023
2 parents 0bc4765 + 995a3d5 commit 5633a0f
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 138 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## Updates

#### 25-Sep-2023

- The allocator callback functions in all headers that support custom allocators have been renamed
from `alloc` and `free` to `alloc_fn` and `free_fn`, this is because the symbol `free` is quite
likely to collide with a preprocessor macro of the same name if the standard C allocator is
replaced with a custom allocator.

This is a breaking change only if you've been providing your own allocator functions to
the sokol headers.

See issue https://github.com/floooh/sokol/issues/903 and PR https://github.com/floooh/sokol/pull/908
for details.

#### 23-Sep-2023

- sokol_gfx.h gl: Allow to inject an external GL framebuffer id into the sokol-gfx default
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Simple
[STB-style](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt)
cross-platform libraries for C and C++, written in C.

[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**17-Sep-2023** debug label support in sokol_gfx.h Metal backend)
[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**25-Sep-2023** POTENTIALLY BREAKING: allocator callbacks have been renamed)

[![Build](/../../actions/workflows/main.yml/badge.svg)](/../../actions/workflows/main.yml) [![Bindings](/../../actions/workflows/gen_bindings.yml/badge.svg)](/../../actions/workflows/gen_bindings.yml) [![build](https://github.com/floooh/sokol-zig/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-zig/actions/workflows/main.yml) [![build](https://github.com/floooh/sokol-nim/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-nim/actions/workflows/main.yml) [![Odin](https://github.com/floooh/sokol-odin/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-odin/actions/workflows/main.yml)[![Rust](https://github.com/floooh/sokol-rust/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-rust/actions/workflows/main.yml)

Expand Down
23 changes: 11 additions & 12 deletions sokol_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,8 @@
return (sapp_desc){
// ...
.allocator = {
.alloc = my_alloc,
.free = my_free,
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
};
Expand Down Expand Up @@ -1493,12 +1493,12 @@ typedef struct sapp_icon_desc {
Used in sapp_desc to provide custom memory-alloc and -free functions
to sokol_app.h. If memory management should be overridden, both the
alloc and free function must be provided (e.g. it's not valid to
alloc_fb and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sapp_allocator {
void* (*alloc)(size_t size, void* user_data);
void (*free)(void* ptr, void* user_data);
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sapp_allocator;

Expand Down Expand Up @@ -2862,10 +2862,9 @@ _SOKOL_PRIVATE void _sapp_clear(void* ptr, size_t size) {
_SOKOL_PRIVATE void* _sapp_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sapp.desc.allocator.alloc) {
ptr = _sapp.desc.allocator.alloc(size, _sapp.desc.allocator.user_data);
}
else {
if (_sapp.desc.allocator.alloc_fn) {
ptr = _sapp.desc.allocator.alloc_fn(size, _sapp.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
Expand All @@ -2881,8 +2880,8 @@ _SOKOL_PRIVATE void* _sapp_malloc_clear(size_t size) {
}

_SOKOL_PRIVATE void _sapp_free(void* ptr) {
if (_sapp.desc.allocator.free) {
_sapp.desc.allocator.free(ptr, _sapp.desc.allocator.user_data);
if (_sapp.desc.allocator.free_fn) {
_sapp.desc.allocator.free_fn(ptr, _sapp.desc.allocator.user_data);
}
else {
free(ptr);
Expand Down Expand Up @@ -2986,7 +2985,7 @@ _SOKOL_PRIVATE bool _sapp_strcpy(const char* src, char* dst, int max_len) {
}

_SOKOL_PRIVATE sapp_desc _sapp_desc_defaults(const sapp_desc* desc) {
SOKOL_ASSERT((desc->allocator.alloc && desc->allocator.free) || (!desc->allocator.alloc && !desc->allocator.free));
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
sapp_desc res = *desc;
res.sample_count = _sapp_def(res.sample_count, 1);
res.swap_interval = _sapp_def(res.swap_interval, 1);
Expand Down
24 changes: 11 additions & 13 deletions sokol_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
sargs_setup(&(sargs_desc){
// ...
.allocator = {
.alloc = my_alloc,
.free = my_free,
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
Expand Down Expand Up @@ -316,12 +316,12 @@ extern "C" {
Used in sargs_desc to provide custom memory-alloc and -free functions
to sokol_args.h. If memory management should be overridden, both the
alloc and free function must be provided (e.g. it's not valid to
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sargs_allocator {
void* (*alloc)(size_t size, void* user_data);
void (*free)(void* ptr, void* user_data);
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sargs_allocator;

Expand Down Expand Up @@ -447,10 +447,9 @@ _SOKOL_PRIVATE void _sargs_clear(void* ptr, size_t size) {
_SOKOL_PRIVATE void* _sargs_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sargs.allocator.alloc) {
ptr = _sargs.allocator.alloc(size, _sargs.allocator.user_data);
}
else {
if (_sargs.allocator.alloc_fn) {
ptr = _sargs.allocator.alloc_fn(size, _sargs.allocator.user_data);
} else {
ptr = malloc(size);
}
SOKOL_ASSERT(ptr);
Expand All @@ -464,10 +463,9 @@ _SOKOL_PRIVATE void* _sargs_malloc_clear(size_t size) {
}

_SOKOL_PRIVATE void _sargs_free(void* ptr) {
if (_sargs.allocator.free) {
_sargs.allocator.free(ptr, _sargs.allocator.user_data);
}
else {
if (_sargs.allocator.free_fn) {
_sargs.allocator.free_fn(ptr, _sargs.allocator.user_data);
} else {
free(ptr);
}
}
Expand Down
26 changes: 12 additions & 14 deletions sokol_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@
saudio_setup(&(saudio_desc){
// ...
.allocator = {
.alloc = my_alloc,
.free = my_free,
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
Expand Down Expand Up @@ -575,12 +575,12 @@ typedef struct saudio_logger {
Used in saudio_desc to provide custom memory-alloc and -free functions
to sokol_audio.h. If memory management should be overridden, both the
alloc and free function must be provided (e.g. it's not valid to
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct saudio_allocator {
void* (*alloc)(size_t size, void* user_data);
void (*free)(void* ptr, void* user_data);
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} saudio_allocator;

Expand Down Expand Up @@ -1146,10 +1146,9 @@ _SOKOL_PRIVATE void _saudio_clear(void* ptr, size_t size) {
_SOKOL_PRIVATE void* _saudio_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_saudio.desc.allocator.alloc) {
ptr = _saudio.desc.allocator.alloc(size, _saudio.desc.allocator.user_data);
}
else {
if (_saudio.desc.allocator.alloc_fn) {
ptr = _saudio.desc.allocator.alloc_fn(size, _saudio.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
Expand All @@ -1165,10 +1164,9 @@ _SOKOL_PRIVATE void* _saudio_malloc_clear(size_t size) {
}

_SOKOL_PRIVATE void _saudio_free(void* ptr) {
if (_saudio.desc.allocator.free) {
_saudio.desc.allocator.free(ptr, _saudio.desc.allocator.user_data);
}
else {
if (_saudio.desc.allocator.free_fn) {
_saudio.desc.allocator.free_fn(ptr, _saudio.desc.allocator.user_data);
} else {
free(ptr);
}
}
Expand Down Expand Up @@ -2485,7 +2483,7 @@ void _saudio_backend_shutdown(void) {
SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) {
SOKOL_ASSERT(!_saudio.valid);
SOKOL_ASSERT(desc);
SOKOL_ASSERT((desc->allocator.alloc && desc->allocator.free) || (!desc->allocator.alloc && !desc->allocator.free));
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
_saudio_clear(&_saudio, sizeof(_saudio));
_saudio.desc = *desc;
_saudio.stream_cb = desc->stream_cb;
Expand Down
24 changes: 11 additions & 13 deletions sokol_fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,8 @@
sfetch_setup(&(sfetch_desc_t){
// ...
.allocator = {
.alloc = my_alloc,
.free = my_free,
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
Expand Down Expand Up @@ -1024,8 +1024,8 @@ typedef struct sfetch_range_t {
override one function but not the other).
*/
typedef struct sfetch_allocator_t {
void* (*alloc)(size_t size, void* user_data);
void (*free)(void* ptr, void* user_data);
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sfetch_allocator_t;

Expand Down Expand Up @@ -1424,10 +1424,9 @@ _SOKOL_PRIVATE void _sfetch_clear(void* ptr, size_t size) {
_SOKOL_PRIVATE void* _sfetch_malloc_with_allocator(const sfetch_allocator_t* allocator, size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (allocator->alloc) {
ptr = allocator->alloc(size, allocator->user_data);
}
else {
if (allocator->alloc_fn) {
ptr = allocator->alloc_fn(size, allocator->user_data);
} else {
ptr = malloc(size);
}
if (0 == ptr) {
Expand All @@ -1447,10 +1446,9 @@ _SOKOL_PRIVATE void* _sfetch_malloc_clear(size_t size) {
}

_SOKOL_PRIVATE void _sfetch_free(void* ptr) {
if (_sfetch->desc.allocator.free) {
_sfetch->desc.allocator.free(ptr, _sfetch->desc.allocator.user_data);
}
else {
if (_sfetch->desc.allocator.free_fn) {
_sfetch->desc.allocator.free_fn(ptr, _sfetch->desc.allocator.user_data);
} else {
free(ptr);
}
}
Expand Down Expand Up @@ -2620,7 +2618,7 @@ _SOKOL_PRIVATE bool _sfetch_validate_request(_sfetch_t* ctx, const sfetch_reques
}

_SOKOL_PRIVATE sfetch_desc_t _sfetch_desc_defaults(const sfetch_desc_t* desc) {
SOKOL_ASSERT((desc->allocator.alloc && desc->allocator.free) || (!desc->allocator.alloc && !desc->allocator.free));
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
sfetch_desc_t res = *desc;
res.max_requests = _sfetch_def(desc->max_requests, 128);
res.num_channels = _sfetch_def(desc->num_channels, 1);
Expand Down
26 changes: 13 additions & 13 deletions sokol_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,8 @@
sg_setup(&(sg_desc){
// ...
.allocator = {
.alloc = my_alloc,
.free = my_free,
.alloc_fn = my_alloc,
.free_fn = my_free,
.user_data = ...,
}
});
Expand Down Expand Up @@ -3131,8 +3131,8 @@ typedef enum sg_log_item {
.max_commit_listeners 1024
.disable_validation false

.allocator.alloc 0 (in this case, malloc() will be called)
.allocator.free 0 (in this case, free() will be called)
.allocator.alloc_fn 0 (in this case, malloc() will be called)
.allocator.free_fn 0 (in this case, free() will be called)
.allocator.user_data 0

.context.color_format: default value depends on selected backend:
Expand Down Expand Up @@ -3280,12 +3280,12 @@ typedef struct sg_commit_listener {

Used in sg_desc to provide custom memory-alloc and -free functions
to sokol_gfx.h. If memory management should be overridden, both the
alloc and free function must be provided (e.g. it's not valid to
alloc_fn and free_fn function must be provided (e.g. it's not valid to
override one function but not the other).
*/
typedef struct sg_allocator {
void* (*alloc)(size_t size, void* user_data);
void (*free)(void* ptr, void* user_data);
void* (*alloc_fn)(size_t size, void* user_data);
void (*free_fn)(void* ptr, void* user_data);
void* user_data;
} sg_allocator;

Expand Down Expand Up @@ -5067,8 +5067,8 @@ _SOKOL_PRIVATE void _sg_clear(void* ptr, size_t size) {
_SOKOL_PRIVATE void* _sg_malloc(size_t size) {
SOKOL_ASSERT(size > 0);
void* ptr;
if (_sg.desc.allocator.alloc) {
ptr = _sg.desc.allocator.alloc(size, _sg.desc.allocator.user_data);
if (_sg.desc.allocator.alloc_fn) {
ptr = _sg.desc.allocator.alloc_fn(size, _sg.desc.allocator.user_data);
} else {
ptr = malloc(size);
}
Expand All @@ -5085,8 +5085,8 @@ _SOKOL_PRIVATE void* _sg_malloc_clear(size_t size) {
}

_SOKOL_PRIVATE void _sg_free(void* ptr) {
if (_sg.desc.allocator.free) {
_sg.desc.allocator.free(ptr, _sg.desc.allocator.user_data);
if (_sg.desc.allocator.free_fn) {
_sg.desc.allocator.free_fn(ptr, _sg.desc.allocator.user_data);
} else {
free(ptr);
}
Expand Down Expand Up @@ -7044,7 +7044,7 @@ _SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) {
_SOKOL_PRIVATE void _sg_gl_setup_backend(const sg_desc* desc) {
_SOKOL_UNUSED(desc);
SOKOL_ASSERT(desc->context.gl.default_framebuffer_cb == 0 || desc->context.gl.default_framebuffer_userdata_cb == 0);

// assumes that _sg.gl is already zero-initialized
_sg.gl.valid = true;

Expand Down Expand Up @@ -16059,7 +16059,7 @@ _SOKOL_PRIVATE sg_desc _sg_desc_defaults(const sg_desc* desc) {
SOKOL_API_IMPL void sg_setup(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT((desc->_start_canary == 0) && (desc->_end_canary == 0));
SOKOL_ASSERT((desc->allocator.alloc && desc->allocator.free) || (!desc->allocator.alloc && !desc->allocator.free));
SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn));
_SG_CLEAR_ARC_STRUCT(_sg_state_t, _sg);
_sg.desc = _sg_desc_defaults(desc);
_sg_setup_pools(&_sg.pools, &_sg.desc);
Expand Down

0 comments on commit 5633a0f

Please sign in to comment.