view src/ploki/re_block.c.h @ 8065:591b1467ccdf

<int-e> le/rn paste/"Paste" is a short story by Henry James. Its contents has been cut into pieces and distributed over numerous tin boxes on the World Wide Web, little pearls of wisdom buried among ordinary pastes.
author HackBot
date Sun, 15 May 2016 13:14:57 +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;
}