5 #include "string_pool.h"
7 typedef struct node_s node_t
;
11 trp_node(node_t
) children
;
14 typedef trp(node_t
) tree_t
;
16 static tree_t tree
= { ~0 };
18 obj_pool_gen(node
, node_t
, 4096);
19 obj_pool_gen(string
, char, 4096);
21 static char *node_value(node_t
* node
)
23 return node
? string_pointer(node
->offset
) : NULL
;
26 static int node_value_cmp(node_t
* a
, node_t
* b
)
28 return strcmp(node_value(a
), node_value(b
));
31 static int node_indentity_cmp(node_t
* a
, node_t
* b
)
33 int r
= node_value_cmp(a
, b
);
34 return r
? r
: (((uintptr_t) a
) > ((uintptr_t) b
))
35 - (((uintptr_t) a
) < ((uintptr_t) b
));
38 trp_gen(static, tree_
, tree_t
, node_t
, children
, node
,
41 static char *pool_fetch(uint32_t entry
)
43 return node_value(node_pointer(entry
));
46 uint32_t pool_intern(char *key
)
49 uint32_t key_len
= strlen(key
) + 1;
50 node_t
*node
= node_pointer(node_alloc(1));
51 node
->offset
= string_alloc(key_len
);
52 strcpy(node_value(node
), key
);
53 match
= tree_psearch(&tree
, node
);
54 if (!match
|| node_value_cmp(node
, match
)) {
55 tree_insert(&tree
, node
);
61 return node_offset(node
);
64 uint32_t pool_tok_r(char *str
, const char *delim
, char **saveptr
)
66 char *token
= strtok_r(str
, delim
, saveptr
);
67 return token
? pool_intern(token
) : ~0;
70 void pool_print_seq(uint32_t len
, uint32_t * seq
, char delim
, FILE * stream
)
73 for (i
= 0; i
< len
; i
++) {
74 fputs(pool_fetch(seq
[i
]), stream
);