view src/ploki/re_block.c.h @ 8427:1fc808cd5b1f

<b_jonas> learn can\'t is the most frequent word whose pronunciation varies between /\xc9\x91\xcb\x90/ and /\xc3\xa6/ depending on dialect. The list is: advance after answer ask aunt brass can\'t cast castle chance class command dance demand draft enhance example fast father glass graph grass half last laugh mask master nasty pass past path plant rather sample shan\'t staff task vast
author HackBot
date Thu, 09 Jun 2016 21:28:47 +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;
}