view src/ploki/re_block.c.h @ 12292:d51f2100210c draft

<kspalaiologos> `` cat <<<"asmbf && bfi output.b" > /hackenv/ibin/asmbf
author HackEso <hackeso@esolangs.org>
date Thu, 02 Jan 2020 15:38:21 +0000
parents ac0403686959
children
line wrap: on
line source

/* vi: set ft=c: */
enum {BLOCK_MAGIC = 32};

struct block_node {
	struct block_node *next;
	size_t used;
	re_node nodes[BLOCK_MAGIC];
};

struct txt_node {
	struct txt_node *next;
	unsigned char *buf;
};

typedef struct {
	struct block_node *root;
	struct txt_node *txt_root;
} t_block;

static void bl_init(t_block *b) {
	b->root = xmalloc(1, sizeof *b->root);
	b->root->next = NULL;
	b->root->used = 0;
	b->txt_root = NULL;
}

static void bl_free(t_block *b) {
	while (b->root) {
		struct block_node *tmp = b->root->next;
		xfree(b->root);
		b->root = tmp;
	}

	while (b->txt_root) {
		struct txt_node *tmp = b->txt_root->next;
		xfree(b->txt_root->buf);
		xfree(b->txt_root);
		b->txt_root = tmp;
	}
}

static re_node *bl_node(t_block *b) {
	if (b->root->used >= sizeof b->root->nodes / sizeof b->root->nodes[0]) {
		struct block_node *tmp = xmalloc(1, sizeof *tmp);
		tmp->used = 0;
		tmp->next = b->root;
		b->root = tmp;
	}

	return b->root->nodes + b->root->used++;
}

static unsigned char *bl_string(t_block *b, size_t len) {
	struct txt_node *tmp = xmalloc(1, sizeof *tmp);
	tmp->buf = xmalloc(len, sizeof *tmp->buf);
	tmp->next = b->txt_root;
	b->txt_root = tmp;

	return b->txt_root->buf;
}