undar-lang/test/undar.h

45 lines
1.0 KiB
C

#ifndef UNDAR_H
#define UNDAR_H
#include <stdio.h>
#include <string.h>
typedef struct arena_s Arena;
struct arena_s {
unsigned char *tape;
unsigned int count;
unsigned int capacity;
};
static void *aalloc(Arena *arena, unsigned int size){
unsigned int pos;
if(arena == NULL) return NULL;
if(arena->count + size > arena->capacity) return NULL;
pos = arena->count;
arena->count += size;
return (void *)&arena->tape[pos];
}
static void *
areturn(Arena *arena, unsigned int checkpoint, const void *src, unsigned int size)
{
void *dest;
if(arena == NULL || src == NULL) return NULL;
dest = (void *)&arena->tape[checkpoint];
if (src == dest) return dest;
memmove(dest, src, size);
arena->count = checkpoint + size;
return dest;
}
#define ARENA_RETURN(arena, ckpt, src_ptr, type) \
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type))
#define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type) * (count))
#endif