feat: add SCF format library and rename components to SCC prefix

- Introduce new SCF (SCC Format) library with header, implementation, and test files
- SCF is a minimal executable/linkable format focused on internal linking with external symbol import/export abstraction
- Rename lexer and lex_parser packages from 'smcc_' to 'scc_' prefix for consistency
- Update hashmap implementation to use 'scc_' prefix for types and structures
- Add build configuration for new format library with dependencies on libcore and libutils
This commit is contained in:
zzy
2025-12-11 17:29:12 +08:00
parent d88fa3b8d3
commit 3aaf3a3991
13 changed files with 1048 additions and 56 deletions

View File

@@ -14,35 +14,35 @@
* @enum hp_entry_state_t
* @brief 哈希表条目状态标识
*/
typedef enum hashmap_entry_state {
typedef enum scc_hashmap_entry_state {
ENTRY_EMPTY, /**< 空槽位(从未使用过) */
ENTRY_ACTIVE, /**< 有效条目(包含键值对) */
ENTRY_TOMBSTONE /**< 墓碑标记(已删除条目) */
} hp_entry_state_t;
} scc_hashtable_entry_state_t;
/**
* @struct hashmap_entry_t
* @struct scc_hashtable_entry_t
* @brief 哈希表条目结构
*
* @note key/value内存由调用者管理哈希表不负责其生命周期
*/
typedef struct hashmap_entry {
const void *key; /**< 键指针(不可变) */
void *value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
hp_entry_state_t state; /**< 当前条目状态 */
} hashmap_entry_t;
typedef struct scc_hashtable_entry {
const void *key; /**< 键指针(不可变) */
void *value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
scc_hashtable_entry_state_t state; /**< 当前条目状态 */
} scc_hashtable_entry_t;
/**
* @struct hashmap_t
* @struct scc_hashtable_t
* @brief 哈希表主体结构
*
* 使用开放寻址法实现,采用墓碑标记处理删除操作
*/
typedef struct smcc_hashmap {
SCC_VEC(hashmap_entry_t) entries; /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
typedef struct scc_hashtable {
SCC_VEC(scc_hashtable_entry_t) entries; /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
/**
* @brief 哈希函数指针
* @param key 键指针
@@ -56,7 +56,7 @@ typedef struct smcc_hashmap {
* @return 相同返回0不同返回非0
*/
int (*key_cmp)(const void *key1, const void *key2);
} hashmap_t;
} scc_hashtable_t;
/**
* @brief 初始化哈希表结构
@@ -64,7 +64,7 @@ typedef struct smcc_hashmap {
*
* @warning 必须设置hash_func和key_cmp后才能使用
*/
void hashmap_init(hashmap_t *ht);
void scc_hashtable_init(scc_hashtable_t *ht);
/**
* @brief 插入/更新键值对
@@ -73,7 +73,7 @@ void hashmap_init(hashmap_t *ht);
* @param value 值指针
* @return 被替换的旧值指针无替换返回NULL
*/
void *hashmap_set(hashmap_t *ht, const void *key, void *value);
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value);
/**
* @brief 查找键对应值
@@ -81,7 +81,7 @@ void *hashmap_set(hashmap_t *ht, const void *key, void *value);
* @param key 查找键指针
* @return 找到返回值指针未找到返回NULL
*/
void *hashmap_get(hashmap_t *ht, const void *key);
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key);
/**
* @brief 删除键值对
@@ -91,7 +91,7 @@ void *hashmap_get(hashmap_t *ht, const void *key);
*
* @note 实际采用墓碑标记方式删除
*/
void *hashmap_del(hashmap_t *ht, const void *key);
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key);
/**
* @brief 销毁哈希表
@@ -99,17 +99,18 @@ void *hashmap_del(hashmap_t *ht, const void *key);
*
* @note 仅释放哈希表内部内存不会释放key/value内存
*/
void hashmap_drop(hashmap_t *ht);
void scc_hashtable_drop(scc_hashtable_t *ht);
/**
* @typedef hashmap_iter_fn
* @typedef scc_hashtable_iter_fn
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*hashmap_iter_fn)(const void *key, void *value, void *context);
typedef int (*scc_hashtable_iter_fn)(const void *key, void *value,
void *context);
/**
* @brief 遍历哈希表所有有效条目
@@ -117,6 +118,7 @@ typedef int (*hashmap_iter_fn)(const void *key, void *value, void *context);
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void hashmap_foreach(hashmap_t *ht, hashmap_iter_fn iter_func, void *context);
void scc_hashmap_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
void *context);
#endif /* __SCC_HASHMAP_H__ */

View File

@@ -18,14 +18,14 @@
* 组合哈希表和专用内存分配器实现的高效字符串存储池
*/
typedef struct strpool {
hashmap_t ht; /**< 哈希表用于快速查找已存储字符串 */
} strpool_t;
scc_hashtable_t ht; /**< 哈希表用于快速查找已存储字符串 */
} scc_strpool_t;
/**
* @brief 初始化字符串池
* @param pool 字符串池实例指针
*/
void init_strpool(strpool_t *pool);
void scc_strpool_init(scc_strpool_t *pool);
/**
* @brief 驻留字符串到池中
@@ -36,7 +36,7 @@ void init_strpool(strpool_t *pool);
* @note 返回值生命周期与字符串池一致
* @note 重复插入相同字符串会返回已有指针
*/
const char *strpool_intern(strpool_t *pool, const char *str);
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str);
/**
* @brief 销毁字符串池
@@ -45,6 +45,25 @@ const char *strpool_intern(strpool_t *pool, const char *str);
* @warning 销毁后已获取的字符串指针将失效
* @note 会自动释放所有驻留字符串内存
*/
void strpool_destroy(strpool_t *pool);
void scc_strpool_drop(scc_strpool_t *pool);
/**
* @typedef scc_hashtable_iter_fn
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*scc_strpool_iter_fn)(const char *key, char *value, void *context);
/**
* @brief 遍历字符串表所有有效条目
* @param ht 字符串表实例指针
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void scc_strpool_foreach(scc_strpool_t *pool, scc_strpool_iter_fn iter_func,
void *context);
#endif /* __SCC_STRPOOL_H__ */

View File

@@ -4,7 +4,7 @@
#define SCC_INIT_HASHMAP_SIZE (32)
#endif
void hashmap_init(hashmap_t *ht) {
void scc_hashtable_init(scc_hashtable_t *ht) {
scc_vec_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
@@ -21,17 +21,18 @@ static int next_power_of_two(int n) {
return n + 1;
}
static hashmap_entry_t *find_entry(hashmap_t *ht, const void *key, u32 hash) {
static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
u32 hash) {
if (ht->entries.cap == 0)
return NULL;
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
u32 probe = 0;
hashmap_entry_t *tombstone = NULL;
scc_hashtable_entry_t *tombstone = NULL;
while (1) {
hashmap_entry_t *entry = &scc_vec_at(ht->entries, index);
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, index);
if (entry->state == ENTRY_EMPTY) {
return tombstone ? tombstone : entry;
}
@@ -53,25 +54,27 @@ static hashmap_entry_t *find_entry(hashmap_t *ht, const void *key, u32 hash) {
return NULL;
}
static void adjust_capacity(hashmap_t *ht, int new_cap) {
static void adjust_capacity(scc_hashtable_t *ht, usize new_cap) {
new_cap = next_power_of_two(new_cap);
Assert(new_cap >= ht->entries.cap);
SCC_VEC(hashmap_entry_t) old_entries;
SCC_VEC(scc_hashtable_entry_t) old_entries;
old_entries.data = ht->entries.data;
old_entries.cap = ht->entries.cap;
// Not used size but for gdb python extention debug
ht->entries.size = new_cap;
ht->entries.cap = new_cap;
ht->entries.data = scc_realloc(NULL, new_cap * sizeof(hashmap_entry_t));
scc_memset(ht->entries.data, 0, new_cap * sizeof(hashmap_entry_t));
ht->entries.data =
scc_realloc(NULL, new_cap * sizeof(scc_hashtable_entry_t));
scc_memset(ht->entries.data, 0, new_cap * sizeof(scc_hashtable_entry_t));
// rehash the all of the old data
for (usize i = 0; i < old_entries.cap; i++) {
hashmap_entry_t *entry = &scc_vec_at(old_entries, i);
scc_hashtable_entry_t *entry = &scc_vec_at(old_entries, i);
if (entry->state == ENTRY_ACTIVE) {
hashmap_entry_t *dest = find_entry(ht, entry->key, entry->hash);
scc_hashtable_entry_t *dest =
find_entry(ht, entry->key, entry->hash);
*dest = *entry;
}
}
@@ -80,7 +83,7 @@ static void adjust_capacity(hashmap_t *ht, int new_cap) {
ht->tombstone_count = 0;
}
void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
int new_cap = ht->entries.cap < SCC_INIT_HASHMAP_SIZE
? SCC_INIT_HASHMAP_SIZE
@@ -89,7 +92,7 @@ void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
}
u32 hash = ht->hash_func(key);
hashmap_entry_t *entry = find_entry(ht, key, hash);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
void *old_value = NULL;
if (entry->state == ENTRY_ACTIVE) {
@@ -107,21 +110,21 @@ void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
return old_value;
}
void *hashmap_get(hashmap_t *ht, const void *key) {
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
u32 hash = ht->hash_func(key);
hashmap_entry_t *entry = find_entry(ht, key, hash);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : NULL;
}
void *hashmap_del(hashmap_t *ht, const void *key) {
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key) {
if (ht->entries.cap == 0)
return NULL;
u32 hash = ht->hash_func(key);
hashmap_entry_t *entry = find_entry(ht, key, hash);
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
if (entry == NULL || entry->state != ENTRY_ACTIVE)
return NULL;
@@ -133,15 +136,16 @@ void *hashmap_del(hashmap_t *ht, const void *key) {
return value;
}
void hashmap_drop(hashmap_t *ht) {
void scc_hashtable_drop(scc_hashtable_t *ht) {
scc_vec_free(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
}
void hashmap_foreach(hashmap_t *ht, hashmap_iter_fn iter_func, void *context) {
void scc_hashmap_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
void *context) {
for (usize i = 0; i < ht->entries.cap; i++) {
hashmap_entry_t *entry = &scc_vec_at(ht->entries, i);
scc_hashtable_entry_t *entry = &scc_vec_at(ht->entries, i);
if (entry->state == ENTRY_ACTIVE) {
if (!iter_func(entry->key, entry->value, context)) {
break; // enable callback function terminal the iter

View File

@@ -1,13 +1,13 @@
#include "strpool.h"
void init_strpool(strpool_t *pool) {
void scc_strpool_init(scc_strpool_t *pool) {
pool->ht.hash_func = (u32 (*)(const void *))scc_strhash32;
pool->ht.key_cmp = (int (*)(const void *, const void *))scc_strcmp;
hashmap_init(&pool->ht);
scc_hashtable_init(&pool->ht);
}
const char *strpool_intern(strpool_t *pool, const char *str) {
void *existing = hashmap_get(&pool->ht, str);
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {
void *existing = scc_hashtable_get(&pool->ht, str);
if (existing) {
return existing;
}
@@ -20,8 +20,13 @@ const char *strpool_intern(strpool_t *pool, const char *str) {
}
scc_memcpy(new_str, str, len);
hashmap_set(&pool->ht, new_str, new_str);
scc_hashtable_set(&pool->ht, new_str, new_str);
return new_str;
}
void strpool_destroy(strpool_t *pool) { hashmap_drop(&pool->ht); }
void scc_strpool_drop(scc_strpool_t *pool) { scc_hashtable_drop(&pool->ht); }
void scc_strpool_foreach(scc_strpool_t *pool, scc_strpool_iter_fn iter_func,
void *context) {
scc_hashmap_foreach(&pool->ht, (scc_hashtable_iter_fn)iter_func, context);
}