From d7f5fd5730ca252c2635efd263ba40282a31ca57 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Thu, 10 May 2018 14:12:02 +0200 Subject: replace const sized array with dynamic array for handling modes --- src/hotkey.h | 3 +-- src/parse.c | 8 ++++---- src/sbuffer.h | 39 +++++++++++++++++++++++++++++++++++++++ src/skhd.c | 2 +- 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/sbuffer.h (limited to 'src') diff --git a/src/hotkey.h b/src/hotkey.h index 530520b..576dadf 100644 --- a/src/hotkey.h +++ b/src/hotkey.h @@ -61,8 +61,7 @@ struct hotkey uint32_t flags; uint32_t key; char *command; - int mode_count; - struct mode *mode_list[16]; + struct mode **mode_list; }; static inline void diff --git a/src/parse.c b/src/parse.c index 3e60dd1..d219d8c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -184,7 +184,7 @@ parse_mode(struct parser *parser, struct hotkey *hotkey) return; } - hotkey->mode_list[hotkey->mode_count++] = mode; + buf_push(hotkey->mode_list, mode); printf("\tmode: '%s'\n", mode->name); if (parser_match(parser, Token_Comma)) { @@ -212,13 +212,13 @@ parse_hotkey(struct parser *parser) } } - if (hotkey->mode_count > 0) { + if (buf_len(hotkey->mode_list) > 0) { if (!parser_match(parser, Token_Insert)) { parser_report_error(parser, Error_Unexpected_Token, "expected '<'"); goto err; } } else { - hotkey->mode_list[hotkey->mode_count++] = find_or_init_default_mode(parser); + buf_push(hotkey->mode_list, find_or_init_default_mode(parser)); } if ((found_modifier = parser_match(parser, Token_Modifier))) { @@ -330,7 +330,7 @@ void parse_config(struct parser *parser) (parser_check(parser, Token_Key_Hex)) || (parser_check(parser, Token_Key))) { if ((hotkey = parse_hotkey(parser))) { - for (int i = 0; i < hotkey->mode_count; ++i) { + for (int i = 0; i < buf_len(hotkey->mode_list); ++i) { mode = hotkey->mode_list[i]; table_add(&mode->hotkey_map, hotkey, hotkey); } diff --git a/src/sbuffer.h b/src/sbuffer.h new file mode 100644 index 0000000..92ac811 --- /dev/null +++ b/src/sbuffer.h @@ -0,0 +1,39 @@ +#ifndef SBUFFER_H +#define SBUFFER_H + +#include +#include + +struct buf_hdr +{ + size_t len; + size_t cap; + char buf[0]; +}; + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define OFFSETOF(t, f) (size_t)((char *)&(((t *)0)->f) - (char *)0) + +#define buf__hdr(b) ((struct buf_hdr *)((char *)(b) - OFFSETOF(struct buf_hdr, buf))) +#define buf__should_grow(b, n) (buf_len(b) + (n) >= buf_cap(b)) +#define buf__fit(b, n) (buf__should_grow(b, n) ? ((b) = buf__grow_f(b, buf_len(b) + (n), sizeof(*(b)))) : 0) + +#define buf_len(b) ((b) ? buf__hdr(b)->len : 0) +#define buf_cap(b) ((b) ? buf__hdr(b)->cap : 0) +#define buf_push(b, x) (buf__fit(b, 1), (b)[buf_len(b)] = (x), buf__hdr(b)->len++) +#define buf_last(b) ((b)[buf_len(b)-1]) +#define buf_free(b) ((b) ? free(buf__hdr(b)) : 0) + +static void *buf__grow_f(const void *buf, size_t new_len, size_t elem_size) +{ + size_t new_cap = MAX(1 + 2*buf_cap(buf), new_len); + size_t new_size = OFFSETOF(struct buf_hdr, buf) + new_cap*elem_size; + struct buf_hdr *new_hdr = realloc(buf ? buf__hdr(buf) : 0, new_size); + new_hdr->cap = new_cap; + if (!buf) { + new_hdr->len = 0; + } + return new_hdr->buf; +} + +#endif diff --git a/src/skhd.c b/src/skhd.c index 2e0d6b1..7ef1533 100644 --- a/src/skhd.c +++ b/src/skhd.c @@ -11,6 +11,7 @@ #define HASHTABLE_IMPLEMENTATION #include "hashtable.h" +#include "sbuffer.h" #include "hotload.h" #include "event_tap.h" @@ -42,7 +43,6 @@ extern bool CGSIsSecureEventInputSet(); internal unsigned major_version = 0; internal unsigned minor_version = 0; internal unsigned patch_version = 14; -internal struct table hotkey_map; internal struct table mode_map; internal struct mode *current_mode; internal char *config_file; -- cgit v1.2.3