1 /* radare - LGPL - Copyright 2019 thestr4ng3r */
2 
3 #include "r_util/r_str_constpool.h"
4 
kv_fini(HtPPKv * kv)5 static void kv_fini(HtPPKv *kv) {
6 	free (kv->key);
7 }
8 
r_str_constpool_init(RStrConstPool * pool)9 R_API bool r_str_constpool_init(RStrConstPool *pool) {
10 	pool->ht = ht_pp_new (NULL, kv_fini, NULL);
11 	return pool->ht != NULL;
12 }
13 
r_str_constpool_fini(RStrConstPool * pool)14 R_API void r_str_constpool_fini(RStrConstPool *pool) {
15 	ht_pp_free (pool->ht);
16 }
17 
r_str_constpool_get(RStrConstPool * pool,const char * str)18 R_API const char *r_str_constpool_get(RStrConstPool *pool, const char *str) {
19 	if (!str) {
20 		return NULL;
21 	}
22 	HtPPKv *kv = ht_pp_find_kv (pool->ht, str, NULL);
23 	if (kv) {
24 		return kv->key;
25 	}
26 	ht_pp_insert (pool->ht, str, NULL);
27 	kv = ht_pp_find_kv (pool->ht, str, NULL);
28 	if (kv) {
29 		return kv->key;
30 	}
31 	return NULL;
32 }
33