@@ -15,39 +15,45 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type {
15
15
pub fn init (parent_allocator : * Allocator , out_stream : OutStreamType ) Self {
16
16
return Self {
17
17
.allocator = Allocator {
18
- .reallocFn = realloc ,
19
- .shrinkFn = shrink ,
18
+ .allocFn = alloc ,
19
+ .resizeFn = resize ,
20
20
},
21
21
.parent_allocator = parent_allocator ,
22
22
.out_stream = out_stream ,
23
23
};
24
24
}
25
25
26
- fn realloc (allocator : * Allocator , old_mem : [] u8 , old_align : u29 , new_size : usize , new_align : u29 ) ! []u8 {
26
+ fn alloc (allocator : * Allocator , len : usize , ptr_align : u29 , len_align : u29 ) error { OutOfMemory } ! []u8 {
27
27
const self = @fieldParentPtr (Self , "allocator" , allocator );
28
- if (old_mem .len == 0 ) {
29
- self .out_stream .print ("allocation of {} " , .{new_size }) catch {};
30
- } else {
31
- self .out_stream .print ("resize from {} to {} " , .{ old_mem .len , new_size }) catch {};
32
- }
33
- const result = self .parent_allocator .reallocFn (self .parent_allocator , old_mem , old_align , new_size , new_align );
28
+ self .out_stream .print ("alloc : {}" , .{len }) catch {};
29
+ const result = self .parent_allocator .callAllocFn (len , ptr_align , len_align );
34
30
if (result ) | buff | {
35
- self .out_stream .print ("success!\n " , .{}) catch {};
31
+ self .out_stream .print (" success!\n " , .{}) catch {};
36
32
} else | err | {
37
- self .out_stream .print ("failure!\n " , .{}) catch {};
33
+ self .out_stream .print (" failure!\n " , .{}) catch {};
38
34
}
39
35
return result ;
40
36
}
41
37
42
- fn shrink (allocator : * Allocator , old_mem : []u8 , old_align : u29 , new_size : usize , new_align : u29 ) [] u8 {
38
+ fn resize (allocator : * Allocator , buf : []u8 , new_len : usize , len_align : u29 ) error { OutOfMemory } ! usize {
43
39
const self = @fieldParentPtr (Self , "allocator" , allocator );
44
- const result = self .parent_allocator .shrinkFn (self .parent_allocator , old_mem , old_align , new_size , new_align );
45
- if (new_size == 0 ) {
46
- self .out_stream .print ("free of {} bytes success!\n " , .{old_mem .len }) catch {};
40
+ if (new_len == 0 ) {
41
+ self .out_stream .print ("free : {}\n " , .{buf .len }) catch {};
42
+ } else if (new_len <= buf .len ) {
43
+ self .out_stream .print ("shrink: {} to {}\n " , .{buf .len , new_len }) catch {};
47
44
} else {
48
- self .out_stream .print ("shrink from {} bytes to {} bytes success!\n " , .{ old_mem .len , new_size }) catch {};
45
+ self .out_stream .print ("expand: {} to {}" , .{ buf .len , new_len }) catch {};
46
+ }
47
+ if (self .parent_allocator .callResizeFn (buf , new_len , len_align )) | resized_len | {
48
+ if (new_len > buf .len ) {
49
+ self .out_stream .print (" success!\n " , .{}) catch {};
50
+ }
51
+ return resized_len ;
52
+ } else | e | {
53
+ std .debug .assert (new_len > buf .len );
54
+ self .out_stream .print (" failure!\n " , .{}) catch {};
55
+ return e ;
49
56
}
50
- return result ;
51
57
}
52
58
};
53
59
}
@@ -60,17 +66,24 @@ pub fn loggingAllocator(
60
66
}
61
67
62
68
test "LoggingAllocator" {
63
- var buf : [255 ]u8 = undefined ;
64
- var fbs = std .io .fixedBufferStream (& buf );
69
+ var log_buf : [255 ]u8 = undefined ;
70
+ var fbs = std .io .fixedBufferStream (& log_buf );
65
71
66
- const allocator = & loggingAllocator (std .testing .allocator , fbs .outStream ()).allocator ;
72
+ var allocator_buf : [10 ]u8 = undefined ;
73
+ var fixedBufferAllocator = std .mem .validationWrap (std .heap .FixedBufferAllocator .init (& allocator_buf ));
74
+ const allocator = & loggingAllocator (& fixedBufferAllocator .allocator , fbs .outStream ()).allocator ;
67
75
68
- const ptr = try allocator .alloc (u8 , 10 );
69
- allocator .free (ptr );
76
+ var a = try allocator .alloc (u8 , 10 );
77
+ a .len = allocator .shrinkBytes (a , 5 , 0 );
78
+ std .debug .assert (a .len == 5 );
79
+ std .testing .expectError (error .OutOfMemory , allocator .callResizeFn (a , 20 , 0 ));
80
+ allocator .free (a );
70
81
71
82
std .testing .expectEqualSlices (u8 ,
72
- \\allocation of 10 success!
73
- \\free of 10 bytes success!
83
+ \\alloc : 10 success!
84
+ \\shrink: 10 to 5
85
+ \\expand: 5 to 20 failure!
86
+ \\free : 5
74
87
\\
75
88
, fbs .getWritten ());
76
89
}
0 commit comments