@@ -14,20 +14,23 @@ use alloc_no_stdlib::{Allocator, SliceWrapperMut, SliceWrapper,
14
14
15
15
declare_stack_allocator_struct ! ( HeapAllocatedFreelist , heap) ;
16
16
declare_stack_allocator_struct ! ( CallocAllocatedFreelist4096 , 4096 , calloc) ;
17
+ declare_stack_allocator_struct ! ( MallocAllocatedFreelist4096 , 4096 , malloc) ;
17
18
declare_stack_allocator_struct ! ( StackAllocatedFreelist4 , 4 , stack) ;
18
19
declare_stack_allocator_struct ! ( StackAllocatedFreelist8 , 8 , stack) ;
19
20
declare_stack_allocator_struct ! ( GlobalAllocatedFreelist , 16 , global) ;
20
21
//trace_macros!(true);
21
22
22
- define_allocator_memory_pool ! ( global_buffer , 16 , u8 , [ 0 ; 1024 * 1024 * 100 ] , global) ;
23
- define_allocator_memory_pool ! ( global_buffer2 , 16 , u8 , [ 0 ; 1024 * 1024 * 100 ] , global) ;
23
+ define_allocator_memory_pool ! ( 16 , u8 , [ 0 ; 1024 * 1024 * 100 ] , global, global_buffer ) ;
24
+ define_allocator_memory_pool ! ( 16 , u8 , [ 0 ; 1024 * 1024 * 100 ] , global, global_buffer2 ) ;
24
25
extern {
25
26
fn calloc ( n_elem : usize , el_size : usize ) -> * mut u8 ;
27
+ fn malloc ( len : usize ) -> * mut u8 ;
28
+ fn free ( item : * mut u8 ) ;
26
29
}
27
30
#[ test]
28
31
fn uninitialized_stack_pool_test ( ) {
29
32
{
30
- define_allocator_memory_pool ! ( stack_global_buffer , 4 , u8 , [ 0 ; 65536 ] , stack) ;
33
+ let mut stack_global_buffer = define_allocator_memory_pool ! ( 4 , u8 , [ 0 ; 65536 ] , stack) ;
31
34
let mut ags = StackAllocatedFreelist4 :: < u8 > :: new_allocator ( & mut stack_global_buffer, uninitialized) ;
32
35
{
33
36
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -56,7 +59,7 @@ fn uninitialized_stack_pool_test() {
56
59
}
57
60
#[ test]
58
61
fn uninitialized_stack_pool_free_null ( ) {
59
- define_allocator_memory_pool ! ( stack_global_buffer , 8 , u8 , [ 0 ; 256 - 8 ] , stack) ;
62
+ let mut stack_global_buffer = define_allocator_memory_pool ! ( 8 , u8 , [ 0 ; 256 - 8 ] , stack) ;
60
63
let mut ags = StackAllocatedFreelist8 :: < u8 > :: new_allocator ( & mut stack_global_buffer, uninitialized) ;
61
64
{
62
65
let s = ags. alloc_cell ( 0 ) ;
@@ -130,7 +133,7 @@ fn uninitialized_stack_pool_free_null() {
130
133
#[ test]
131
134
fn uninitialized_heap_pool_test ( ) {
132
135
{
133
- define_allocator_memory_pool ! ( heap_global_buffer , 4096 , u8 , [ 0 ; 6 * 1024 * 1024 ] , heap) ;
136
+ let mut heap_global_buffer = define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 6 * 1024 * 1024 ] , heap) ;
134
137
let mut ags = HeapAllocatedFreelist :: < u8 > :: new_allocator ( 4096 , & mut heap_global_buffer, uninitialized) ;
135
138
{
136
139
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -161,7 +164,7 @@ fn uninitialized_heap_pool_test() {
161
164
fn uninitialized_calloc_pool_test ( ) {
162
165
163
166
{
164
- define_allocator_memory_pool ! ( calloc_global_buffer , 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc) ;
167
+ let calloc_global_buffer = unsafe { define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc) } ;
165
168
let mut ags = CallocAllocatedFreelist4096 :: < u8 > :: new_allocator ( calloc_global_buffer, uninitialized) ;
166
169
{
167
170
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -193,7 +196,9 @@ println!("{:?}", ags.free_list_start);
193
196
fn uninitialized_global_pool_test ( ) {
194
197
{
195
198
let mut ags = GlobalAllocatedFreelist :: < u8 > :: new_allocator ( uninitialized) ;
199
+ unsafe {
196
200
bind_global_buffers_to_allocator ! ( ags, global_buffer, u8 ) ;
201
+ }
197
202
{
198
203
let mut x = ags. alloc_cell ( 9999 ) ;
199
204
x. slice_mut ( ) [ 0 ] = 4 ;
@@ -223,7 +228,7 @@ fn uninitialized_global_pool_test() {
223
228
#[ test]
224
229
fn stack_pool_test ( ) {
225
230
{
226
- define_allocator_memory_pool ! ( stack_global_buffer , 4 , u8 , [ 0 ; 65536 ] , stack) ;
231
+ let mut stack_global_buffer = define_allocator_memory_pool ! ( 4 , u8 , [ 0 ; 65536 ] , stack) ;
227
232
let mut ags = StackAllocatedFreelist4 :: < u8 > :: new_allocator ( & mut stack_global_buffer, bzero) ;
228
233
{
229
234
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -252,7 +257,7 @@ fn stack_pool_test() {
252
257
}
253
258
#[ test]
254
259
fn stack_pool_free_null ( ) {
255
- define_allocator_memory_pool ! ( stack_global_buffer , 8 , u8 , [ 0 ; 256 - 8 ] , stack) ;
260
+ let mut stack_global_buffer = define_allocator_memory_pool ! ( 8 , u8 , [ 0 ; 256 - 8 ] , stack) ;
256
261
let mut ags = StackAllocatedFreelist8 :: < u8 > :: new_allocator ( & mut stack_global_buffer, bzero) ;
257
262
{
258
263
let s = ags. alloc_cell ( 0 ) ;
@@ -326,7 +331,7 @@ fn stack_pool_free_null() {
326
331
#[ test]
327
332
fn heap_pool_test ( ) {
328
333
{
329
- define_allocator_memory_pool ! ( heap_global_buffer , 4096 , u8 , [ 0 ; 6 * 1024 * 1024 ] , heap) ;
334
+ let mut heap_global_buffer = define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 6 * 1024 * 1024 ] , heap) ;
330
335
let mut ags = HeapAllocatedFreelist :: < u8 > :: new_allocator ( 4096 , & mut heap_global_buffer, bzero) ;
331
336
{
332
337
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -357,7 +362,7 @@ fn heap_pool_test() {
357
362
fn calloc_pool_test ( ) {
358
363
359
364
{
360
- define_allocator_memory_pool ! ( calloc_global_buffer , 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc) ;
365
+ let calloc_global_buffer = unsafe { define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc) } ;
361
366
let mut ags = CallocAllocatedFreelist4096 :: < u8 > :: new_allocator ( calloc_global_buffer, bzero) ;
362
367
{
363
368
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -391,7 +396,7 @@ fn calloc_pool_test() {
391
396
fn calloc_leak_pool_test ( ) {
392
397
393
398
{
394
- define_allocator_memory_pool ! ( calloc_global_buffer , 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc_no_free) ;
399
+ let calloc_global_buffer = unsafe { define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , calloc_no_free) } ;
395
400
let mut ags = CallocAllocatedFreelist4096 :: < u8 > :: new_allocator ( calloc_global_buffer, bzero) ;
396
401
{
397
402
let mut x = ags. alloc_cell ( 9999 ) ;
@@ -419,11 +424,49 @@ fn calloc_leak_pool_test() {
419
424
}
420
425
}
421
426
427
+
428
+ #[ test]
429
+ fn malloc_pool_test ( ) {
430
+
431
+ {
432
+ let malloc_global_buffer = unsafe { define_allocator_memory_pool ! ( 4096 , u8 , [ 0 ; 200 * 1024 * 1024 ] , malloc) } ;
433
+ let mut ags = MallocAllocatedFreelist4096 :: < u8 > :: new_allocator ( malloc_global_buffer, bzero) ;
434
+ {
435
+ let mut x = ags. alloc_cell ( 9999 ) ;
436
+ x. slice_mut ( ) [ 0 ] = 4 ;
437
+ let mut y = ags. alloc_cell ( 4 ) ;
438
+ y[ 0 ] = 5 ;
439
+ ags. free_cell ( y) ;
440
+
441
+ let mut three = ags. alloc_cell ( 3 ) ;
442
+ three[ 0 ] = 6 ;
443
+ ags. free_cell ( three) ;
444
+
445
+ let mut z = ags. alloc_cell ( 4 ) ;
446
+ z. slice_mut ( ) [ 1 ] = 8 ;
447
+ let mut reget_three = ags. alloc_cell ( 4 ) ;
448
+ reget_three. slice_mut ( ) [ 1 ] = 9 ;
449
+ //y.mem[0] = 6; // <-- this is an error (use after free)
450
+ assert_eq ! ( x[ 0 ] , 4 ) ;
451
+ assert_eq ! ( z[ 0 ] , 0 ) ;
452
+ assert_eq ! ( z[ 1 ] , 8 ) ;
453
+ assert_eq ! ( reget_three[ 0 ] , 0 ) ;
454
+ assert_eq ! ( reget_three[ 1 ] , 9 ) ;
455
+ let mut _z = ags. alloc_cell ( 1 ) ;
456
+ }
457
+ }
458
+ }
459
+
460
+
461
+
462
+
422
463
#[ test]
423
464
fn global_pool_test ( ) {
424
465
{
425
466
let mut ags = GlobalAllocatedFreelist :: < u8 > :: new_allocator ( bzero) ;
467
+ unsafe {
426
468
bind_global_buffers_to_allocator ! ( ags, global_buffer2, u8 ) ;
469
+ }
427
470
{
428
471
let mut x = ags. alloc_cell ( 9999 ) ;
429
472
x. slice_mut ( ) [ 0 ] = 4 ;
0 commit comments