struct app * app_alloc(void) { struct app *app = calloc_or_die(1, sizeof(struct app)); app->views = calloc_or_die(1, sizeof(struct view *)); app->views[0] = screen_view_alloc(); app->views_count = 1; return app; }
void *bst_add(struct bst *current_bst, long key, size_t blknum, size_t blksize) { if (NULL == current_bst) { errno = EINVAL; return NULL; } struct bst_node_ *bstnode = calloc_or_die(1, sizeof(struct bst_node_)); if (SIZE_MAX == current_bst->numnode) { #ifndef NDEBUG const char *const err_msg = "bst: max node count reached\n"; write(STDERR_FILENO, err_msg, strlen(err_msg)); #endif free(bstnode); errno = EOVERFLOW; return NULL; } bstnode->key = key; bstnode->memblk = calloc_or_die(blknum, blksize); bstnode->blknum = blknum; bstnode->blksize = blksize; /*again, redundant*/ bstnode->link[BST_LEFT] = NULL; bstnode->link[BST_RIGHT] = NULL; /* *start to insert the new node into the bst */ if (NULL == current_bst->root) { current_bst->root = bstnode; } else { enum bst_link_dir dir = BST_LEFT; struct bst_node_ *tmp_ptr = current_bst->root; while (true) { /*note that duplicate keys are NOT allowed*/ if (bstnode->key == tmp_ptr->key) { free(bstnode); return NULL; } dir = bstnode->key > tmp_ptr->key; if (NULL == tmp_ptr->link[dir]) break; tmp_ptr = tmp_ptr->link[dir]; } tmp_ptr->link[dir] = bstnode; } current_bst->numnode++; return bstnode->memblk; }
struct selection * selection_alloc_or_die(char const *title) { assert(title && title[0]); struct selection *selection = calloc_or_die(1, sizeof(struct selection)); selection->index = -1; selection->items = calloc_or_die(1, sizeof(ITEM *)); selection->title = strdup_or_die(title); return selection; }
struct result selection_add_item(struct selection *selection, char const *description, selection_action_fn *action) { if (!selection) return result_set_system_error(EINVAL); if (!description) return result_set_system_error(EINVAL); if (!description[0]) return result_set_system_error(EINVAL); int index = selection->items_count; int null_index = index + 1; ++selection->items_count; selection->items = reallocarray_or_die(selection->items, selection->items_count + 1, sizeof(ITEM *)); char name[] = "1"; name[0] += index; char *name_dup = strdup_or_die(name); char *description_dup = strdup_or_die(description); selection->items[index] = new_item(name_dup, description_dup); if (!selection->items[index]) { free_or_die(description_dup); free_or_die(name_dup); return result_system_error(); } struct selection_item *selection_item = calloc_or_die(1, sizeof(struct selection_item)); selection_item->action = action; set_item_userptr(selection->items[index], selection_item); selection->items[null_index] = NULL; return result_success(); }
struct tile * tile_alloc_copy(struct tile *tile) { struct tile *copy = calloc_or_die(1, sizeof(struct tile)); *copy = *tile; return copy; }
struct tile * tile_alloc(struct point point, enum tile_type type) { struct tile *tile = calloc_or_die(1, sizeof(struct tile)); tile->point = point; tile->type = type; return tile; }
struct text_rectangle * text_rectangle_alloc(int column_count, int row_count) { struct text_rectangle *text_rectangle = calloc_or_die(1, sizeof(struct text_rectangle)); text_rectangle->chars = malloc_or_die(chars_size(column_count, row_count)); text_rectangle->column_count = column_count; text_rectangle->row_count = row_count; text_rectangle_clear(text_rectangle); return text_rectangle; }
struct bst *bst_init(void) { struct bst *new_bst = calloc_or_die(1, sizeof(struct bst)); /*redundant but logical*/ new_bst->numnode = 0; new_bst->root = NULL; return new_bst; }
struct dungeon_options * dungeon_options_alloc(int max_iteration_count, struct size max_size, int padding) { struct dungeon_options *dungeon_options = calloc_or_die(1, sizeof(struct dungeon_options)); dungeon_options->max_iteration_count = max_iteration_count; dungeon_options->max_size = max_size; dungeon_options->padding = padding; return dungeon_options; }
struct area * area_alloc(enum area_type area_type, enum direction direction, struct box box, enum tile_type tile_type) { struct area *area = calloc_or_die(1, sizeof(struct area)); area->box = box; area->direction = direction; area->type = area_type; return area; }
struct options * options_alloc(int argc, char *argv[]) { struct options *options = calloc_or_die(1, sizeof(struct options)); options->command_name = strdup_or_die(basename(argv[0])); options->rnd = rnd_alloc(); int action_index = get_options(options, argc, argv); if (options->error) return options; get_action(options, argc, argv, action_index); return options; }
struct view * view_alloc(view_create_fn *create, view_destroy_fn *destroy, view_draw_fn *draw, view_on_key_fn *on_key, void *user_data) { struct view *view = calloc_or_die(1, sizeof(struct view)); view->create = create; view->destroy = destroy; view->draw = draw; view->on_key = on_key; view->user_data = user_data; return view; }