Use the ARRAY abstraction

This commit is contained in:
Přemysl Eric Janouch 2017-06-05 18:57:54 +02:00
parent b050113eed
commit e62c41f4a1
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 6 additions and 13 deletions

View File

@ -2675,8 +2675,7 @@ struct debug_item
static struct static struct
{ {
struct tab super; ///< Parent class struct tab super; ///< Parent class
struct debug_item *items; ///< Items ARRAY (struct debug_item, items) ///< Items
size_t items_alloc; ///< How many items are allocated
bool active; ///< The tab is present bool active; ///< The tab is present
} }
g_debug_tab; g_debug_tab;
@ -2684,7 +2683,7 @@ g_debug_tab;
static void static void
debug_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width) debug_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
{ {
hard_assert (item_index <= g_debug_tab.super.item_count); hard_assert (item_index < g_debug_tab.items_len);
struct debug_item *item = &g_debug_tab.items[item_index]; struct debug_item *item = &g_debug_tab.items[item_index];
char buf[16]; char buf[16];
@ -2707,16 +2706,11 @@ debug_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
static void static void
debug_tab_push (const char *message, chtype attrs) debug_tab_push (const char *message, chtype attrs)
{ {
// TODO: uh... aren't we rather going to write our own abstraction? ARRAY_RESERVE (g_debug_tab.items, 1);
if (g_debug_tab.items_alloc <= g_debug_tab.super.item_count)
{
g_debug_tab.items = xreallocarray (g_debug_tab.items,
sizeof *g_debug_tab.items, (g_debug_tab.items_alloc <<= 1));
}
// TODO: there should be a better, more efficient mechanism for this // TODO: there should be a better, more efficient mechanism for this
struct debug_item *item = struct debug_item *item = &g_debug_tab.items[g_debug_tab.items_len++];
&g_debug_tab.items[g_debug_tab.super.item_count++]; g_debug_tab.super.item_count = g_debug_tab.items_len;
item->text = xstrdup (message); item->text = xstrdup (message);
item->attrs = attrs; item->attrs = attrs;
item->timestamp = clock_msec (CLOCK_REALTIME); item->timestamp = clock_msec (CLOCK_REALTIME);
@ -2727,8 +2721,7 @@ debug_tab_push (const char *message, chtype attrs)
static struct tab * static struct tab *
debug_tab_init (void) debug_tab_init (void)
{ {
g_debug_tab.items = xcalloc ARRAY_INIT (g_debug_tab.items);
((g_debug_tab.items_alloc = 16), sizeof *g_debug_tab.items);
g_debug_tab.active = true; g_debug_tab.active = true;
struct tab *super = &g_debug_tab.super; struct tab *super = &g_debug_tab.super;