把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
543 lines
19 KiB
Plaintext
543 lines
19 KiB
Plaintext
import t, c
|
|
from t import CInt, CPtr, CChar, CUInt8T, CUInt32T, CUInt64T, CSizeT
|
|
import viperlib
|
|
import viperio
|
|
import stdlib
|
|
import memhub
|
|
import string
|
|
import testcheck
|
|
import w32.win32process
|
|
import w32.win32sync
|
|
import w32.win32base
|
|
|
|
|
|
# Thread body: alloc/free on a shared MBuddy to exercise the spinlock
|
|
def _thread_alloc_free(param: t.CPtr) -> uint32:
|
|
bd: memhub.MemBuddy* = (memhub.MemBuddy*)(param)
|
|
i: CInt
|
|
for i in range(100):
|
|
p: bytes = bd.alloc(32)
|
|
if p != None:
|
|
p_c: CUInt8T | CPtr = CPtr(p)
|
|
p_c[0] = CUInt8T(i & 0xFF)
|
|
bd.free(p)
|
|
return 0
|
|
|
|
|
|
def mbuddy_main() -> CInt:
|
|
buf: bytes = stdlib.malloc(256)
|
|
|
|
testcheck.begin("BuddyTest: 伙伴系统内存分配器测试")
|
|
|
|
# === Basic alloc/free ===
|
|
testcheck.section("基础 alloc/free")
|
|
arena: bytes = stdlib.malloc(65536)
|
|
bd: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena, 65536)
|
|
|
|
viperlib.snprintf(buf, 256, "stats=%lu, free_count=%lu", bd.stats(), bd.free_count())
|
|
testcheck.info(buf)
|
|
|
|
p1: bytes = bd.alloc(16)
|
|
p2: bytes = bd.alloc(64)
|
|
p3: bytes = bd.alloc(256)
|
|
testcheck.check(p1 != None and p2 != None and p3 != None,
|
|
"3 allocs OK", "alloc FAILED")
|
|
|
|
# Write data to verify the returned pointer is usable
|
|
p_c: CUInt8T | CPtr = CPtr(p1)
|
|
p_c[0] = CUInt8T(0xAB)
|
|
p_c[1] = CUInt8T(0xCD)
|
|
viperlib.snprintf(buf, 256, "p1[0]=0x%x p1[1]=0x%x", p_c[0], p_c[1])
|
|
testcheck.info(buf)
|
|
testcheck.check(p_c[0] == CUInt8T(0xAB) and p_c[1] == CUInt8T(0xCD),
|
|
"write/read OK", "write/read FAILED")
|
|
|
|
bd.free(p1)
|
|
bd.free(p2)
|
|
bd.free(p3)
|
|
testcheck.ok("3 frees OK")
|
|
|
|
# After free, coalescing must restore the initial 10 free blocks
|
|
# (65536 - 264 = 65272 decomposes into 10 power-of-two blocks)
|
|
testcheck.check(bd.free_count() == 10,
|
|
"coalesce: free_count=10 OK", "coalesce: free_count != 10")
|
|
|
|
# Coalesced space must allow a large alloc again
|
|
big: bytes = bd.alloc(16000)
|
|
testcheck.check(big != None, "big alloc OK", "big alloc FAILED")
|
|
bd.free(big)
|
|
|
|
stdlib.free(arena)
|
|
|
|
# === calloc (verify zeroing) ===
|
|
testcheck.section("calloc 清零")
|
|
arena2: bytes = stdlib.malloc(4096)
|
|
viperlib.snprintf(buf, 256, "arena2 = %p", arena2)
|
|
testcheck.info(buf)
|
|
bd2: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena2, 4096)
|
|
|
|
dirty: bytes = bd2.alloc(64)
|
|
viperlib.snprintf(buf, 256, "dirty = %p", dirty)
|
|
testcheck.info(buf)
|
|
d_c: CUInt8T | CPtr = CPtr(dirty)
|
|
i: CInt
|
|
for i in range(64):
|
|
d_c[i] = CUInt8T(0xFF)
|
|
bd2.free(dirty)
|
|
|
|
cl: bytes = bd2.calloc(8, 8)
|
|
viperlib.snprintf(buf, 256, "cl = %p (arena2=%p, dirty was=%p)", cl, arena2, dirty)
|
|
testcheck.info(buf)
|
|
if cl != None:
|
|
cl_c: CUInt8T | CPtr = CPtr(cl)
|
|
all_zero: CInt = 1
|
|
j: CInt
|
|
for j in range(64):
|
|
if cl_c[j] != 0:
|
|
all_zero = 0
|
|
break
|
|
testcheck.check(all_zero == 1, "calloc zeroed OK", "calloc NOT zeroed")
|
|
bd2.free(cl)
|
|
else:
|
|
testcheck.fail("calloc FAILED")
|
|
|
|
stdlib.free(arena2)
|
|
|
|
# === realloc grow ===
|
|
testcheck.section("realloc 扩展")
|
|
arena3: bytes = stdlib.malloc(4096)
|
|
bd3: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena3, 4096)
|
|
|
|
rp: bytes = bd3.alloc(32)
|
|
rp_c: CUInt8T | CPtr = CPtr(rp)
|
|
k: CInt
|
|
for k in range(32):
|
|
rp_c[k] = CUInt8T(k + 1)
|
|
|
|
rp2: bytes = bd3.realloc(rp, 128)
|
|
if rp2 != None:
|
|
rp2_c: CUInt8T | CPtr = CPtr(rp2)
|
|
ok: CInt = 1
|
|
m: CInt
|
|
for m in range(32):
|
|
if rp2_c[m] != CUInt8T(m + 1):
|
|
ok = 0
|
|
break
|
|
testcheck.check(ok == 1, "realloc grow data preserved OK", "realloc grow data LOST")
|
|
bd3.free(rp2)
|
|
else:
|
|
testcheck.fail("realloc grow FAILED")
|
|
|
|
stdlib.free(arena3)
|
|
|
|
# === in-place shrink (verify no leak) ===
|
|
testcheck.section("原地收缩 (realloc shrink)")
|
|
arena_shrink: bytes = stdlib.malloc(4096)
|
|
bds: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_shrink, 4096)
|
|
|
|
sp: bytes = bds.alloc(256)
|
|
testcheck.check(sp != None, "alloc 256 OK", "alloc 256 FAILED")
|
|
|
|
fc_before: CSizeT = bds.free_count()
|
|
# Shrink 256 -> 32: order 4 (264) to order 1 (40); since new order <= old,
|
|
# realloc must return the same pointer in place
|
|
sp2: bytes = bds.realloc(sp, 32)
|
|
fc_after: CSizeT = bds.free_count()
|
|
|
|
testcheck.check(sp2 == sp, "shrink same ptr OK", "shrink ptr changed")
|
|
testcheck.check(fc_after == fc_before, "shrink no leak OK", "shrink LEAKED blocks")
|
|
|
|
# Data must remain readable/writable after shrink
|
|
sp_c: CUInt8T | CPtr = CPtr(sp2)
|
|
sp_c[0] = CUInt8T(0x42)
|
|
testcheck.check(sp_c[0] == CUInt8T(0x42), "shrink data OK", "shrink data FAILED")
|
|
|
|
bds.free(sp2)
|
|
testcheck.check(bds.free_count() == 6, "shrink+free coalesce OK", "shrink+free coalesce FAILED")
|
|
|
|
stdlib.free(arena_shrink)
|
|
|
|
# === Edge cases ===
|
|
testcheck.section("边界情况")
|
|
arena4: bytes = stdlib.malloc(4096)
|
|
bd4: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena4, 4096)
|
|
|
|
# alloc(0) must return None
|
|
z: bytes = bd4.alloc(0)
|
|
testcheck.check(z == None, "alloc(0)=None OK", "alloc(0) should be None")
|
|
|
|
# free(None) must be safe
|
|
bd4.free(None)
|
|
testcheck.ok("free(None) safe")
|
|
|
|
# realloc(None, size) behaves like alloc
|
|
rn: bytes = bd4.realloc(None, 32)
|
|
testcheck.check(rn != None, "realloc(None)=alloc OK", "realloc(None) FAILED")
|
|
bd4.free(rn)
|
|
|
|
# realloc(ptr, 0) behaves like free
|
|
rz: bytes = bd4.alloc(32)
|
|
rz2: bytes = bd4.realloc(rz, 0)
|
|
testcheck.check(rz2 == None, "realloc(ptr,0)=free OK", "realloc(ptr,0) should be None")
|
|
|
|
stdlib.free(arena4)
|
|
|
|
# === Illegal pointer defense ===
|
|
testcheck.section("非法指针防护")
|
|
arena5: bytes = stdlib.malloc(4096)
|
|
bd5: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena5, 4096)
|
|
|
|
valid: bytes = bd5.alloc(32)
|
|
testcheck.check(valid != None, "alloc for illegal test OK", "alloc FAILED")
|
|
|
|
# 1. free a random address (address 1)
|
|
bd5.free(t.CPtr(uint64(1)))
|
|
testcheck.ok("free(addr=1) ignored")
|
|
|
|
# 2. free NULL
|
|
bd5.free(None)
|
|
testcheck.ok("free(NULL) ignored")
|
|
|
|
# 3. free an out-of-range pointer (before arena)
|
|
bd5.free(t.CPtr(uint64(arena5)))
|
|
testcheck.ok("free(before arena) ignored")
|
|
|
|
# 4. free an out-of-range pointer (after arena)
|
|
bd5.free(t.CPtr(uint64(arena5) + 99999))
|
|
testcheck.ok("free(after arena) ignored")
|
|
|
|
# 5. free a misaligned pointer (inside mem but not aligned)
|
|
bd5.free(t.CPtr(uint64(bd5.base) + 1))
|
|
testcheck.ok("free(misaligned) ignored")
|
|
|
|
# 6. a valid free must still work (block reusable after)
|
|
bd5.free(valid)
|
|
retry5: bytes = bd5.alloc(32)
|
|
testcheck.check(retry5 != None, "valid free still works OK", "valid free BROKEN")
|
|
if retry5 != None:
|
|
bd5.free(retry5)
|
|
|
|
# 7. realloc on an illegal pointer must return None
|
|
r_illegal: bytes = bd5.realloc(t.CPtr(1), 64)
|
|
testcheck.check(r_illegal == None, "realloc(illegal)=None OK", "realloc(illegal) should be None")
|
|
|
|
stdlib.free(arena5)
|
|
|
|
# === OOM exhaustion ===
|
|
testcheck.section("OOM 内存耗尽")
|
|
arena6: bytes = stdlib.malloc(4096)
|
|
bd6: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena6, 4096)
|
|
|
|
# Allocate until failure
|
|
alloc_count: CInt = 0
|
|
ptrs_arr: bytes = stdlib.malloc(8 * 256) # Pointer storage array
|
|
ptrs: CUInt64T | CPtr = CPtr(ptrs_arr)
|
|
|
|
while alloc_count < 256:
|
|
p: bytes = bd6.alloc(32)
|
|
if p == None:
|
|
break
|
|
ptrs[alloc_count] = uint64(p)
|
|
alloc_count += 1
|
|
|
|
viperlib.snprintf(buf, 256, "allocated %d blocks before OOM", alloc_count)
|
|
testcheck.info(buf)
|
|
testcheck.check(alloc_count > 0, "OOM: got some allocs OK", "OOM: no allocs at all")
|
|
|
|
# Another alloc must now fail
|
|
oom_p: bytes = bd6.alloc(32)
|
|
testcheck.check(oom_p == None, "OOM: alloc fails OK", "OOM: alloc should fail")
|
|
|
|
# After freeing one block, allocation must succeed again
|
|
if alloc_count > 0:
|
|
first_ptr: bytes = t.CPtr(ptrs[0])
|
|
bd6.free(first_ptr)
|
|
retry: bytes = bd6.alloc(32)
|
|
testcheck.check(retry != None, "OOM: free+alloc OK", "OOM: free+alloc FAILED")
|
|
if retry != None:
|
|
bd6.free(retry)
|
|
|
|
# Free all blocks
|
|
for idx in range(1, alloc_count):
|
|
cur_p: bytes = t.CPtr(ptrs[idx])
|
|
bd6.free(cur_p)
|
|
|
|
# Full free must coalesce back to the initial 6 blocks
|
|
testcheck.check(bd6.free_count() == 6,
|
|
"OOM: full coalesce OK", "OOM: free_count != 6 after full free")
|
|
|
|
stdlib.free(ptrs_arr)
|
|
stdlib.free(arena6)
|
|
|
|
# === Free-list state ===
|
|
testcheck.section("空闲链表状态")
|
|
arena7: bytes = stdlib.malloc(4096)
|
|
bd7: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena7, 4096)
|
|
|
|
# Initial: non-power-of-two arena (4096 - 264 = 3832) splits into 6 blocks
|
|
testcheck.check(bd7.free_count() == 6, "init: 6 free blocks OK", "init: free_count != 6")
|
|
|
|
# After alloc: free count grows because a block is split
|
|
a1: bytes = bd7.alloc(32)
|
|
fc1: CSizeT = bd7.free_count()
|
|
viperlib.snprintf(buf, 256, "after alloc(32): free_count=%lu", fc1)
|
|
testcheck.info(buf)
|
|
testcheck.check(fc1 > 1, "after alloc: split OK", "after alloc: no split")
|
|
|
|
# Second alloc
|
|
a2: bytes = bd7.alloc(32)
|
|
fc2: CSizeT = bd7.free_count()
|
|
testcheck.check(fc2 >= 1, "after 2nd alloc: free >= 1 OK", "after 2nd alloc: free < 1")
|
|
|
|
# Free a1: free count must increase
|
|
bd7.free(a1)
|
|
fc3: CSizeT = bd7.free_count()
|
|
testcheck.check(fc3 > fc2, "after free a1: free increased OK", "after free a1: free not increased")
|
|
|
|
# Free a2: coalescing must restore the initial 6 blocks
|
|
bd7.free(a2)
|
|
testcheck.check(bd7.free_count() == 6,
|
|
"after free all: coalesced to 6 OK", "after free all: free_count != 6")
|
|
|
|
stdlib.free(arena7)
|
|
|
|
# === with context manager ===
|
|
testcheck.section("with 上下文管理器")
|
|
arena8: bytes = stdlib.malloc(4096)
|
|
with memhub.MemBuddy(arena8, 4096) as wbd:
|
|
wp1: bytes = wbd.alloc(32)
|
|
wp2: bytes = wbd.alloc(64)
|
|
testcheck.check(wp1 != None and wp2 != None,
|
|
"with: allocs OK", "with: allocs FAILED")
|
|
# Leaving the with block triggers __exit__ auto-reset
|
|
|
|
# Re-enter after exit to verify the reset took effect
|
|
with memhub.MemBuddy(arena8, 4096) as wbd2:
|
|
wp3: bytes = wbd2.alloc(1024)
|
|
testcheck.check(wp3 != None, "with: reset+realloc OK", "with: reset+realloc FAILED")
|
|
|
|
stdlib.free(arena8)
|
|
|
|
# === Double-free detection ===
|
|
testcheck.section("双重 free 检测")
|
|
arena_df: bytes = stdlib.malloc(4096)
|
|
bd_df: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_df, 4096)
|
|
|
|
df: bytes = bd_df.alloc(64)
|
|
testcheck.check(df != None, "df: alloc OK", "df: alloc FAILED")
|
|
|
|
# First free
|
|
bd_df.free(df)
|
|
fc_df1: CSizeT = bd_df.free_count()
|
|
testcheck.ok("first free OK")
|
|
|
|
# Second free (double free): must be silently rejected
|
|
bd_df.free(df)
|
|
fc_df2: CSizeT = bd_df.free_count()
|
|
testcheck.check(fc_df2 == fc_df1, "double free rejected OK", "double free CORRUPTED state")
|
|
|
|
# Allocator must still be functional
|
|
df2: bytes = bd_df.alloc(64)
|
|
testcheck.check(df2 != None, "df: alloc after double-free OK", "df: alloc after double-free FAILED")
|
|
if df2 != None:
|
|
bd_df.free(df2)
|
|
|
|
stdlib.free(arena_df)
|
|
|
|
# === Self-check ===
|
|
testcheck.section("自检功能")
|
|
arena_sc: bytes = stdlib.malloc(4096)
|
|
bd_sc: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_sc, 4096)
|
|
|
|
# Self-check on initial state
|
|
testcheck.check(bd_sc.self_check() == 0, "self_check init OK", "self_check init FAILED")
|
|
|
|
# Self-check after alloc
|
|
sc_p1: bytes = bd_sc.alloc(32)
|
|
sc_p2: bytes = bd_sc.alloc(128)
|
|
testcheck.check(bd_sc.self_check() == 0, "self_check after alloc OK", "self_check after alloc FAILED")
|
|
|
|
# Self-check after partial free
|
|
bd_sc.free(sc_p1)
|
|
testcheck.check(bd_sc.self_check() == 0, "self_check after partial free OK", "self_check after partial free FAILED")
|
|
|
|
# Self-check after full free (should coalesce to one block)
|
|
bd_sc.free(sc_p2)
|
|
testcheck.check(bd_sc.self_check() == 0, "self_check after full free OK", "self_check after full free FAILED")
|
|
|
|
# Self-check after double free (state must be intact)
|
|
sc_p3: bytes = bd_sc.alloc(64)
|
|
bd_sc.free(sc_p3)
|
|
bd_sc.free(sc_p3) # Double free, must be rejected
|
|
testcheck.check(bd_sc.self_check() == 0, "self_check after double-free OK", "self_check after double-free CORRUPTED")
|
|
|
|
stdlib.free(arena_sc)
|
|
|
|
# === Concurrent threads (spinlock) ===
|
|
testcheck.section("并发多线程 (spinlock)")
|
|
arena_mt: bytes = stdlib.malloc(65536)
|
|
bd_mt: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_mt, 65536)
|
|
|
|
NUM_THREADS: CInt = 4
|
|
handles_arr: bytes = stdlib.malloc(8 * 4)
|
|
handles: CUInt64T | CPtr = CPtr(handles_arr)
|
|
tid: CUInt32T = 0
|
|
|
|
# Spawn 4 threads doing concurrent alloc/free
|
|
t_idx: CInt
|
|
for t_idx in range(NUM_THREADS):
|
|
h: bytes = w32.win32process.CreateThread(None, 0, _thread_alloc_free, t.CPtr(bd_mt), 0, &tid)
|
|
handles[t_idx] = CUInt64T(h)
|
|
|
|
# Wait for all threads to finish
|
|
for t_idx in range(NUM_THREADS):
|
|
h: bytes = t.CPtr(handles[t_idx])
|
|
w32.win32sync.WaitForSingleObject(h, w32.win32base.INFINITE)
|
|
w32.win32base.CloseHandle(h)
|
|
|
|
# Verify allocator state: self-check passes and all blocks coalesced
|
|
testcheck.check(bd_mt.self_check() == 0, "mt: self_check OK", "mt: self_check FAILED")
|
|
testcheck.check(bd_mt.free_count() == 10, "mt: free_count=10 OK", "mt: free_count != 10 (leaked)")
|
|
|
|
viperlib.snprintf(buf, 256, "mt: free_count=%lu", bd_mt.free_count())
|
|
testcheck.info(buf)
|
|
|
|
stdlib.free(handles_arr)
|
|
stdlib.free(arena_mt)
|
|
|
|
# === Extreme boundaries ===
|
|
testcheck.section("极端边界")
|
|
|
|
# 1. Alloc size exactly at a block boundary
|
|
arena_ex: bytes = stdlib.malloc(4096)
|
|
bd_ex: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_ex, 4096)
|
|
|
|
# 24 + 16 = 40 > 32 (8 header + 8 sentinel), needs order 1 (64 bytes)
|
|
exact_fit: bytes = bd_ex.alloc(24)
|
|
testcheck.check(exact_fit != None, "exact fit order 0 OK", "exact fit order 0 FAILED")
|
|
|
|
# 25 + 16 = 41 > 32, needs order 1 (64 bytes)
|
|
over_fit: bytes = bd_ex.alloc(25)
|
|
testcheck.check(over_fit != None, "over by 1 byte OK", "over by 1 byte FAILED")
|
|
|
|
bd_ex.free(exact_fit)
|
|
bd_ex.free(over_fit)
|
|
testcheck.check(bd_ex.free_count() == 6, "boundary: coalesce OK", "boundary: coalesce FAILED")
|
|
|
|
# 2. Alloc near the largest block limit (order6 = 2048): usable = 2048 - 16,
|
|
# so alloc(max_usable) exactly fills one order6 block
|
|
max_usable: CSizeT = 2048 - 16
|
|
viperlib.snprintf(buf, 256, "mem_size=%lu, max_block=2048, max_usable=%lu", bd_ex.mem_size, max_usable)
|
|
testcheck.info(buf)
|
|
|
|
# Exactly consumes the order6 block (5 other blocks stay free)
|
|
exact_max: bytes = bd_ex.alloc(max_usable)
|
|
testcheck.check(exact_max != None, "exact max alloc OK", "exact max alloc FAILED")
|
|
testcheck.check(bd_ex.free_count() == 5, "max alloc: 5 free blocks left OK", "max alloc: free_count != 5")
|
|
|
|
bd_ex.free(exact_max)
|
|
testcheck.check(bd_ex.free_count() == 6, "max free: coalesce OK", "max free: coalesce FAILED")
|
|
|
|
# Alloc the same size again to verify coalesced space is reusable
|
|
exact_max2: bytes = bd_ex.alloc(max_usable)
|
|
testcheck.check(exact_max2 != None, "exact max re-alloc OK", "exact max re-alloc FAILED")
|
|
bd_ex.free(exact_max2)
|
|
|
|
# 3. Alloc beyond the arena limit must fail
|
|
over_max: bytes = bd_ex.alloc(bd_ex.mem_size)
|
|
testcheck.check(over_max == None, "over max: fails OK", "over max: should fail")
|
|
|
|
over_max2: bytes = bd_ex.alloc(max_usable + 1)
|
|
testcheck.check(over_max2 == None, "max+1: fails OK", "max+1: should fail")
|
|
|
|
stdlib.free(arena_ex)
|
|
|
|
# 4. Arena too small (only fits the 264-byte free-list overhead)
|
|
arena_tiny: bytes = stdlib.malloc(264)
|
|
bd_tiny: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_tiny, 264)
|
|
testcheck.check(bd_tiny.stats() == 0, "tiny arena: stats=0 OK", "tiny arena: stats != 0")
|
|
tiny_p: bytes = bd_tiny.alloc(32)
|
|
testcheck.check(tiny_p == None, "tiny arena: alloc fails OK", "tiny arena: should fail")
|
|
stdlib.free(arena_tiny)
|
|
|
|
# 5. Arena fits exactly one minimal block (264 + 32 = 296)
|
|
arena_one: bytes = stdlib.malloc(296)
|
|
bd_one: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_one, 296)
|
|
testcheck.check(bd_one.stats() == 32, "one block: stats=32 OK", "one block: stats != 32")
|
|
|
|
# Minimal block 32 = data 16 + header 8 + sentinel 8; alloc(16) fills it
|
|
one_p: bytes = bd_one.alloc(16)
|
|
testcheck.check(one_p != None, "one block: alloc OK", "one block: alloc FAILED")
|
|
|
|
one_p2: bytes = bd_one.alloc(16)
|
|
testcheck.check(one_p2 == None, "one block: OOM OK", "one block: should be OOM")
|
|
|
|
bd_one.free(one_p)
|
|
testcheck.check(bd_one.free_count() == 1, "one block: free OK", "one block: free FAILED")
|
|
stdlib.free(arena_one)
|
|
|
|
# 6. Power-of-two vs non-power-of-two arena
|
|
arena_pow2: bytes = stdlib.malloc(512)
|
|
bd_pow2: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_pow2, 512)
|
|
# 512 - 264 = 248, decomposes to 128 + 64 + 32 = 224 (24-byte tail dropped)
|
|
testcheck.check(bd_pow2.stats() == 224, "pow2 arena: stats=224 OK", "pow2 arena: stats != 224")
|
|
stdlib.free(arena_pow2)
|
|
|
|
arena_non2: bytes = stdlib.malloc(500)
|
|
bd_non2: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_non2, 500)
|
|
# 500 - 264 = 236, decomposes to 128 + 64 + 32 = 224 (12-byte tail dropped)
|
|
testcheck.check(bd_non2.stats() == 224, "non-pow2 arena: stats=224 OK", "non-pow2 arena: stats != 224")
|
|
stdlib.free(arena_non2)
|
|
|
|
# 7. Stress: 1000 alloc/free cycles of the same size
|
|
arena_stress: bytes = stdlib.malloc(4096)
|
|
bd_stress: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_stress, 4096)
|
|
|
|
s: CInt
|
|
for s in range(1000):
|
|
sp: bytes = bd_stress.alloc(64)
|
|
if sp != None:
|
|
sp_c: CUInt8T | CPtr = CPtr(sp)
|
|
sp_c[0] = CUInt8T(s & 0xFF)
|
|
bd_stress.free(sp)
|
|
|
|
testcheck.check(bd_stress.self_check() == 0, "stress: self_check OK", "stress: self_check FAILED")
|
|
testcheck.check(bd_stress.free_count() == 6, "stress: free_count=6 OK", "stress: leaked")
|
|
stdlib.free(arena_stress)
|
|
|
|
# 8. Allocate all minimal blocks, then free them all
|
|
arena_fill: bytes = stdlib.malloc(4096)
|
|
bd_fill: memhub.MemBuddy | CPtr = memhub.MemBuddy(arena_fill, 4096)
|
|
|
|
fill_count: CInt = 0
|
|
fill_ptrs_arr: bytes = stdlib.malloc(8 * 256)
|
|
fill_ptrs: CUInt64T | CPtr = CPtr(fill_ptrs_arr)
|
|
|
|
fc: CInt
|
|
for fc in range(256):
|
|
fp: bytes = bd_fill.alloc(24)
|
|
if fp == None:
|
|
break
|
|
fill_ptrs[fill_count] = uint64(fp)
|
|
fill_count += 1
|
|
|
|
viperlib.snprintf(buf, 256, "fill: %d min blocks allocated", fill_count)
|
|
testcheck.info(buf)
|
|
testcheck.check(fill_count > 0, "fill: got blocks OK", "fill: no blocks")
|
|
testcheck.check(bd_fill.self_check() == 0, "fill: self_check OK", "fill: self_check FAILED")
|
|
|
|
# Free all
|
|
fi: CInt
|
|
for fi in range(fill_count):
|
|
fp2: bytes = t.CPtr(fill_ptrs[fi])
|
|
bd_fill.free(fp2)
|
|
|
|
testcheck.check(bd_fill.free_count() == 6, "fill: all freed OK", "fill: leaked after free all")
|
|
testcheck.check(bd_fill.self_check() == 0, "fill: self_check after free OK", "fill: self_check FAILED")
|
|
|
|
stdlib.free(fill_ptrs_arr)
|
|
stdlib.free(arena_fill)
|
|
|
|
stdlib.free(buf)
|
|
return testcheck.end()
|