core:mem/tlsf: Add early-out in OOM logic

This implementation doesn't allow for out-of-band allocations to be passed through, as it's not designed to
track those. Nor is it able to signal those allocations then need to be freed on the backing allocator,
as opposed to regular allocations handled for you when you `destroy` the TLSF instance.

So if we're asked for more than we're configured to grow by, we can fail with an OOM error early, without adding a new pool.
This commit is contained in:
Jeroen van Rijn
2025-04-14 20:40:05 +02:00
parent 3d13beb3ec
commit 0fc141db5d
2 changed files with 15 additions and 6 deletions
+13 -1
View File
@@ -187,8 +187,20 @@ alloc_bytes_non_zeroed :: proc(control: ^Allocator, size: uint, align: uint) ->
if block == nil {
// OOM: Couldn't find block of `aligned_size` bytes.
if control.new_pool_size > 0 && control.pool.allocator.procedure != nil {
// TLSF is configured to grow. Trying to allocate a new pool of `control.new_pool_size` bytes.
// TLSF is configured to grow.
/*
This implementation doesn't allow for out-of-band allocations to be passed through, as it's not designed to
track those. Nor is it able to signal those allocations then need to be freed on the backing allocator,
as opposed to regular allocations handled for you when you `destroy` the TLSF instance.
So if we're asked for more than we're configured to grow by, we can fail with an OOM error early, without adding a new pool.
*/
if aligned_size > control.new_pool_size {
return nil, .Out_Of_Memory
}
// Trying to allocate a new pool of `control.new_pool_size` bytes.
new_pool_buf := runtime.make_aligned([]byte, control.new_pool_size, ALIGN_SIZE, control.pool.allocator) or_return
// Add new pool to control structure
+2 -5
View File
@@ -111,11 +111,8 @@ tlsf_test_grow_pools :: proc(t: ^testing.T) {
}
context.allocator = tlsf.allocator(&alloc)
err: mem.Allocator_Error
s: []byte
for err == .None && len(allocations) < NUM_ALLOCATIONS {
s, err = make([]byte, ALLOC_SIZE)
for len(allocations) < NUM_ALLOCATIONS {
s := make([]byte, ALLOC_SIZE) or_break
testing.expect_value(t, len(s), ALLOC_SIZE)
append(&allocations, s)
}