Skip to content

Commit 4f7e540

Browse files
committed
[api] Simplify debug allocator and fix handling of 0 size realloc.
1 parent a0f4795 commit 4f7e540

1 file changed

Lines changed: 13 additions & 39 deletions

File tree

lib/bl/api/std/debug_allocator/debug_allocator.bl

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
// =================================================================================================
2-
// bl
3-
//
4-
// File: debug_allocator.bl
5-
// Author: Martin Dorazil
6-
// Date: 18/9/20
7-
//
8-
// Copyright 2020 Martin Dorazil
9-
//
10-
// Permission is hereby granted, free of charge, to any person obtaining a copy
11-
// of this software and associated documentation files (the "Software"), to deal
12-
// in the Software without restriction, including without limitation the rights
13-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14-
// copies of the Software, and to permit persons to whom the Software is
15-
// furnished to do so, subject to the following conditions:
16-
//
17-
// The above copyright notice and this permission notice shall be included in
18-
// all copies or substantial portions of the Software.
19-
//
20-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26-
// SOFTWARE.
27-
// =================================================================================================
28-
291
//! # Debug Allocator
302
//!
313
//! `#import "std/debug_allocator"`
@@ -194,23 +166,28 @@ Meta :: struct {
194166

195167
handler :: fn (dbgalloc: *DebugAllocator, operation: AllocOp, ptr: *u8, size: usize, alignment: usize, file: string_view, line: s32) (mem: *u8, err: Error) {
196168
using AllocOp;
169+
mutex_lock(&dbgalloc.mutex);
170+
defer mutex_unlock(&dbgalloc.mutex);
171+
197172
switch operation {
198173
ALLOCATE {
199-
mem, err :: alloc(size, alignment, dbgalloc.allocator);
200-
if err { return mem, err; }
174+
mem :: alloc(size, alignment, dbgalloc.allocator) catch return null, $;
201175
add_allocation(dbgalloc, mem, size, alignment, file, line);
202-
return mem, err;
176+
return mem, OK;
203177
}
204178
REALLOCATE {
205179
if ptr {
206180
_, previous_alignment :: remove_allocation(dbgalloc, ptr);
207181
if previous_alignment != alignment {
208182
panic("Alignment of reallocated memory block must be the same as the previous one!"
209-
" Previous alignment is %B and current is %B.", previous_alignment, alignment);
183+
" Previous alignment is %B and current is %B.", previous_alignment, alignment);
210184
}
211185
}
212-
mem, err :: realloc(ptr, size, alignment, dbgalloc.allocator);
213-
if err { return mem, err; }
186+
if size == 0 {
187+
free(ptr, dbgalloc.allocator);
188+
return null, OK;
189+
}
190+
mem :: realloc(ptr, size, alignment, dbgalloc.allocator) catch return null, $;
214191
add_allocation(dbgalloc, mem, size, alignment, file, line);
215192
return mem, OK;
216193
}
@@ -234,9 +211,7 @@ handler :: fn (dbgalloc: *DebugAllocator, operation: AllocOp, ptr: *u8, size: us
234211
}
235212

236213
add_allocation :: fn (dbgalloc: *DebugAllocator, ptr: *u8, size: usize, alignment: usize, file: string_view, line: s32) {
237-
mutex_lock(&dbgalloc.mutex);
238-
defer mutex_unlock(&dbgalloc.mutex);
239-
214+
assert(ptr);
240215
dbgalloc.serial += 1;
241216
if dbgalloc.break_on == dbgalloc.serial { debugbreak; }
242217

@@ -251,8 +226,7 @@ add_allocation :: fn (dbgalloc: *DebugAllocator, ptr: *u8, size: usize, alignmen
251226
}
252227

253228
remove_allocation :: fn (dbgalloc: *DebugAllocator, ptr: *u8) (size: usize, alignment: usize) {
254-
mutex_lock(&dbgalloc.mutex);
255-
defer mutex_unlock(&dbgalloc.mutex);
229+
assert(ptr);
256230
value :: tbl_lookup_ptr(&dbgalloc.alloc_table, ptr);
257231
if !value {
258232
panic("Attempt to free already freed or unallocated memory on address %!", ptr);

0 commit comments

Comments
 (0)