summaryrefslogtreecommitdiff
path: root/usual/strpool.c
blob: 8282cdc2ea5cac9bffb2af0fb83f36aaed2d2baf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Pool for shared strings.
 *
 * Copyright (c) 2010  Marko Kreen, Skype Technologies OÜ
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <usual/strpool.h>

#include <usual/cbtree.h>
#include <string.h>

/*
 * Put all strings into cbtree.
 */
struct StrPool {
	CxMem *ca;
	struct CBTree *tree;
	int count;
};

/* pass key info to cbtree */
static size_t get_key(void *ctx, void *obj, const void **dst_p)
{
	struct PStr *s = obj;
	*dst_p = s->str;
	return s->len;
}

/* free PStr obj */
static bool free_str(void *arg, void *obj)
{
	struct PStr *p = obj;
	struct StrPool *sp = p->pool;

	memset(p, 0, offsetof(struct PStr, str) + 1);
	cx_free(sp->ca, obj);
	return true;
}

/* create main structure */
struct StrPool *strpool_create(CxMem *ca)
{
	struct StrPool *sp;

	sp = cx_alloc(ca, sizeof(*sp));
	if (!sp)
		return NULL;
	sp->count = 0;
	sp->ca = ca;
	sp->tree = cbtree_create(get_key, NULL, NULL, ca);
	if (!sp->tree) {
		cx_free(ca, sp);
		return NULL;
	}
	return sp;
}

/* free main structure */
void strpool_free(struct StrPool *sp)
{
	if (sp) {
		cbtree_walk(sp->tree, free_str, sp);
		cbtree_destroy(sp->tree);
		cx_free(sp->ca, sp);
	}
}

/* return total count of strings in pool */
int strpool_total(struct StrPool *sp)
{
	return sp->count;
}

/* get new reference to str */
struct PStr *strpool_get(struct StrPool *sp, const char *str, ssize_t len)
{
	struct PStr *cstr;
	bool ok;

	if (len < 0)
		len = strlen(str);

	/* search */
	cstr = cbtree_lookup(sp->tree, str, len);
	if (cstr) {
		cstr->refcnt++;
		return cstr;
	}

	/* create */
	cstr = cx_alloc(sp->ca, sizeof(*cstr) + len + 1);
	if (!cstr)
		return NULL;
	cstr->pool = sp;
	cstr->refcnt = 1;
	cstr->len = len;
	memcpy(cstr->str, str, len + 1);

	/* insert */
	ok = cbtree_insert(sp->tree, cstr);
	if (!ok) {
		cx_free(sp->ca, cstr);
		return NULL;
	}
	sp->count++;
	return cstr;
}

/* add reference */
void strpool_incref(struct PStr *s)
{
	if (s)
		s->refcnt++;
}

/* drop reference, free if none left */
void strpool_decref(struct PStr *s)
{
	struct StrPool *sp;

	if (!s)
		return;
	Assert(s->refcnt > 0);

	s->refcnt--;
	if (s->refcnt > 0)
		return;

	/* remove */
	sp = s->pool;
	sp->count--;
	cbtree_delete(sp->tree, s->str, s->len);
	free_str(NULL, s);
}