From: Johnny Richard <johnny@johnnyrichard.com>
To: Carlos Maniero <carlos@maniero.me>
Cc: ~johnnyrichard/olang-devel@lists.sr.ht
Subject: Re: [PATCH olang 2/2] utils: create hash map data structure
Date: Tue, 27 Feb 2024 20:30:56 +0100 [thread overview]
Message-ID: <4vjiv6ckgbqpm7ywtwsv5fjh4n3fn356ugotr6xv7nm35y2ivz@wkqdfiuqfigv> (raw)
In-Reply-To: <CZG1CU5AIUW0.3VVCBS15UFO08@maniero.me>
On Tue, Feb 27, 2024 at 02:26:17PM -0300, Carlos Maniero wrote:
> diff --git a/src/arena.h b/src/arena.h
> index 157165c..230eb5b 100644
> --- a/src/arena.h
> +++ b/src/arena.h
> @@ -38,4 +38,7 @@ arena_release(arena_t *arena);
> void
> arena_free(arena_t *arena);
>
> +char *
> +arena_strdup(arena_t *arena, const char *s);
I would move it to arena if the function wasn't cstr specific. I mean,
a memcpy would be a better fit since it's agnostic / generic.
> +
> #endif
> diff --git a/src/map.c b/src/map.c
> index 532ba3b..849130e 100644
> --- a/src/map.c
> +++ b/src/map.c
> @@ -31,8 +31,8 @@ u32_fnv1a_hash(const char *s);
> static void
> map_init(map_t *map);
>
> -static char *
> -__strdup(const char *s, arena_t *arena);
> +static uint32_t
> +map_index(map_t *map, uint32_t hash);
>
> map_t *
> map_new(arena_t *arena)
> @@ -78,24 +78,27 @@ map_put(map_t *map, char *key, void *value)
> {
> assert(map && key);
> uint32_t hash = u32_fnv1a_hash(key);
> - map_entry_t *entry = &(map->entries[hash & (map->capacity - 1)]);
> + map_entry_t *entry = map->entries + map_index(map, hash);
>
> - while (entry != NULL) {
> + if (entry == NULL) {
> + *entry = (map_entry_t){ .key = arena_strdup(map->arena, key), .hash = hash, .value = value, .next = NULL };
It can cause a segfault, perhaps we should keep the 'entry->key == NULL'
since the entries are filled up with zeros (0).
> +static uint32_t
> +map_index(map_t *map, uint32_t hash)
Nice change, what do you think about rename it to **map_get_index**?
> diff --git a/tests/unit/map_test.c b/tests/unit/map_test.c
> index 3eb9acd..fab4c69 100644
> --- a/tests/unit/map_test.c
> +++ b/tests/unit/map_test.c
> @@ -52,7 +52,10 @@ test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
> assert_int(*((int *)map_get(map, "n1")), ==, n1);
> assert_int(*((int *)map_get(map, "n2")), ==, n2);
>
> + map_put(map, "n1", (void *)&n2);
> +
> arena_free(&arena);
> + assert_int(*((int *)map_get(map, "n1")), ==, n2);
Wired this work after free arena.
next prev parent reply other threads:[~2024-02-27 18:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-21 22:20 [PATCH olang 0/2] introduce " Johnny Richard
2024-02-21 22:20 ` [PATCH olang 1/2] build: add clean target on root Makefile Johnny Richard
2024-02-21 22:20 ` [PATCH olang 2/2] utils: create hash map data structure Johnny Richard
2024-02-21 21:24 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-02-27 17:26 ` [PATCH olang 2/2] utils: create hash map data structure Carlos Maniero
2024-02-27 19:30 ` Johnny Richard [this message]
2024-02-27 18:44 ` Carlos Maniero
2024-02-27 20:05 ` Johnny Richard
2024-02-27 20:03 ` Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4vjiv6ckgbqpm7ywtwsv5fjh4n3fn356ugotr6xv7nm35y2ivz@wkqdfiuqfigv \
--to=johnny@johnnyrichard.com \
--cc=carlos@maniero.me \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox