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
21 typedef struct _cstring_buffer
{
22 struct cstring_data
* str
;
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 }; \
31 var->str = &var##_cstring_data;
33 #define CSTRING_LITERAL(var, cstr) \
34 static cstring var = NULL; \
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); \
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
);
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
, ...)
58 __attribute__((format(printf
, 2, 3)))
61 int cstring_equal(cstring a
, cstring b
);
62 uint32_t cstring_hash(cstring s
);