ok I need to actually do school work
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
.equ align_imask, 0b111
|
.equ align_imask, 0b111
|
||||||
.equ align_mask, ~align_imask
|
.equ align_mask, ~align_imask
|
||||||
|
|
||||||
.equ used_mask, 0b001
|
.equ prev_used, 0b001
|
||||||
.equ size_mask, align_mask
|
.equ size_mask, align_mask
|
||||||
|
|
||||||
# block node
|
# block node
|
||||||
@@ -36,14 +36,16 @@ heap_oom:
|
|||||||
.section .data
|
.section .data
|
||||||
|
|
||||||
.align 4
|
.align 4
|
||||||
.equ heap_start, 0
|
.global heap_info # global for testing only
|
||||||
.equ heap_first_free, 8 # matches fb_next
|
.equ heap_last_used, 0 # last free block will set this
|
||||||
.equ heap_end, 16
|
.equ heap_first_free, 8 # matches fb_next
|
||||||
.global heap_info # for testing only
|
.equ heap_start, 16
|
||||||
|
.equ heap_end, 24
|
||||||
heap_info:
|
heap_info:
|
||||||
.dword __global_pointer$
|
|
||||||
.dword 0
|
.dword 0
|
||||||
.dword 0
|
.dword 0
|
||||||
|
.dword __global_pointer$
|
||||||
|
.dword 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -72,23 +74,23 @@ heap_init:
|
|||||||
li a0, 0
|
li a0, 0
|
||||||
jal brk
|
jal brk
|
||||||
andi a0, a0, align_mask
|
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
|
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
|
move a1, a0
|
||||||
add a0, t1, t3
|
add a0, t1, t3
|
||||||
jal brk_or_panic # if not, get more
|
jal brk_or_panic # if not, get more
|
||||||
sub t2, a0, t1 # recalc size (t2)
|
sub t2, a0, t1 # recalc size (t2)
|
||||||
0:
|
0:
|
||||||
sd a0, heap_end(t0)
|
sd a0, heap_end(t0)
|
||||||
|
|
||||||
# create initial free block
|
# create initial free block
|
||||||
|
|
||||||
ori t2, t2, 0x1 # add prev used
|
ori t2, t2, prev_used # add prev used
|
||||||
sd t2, bsize(t1) # store size
|
sd t2, bsize(t1) # store size
|
||||||
sd t0, fb_next(t1) # store next
|
sd t0, fb_next(t1) # store next
|
||||||
sd t1, -8(a0) # store addr at end
|
sd t1, -8(a0) # store addr at end
|
||||||
|
|
||||||
sd t1, heap_first_free(t0)
|
sd t1, heap_first_free(t0)
|
||||||
|
|
||||||
@@ -149,10 +151,9 @@ heap_alloc:
|
|||||||
jal brk_or_panic # set heap end (a0)
|
jal brk_or_panic # set heap end (a0)
|
||||||
sd a0, heap_end(t3) # update end in info
|
sd a0, heap_end(t3) # update end in info
|
||||||
sub t2, a0, t1 # new free space (t2)
|
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 t5, bsize(t1) # update size
|
||||||
sd t3, fb_next(t1) # update next (for case 3)
|
sd t3, fb_next(t1) # update next (for case 3)
|
||||||
|
|
||||||
0:
|
0:
|
||||||
|
|
||||||
# deal with extra free space
|
# deal with extra free space
|
||||||
@@ -165,7 +166,7 @@ heap_alloc:
|
|||||||
sd t3, fb_next(t6) # set prev block next to new
|
sd t3, fb_next(t6) # set prev block next to new
|
||||||
add t6, t3, t5 # store addr at end
|
add t6, t3, t5 # store addr at end
|
||||||
sd t3, -8(t6)
|
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)
|
sd t6, bsize(t3)
|
||||||
ld t6, fb_next(t1) # copy next
|
ld t6, fb_next(t1) # copy next
|
||||||
sd t6, fb_next(t3)
|
sd t6, fb_next(t3)
|
||||||
@@ -178,14 +179,14 @@ heap_alloc:
|
|||||||
add t3, t1, t0
|
add t3, t1, t0
|
||||||
beq t3, t4, 1f # if this is not at the end
|
beq t3, t4, 1f # if this is not at the end
|
||||||
ld t4, bsize(t3) # set next's prev used to 1
|
ld t4, bsize(t3) # set next's prev used to 1
|
||||||
ori t4, t4, 1
|
ori t4, t4, prev_used
|
||||||
sd t4, bsize(t3)
|
sd t4, bsize(t3)
|
||||||
1:
|
1:
|
||||||
|
|
||||||
# create used block
|
# create used block
|
||||||
|
|
||||||
add t2, t0, t1 # end of 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 t0, bsize(t1) # store size
|
||||||
sd zero, fb_next(t1) # remove free block stuff
|
sd zero, fb_next(t1) # remove free block stuff
|
||||||
sd zero, -8(t2)
|
sd zero, -8(t2)
|
||||||
|
|||||||
@@ -27,11 +27,10 @@ strlen:
|
|||||||
strcpy:
|
strcpy:
|
||||||
0:
|
0:
|
||||||
lb t0, (a1)
|
lb t0, (a1)
|
||||||
beqz t0, 0f
|
|
||||||
sb t0, (a0)
|
sb t0, (a0)
|
||||||
|
beqz t0, 0f
|
||||||
addi a0, a0, 1
|
addi a0, a0, 1
|
||||||
addi a1, a1, 1
|
addi a1, a1, 1
|
||||||
j 0b
|
j 0b
|
||||||
0:
|
0:
|
||||||
sb t0, (a0)
|
|
||||||
ret
|
ret
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
.section .rodata
|
.section .rodata
|
||||||
|
|
||||||
.word 0f - print_msg
|
.word 0f - hinfo_msg
|
||||||
print_msg:
|
hinfo_msg:
|
||||||
.string "Current heap:"
|
.string "Current heap info:"
|
||||||
|
0:
|
||||||
|
|
||||||
|
.word 0f - block_msg
|
||||||
|
block_msg:
|
||||||
|
.string "Current heap blocks:"
|
||||||
0:
|
0:
|
||||||
|
|
||||||
.word 0f - dots
|
.word 0f - dots
|
||||||
dots:
|
dots:
|
||||||
.string "................"
|
.string "..."
|
||||||
0:
|
0:
|
||||||
|
|
||||||
.word 0f - hexstr
|
.word 0f - hexstr
|
||||||
@@ -73,19 +78,57 @@ heap_test:
|
|||||||
ret
|
ret
|
||||||
|
|
||||||
print_heap:
|
print_heap:
|
||||||
addi sp, sp, -(3*8)
|
addi sp, sp, -(4*8)
|
||||||
sd ra, 0(sp)
|
sd ra, 0(sp)
|
||||||
sd s0, 8(sp)
|
sd s0, 8(sp)
|
||||||
sd s1, 16(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)
|
lw a1, -4(a0)
|
||||||
jal println
|
jal println
|
||||||
|
|
||||||
la t0, heap_info
|
la t0, heap_info
|
||||||
ld s0, 0(t0)
|
ld s0, 16(t0)
|
||||||
ld s1, 16(t0)
|
ld s1, 24(t0)
|
||||||
0:
|
0:
|
||||||
|
jal print_space
|
||||||
|
jal print_space
|
||||||
|
|
||||||
la a0, hexstr
|
la a0, hexstr
|
||||||
lw a1, -4(a0)
|
lw a1, -4(a0)
|
||||||
jal print
|
jal print
|
||||||
@@ -121,5 +164,6 @@ print_heap:
|
|||||||
ld ra, 0(sp)
|
ld ra, 0(sp)
|
||||||
ld s0, 8(sp)
|
ld s0, 8(sp)
|
||||||
ld s1, 16(sp)
|
ld s1, 16(sp)
|
||||||
addi sp, sp, +(3*8)
|
ld s2, 24(sp)
|
||||||
|
addi sp, sp, +(4*8)
|
||||||
ret
|
ret
|
||||||
|
|||||||
Reference in New Issue
Block a user