[util] use rgn_new() function for easier management of sub-regions
This commit is contained in:
parent
e349a336af
commit
940d4231d5
|
@ -6,12 +6,11 @@ static void msgq_cleanup(struct msgq* mq);
|
|||
struct msgq* msgq_new(struct rgn* init_rgn)
|
||||
{
|
||||
struct msgq* mq = ralloc_typed(init_rgn, struct msgq, 1);
|
||||
rgn_pin(init_rgn, mq, (void*) msgq_cleanup);
|
||||
|
||||
rgn_init(&mq->rgn, SMALL);
|
||||
mq->rgn = rgn_new(init_rgn, MEDIUM);
|
||||
mq->head = mq->tail = NULL;
|
||||
pthread_mutex_init(&mq->mx, NULL);
|
||||
pthread_cond_init(&mq->cv, NULL);
|
||||
rgn_pin(init_rgn, mq, (void*) msgq_cleanup);
|
||||
return mq;
|
||||
}
|
||||
|
||||
|
@ -22,13 +21,12 @@ static void msgq_cleanup(struct msgq* mq)
|
|||
|
||||
pthread_cond_destroy(&mq->cv);
|
||||
pthread_mutex_destroy(&mq->mx);
|
||||
rgn_cleanup(&mq->rgn);
|
||||
}
|
||||
|
||||
void* msgq_begin_send(struct msgq* mq, int type, size_t payload_size)
|
||||
{
|
||||
pthread_mutex_lock(&mq->mx);
|
||||
struct msgq_msg_list* msg = ralloc(&mq->rgn, sizeof(struct msgq_msg_list) + payload_size);
|
||||
struct msgq_msg_list* msg = ralloc(mq->rgn, sizeof(struct msgq_msg_list) + payload_size);
|
||||
msg->next = NULL;
|
||||
msg->type = type;
|
||||
if (mq->head == NULL) {
|
||||
|
@ -78,6 +76,6 @@ void* msgq_recv1(struct msgq* mq, int* out_type)
|
|||
void msgq_end_recv(struct msgq* mq)
|
||||
{
|
||||
mq->head = mq->tail = NULL;
|
||||
rgn_clear(&mq->rgn);
|
||||
rgn_clear(mq->rgn);
|
||||
pthread_mutex_unlock(&mq->mx);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
struct msgq_msg_list;
|
||||
|
||||
struct msgq {
|
||||
struct rgn rgn;
|
||||
struct rgn* rgn;
|
||||
struct msgq_msg_list* head;
|
||||
struct msgq_msg_list* tail;
|
||||
pthread_mutex_t mx;
|
||||
|
|
|
@ -18,7 +18,7 @@ struct ax_ctxt {
|
|||
bool log_auto_close;
|
||||
|
||||
// errors
|
||||
struct rgn err_rgn;
|
||||
struct rgn* err_rgn;
|
||||
const char* err;
|
||||
|
||||
// backend
|
||||
|
@ -26,7 +26,7 @@ struct ax_ctxt {
|
|||
struct ax_backend* bac;
|
||||
|
||||
// theme
|
||||
struct rgn thmb_rgn;
|
||||
struct rgn* thmb_rgn;
|
||||
struct ax_theme_builder* thmb;
|
||||
struct ax_theme* sel_theme;
|
||||
};
|
||||
|
@ -36,8 +36,8 @@ void ax__ctxt_cleanup(struct ax_ctxt* ax);
|
|||
|
||||
static inline char* ax__make_error_buf(struct ax_ctxt* ax, size_t n)
|
||||
{
|
||||
rgn_clear(&ax->err_rgn);
|
||||
char* buf = ralloc_typed(&ax->err_rgn, char, n);
|
||||
rgn_clear(ax->err_rgn);
|
||||
char* buf = ralloc_typed(ax->err_rgn, char, n);
|
||||
ax->err = buf;
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -54,12 +54,10 @@ void ax__ctxt_init(struct ax_ctxt* ax, struct rgn* init_rgn)
|
|||
ax->log_out = NULL;
|
||||
ax->log_auto_close = false;
|
||||
|
||||
rgn_init(&ax->err_rgn, SMALL);
|
||||
rgn_pin(init_rgn, &ax->err_rgn, (void*) rgn_cleanup);
|
||||
ax->err_rgn = rgn_new(init_rgn, SMALL);
|
||||
ax->err = "";
|
||||
|
||||
rgn_init(&ax->thmb_rgn, THEME_BUILDER_DESIRED_REGION_SIZE);
|
||||
rgn_pin(init_rgn, &ax->thmb_rgn, (void*) rgn_cleanup);
|
||||
ax->thmb_rgn = rgn_new(init_rgn, THEME_BUILDER_DESIRED_REGION_SIZE);
|
||||
ax->thmb = NULL;
|
||||
ax->sel_theme = NULL;
|
||||
|
||||
|
@ -105,9 +103,8 @@ void ax_log(struct ax_ctxt* ax, const char* string)
|
|||
|
||||
void ax_begin_theme(struct ax_ctxt* ax)
|
||||
{
|
||||
rgn_clear(&ax->thmb_rgn);
|
||||
ax->thmb = ralloc_typed(&ax->thmb_rgn, struct ax_theme_builder, 1);
|
||||
ax__theme_builder_init(ax->thmb, &ax->thmb_rgn);
|
||||
rgn_clear(ax->thmb_rgn);
|
||||
ax->thmb = ax__theme_builder_new(ax->thmb_rgn);
|
||||
}
|
||||
|
||||
void ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm)
|
||||
|
@ -115,7 +112,8 @@ void ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm)
|
|||
ASSERT(ax->thmb != NULL, "`ax_end_theme' called while not building a theme");
|
||||
|
||||
struct ax_theme* thm = ax__theme_builder_finish(ax->thmb, ax->init_rgn);
|
||||
rgn_clear(&ax->thmb_rgn);
|
||||
ax->thmb = NULL;
|
||||
rgn_clear(ax->thmb_rgn);
|
||||
|
||||
if (out_thm != NULL) {
|
||||
*out_thm = thm;
|
||||
|
@ -136,7 +134,7 @@ void ax_set_theme_color(
|
|||
enum ax_color_cat cat;
|
||||
if (ax__string_to_color_cat(cat_name, &cat) != 0) {
|
||||
ax->thmb->err =
|
||||
rsprintf(&ax->thmb_rgn, "invalid color category `%s'", cat_name);
|
||||
rsprintf(ax->thmb_rgn, "invalid color category `%s'", cat_name);
|
||||
} else {
|
||||
ax__theme_set_color(ax->thmb, cat, rgb);
|
||||
}
|
||||
|
@ -149,7 +147,7 @@ void ax_set_theme_font(
|
|||
enum ax_font_cat cat;
|
||||
if (ax__string_to_font_cat(cat_name, &cat) != 0) {
|
||||
ax->thmb->err =
|
||||
rsprintf(&ax->thmb_rgn, "invalid font category `%s'", cat_name);
|
||||
rsprintf(ax->thmb_rgn, "invalid font category `%s'", cat_name);
|
||||
} else {
|
||||
ax__theme_set_font(ax->thmb, cat, fnt_path, fnt_size);
|
||||
}
|
||||
|
|
|
@ -16,8 +16,10 @@ static const char* default_font_path(struct rgn* rgn)
|
|||
* Theme builder functions
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
void ax__theme_builder_init(struct ax_theme_builder* thmb, struct rgn* rgn)
|
||||
struct ax_theme_builder* ax__theme_builder_new(struct rgn* rgn)
|
||||
{
|
||||
struct ax_theme_builder* thmb =
|
||||
ralloc_typed(rgn, struct ax_theme_builder, 1);
|
||||
thmb->rgn = rgn;
|
||||
thmb->err = NULL;
|
||||
thmb->dflt_font_path = NULL;
|
||||
|
@ -43,6 +45,8 @@ void ax__theme_builder_init(struct ax_theme_builder* thmb, struct rgn* rgn)
|
|||
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
|
||||
thmb->font_path[i] = NULL;
|
||||
}
|
||||
|
||||
return thmb;
|
||||
}
|
||||
|
||||
void ax__theme_set_color(
|
||||
|
|
|
@ -58,8 +58,7 @@ struct ax_theme_builder {
|
|||
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat);
|
||||
int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat);
|
||||
|
||||
void ax__theme_builder_init(
|
||||
struct ax_theme_builder* thmb, struct rgn* rgn);
|
||||
struct ax_theme_builder* ax__theme_builder_new(struct rgn* rgn);
|
||||
|
||||
void ax__theme_set_color(
|
||||
struct ax_theme_builder* thmb,
|
||||
|
|
|
@ -49,6 +49,12 @@ void rgn_pin(struct rgn* rgn, void* obj, void (*dtor)(void*));
|
|||
* Derived utilities
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
#define ralloc_typed(_rgn, T, _n) \
|
||||
((T*) ralloc(_rgn, sizeof(T) * (_n)))
|
||||
|
||||
#define rmemdup_typed(_rgn, T, _p, _n) \
|
||||
((T*) rmemdup(_rgn, _p, sizeof(T) * (_n)))
|
||||
|
||||
static inline void* rmemdup(struct rgn* rgn, const void* ptr, size_t siz)
|
||||
{
|
||||
void* new = ralloc(rgn, siz);
|
||||
|
@ -61,11 +67,13 @@ static inline char* rstrdup(struct rgn* rgn, const char* str)
|
|||
return rmemdup(rgn, str, strlen(str) + 1);
|
||||
}
|
||||
|
||||
#define ralloc_typed(_rgn, T, _n) \
|
||||
((T*) ralloc(_rgn, sizeof(T) * (_n)))
|
||||
|
||||
#define rmemdup_typed(_rgn, T, _p, _n) \
|
||||
((T*) rmemdup(_rgn, _p, sizeof(T) * (_n)))
|
||||
static inline struct rgn* rgn_new(struct rgn* parent_rgn, size_t pgsize)
|
||||
{
|
||||
struct rgn* rgn = ralloc_typed(parent_rgn, struct rgn, 1);
|
||||
rgn_init(rgn, pgsize);
|
||||
rgn_pin(parent_rgn, rgn, (void*) rgn_cleanup);
|
||||
return rgn;
|
||||
}
|
||||
|
||||
#define rsprintf(_rgn, ...) ({ \
|
||||
size_t sprintf__len = \
|
||||
|
|
Loading…
Reference in New Issue