' debug_memwatch.pb — Memory watch/tracker simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL allocSizes(100) AS LONG
    LOCAL allocAddrs(100) AS LONG
    LOCAL allocTypes(100) AS LONG
    LOCAL allocCount AS LONG
    LOCAL maxAllocs AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL totalAlloced AS LONG
    LOCAL totalFreed AS LONG
    LOCAL currentUsage AS LONG
    LOCAL peakUsage AS LONG
    LOCAL blockSize AS LONG
    LOCAL blockAddr AS LONG
    LOCAL blockType AS LONG
    LOCAL leakCount AS LONG
    LOCAL leakSize AS LONG
    LOCAL iter AS LONG
    LOCAL numOps AS LONG
    LOCAL operation AS LONG
    LOCAL freedIdx AS LONG
    LOCAL found AS LONG
    LOCAL typeStr AS STRING

    maxAllocs = 20

    PRINT "=== Memory Watch Simulation ==="

    FOR numOps = 1 TO 30
        operation = numOps MOD 3
        IF operation = 0 OR operation = 1 THEN
            IF allocCount < maxAllocs THEN
                blockSize = (numOps * 100) + 50
                blockAddr = 10000 + allocCount * 1000
                blockType = numOps MOD 2
                allocSizes(allocCount) = blockSize
                allocAddrs(allocCount) = blockAddr
                allocTypes(allocCount) = blockType
                allocCount = allocCount + 1
                totalAlloced = totalAlloced + blockSize
                currentUsage = currentUsage + blockSize
                IF currentUsage > peakUsage THEN peakUsage = currentUsage
                IF blockType = 0 THEN typeStr = "heap"
                IF blockType = 1 THEN typeStr = "stack"
                PRINT "  Alloc " + STR$(blockSize) + " bytes at 0x" + STR$(blockAddr) + " (" + typeStr + ")"
            END IF
        END IF

        IF operation = 2 THEN
            IF allocCount > 0 THEN
                freedIdx = (numOps / 3) MOD allocCount
                blockSize = allocSizes(freedIdx)
                blockAddr = allocAddrs(freedIdx)
                totalFreed = totalFreed + blockSize
                currentUsage = currentUsage - blockSize

                FOR i = freedIdx TO allocCount - 2
                    allocSizes(i) = allocSizes(i + 1)
                    allocAddrs(i) = allocAddrs(i + 1)
                    allocTypes(i) = allocTypes(i + 1)
                NEXT i
                allocCount = allocCount - 1
                PRINT "  Free at 0x" + STR$(blockAddr) + " (" + STR$(blockSize) + " bytes)"
            END IF
        END IF
    NEXT numOps

    leakCount = allocCount
    leakSize = 0
    FOR i = 0 TO allocCount - 1
        leakSize = leakSize + allocSizes(i)
    NEXT i

    PRINT "--- Memory Summary ---"
    PRINT "Total allocated: " + STR$(totalAlloced) + " bytes"
    PRINT "Total freed: " + STR$(totalFreed) + " bytes"
    PRINT "Current usage: " + STR$(currentUsage) + " bytes"
    PRINT "Peak usage: " + STR$(peakUsage) + " bytes"
    PRINT "Leaks: " + STR$(leakCount) + " blocks, " + STR$(leakSize) + " bytes"
    PRINT "=== End MemWatch ==="
    FUNCTION = 0
END FUNCTION
