From: Egor Date: Sat, 1 Mar 2025 21:37:19 +0000 (+0200) Subject: map: specify types X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=e00f13ec25c4935e2c4c318027b390e2c96aafd4;p=patchyircd.git map: specify types --- diff --git a/src/admin.cpp b/src/admin.cpp index 46eb974..b059b1a 100644 --- a/src/admin.cpp +++ b/src/admin.cpp @@ -12,7 +12,7 @@ static lua_State *GL = 0; #define MAPTYPE_CHANNEL 2 static int map_index(lua_State *L) { - map_t *m = *(map_t **)luaL_checkudata(L, 1, "map"); + map_base *m = *(map_base **)luaL_checkudata(L, 1, "map"); const char *key = luaL_checkstring(L, 2); void *value = map_get(m, key); if (!value) @@ -36,7 +36,7 @@ static int map_index(lua_State *L) static int map_newindex(lua_State *L) { - map_t *m = *(map_t **)luaL_checkudata(L, 1, "map"); + map_base *m = *(map_base **)luaL_checkudata(L, 1, "map"); const char *key = luaL_checkstring(L, 2); luaL_checkany(L, 3); if (lua_isnil(L, 3)) { @@ -93,7 +93,7 @@ static int map_iter_next(lua_State *L) } static int map_pairs(lua_State *L) { - map_t *m = *(map_t **)luaL_checkudata(L, 1, "map"); + map_base *m = *(map_base **)luaL_checkudata(L, 1, "map"); lua_pushcfunction(L, map_iter_next); map_iter_t *it = (map_iter_t *)lua_newuserdatauv(L, sizeof(map_iter_t), 2); memset(it, 0, sizeof(*it)); @@ -106,9 +106,9 @@ static int map_pairs(lua_State *L) lua_pushnil(L); return 3; } -static void map_push(lua_State *L, map_t *map, int type) +static void map_push(lua_State *L, map_base *map, int type) { - *(map_t **)lua_newuserdatauv(L, sizeof(map_t *), 1) = map; + *(map_base **)lua_newuserdatauv(L, sizeof(map_base *), 1) = map; luaL_setmetatable(L, "map"); lua_pushinteger(L, type); lua_setiuservalue(L, -2, 1); diff --git a/src/common.h b/src/common.h index f2d98c8..3827de8 100644 --- a/src/common.h +++ b/src/common.h @@ -59,9 +59,9 @@ struct client_data { size_t wbufLen; char umode; char regstate; - map_t channels; - map_t invites; - map_t monitor; + channelmap channels; + channelmap invites; + stringset monitor; int cookie; time_t signon; time_t lastactivity; @@ -85,18 +85,18 @@ struct channel { char *topic_who; time_t topic_when; time_t creation; - map_t members; - map_t o; - map_t v; + clientmap members; + clientmap o; + clientmap v; int member_count; char dmode; - map_t listb; - map_t liste; - map_t listI; - map_t listq; - map_t listwo; - map_t listwv; - map_t invitees; + stringset listb; + stringset liste; + stringset listI; + stringset listq; + stringset listwo; + stringset listwv; + clientmap invitees; char *chankey; int member_limit; }; @@ -108,9 +108,9 @@ struct channel { #define DMODE_TOPICPROT 32 #define is_chanop(chan, client) (!!map_get(&(chan)->o, (client)->key)) #define is_chanvoice(chan, client) (!!map_get(&(chan)->v, (client)->key)) -extern map_t client_map; -extern map_t channel_map; -extern map_t autojoin; +extern clientmap client_map; +extern channelmap channel_map; +extern stringset autojoin; extern struct client_data *clients; extern struct client_data *dyingclients; diff --git a/src/irc.cpp b/src/irc.cpp index f82b200..c9cd878 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -5,9 +5,9 @@ struct dynarr databuf; -map_t client_map = {}; -map_t channel_map = {}; -map_t autojoin = {}; +clientmap client_map = {}; +channelmap channel_map = {}; +stringset autojoin = {}; const char *hostname = IRCDHOSTNAME; void sendbuf(struct client_data *client) { @@ -36,7 +36,7 @@ static void sendbuf_chan_caps(struct channel *chan, unsigned positive, unsigned static void sendbuf_chan(struct channel *chan) { sendbuf_chan_caps(chan, 0, 0); } -static void sendbuf_list_sans_self_caps(struct client_data *client, map_t *ls, int cookie, int positive, int negative) { +static void sendbuf_list_sans_self_caps(struct client_data *client, clientmap *ls, int cookie, int positive, int negative) { struct client_data *cl; map_foreach(ls, 0, &cl) if (client != cl) { @@ -50,7 +50,7 @@ static void sendbuf_list_sans_self_caps(struct client_data *client, map_t *ls, i sendbuf(cl); } } -static void sendbuf_list_sans_self(struct client_data *client, map_t *ls, int cookie) { +static void sendbuf_list_sans_self(struct client_data *client, clientmap *ls, int cookie) { sendbuf_list_sans_self_caps(client, ls, cookie, 0, 0); } void sendbuf_monitor(const char *nick, int cookie, int positive, int negative) @@ -102,7 +102,7 @@ const char *format_umodes(struct client_data *cl) { *p++ = '\0'; return umodes; } -static int match_list(struct client_data *cl, map_t *list) +static int match_list(struct client_data *cl, stringset *list) { const char *key; map_foreach(list, &key, 0) @@ -1645,7 +1645,7 @@ void channel_mode(struct client_data *client, struct channel *chan, char *args[] err_unknowncommand(client, "MODE"), sendbuf(client); return; } - map_t *list; + stringset *list; while (*p) { switch (*p) { case '+': is_plus = 1; break; diff --git a/src/map.cpp b/src/map.cpp index bfd1ebc..dbe039f 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1,7 +1,7 @@ #include "map.h" #include #include -static int ent_find(map_t *root, const char *key, int *found) +static int ent_find(map_base *root, const char *key, int *found) { int lo = 0; int hi = root->len; @@ -20,13 +20,13 @@ static int ent_find(map_t *root, const char *key, int *found) *found = 0; return lo; } -void *map_get(map_t *root, const char *key) +void *map_get(map_base *root, const char *key) { int found; int i = ent_find(root, key, &found); return found ? root->ptr[i].value : 0; } -int map_set(map_t *root, const char *key, const void *value, void **oldvalue) +int map_set(map_base *root, const char *key, const void *value, void **oldvalue) { int found; int i = ent_find(root, key, &found); @@ -65,7 +65,7 @@ int map_set(map_t *root, const char *key, const void *value, void **oldvalue) } return 0; } -void map_free(map_t *root) +void map_free(map_base *root) { while (root->len) { free(root->ptr[--root->len].key); @@ -85,7 +85,7 @@ int map_foriter(map_iter_t *it, const char **outkey, void *outvalue) it->index++; return 1; } -void *map_pop(map_t *root) +void *map_pop(map_base *root) { return root->len ? root->ptr[root->len-1].value : 0; } diff --git a/src/map.h b/src/map.h index 0b67571..b4c79a1 100644 --- a/src/map.h +++ b/src/map.h @@ -16,15 +16,22 @@ struct map_value { char *key; void *value; }; -typedef struct map { +struct map_base { struct map_value *ptr; size_t len, cap; -} map_t; +}; typedef struct map_iter { - map_t *root; + map_base *root; size_t index; } map_iter_t; +struct clientmap : map_base { +}; +struct channelmap : map_base { +}; +struct stringset : map_base { +}; + /* A map abstract data type with following properties: * - keys are lexicographically-sorted unique NUL-terminated strings * - values are non-NULL pointers @@ -38,19 +45,19 @@ typedef struct map_iter { /* Return the associated value for a given key. * Dynarr complexity: O(log n) */ -void *map_get(map_t *root, const char *key); +void *map_get(map_base *root, const char *key); /* Associate a new value with a given key. * Returns 0 on success, -1 on allocation error. * If oldvalue is not NULL, it receives the old value of key. * Dynarr complexity: O(n) */ -NODISCARD int map_set(map_t *root, const char *key, const void *value, void **oldvalue); +NODISCARD int map_set(map_base *root, const char *key, const void *value, void **oldvalue); /* Remove a key. * Same as associating a key to NULL. No allocation failures. */ #define map_unset(root, key, oldvalue) ((void)map_set((root), (key), 0, (oldvalue))) /* Removes all keys from the map and frees any associated memory. * The map_t is reset to zero-initialized state and can be used again. * Dynarr complexity: O(n) */ -void map_free(map_t *root); +void map_free(map_base *root); /* Iterate over a map. * @@ -76,5 +83,5 @@ NODISCARD int map_foriter(map_iter_t *f, const char **outkey, void *outvalue); * } * map_free(&map); */ -NODISCARD void *map_pop(map_t *root); +NODISCARD void *map_pop(map_base *root); #endif