SCI/FreeSCI/Kernel hacking/Hunk and heap

From ScummVM :: Wiki
Jump to navigation Jump to search

Hunk and heap

Accessing the heap for both reading and writing is surprisingly important for the kernel, especially when it has to deal with functions that would usually belong into user space, like handling of doubly-linked lists. To ease this, three macros are available:

GET_HEAP(x)
reads a signed SCI word (gint16) from heap address x
UGET_HEAP(x)
reads an unsigned SCI word (guint16)
PUT_HEAP(x, foo)
writes the value foo to the specified heap address

Some kernel functions, especially graphical kernel functions, additionally require the usage of what Sierra referred to as "hunk space". This is dynamically allocated memory; it can even be allocated and unallocated manually from SCI scripts by using the Load() and UnLoad() system calls (this is the sci_memory resource). To allow usage of this kind of memory, three functions have been provided:

int kalloc(state_t *, space)
allocate space bytes and return a handle
byte *kmem(state_t *, handle)
resolve a handle and return the memory address it points to
int kfree(state_t *, handle)
unallocate memory associated with a handle. Returns 0 on success, 1 otherwise