From 1c6dc303b83de5b7c86037050d12c30c20bae18b Mon Sep 17 00:00:00 2001 From: Bryan McShea Date: Tue, 11 Oct 2022 21:02:17 -0400 Subject: [PATCH] ok I need to actually do school work --- src/lib/heap.s | 39 ++++++++++++++++--------------- src/lib/str.s | 3 +-- src/test/heap.s | 62 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/lib/heap.s b/src/lib/heap.s index 8ae1e5c..34967b0 100644 --- a/src/lib/heap.s +++ b/src/lib/heap.s @@ -7,7 +7,7 @@ .equ align_imask, 0b111 .equ align_mask, ~align_imask -.equ used_mask, 0b001 +.equ prev_used, 0b001 .equ size_mask, align_mask # block node @@ -36,14 +36,16 @@ heap_oom: .section .data .align 4 -.equ heap_start, 0 -.equ heap_first_free, 8 # matches fb_next -.equ heap_end, 16 -.global heap_info # for testing only +.global heap_info # global for testing only +.equ heap_last_used, 0 # last free block will set this +.equ heap_first_free, 8 # matches fb_next +.equ heap_start, 16 +.equ heap_end, 24 heap_info: - .dword __global_pointer$ .dword 0 .dword 0 + .dword __global_pointer$ + .dword 0 @@ -72,23 +74,23 @@ heap_init: li a0, 0 jal brk andi a0, a0, align_mask - sub t2, a0, t1 # size = end - start (t2) + sub t2, a0, t1 # size = end - start (t2) li t3, sizeof_fb - bgeu t2, t3, 0f # check if enough mem to start + bgeu t2, t3, 0f # check if enough mem to start move a1, a0 add a0, t1, t3 - jal brk_or_panic # if not, get more - sub t2, a0, t1 # recalc size (t2) + jal brk_or_panic # if not, get more + sub t2, a0, t1 # recalc size (t2) 0: sd a0, heap_end(t0) # create initial free block - ori t2, t2, 0x1 # add prev used - sd t2, bsize(t1) # store size - sd t0, fb_next(t1) # store next - sd t1, -8(a0) # store addr at end + ori t2, t2, prev_used # add prev used + sd t2, bsize(t1) # store size + sd t0, fb_next(t1) # store next + sd t1, -8(a0) # store addr at end sd t1, heap_first_free(t0) @@ -149,10 +151,9 @@ heap_alloc: jal brk_or_panic # set heap end (a0) sd a0, heap_end(t3) # update end in info sub t2, a0, t1 # new free space (t2) - andi t5, t2, 1 # prev used bit + andi t5, t2, prev_used # prev used bit sd t5, bsize(t1) # update size sd t3, fb_next(t1) # update next (for case 3) - 0: # deal with extra free space @@ -165,7 +166,7 @@ heap_alloc: sd t3, fb_next(t6) # set prev block next to new add t6, t3, t5 # store addr at end sd t3, -8(t6) - ori t6, t5, 1 # store size with prev in use + ori t6, t5, prev_used # store size with prev in use sd t6, bsize(t3) ld t6, fb_next(t1) # copy next sd t6, fb_next(t3) @@ -178,14 +179,14 @@ heap_alloc: add t3, t1, t0 beq t3, t4, 1f # if this is not at the end ld t4, bsize(t3) # set next's prev used to 1 - ori t4, t4, 1 + ori t4, t4, prev_used sd t4, bsize(t3) 1: # create used block add t2, t0, t1 # end of block - ori t0, t0, 1 # prev used bit + ori t0, t0, prev_used # prev used bit sd t0, bsize(t1) # store size sd zero, fb_next(t1) # remove free block stuff sd zero, -8(t2) diff --git a/src/lib/str.s b/src/lib/str.s index b412e10..ee4b66c 100644 --- a/src/lib/str.s +++ b/src/lib/str.s @@ -27,11 +27,10 @@ strlen: strcpy: 0: lb t0, (a1) - beqz t0, 0f sb t0, (a0) + beqz t0, 0f addi a0, a0, 1 addi a1, a1, 1 j 0b 0: - sb t0, (a0) ret diff --git a/src/test/heap.s b/src/test/heap.s index 3b4ccc8..8fc6b81 100644 --- a/src/test/heap.s +++ b/src/test/heap.s @@ -1,13 +1,18 @@ .section .rodata -.word 0f - print_msg -print_msg: - .string "Current heap:" +.word 0f - hinfo_msg +hinfo_msg: + .string "Current heap info:" +0: + +.word 0f - block_msg +block_msg: + .string "Current heap blocks:" 0: .word 0f - dots dots: - .string "................" + .string "..." 0: .word 0f - hexstr @@ -73,19 +78,57 @@ heap_test: ret print_heap: - addi sp, sp, -(3*8) + addi sp, sp, -(4*8) sd ra, 0(sp) sd s0, 8(sp) sd s1, 16(sp) + sd s2, 24(sp) - la a0, print_msg + # info + + la a0, hinfo_msg + lw a1, -4(a0) + jal print + jal print_space + + la a0, hexstr + lw a1, -4(a0) + jal print + + la s0, heap_info + move a0, s0 + jal print_hex + jal printnl + + li s1, 0 + li s2, 4 +0: + beq s1, s2, 0f + jal print_space + jal print_space + li t1, 8 + mul t0, s1, t1 + add t0, s0, t0 + ld a0, 0(t0) + jal print_hex + jal printnl + addi s1, s1, 1 + j 0b +0: + + # blocks + + la a0, block_msg lw a1, -4(a0) jal println la t0, heap_info - ld s0, 0(t0) - ld s1, 16(t0) + ld s0, 16(t0) + ld s1, 24(t0) 0: + jal print_space + jal print_space + la a0, hexstr lw a1, -4(a0) jal print @@ -121,5 +164,6 @@ print_heap: ld ra, 0(sp) ld s0, 8(sp) ld s1, 16(sp) - addi sp, sp, +(3*8) + ld s2, 24(sp) + addi sp, sp, +(4*8) ret