From 48fbf21a6d6085d561757afb162d9f7b98792bf9 Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Fri, 5 Dec 2025 21:54:50 -0700 Subject: map: stop setting errno on error the return of these functions already give all the necessary information. this is not c standard library code, we have no need of such a nonsensical error reporting mechanism NOTE: since errno needs to be thread local accessing it from non-libc code ends up being a function call and serves as a pointless optimization barrier. --- map.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) (limited to 'map.c') diff --git a/map.c b/map.c index f6e5c64..6ec02d6 100644 --- a/map.c +++ b/map.c @@ -11,7 +11,6 @@ */ #include #include -#include #include #include "map.h" @@ -63,14 +62,10 @@ void *map_get(const Map *map, const char *key) void *map_closest(const Map *map, const char *prefix) { - errno = 0; - void *v = map_get(map, prefix); - if (v) - return v; - const Map *m = map_prefix(map, prefix); - if (map_empty(m)) - errno = ENOENT; - return m->v; + void *result = map_get(map, prefix); + if (!result) + result = map_prefix(map, prefix)->v; + return result; } bool map_contains(const Map *map, const char *prefix) @@ -88,15 +83,11 @@ bool map_put(Map *map, const char *k, const void *value) uint8_t bit_num, new_dir; char *key; - if (!value) { - errno = EINVAL; + if (!value) return false; - } - if (!(key = strdup(k))) { - errno = ENOMEM; + if (!(key = strdup(k))) return false; - } /* Empty map? */ if (!map->u.n) { @@ -113,7 +104,6 @@ bool map_put(Map *map, const char *k, const void *value) if (key[byte_num] == '\0') { /* All identical! */ free(key); - errno = EEXIST; return false; } } @@ -130,7 +120,6 @@ bool map_put(Map *map, const char *k, const void *value) newn = malloc(sizeof(*newn)); if (!newn) { free(key); - errno = ENOMEM; return false; } newn->byte_num = byte_num; @@ -171,10 +160,8 @@ void *map_delete(Map *map, const char *key) uint8_t direction; /* Empty map? */ - if (!map->u.n) { - errno = ENOENT; + if (!map->u.n) return NULL; - } /* Find closest, but keep track of parent. */ n = map; @@ -193,10 +180,8 @@ void *map_delete(Map *map, const char *key) } /* Did we find it? */ - if (strcmp(key, n->u.s)) { - errno = ENOENT; + if (strcmp(key, n->u.s)) return NULL; - } free((char*)n->u.s); value = n->v; -- cgit v1.2.3