view src/ploki/variable.c @ 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

#include "config.h"
#include "Str.h"
#include "strhash.h"
#include "variable.h"
#include "xmalloc.h"

#include <assert.h>

enum {MAGIC = 23};

static void delcookie(void *c) {
	xfree(c);
}

t_vr_container *vr_new(void (*del)(void *)) {
	t_vr_container *v = xmalloc(1, sizeof *v);
	v->data = xmalloc(v->size = MAGIC, sizeof *v->data);
	v->length = 0;
	v->del = del;
	v->hash = sh_new(delcookie);
	return v;
}

void vr_delete(t_vr_container *v) {
	assert(v != NULL);
	if (v->hash) {
		vr_freeze(v);
	}
	while (v->length) {
		--v->length;
		if (v->del) {
			v->del(v->data[v->length]);
		}
	}
	xfree(v->data);
	xfree(v);
}

t_vr_cookie vr_exists(const t_vr_container *v, const char *key, size_t len) {
	t_vr_cookie *c;
	assert(v->hash != NULL);
	return (c = sh_get(v->hash, key, len)) ? *c : VR_NO_COOKIE;
}

t_vr_cookie vr_register(t_vr_container *v, const char *key, size_t len, void *val) {
	t_vr_cookie *cookie;

	assert(v->hash != NULL);
	assert(sh_get(v->hash, key, len) == NULL);

	if (v->length >= v->size) {
		v->data = xrealloc(v->data, v->size *= 2);
	}

	cookie = xmalloc(1, sizeof *cookie);
	v->data[*cookie = v->length++] = val;
	sh_put(v->hash, key, len, cookie);

	return *cookie;
}

void vr_freeze(t_vr_container *v) {
	assert(v->hash != NULL);
	sh_delete(v->hash);
	v->hash = NULL;
}

void *(vr_data)(const t_vr_container *v, t_vr_cookie c) {
	assert(v->hash == NULL);
	assert(c < v->length);
	return v->data[c];
}