riscv – RISC-V: Runtime Exception with Syscall 9 Exceeding Heap Storage

I’m implementing a routine in RISC-V to allocate and initialize nodes for a doubly linked list. Each node contains:

  • A coefficient (4 bytes)
  • A degree (4 bytes)
  • Two pointers for the previous and next nodes (8 bytes each)
  • The total size of a node is 24 bytes.

Here’s the allocT routine I’m using to allocate memory for a node on the heap:

allocT:
    # Size of a node: 4 (coefficient) + 4 (degree) + 8 (previous) + 8 (next) = 24 bytes
    li t0, 24           # Node size in bytes
    li a7, 9            # Syscall 9 for memory allocation
    ecall               # Perform allocation
    beqz a0, alloc_fail # If allocation fails, return 0

    # Initialize node fields
    sw a0, 0(a0)        # Coefficient
    sw a1, 4(a0)        # Degree
    li t1, 0            # NULL value
    sd t1, 8(a0)        # Previous pointer
    sd t1, 16(a0)       # Next pointer

    # Return address of the allocated node
    jr ra

alloc_fail:
    li a0, 0            # Return 0 if allocation fails
    jr ra
#However, when I test the routine in RARS with the following setup:
    li a0, 10          # Coefficient
    li a1, 3           # Degree
    call allocT        # Allocate a node

I get the following runtime exception:

Runtime exception at 0x00400018: request (268697600) exceeds available heap storage (syscall 9).

Read more here: Source link