~ashn/autil

efa8ee3859fb547a7c9e097a0bdeb5551c5cf294 — ashn 6 months ago b80ac34
Remove autil_sipool and associated functions

The `struct autil_sipool` type was originally added to support Sunder
interned strings. However, this type was eventually replaced with
Sunder's own interned string hash set. The current sipool implementation
wraps map operations, and thus could be re-implemented in individual
projects using `struct autil_map` if needed.
4 files changed, 1 insertions(+), 117 deletions(-)

M CHANGELOG-autil.txt
M Makefile
M autil.h
D tests/autil_sipool.example.c
M CHANGELOG-autil.txt => CHANGELOG-autil.txt +1 -0
@@ 1,6 1,7 @@
Unreleased
--------------------------------
+ Replace autil_string_split* functions with a single autil_string_split.
+ Remove autil_sipool and associated functions.

v1.0.2 - 2022-06-03
--------------------------------

M Makefile => Makefile +0 -1
@@ 113,7 113,6 @@ TESTS = \
	tests/autil_string_append_fmt.test \
	tests/autil_string_trim.test \
	tests/autil_string_split.test \
	tests/autil_sipool.example.test \
	tests/autil_vec_reserve.test \
	tests/autil_vec_resize.test \
	tests/autil_vec_insert.test \

M autil.h => autil.h +0 -85
@@ 65,7 65,6 @@ LICENSE
#include <stdio.h> /* FILE*, printf-family */

struct autil_vstr;
struct autil_sipool;
struct autil_bitarr;
struct autil_bigint;
struct autil_string;


@@ 310,26 309,6 @@ autil_vstr_ends_with(
    struct autil_vstr const* vstr, struct autil_vstr const* target);

////////////////////////////////////////////////////////////////////////////////
//////// CSTR INTERN POOL //////////////////////////////////////////////////////

// Allocate and initialize a string intern pool.
AUTIL_API struct autil_sipool*
autil_sipool_new(void);
// Deinitialize and free the string intern pool.
// Does nothing if self == NULL.
AUTIL_API void
autil_sipool_del(struct autil_sipool* self);

// Intern the string specified by the first count bytes of start.
// Returns the canonical NUL-terminated representation of the interned string.
AUTIL_API char const*
autil_sipool_intern(struct autil_sipool* self, char const* start, size_t count);
// Intern the string specified by the provided NUL-terminated cstring.
// Returns the canonical NUL-terminated representation of the interned string.
AUTIL_API char const*
autil_sipool_intern_cstr(struct autil_sipool* self, char const* cstr);

////////////////////////////////////////////////////////////////////////////////
//////// STRETCHY BUFFER ///////////////////////////////////////////////////////
// General purpose type-safe dynamic array (a.k.a stretchy buffer).
//


@@ 1498,70 1477,6 @@ autil_vstr_ends_with(
    return autil_memcmp(start, target->start, target->count) == 0;
}

struct autil_sipool {
    // List of heap-allocated strings interned within this pool. The key and
    // val elements of the map member reference memory owned by this list.
    autil_sbuf(char*) strings;
    // KEY TYPE: struct autil_vstr
    // VAL TYPE: char const*
    struct autil_map* map;
};

AUTIL_API struct autil_sipool*
autil_sipool_new(void)
{
    struct autil_sipool* const self = autil_xalloc(NULL, sizeof(*self));
    self->strings = NULL;
    self->map = autil_map_new(
        sizeof(struct autil_vstr), sizeof(char const*), autil_vstr_vpcmp);
    return self;
}

AUTIL_API void
autil_sipool_del(struct autil_sipool* self)
{
    if (self == NULL) {
        return;
    }

    for (size_t i = 0; i < autil_sbuf_count(self->strings); ++i) {
        autil_xalloc(self->strings[i], AUTIL_XALLOC_FREE);
    }
    autil_sbuf_fini(self->strings);
    autil_map_del(self->map);

    memset(self, 0x00, sizeof(*self)); // scrub
    autil_xalloc(self, AUTIL_XALLOC_FREE);
}

AUTIL_API char const*
autil_sipool_intern(struct autil_sipool* self, char const* start, size_t count)
{
    assert(self != NULL);
    assert(start != NULL || count == 0);

    char const* const* const pexisting =
        autil_map_lookup_const(self->map, AUTIL_VSTR_LOCAL_PTR(start, count));
    if (pexisting != NULL) {
        return *pexisting;
    }

    char* str = autil_cstr_new(start, count);
    autil_sbuf_push(self->strings, str);
    autil_map_insert(
        self->map, AUTIL_VSTR_LOCAL_PTR(str, count), &str, NULL, NULL);
    return str;
}

AUTIL_API char const*
autil_sipool_intern_cstr(struct autil_sipool* self, char const* cstr)
{
    assert(self != NULL);
    assert(cstr != NULL);

    return autil_sipool_intern(self, cstr, strlen(cstr));
}

AUTIL_STATIC_ASSERT(
    SBUF_HEADER_OFFSET_IS_ALIGNED,
    AUTIL__SBUF_HEADER_OFFSET_ % AUTIL_ALIGNOF(autil_max_align_type) == 0);

D tests/autil_sipool.example.c => tests/autil_sipool.example.c +0 -31
@@ 1,31 0,0 @@
#define AUTIL_IMPLEMENTATION
#include "../autil.h"
#include "test.h"

int
main(void)
{
    struct autil_sipool* const pool = autil_sipool_new();

    char const str1[] = {'f', 'o', 'o'};
    char const* const str2 = "foo";
    char* const str3 = autil_cstr_new_cstr("foo");
    char const* const str4 = "fo";

    char const* const intern1 = autil_sipool_intern(pool, str1, 3);
    char const* const intern2 = autil_sipool_intern_cstr(pool, str2);
    char const* const intern3 = autil_sipool_intern_cstr(pool, str3);
    char const* const intern4 = autil_sipool_intern_cstr(pool, str4);
    char const* const intern5 = autil_sipool_intern(pool, "foobar", 2);
    char const* const intern6 = autil_sipool_intern(pool, NULL, 0);
    char const* const intern7 = autil_sipool_intern_cstr(pool, "");

    ASSERT(intern1 == intern2);
    ASSERT(intern1 == intern3);
    ASSERT(intern1 != intern4);
    ASSERT(intern4 == intern5);
    ASSERT(intern6 == intern7);

    autil_xalloc(str3, AUTIL_XALLOC_FREE);
    autil_sipool_del(pool);
}