mempool_t *create_mempool(size_t alloc_size, size_t pool_size) { #ifdef DIAGNOSTICS printf("creating mempool alloc size %lu pool size %lu\n", alloc_size, pool_size); #endif mempool_t *pool = malloc(sizeof(mempool_t)); if (!pool) err_oom("mempool_create"); pool->next = NULL; pool->alloc_size = alloc_size; pool->pool_size = pool_size; pool->mem_start = calloc(alloc_size, pool_size); if (!pool->mem_start) err_oom("mempool_create"); pool->mem_end = pool->mem_start + alloc_size*pool_size; pool->alloc_ptr = pool->mem_start; pool->free_spots_size = 0; pool->free_spots_res = 128; pool->free_spots = calloc(sizeof(void*), pool->free_spots_res); if (!pool->free_spots) err_oom("mempool_create"); return pool; }
static void add_structured_data_param(struct list_head *ls, const char *param) { struct structured_data *sd; if (list_empty(ls)) errx(EXIT_FAILURE, _("--sd-id was not specified for --sd-param %s"), param); assert(param); sd = list_last_entry(ls, struct structured_data, sds); if (strv_extend(&sd->params, param)) err_oom(); }
int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_symbols *sy; struct libscols_cell *title; setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ scols_init_debug(0); tb = scols_new_table(); if (!tb) err(EXIT_FAILURE, "failed to create output table"); scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, "foo", "bla bla bla"); add_line(tb, "bar", "alb alb alb"); title = scols_table_get_title(tb); /* right */ scols_cell_set_data(title, "This is right title"); scols_cell_set_color(title, "red"); scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); scols_print_table(tb); /* center */ sy = scols_new_symbols(); if (!sy) err_oom(); scols_table_set_symbols(tb, sy); scols_symbols_set_title_padding(sy, "="); scols_cell_set_data(title, "This is center title (with padding)"); scols_cell_set_color(title, "green"); scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); scols_print_table(tb); /* left */ scols_symbols_set_title_padding(sy, "-"); scols_cell_set_data(title, "This is left title (with padding)"); scols_cell_set_color(title, "blue"); scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }
static void add_structured_data_paramf(struct list_head *ls, const char *fmt, ...) { struct structured_data *sd; va_list ap; int x; assert(!list_empty(ls)); assert(fmt); sd = list_last_entry(ls, struct structured_data, sds); va_start(ap, fmt); x = strv_extendv(&sd->params, fmt, ap); va_end(ap); if (x) err_oom(); }
void mempool_free(mempool_t *pool, void *ptr) { /* Check if the pointer is in this pool */ if (pool->mem_start <= (char*)ptr && (char*)ptr <= pool->mem_end) { if (pool->free_spots_size == pool->free_spots_res) { size_t new_size = 2*sizeof(char*)*pool->free_spots_res; char **new_arr = realloc(pool->free_spots, new_size); if (!new_arr) err_oom("mempool_free"); pool->free_spots = new_arr; pool->free_spots_res *= 2; } pool->free_spots[pool->free_spots_size] = ptr; pool->free_spots_size++; return; } else if (pool->next) { mempool_free(pool->next, ptr); return; } else { fprintf(stderr, "tried to mempool_free a pointer not in the memory pool!\n"); exit(EXIT_FAILURE); } }
int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_symbols *sy; struct libscols_cell *title; int c; static const struct option longopts[] = { { "maxout", 0, NULL, 'm' }, { "width", 1, NULL, 'w' }, { NULL, 0, NULL, 0 }, }; setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ scols_init_debug(0); tb = scols_new_table(); if (!tb) err(EXIT_FAILURE, "failed to create output table"); while((c = getopt_long(argc, argv, "mw:", longopts, NULL)) != -1) { switch(c) { case 'm': scols_table_enable_maxout(tb, TRUE); break; case 'w': scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); break; } } scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, "foo", "bla bla bla"); add_line(tb, "bar", "alb alb alb"); title = scols_table_get_title(tb); /* right */ scols_cell_set_data(title, "This is right title"); scols_cell_set_color(title, "red"); scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); scols_print_table(tb); /* center */ sy = scols_new_symbols(); if (!sy) err_oom(); scols_table_set_symbols(tb, sy); scols_symbols_set_title_padding(sy, "="); scols_cell_set_data(title, "This is center title (with padding)"); scols_cell_set_color(title, "green"); scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); scols_print_table(tb); /* left */ scols_symbols_set_title_padding(sy, "-"); scols_cell_set_data(title, "This is left title (with padding)"); scols_cell_set_color(title, "blue"); scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }