diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-14 20:40:05 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-14 20:40:05 +0200 |
| commit | 0fc141db5de0a635d7f20782e694636af18383f2 (patch) | |
| tree | a9a1fc84494f7f5033248c409e13b53a49e19d25 /core/mem | |
| parent | 3d13beb3eca4e8f17e751962074a25cdaeb846d1 (diff) | |
`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.
Diffstat (limited to 'core/mem')
| -rw-r--r-- | core/mem/tlsf/tlsf_internal.odin | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/core/mem/tlsf/tlsf_internal.odin b/core/mem/tlsf/tlsf_internal.odin index 9aee2097d..f8a9bf60c 100644 --- a/core/mem/tlsf/tlsf_internal.odin +++ b/core/mem/tlsf/tlsf_internal.odin @@ -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 |