aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-01-23 22:31:55 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2017-01-23 22:32:21 +0100
commitc43e9f348f17382bd90e337be07dfbe15d0746b9 (patch)
tree9f4fb6c4cca5038675f9169971b01db75f07bc7b
parentb4cc98e63c38bd445978610839f64590f6fad570 (diff)
downloadhex-c43e9f348f17382bd90e337be07dfbe15d0746b9.tar.gz
hex-c43e9f348f17382bd90e337be07dfbe15d0746b9.tar.xz
hex-c43e9f348f17382bd90e337be07dfbe15d0746b9.zip
Improve ARRAY
-rw-r--r--hex.c21
-rw-r--r--tui.c1
2 files changed, 10 insertions, 12 deletions
diff --git a/hex.c b/hex.c
index 3b4ad26..1c28ae6 100644
--- a/hex.c
+++ b/hex.c
@@ -103,23 +103,20 @@ update_curses_terminal_size (void)
// --- Simple array support ----------------------------------------------------
-// Primitives for arrays
+// The most basic helper macros to make working with arrays not suck
#define ARRAY(type, name) type *name; size_t name ## _len, name ## _size;
-#define ARRAY_INIT(a) \
+#define ARRAY_INIT_SIZED(a, n) \
BLOCK_START \
- a = xcalloc (sizeof *a, (a ## _size = 16)); \
- a ## _len = 0; \
+ (a) = xcalloc (sizeof *(a), (a ## _size) = (n)); \
+ (a ## _len) = 0; \
BLOCK_END
+#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
#define ARRAY_RESERVE(a, n) \
- array_reserve ((void **) &a, sizeof *a, a ## _len + n, &a ## _size)
-
-static void
-array_reserve (void **array, size_t element_size, size_t len, size_t *size)
-{
- while (len > *size)
- *array = xreallocarray (*array, element_size, *size <<= 1);
-}
+ BLOCK_START \
+ while ((a ## _size) - (a ## _len) < n) \
+ (a) = xreallocarray ((a), sizeof *(a), (a ## _size) <<= 1); \
+ BLOCK_END
// --- Application -------------------------------------------------------------
diff --git a/tui.c b/tui.c
index 3887369..4d803df 100644
--- a/tui.c
+++ b/tui.c
@@ -90,6 +90,7 @@ struct row_char
struct row_buffer
{
+ // TODO: rewrite this using ARRAY
struct row_char *chars; ///< Characters
size_t chars_len; ///< Character count
size_t chars_alloc; ///< Characters allocated