Skip to content
Myr

exec_stack.h

This file

Path

src/exec_stack.h

114 lines · 3.3 KiB · C

Last changed by

Not known.

History of this file

Take it

1#ifndef EXEC_STACK_H2#define EXEC_STACK_H3#include <stddef.h>4#include <stdint.h>5#include <stdlib.h>6#include <string.h>7#include "jv_alloc.h"89/*10 * The stack is a directed forest of variably sized blocks. Each block has a11 * "next" block which is at a higher memory address, or 0 if the block has no12 * "next" block. More than one block may have no "next" block. A block may be13 * the "next" block of more than one other block. Pushed blocks are added at14 * the low-address end of the stack.15 *16 * Stack pointers are negative integers that are offsets relative to "mem_end",17 * the end of the allocated region. The stack "bound" is the stack pointer of18 * the last block that would be able to fit in the currently allocated region.19 * The stack "limit" is the stack pointer of the last block currently in the20 * stack. The stack pointer of the "next" block is stored directly below each21 * block.22 *23 *                      <- mem_end = 0x10024 * 0xF8  +------------+25 * 0xF0  |            |26 * 0xE8  +------------+ <- stack_ptr1 = -0x1827 * 0xE0  next = 028 * 0xD8  +------------+29 * 0xD0  |            |30 * 0xC8  |            |31 * 0xC0  +------------+ <- stack_ptr2 = limit = -0x4032 * 0xB8  next = -0x1833 * 0xB034 * 0xA8                 <- bound = -0x5835 * 0xA036 */3738struct determine_alignment {39  char x;40  union { int i; double d; uint64_t u64; size_t sz; void* ptr; } u;41};42enum {ALIGNMENT = offsetof(struct determine_alignment, u)};4344static size_t align_round_up(size_t sz) {45  return ((sz + (ALIGNMENT - 1)) / ALIGNMENT) * ALIGNMENT;46}4748typedef int stack_ptr;4950struct stack {51  char* mem_end; // one-past-the-end of allocated region52  stack_ptr bound;53  stack_ptr limit; // 0 - stack is empty54};5556static void stack_init(struct stack* s) {57  s->mem_end = 0;58  s->bound = ALIGNMENT;59  s->limit = 0;60}6162static void stack_reset(struct stack* s) {63  assert(s->limit == 0 && "stack freed while not empty");64  if(s->mem_end != NULL) {65    char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT);66    free(mem_start);67  }68  stack_init(s);69}7071static int stack_pop_will_free(struct stack* s, stack_ptr p) {72  return p == s->limit;73}7475static void* stack_block(struct stack* s, stack_ptr p) {76  return (void*)(s->mem_end + p);77}7879static stack_ptr* stack_block_next(struct stack* s, stack_ptr p) {80  return &((stack_ptr*)stack_block(s, p))[-1];81}8283static void stack_reallocate(struct stack* s, size_t sz) {84  int old_mem_length = -(s->bound) + ALIGNMENT;85  char* old_mem_start = (s->mem_end != NULL) ? (s->mem_end - old_mem_length) : NULL;8687  int new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);88  char* new_mem_start = jv_mem_realloc(old_mem_start, new_mem_length);89  memmove(new_mem_start + (new_mem_length - old_mem_length),90            new_mem_start, old_mem_length);91  s->mem_end = new_mem_start + new_mem_length;92  s->bound = -(new_mem_length - ALIGNMENT);93}9495static stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {96  int alloc_sz = align_round_up(sz) + ALIGNMENT;97  stack_ptr r = s->limit - alloc_sz;98  if (r < s->bound) {99    stack_reallocate(s, alloc_sz);100  }101  s->limit = r;102  *stack_block_next(s, r) = p;103  return r;104}105106static stack_ptr stack_pop_block(struct stack* s, stack_ptr p, size_t sz) {107  stack_ptr r = *stack_block_next(s, p);108  if (p == s->limit) {109    int alloc_sz = align_round_up(sz) + ALIGNMENT;110    s->limit += alloc_sz;111  }112  return r;113}114#endif