74#define SBRM_PRIV_HERE_SIG const char * func, const char * file, const int line
75#define SBRM_PRIV_ATOM(statements) do { statements; } while (0)
76#define SBRM_PRIV_NARGS___(_5, _4, _3, _2, _1, N, ...) N
77#define SBRM_PRIV_NARGS__(...) SBRM_PRIV_NARGS___(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
78#define SBRM_PRIV_NARGS_0(...) SBRM_PRIV_NARGS__(__VA_ARGS__)
79#define SBRM_PRIV_NARGS_1(...) 0
80#define SBRM_PRIV_NARGS(...) \
81 SBRM_PRIV_CAT(SBRM_PRIV_NARGS_, SBRM_PRIV_ISEMPTY(__VA_ARGS__))(__VA_ARGS__)
82#define SBRM_PRIV_CAT_(x, y) x ## y
83#define SBRM_PRIV_CAT(x, y) SBRM_PRIV_CAT_(x, y)
84#define SBRM_PRIV_CASSERT(cond, msg) \
85 typedef char STATIC_ASSERT__##msg[2 * !!(cond) - 1]
86#define SBRM_PRIV_DECONSTIFY(name) ((__sbrm_deconst_t*)&name)->mut
89#define SBRM_PRIV_ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, \
90 _13, _14, _15, ...) _15
91#define SBRM_PRIV_HAS_COMMA(...) SBRM_PRIV_ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1,\
92 1, 1, 1, 1, 1, 1, 1, 1, 0)
93#define SBRM_PRIV_TRIGGER_PAREN(...) ,
94#define SBRM_PRIV_EMPTY_CASE_0001 ,
95#define SBRM_PRIV_PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
96#define SBRM_PRIV_ISEMPTY_(_0, _1, _2, _3) SBRM_PRIV_HAS_COMMA(SBRM_PRIV_PASTE5\
97 (SBRM_PRIV_EMPTY_CASE_, _0, _1, _2, _3))
98#define SBRM_PRIV_ISEMPTY(...) SBRM_PRIV_ISEMPTY_( \
99 SBRM_PRIV_HAS_COMMA(__VA_ARGS__), \
100 SBRM_PRIV_HAS_COMMA(SBRM_PRIV_TRIGGER_PAREN __VA_ARGS__), \
101 SBRM_PRIV_HAS_COMMA(__VA_ARGS__ ()), \
102 SBRM_PRIV_HAS_COMMA(SBRM_PRIV_TRIGGER_PAREN __VA_ARGS__ ()))
113#if defined(SBRM_REGISTRY_VARNAME)
114# define SBRM_PRIV_REGISTRY SBRM_REGISTRY_VARNAME
116# define SBRM_PRIV_REGISTRY __sbrm_registry
122#define SBRM_HERE cpl_func, __FILE__, __LINE__
146#define SBRM_HANDLE_POINTR(type) type *
147#define SBRM_HANDLE_STRUCT(type) struct { type * o; }
153#define SBRM_INITV(size, name, ...) SBRM_INIT(size, name, ##__VA_ARGS__)
154#define SBRM_RESET(name) SBRM_SET(name)
239#define SBRM_PRIV_INIT4(size, name, type, fp, dtor) SBRM_PRIV_INIT0(size + 1); \
240 SBRM_SET(name, type, fp, dtor) = NULL; SBRM_RVAL(name); name->o
241#define SBRM_PRIV_INIT3(size, name, type, fp, ...) \
242 SBRM_PRIV_INIT4(size, name, type, fp, SBRM_PRIV_CAT(type, _delete))
243#define SBRM_PRIV_INIT2(size, name, type, ...) \
244 SBRM_PRIV_INIT4(size, name, type, v, SBRM_PRIV_CAT(type, _delete))
245#define SBRM_PRIV_INIT1(size, name, ...) \
246 SBRM_PRIV_CASSERT(0, Wrong_number_of_arguments_given_to_SBRM_INIT)
247#define SBRM_PRIV_INIT0(size, ...) \
248 sbrm_registry * SBRM_PRIV_REGISTRY = sbrm_init(size, SBRM_HERE)
249#define SBRM_INIT(size, ...) SBRM_PRIV_CAT(SBRM_PRIV_INIT, \
250 SBRM_PRIV_NARGS(__VA_ARGS__)) (size, ##__VA_ARGS__)
383#define SBRM_PRIV_SETv(type) SBRM_PRIV_CAT(type, _delete)
384#define SBRM_PRIV_SETi(type) SBRM_PRIV_CAT(type, _delete)
385#define SBRM_PRIV_SETf(type) SBRM_PRIV_CAT(type, _delete)
386#define SBRM_PRIV_SETd(type) SBRM_PRIV_CAT(type, _delete)
387#define SBRM_PRIV_SETp(type) SBRM_PRIV_CAT(type, _unwrap)
388#define SBRM_PRIV_SETw(type) SBRM_PRIV_CAT(type, _delete)
390#define SBRM_PRIV_SET3(name, type, fp, dtor) \
391 SBRM_HANDLE_STRUCT(type) * const name = SBRM_PRIV_REGISTRY->set( \
392 SBRM_PRIV_REGISTRY, #fp[0], dtor, NULL, __LINE__); name->o
393#define SBRM_PRIV_SET2(name, type, fp, ...) \
394 SBRM_PRIV_SET3(name, type, fp, SBRM_PRIV_CAT(SBRM_PRIV_SET, fp)(type))
395#define SBRM_PRIV_SET1(name, type, ...) \
396 SBRM_PRIV_SET3(name, type, v, SBRM_PRIV_CAT(SBRM_PRIV_SET, v)(type))
397#define SBRM_PRIV_SET0(name, ...) *(SBRM_FREE(name), &name->o)
398#define SBRM_SET(name, ...) SBRM_PRIV_CAT(SBRM_PRIV_SET, \
399 SBRM_PRIV_NARGS(__VA_ARGS__)) (name, ##__VA_ARGS__)
401#define SBRM_PRIV_CSET3(name, type, fp, dtor) \
402 SBRM_HANDLE_STRUCT(const type) * const name = SBRM_PRIV_REGISTRY->set( \
403 SBRM_PRIV_REGISTRY, #fp[0], dtor, NULL, __LINE__); name->o
404#define SBRM_PRIV_CSET2(name, type, fp, ...) \
405 SBRM_PRIV_CSET3(name, type, fp, SBRM_PRIV_CAT(SBRM_PRIV_SET, fp)(type))
406#define SBRM_PRIV_CSET1(name, type, ...) \
407 SBRM_PRIV_CSET3(name, type, v, SBRM_PRIV_CAT(SBRM_PRIV_SET, v)(type))
408#define SBRM_PRIV_CSET0(name, ...) *(SBRM_FREE(name), &name->o)
409#define SBRM_CSET(name, ...) SBRM_PRIV_CAT(SBRM_PRIV_CSET, \
410 SBRM_PRIV_NARGS(__VA_ARGS__)) (name, ##__VA_ARGS__)
427#define SBRM_RVAL(name) SBRM_PRIV_ATOM( \
428 SBRM_PRIV_REGISTRY->rval(SBRM_PRIV_REGISTRY, name); if (0) return name->o; )
442#define SBRM_FREE(name) SBRM_PRIV_REGISTRY->free(SBRM_PRIV_REGISTRY, name)
474#define SBRM_PRIV_CLEAN1(name) (SBRM_PRIV_REGISTRY->rv = (void*)name, \
475 SBRM_PRIV_DECONSTIFY(name) = SBRM_LCP(__sbrm_entry_t, name), \
476 SBRM_PRIV_REGISTRY->cleanup(SBRM_PRIV_REGISTRY, 0), name->o)
479#define SBRM_PRIV_CLEAN0() SBRM_PRIV_REGISTRY->cleanup(SBRM_PRIV_REGISTRY, 0)
480#define SBRM_CLEANUP(...) \
481 SBRM_PRIV_CAT(SBRM_PRIV_CLEAN, SBRM_PRIV_NARGS(__VA_ARGS__)) (__VA_ARGS__)
500#define SBRM_PRIV_YANK1(name, type) (type*)SBRM_PRIV_REGISTRY->yank(name)
501#define SBRM_PRIV_YANK0(name, ...) SBRM_PRIV_REGISTRY->yank(name)
502#define SBRM_YANK(name, ...) SBRM_PRIV_CAT(SBRM_PRIV_YANK, \
503 SBRM_PRIV_NARGS(__VA_ARGS__)) (name, ##__VA_ARGS__)
526#define SBRM_PRIV_ABORT11(code, ...) 0, 0
527#define SBRM_PRIV_ABORT10(code, ...) 0, ##__VA_ARGS__
528#define SBRM_PRIV_ABORT01(code, ...) code, 0
529#define SBRM_PRIV_ABORT00(code, ...) code, ##__VA_ARGS__
530#define SBRM_ABORT(code, ...) SBRM_PRIV_REGISTRY->abort(SBRM_PRIV_REGISTRY, \
531 SBRM_HERE, SBRM_PRIV_CAT(SBRM_PRIV_ABORT, SBRM_PRIV_CAT(SBRM_PRIV_ISEMPTY( \
532 code), SBRM_PRIV_ISEMPTY(__VA_ARGS__))) (code, ##__VA_ARGS__))
555#define SBRM_PRIV_DBG0(...) __VA_ARGS__
556#define SBRM_PRIV_DBG1(...) 0
557#define SBRM_DBG(...) SBRM_PRIV_REGISTRY->debug(SBRM_PRIV_REGISTRY, SBRM_HERE, \
558 SBRM_PRIV_CAT(SBRM_PRIV_DBG, SBRM_PRIV_ISEMPTY(__VA_ARGS__)) (__VA_ARGS__))
560#define SBRM_PRIV_INFO0(...) __VA_ARGS__
561#define SBRM_PRIV_INFO1(...) 0
562#define SBRM_INFO(...) SBRM_PRIV_REGISTRY->info(SBRM_PRIV_REGISTRY, SBRM_HERE, \
563 SBRM_PRIV_CAT(SBRM_PRIV_INFO, SBRM_PRIV_ISEMPTY(__VA_ARGS__)) (__VA_ARGS__))
565#define SBRM_PRIV_WARN0(...) __VA_ARGS__
566#define SBRM_PRIV_WARN1(...) 0
567#define SBRM_WARN(...) SBRM_PRIV_REGISTRY->warn(SBRM_PRIV_REGISTRY, SBRM_HERE, \
568 SBRM_PRIV_CAT(SBRM_PRIV_WARN, SBRM_PRIV_ISEMPTY(__VA_ARGS__)) (__VA_ARGS__))
617# if defined(SBRM_UNDERSCORE_MACRO)
618# error "Sorry, SBRM's _ macro cannot be used: the identifier is taken"
621# if defined(SBRM_UNDERSCORE_MACRO)
622# define _(...) (__VA_ARGS__); \
623 SBRM_PRIV_ATOM( if (cpl_error_get_code()) return SBRM_ABORT(); )
624# define X(...) (__VA_ARGS__); \
625 SBRM_PRIV_ATOM( if (cpl_error_get_code()) SBRM_WARN(); )
633typedef union {
void * mut;
void *
const immut; } __sbrm_deconst_t;
634typedef SBRM_HANDLE_STRUCT(
void) * __sbrm_void_wrapper_t;
635typedef struct {
void * o;
char fp;
void * dtor;
int ident; } __sbrm_entry_t;
636typedef struct __sbrm_registry_s sbrm_registry;
637struct __sbrm_registry_s {
641 cpl_errorstate estate;
642 void * (*set)(sbrm_registry * r,
char fp,
void * dtor,
void * initval,
644 __sbrm_void_wrapper_t (*reset1)(sbrm_registry * r,
void * target);
645 void ** (*reset2)(sbrm_registry * r,
void * target);
646 void (*free)(sbrm_registry * r,
void * target);
647 void * (*cleanup)(sbrm_registry * r,
void * target);
648 void (*debug)(sbrm_registry * r, SBRM_PRIV_HERE_SIG,
const char * fmt, ...);
649 void (*info)(sbrm_registry * r, SBRM_PRIV_HERE_SIG,
const char * fmt, ...);
650 void (*warn)(sbrm_registry * r, SBRM_PRIV_HERE_SIG,
const char * fmt, ...);
651 void * (*abort)(sbrm_registry * r, SBRM_PRIV_HERE_SIG,
const unsigned code,
652 const char * fmt, ...);
653 void (*rval)(sbrm_registry * r,
void * target);
654 void * (*yank)(
void * target);
655 __sbrm_entry_t list[];
662void * __sbrm_cp(
void * dest,
void * src,
size_t sz,
int release );
663sbrm_registry * sbrm_init(
const int sz, SBRM_PRIV_HERE_SIG);
701#define SBRM_ALLOC(type, ...) memcpy(cpl_malloc(sizeof((type){__VA_ARGS__})), \
702 (&(type){__VA_ARGS__}), sizeof((type){__VA_ARGS__}))
773#define SBRM_PRIV_VALLOC1(...)
774#define SBRM_PRIV_VALLOC0(...) , ##__VA_ARGS__
775#define SBRM_VALLOC(list, scalar, n, ...) memcpy(cpl_calloc(1, sizeof(list) + \
776 (n) * sizeof(scalar)), (&(list){(n) SBRM_PRIV_CAT(SBRM_PRIV_VALLOC, \
777 SBRM_PRIV_ISEMPTY(__VA_ARGS__))(__VA_ARGS__)}), sizeof(list))
778#define SBRM_NALLOC(list, scalar, n, ...) memcpy(SBRM_VALLOC(list, scalar, n) +\
779 offsetof(list, data), (&(scalar[]){__VA_ARGS__}), (n) * sizeof(scalar)) - \
781#define SBRM_VLIST(scalar) struct { const size_t sz; scalar data[]; }
799#define SBRM_LOCAL(type, src) (type*)__sbrm_cp(&(type){0},(src),sizeof(type),1)
800#define SBRM_LCP(type, src) __sbrm_cp(&(type){0}, (src), sizeof(type), 0)