- 在 `core_mem.h` 中新增 `smcc_strlen` 函数,用于计算字符串长度 - 调整 `VEC` 宏定义参数,移除冗余的 name 参数,增强结构体声明一致性 - 修改 `cstring_from_cstr` 返回值字段顺序,保持代码风格统一 - 在 `libcore.h` 中增加日志相关宏定义的保护判断,防止重复定义冲突
28 lines
757 B
C
28 lines
757 B
C
#include "strpool.h"
|
|
|
|
void init_strpool(strpool_t *pool) {
|
|
pool->ht.hash_func = (u32 (*)(const void *))smcc_strhash32;
|
|
pool->ht.key_cmp = (int (*)(const void *, const void *))smcc_strcmp;
|
|
hashmap_init(&pool->ht);
|
|
}
|
|
|
|
const char *strpool_intern(strpool_t *pool, const char *str) {
|
|
void *existing = hashmap_get(&pool->ht, str);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
usize len = smcc_strlen(str) + 1;
|
|
char *new_str = smcc_malloc(len);
|
|
if (!new_str) {
|
|
LOG_ERROR("strpool: Failed to allocate memory for string");
|
|
return NULL;
|
|
}
|
|
smcc_memcpy(new_str, str, len);
|
|
|
|
hashmap_set(&pool->ht, new_str, new_str);
|
|
return new_str;
|
|
}
|
|
|
|
void strpool_destroy(strpool_t *pool) { hashmap_drop(&pool->ht); }
|