' Memory pool: block allocator with allocate/free tracking
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL pool(1 TO 10) AS LONG
    LOCAL used(1 TO 10) AS LONG
    LOCAL i  AS LONG
    LOCAL idx  AS LONG
    LOCAL allocCount  AS LONG
    PRINT "=== Memory Pool Allocator ==="
    PRINT
    FOR i = 1 TO 10
        pool(i) = i * 100
        used(i) = 0
    NEXT i
    PRINT "Pool initialized with 10 blocks"
    PRINT
    allocCount = 0
    FOR i = 1 TO 10
        IF used(i) = 0 THEN
            used(i) = 1
            pool(i) = allocCount * 1000 + 42
            allocCount = allocCount + 1
            PRINT "Allocated block " + FORMAT$(i) + " value=" + FORMAT$(pool(i))
            IF allocCount >= 3 THEN EXIT FOR
        END IF
    NEXT i
    PRINT "Allocated: " + FORMAT$(allocCount) + " blocks"
    PRINT
    PRINT "Freeing block 2..."
    used(2) = 0
    PRINT "Freeing block 3..."
    used(3) = 0
    PRINT
    allocCount = 0
    FOR i = 1 TO 10
        IF used(i) = 0 THEN
            used(i) = 1
            pool(i) = 9999
            allocCount = allocCount + 1
            PRINT "Re-allocated block " + FORMAT$(i) + " value=" + FORMAT$(pool(i))
            IF allocCount >= 2 THEN EXIT FOR
        END IF
    NEXT i
    PRINT
    allocCount = 0
    FOR i = 1 TO 10
        IF used(i) = 1 THEN allocCount = allocCount + 1
    NEXT i
    PRINT "Currently allocated: " + FORMAT$(allocCount) + " / 10 blocks"
    PRINT
    PRINT "Memory pool tests complete."
    FUNCTION = 0
END FUNCTION
