From 2d5f59777de38f2dceeae1acf85922fb4f220443 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 21 Jan 2025 17:25:32 +0900 Subject: [PATCH] [qfcc] Use string offset/id slots in strpool This makes it a little easier to deal with unique strings, particularly for spir-v. --- tools/qfcc/include/strpool.h | 14 ++++++- tools/qfcc/source/strpool.c | 80 ++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/tools/qfcc/include/strpool.h b/tools/qfcc/include/strpool.h index 557ee68d4..16c5218fe 100644 --- a/tools/qfcc/include/strpool.h +++ b/tools/qfcc/include/strpool.h @@ -31,16 +31,28 @@ #ifndef __strpool_h #define __strpool_h +#include "QF/darray.h" + +typedef struct strid_s { + uint32_t offset; + uint32_t id; // external (to strpool) id (eg, for spir-v) +} strid_t; + +typedef struct DARRAY_TYPE (strid_t) strid_set_t; + typedef struct strpool_s { char *strings; struct hashtab_s *str_tab; - int size, max_size; + size_t size, max_size; int qfo_space; + strid_set_t strids; } strpool_t; strpool_t *strpool_new (void); strpool_t *strpool_build (const char *strings, int size); void strpool_delete (strpool_t *strpool); +//XXX NOTE: not pointer-stable, do not hold onto the pointer!!! +strid_t *strpool_addstrid (strpool_t *strpool, const char *str); int strpool_addstr (strpool_t *strpool, const char *str); int strpool_findstr (strpool_t *strpool, const char *str); diff --git a/tools/qfcc/source/strpool.c b/tools/qfcc/source/strpool.c index 6d4a04f26..60da261de 100644 --- a/tools/qfcc/source/strpool.c +++ b/tools/qfcc/source/strpool.c @@ -49,15 +49,17 @@ #include "tools/qfcc/include/options.h" #include "tools/qfcc/include/strpool.h" +#define STR_GROW 16384 + static hashtab_t *saved_strings; static const char * -strpool_get_key (const void *_str, void *_strpool) +strpool_get_key (const void *_ind, void *_strpool) { - long str = (intptr_t) _str; - strpool_t *strpool = (strpool_t *) _strpool; - - return strpool->strings + str; + auto ind = (size_t) _ind; + auto strpool = (strpool_t *) _strpool; + auto strid = &strpool->strids.a[ind]; + return strpool->strings + strid->offset; } strpool_t * @@ -65,28 +67,35 @@ strpool_new (void) { strpool_t *strpool = calloc (1, sizeof (strpool_t)); - strpool->str_tab = Hash_NewTable (16381, strpool_get_key, 0, strpool, 0); - strpool->size = 1; - strpool->max_size = 16384; - strpool->strings = malloc (strpool->max_size); + *strpool = (strpool_t) { + .strings = malloc (STR_GROW), + .str_tab = Hash_NewTable (16381, strpool_get_key, 0, strpool, 0), + .size = 1, + .max_size = STR_GROW, + .strids = DARRAY_STATIC_INIT (512), + }; strpool->strings[0] = 0; + DARRAY_APPEND (&strpool->strids, (strid_t) { }); return strpool; } strpool_t * strpool_build (const char *strings, int size) { - intptr_t s; + uintptr_t s; strpool_t *strpool = malloc (sizeof (strpool_t)); strpool->str_tab = Hash_NewTable (16381, strpool_get_key, 0, strpool, 0); strpool->size = size + (*strings != 0); - strpool->max_size = (strpool->size + 16383) & ~16383; + strpool->max_size = (strpool->size + (STR_GROW - 1)) & ~(STR_GROW - 1); strpool->strings = malloc (strpool->max_size); memcpy (strpool->strings + (*strings != 0), strings, strpool->size); strpool->strings[0] = 0; for (s = 1; s < strpool->size; s += strlen (strpool->strings + s) + 1) { - Hash_Add (strpool->str_tab, (void *) s); + size_t ind = strpool->strids.size; + DARRAY_APPEND (&strpool->strids, (strid_t) { .offset = s }); + auto strid = &strpool->strids.a[ind]; + Hash_Add (strpool->str_tab, strid); } return strpool; } @@ -99,27 +108,38 @@ strpool_delete (strpool_t *strpool) free (strpool); } +strid_t * +strpool_addstrid (strpool_t *strpool, const char *str) +{ + if (!str || !*str) { + return &strpool->strids.a[0]; + } + auto ind = (size_t) Hash_Find (strpool->str_tab, str); + if (ind) { + return &strpool->strids.a[ind]; + } + size_t len = strlen (str) + 1; + if (strpool->size + len > strpool->max_size) { + strpool->max_size += (len + (STR_GROW - 1)) & ~(STR_GROW - 1); + strpool->strings = realloc (strpool->strings, strpool->max_size); + } + + size_t s = strpool->size; + strpool->size += len; + strcpy (strpool->strings + s, str); + ind = strpool->strids.size; + DARRAY_APPEND (&strpool->strids, (strid_t) { .offset = s }); + auto strid = &strpool->strids.a[ind]; + Hash_Add (strpool->str_tab, (void *) ind); + + return strid; +} + int strpool_addstr (strpool_t *strpool, const char *str) { - intptr_t s; - int len; - - if (!str) - return 0; - s = (intptr_t) Hash_Find (strpool->str_tab, str); - if (s) - return s; - len = strlen (str) + 1; - if (strpool->size + len > strpool->max_size) { - strpool->max_size += (len + 16383) & ~16383; - strpool->strings = realloc (strpool->strings, strpool->max_size); - } - s = strpool->size; - strpool->size += len; - strcpy (strpool->strings + s, str); - Hash_Add (strpool->str_tab, (void *) s); - return s; + auto strid = strpool_addstrid (strpool, str); + return strid->offset; } int