197 lines
4.5 KiB
Text
197 lines
4.5 KiB
Text
//-----------------------------------------------------------
|
|
// Memory Library Demo 2: Defrag & Stats
|
|
// Demonstrates heap defragmentation and statistics
|
|
//-----------------------------------------------------------
|
|
|
|
#PRAGMA _P_REMOVE_UNUSED 1
|
|
|
|
#INCLUDE <c64start.c65>
|
|
#INCLUDE <c64defs.c65>
|
|
#INCLUDE <memlib.c65>
|
|
#INCLUDE <cbmiolib.c65>
|
|
|
|
#PRAGMA _P_USE_CBM_STRINGS 1
|
|
|
|
GOTO start
|
|
|
|
WORD keep1
|
|
WORD keep2
|
|
WORD ptr_a
|
|
WORD ptr_b
|
|
WORD ptr_c
|
|
WORD ptr_d
|
|
|
|
WORD free_blocks
|
|
WORD total_free
|
|
WORD largest_free
|
|
|
|
//-----------------------------------------------------------
|
|
// Wait for key
|
|
//-----------------------------------------------------------
|
|
FUNC wait_key
|
|
BYTE key
|
|
|
|
WHILE 1
|
|
key = PEEK $c5
|
|
IF key = 64
|
|
BREAK
|
|
ENDIF
|
|
WEND
|
|
|
|
WHILE 1
|
|
key = PEEK $c5
|
|
IF key != 64
|
|
BREAK
|
|
ENDIF
|
|
WEND
|
|
|
|
POKE $c6 WITH 0
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Print stats with label (hex output)
|
|
//-----------------------------------------------------------
|
|
FUNC print_stats
|
|
lib_cbmio_printlf("stats:")
|
|
|
|
lib_mem_stats(free_blocks, total_free, largest_free)
|
|
|
|
lib_cbmio_print(" blocks: $")
|
|
lib_cbmio_hexoutw(free_blocks)
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print(" free: $")
|
|
lib_cbmio_hexoutw(total_free)
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print(" biggest:$")
|
|
lib_cbmio_hexoutw(largest_free)
|
|
lib_cbmio_lf()
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Main demo
|
|
//-----------------------------------------------------------
|
|
FUNC main
|
|
|
|
lib_cbmio_cls()
|
|
|
|
lib_cbmio_printlf("memlib demo 2: defrag")
|
|
lib_cbmio_printlf("======================")
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_print("initializing...")
|
|
lib_mem_init()
|
|
lib_cbmio_printlf("ready")
|
|
lib_cbmio_lf()
|
|
|
|
// Show initial state
|
|
lib_cbmio_printlf("after init:")
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Allocate 6 blocks.
|
|
// Keep keep1 and keep2 allocated to create three gaps:
|
|
// heap: [keep1][free][keep2][free][free]
|
|
lib_cbmio_printlf("allocating 6 blocks")
|
|
lib_mem_malloc(20, keep1)
|
|
lib_mem_malloc(30, ptr_a)
|
|
lib_mem_malloc(40, keep2)
|
|
lib_mem_malloc(50, ptr_b)
|
|
lib_mem_malloc(20, ptr_c)
|
|
lib_mem_malloc(60, ptr_d)
|
|
|
|
lib_cbmio_print("keep1: $")
|
|
lib_cbmio_hexoutw(keep1)
|
|
lib_cbmio_print(" ptr_a: $")
|
|
lib_cbmio_hexoutw(ptr_a)
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("keep2: $")
|
|
lib_cbmio_hexoutw(keep2)
|
|
lib_cbmio_print(" ptr_b: $")
|
|
lib_cbmio_hexoutw(ptr_b)
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("ptr_c: $")
|
|
lib_cbmio_hexoutw(ptr_c)
|
|
lib_cbmio_print(" ptr_d: $")
|
|
lib_cbmio_hexoutw(ptr_d)
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Free ptr_a and ptr_b (non-adjacent, between keep1 and keep2, and after keep2)
|
|
lib_cbmio_printlf("freeing ptr_a and ptr_b")
|
|
lib_mem_free(ptr_a)
|
|
lib_mem_free(ptr_b)
|
|
lib_cbmio_printlf("freed")
|
|
lib_cbmio_lf()
|
|
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Free ptr_c and ptr_d (adjacent to each other, after ptr_b's free block)
|
|
// Now gaps are: [keep1][gap_a][keep2][gap_b][gap_c]
|
|
// gap_a and gap_b are separated by keep2
|
|
// gap_b and gap_c are adjacent -> should merge after freeing gap_c
|
|
lib_cbmio_printlf("freeing ptr_c and ptr_d")
|
|
lib_mem_free(ptr_c)
|
|
lib_mem_free(ptr_d)
|
|
lib_cbmio_printlf("freed")
|
|
lib_cbmio_lf()
|
|
|
|
// Show fragmented state (keep1 and keep2 still allocated)
|
|
lib_cbmio_printlf("before defrag (keep1,keep2 allocated):")
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Defrag!
|
|
lib_cbmio_print("running defrag...")
|
|
lib_mem_defrag()
|
|
lib_cbmio_printlf("done")
|
|
lib_cbmio_lf()
|
|
|
|
// Show defragged state:
|
|
// After defrag: [keep1][merged_gap_a][keep2][merged_gap_b+gap_c]
|
|
// Should be 2 free blocks (gap_a and gap_b+gap_c are separate, keep2 sits between)
|
|
lib_cbmio_printlf("after defrag:")
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
|
|
// Now free keep1 and keep2 and defrag again to show full coalesce
|
|
lib_cbmio_printlf("freeing keep1 and keep2")
|
|
lib_mem_free(keep1)
|
|
lib_mem_free(keep2)
|
|
lib_cbmio_printlf("all freed")
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_print("running defrag...")
|
|
lib_mem_defrag()
|
|
lib_cbmio_printlf("done")
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_printlf("after full defrag:")
|
|
print_stats()
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_printlf("done!")
|
|
FEND
|
|
|
|
LABEL start
|
|
main()
|
|
SUBEND
|