Files

102 lines
2.4 KiB
Plaintext

mem :: import "@std/mem"
raw_allocator_test func() i32 {
resized ?*mut u8 := mem.raw_realloc(mem.c_allocator, null, 0, 4, 1)
if resized |bytes| {
bytes[0] = 10
bytes[1] = 20
bytes[2] = 30
bytes[3] = 40
} else {
return 20
}
grown ?*mut u8 := mem.raw_realloc(mem.c_allocator, resized, 4, 8, 1)
if grown |bytes| {
resized = grown
if bytes[0] != 10 or bytes[1] != 20 or bytes[2] != 30 or bytes[3] != 40 {
mem.raw_free(mem.c_allocator, grown, 8, 1)
return 21
}
} else {
mem.raw_free(mem.c_allocator, resized, 4, 1)
return 22
}
shrunk ?*mut u8 := mem.raw_realloc(mem.c_allocator, resized, 8, 2, 1)
if shrunk |bytes| {
resized = shrunk
if bytes[0] != 10 or bytes[1] != 20 {
mem.raw_free(mem.c_allocator, shrunk, 2, 1)
return 23
}
} else {
mem.raw_free(mem.c_allocator, resized, 8, 1)
return 24
}
invalid ?*mut u8 := mem.raw_realloc(mem.c_allocator, resized, 2, 4, 24)
if invalid |memory| {
mem.raw_free(mem.c_allocator, memory, 4, 24)
mem.raw_free(mem.c_allocator, resized, 2, 1)
return 25
}
if resized |bytes| {
if bytes[0] != 10 or bytes[1] != 20 {
mem.raw_free(mem.c_allocator, resized, 2, 1)
return 26
}
}
resized = mem.raw_realloc(mem.c_allocator, resized, 2, 0, 1)
if resized |memory| {
mem.raw_free(mem.c_allocator, memory, 0, 1)
return 27
}
over_aligned ?*mut u8 := mem.raw_alloc(mem.c_allocator, 4, 32)
if over_aligned |bytes| {
bytes[0] = 11
bytes[1] = 22
} else {
return 28
}
over_aligned_grown ?*mut u8 := mem.raw_realloc(mem.c_allocator, over_aligned, 4, 8, 32)
if over_aligned_grown |bytes| {
if bytes[0] != 11 or bytes[1] != 22 {
mem.raw_free(mem.c_allocator, over_aligned_grown, 8, 32)
return 29
}
mem.raw_free(mem.c_allocator, over_aligned_grown, 8, 32)
} else {
mem.raw_free(mem.c_allocator, over_aligned, 4, 32)
return 30
}
zero_alignment ?*mut u8 := mem.raw_alloc(mem.c_allocator, 8, 0)
if zero_alignment |memory| {
mem.raw_free(mem.c_allocator, memory, 8, 0)
return 1
}
bad_alignment ?*mut u8 := mem.raw_alloc(mem.c_allocator, 8, 24)
if bad_alignment |memory| {
mem.raw_free(mem.c_allocator, memory, 8, 24)
return 2
}
aligned ?*mut u8 := mem.raw_alloc(mem.c_allocator, 64, 32)
defer mem.raw_free(mem.c_allocator, aligned, 64, 32)
if aligned |bytes| {
bytes[0] = 1
bytes[63] = 2
if bytes[0] + bytes[63] != 3 {
return 4
}
} else {
return 3
}
return 0
}