modified: n.fq
[GalaxyCodeBases.git] / c_cpp / lib / cstring / cstring.h
blob080afe3146714a0de18e5111634f8e4208191746
1 #ifndef cstring_h
2 #define cstring_h
4 #include <stdint.h>
5 #include <stddef.h>
7 #define CSTRING_PERMANENT 1
8 #define CSTRING_INTERNING 2
9 #define CSTRING_ONSTACK 4
11 #define CSTRING_INTERNING_SIZE 32
12 #define CSTRING_STACK_SIZE 128
14 struct cstring_data {
15 char * cstr;
16 uint32_t hash_size;
17 uint16_t type;
18 uint16_t ref;
21 typedef struct _cstring_buffer {
22 struct cstring_data * str;
23 } cstring_buffer[1];
25 typedef struct cstring_data * cstring;
27 #define CSTRING_BUFFER(var) \
28 char var##_cstring [CSTRING_STACK_SIZE] = { '\0' }; \
29 struct cstring_data var##_cstring_data = { var##_cstring , 0, CSTRING_ONSTACK, 0 }; \
30 cstring_buffer var; \
31 var->str = &var##_cstring_data;
33 #define CSTRING_LITERAL(var, cstr) \
34 static cstring var = NULL; \
35 if (var) {} else { \
36 cstring tmp = cstring_persist(""cstr, (sizeof(cstr)/sizeof(char))-1); \
37 if (!__sync_bool_compare_and_swap(&var, NULL, tmp)) { \
38 cstring_free_persist(tmp); \
39 } \
42 #define CSTRING(s) ((s)->str)
44 #define CSTRING_CLOSE(var) \
45 if ((var)->str->type != 0) {} else \
46 cstring_release((var)->str);
48 /* low level api, don't use directly */
49 cstring cstring_persist(const char * cstr, size_t sz);
50 void cstring_free_persist(cstring s);
52 /* public api */
53 cstring cstring_grab(cstring s);
54 void cstring_release(cstring s);
55 cstring cstring_cat(cstring_buffer sb, const char * str);
56 cstring cstring_printf(cstring_buffer sb, const char * format, ...)
57 #ifdef __GNUC__
58 __attribute__((format(printf, 2, 3)))
59 #endif
61 int cstring_equal(cstring a, cstring b);
62 uint32_t cstring_hash(cstring s);
64 #endif