feat(core): 添加字符串长度计算函数并优化数据结构定义

- 在 `core_mem.h` 中新增 `smcc_strlen` 函数,用于计算字符串长度
- 调整 `VEC` 宏定义参数,移除冗余的 name 参数,增强结构体声明一致性
- 修改 `cstring_from_cstr` 返回值字段顺序,保持代码风格统一
- 在 `libcore.h` 中增加日志相关宏定义的保护判断,防止重复定义冲突
This commit is contained in:
zzy
2025-11-20 22:26:49 +08:00
parent d1fafa830d
commit f29fd92fdf
10 changed files with 2082 additions and 94 deletions

View File

@@ -1,47 +1,27 @@
#include "strpool.h"
u32 rt_strhash(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
hash *= 16777619u;
}
return hash;
}
int rt_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
void init_strpool(strpool_t *pool) {
pool->ht.hash_func = (u32 (*)(const void *))rt_strhash;
pool->ht.key_cmp = (int (*)(const void *, const void *))rt_strcmp;
init_hashtable(&pool->ht);
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 = hashtable_get(&pool->ht, str);
void *existing = hashmap_get(&pool->ht, str);
if (existing) {
return existing;
}
rt_size_t len = rt_strlen(str) + 1;
char *new_str = lalloc_alloc(&pool->stralloc, len);
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;
}
rt_memcpy(new_str, str, len);
smcc_memcpy(new_str, str, len);
hashtable_set(&pool->ht, new_str, new_str);
hashmap_set(&pool->ht, new_str, new_str);
return new_str;
}
void strpool_destroy(strpool_t *pool) {
hashtable_destory(&pool->ht);
lalloc_destroy(&pool->stralloc);
}
void strpool_destroy(strpool_t *pool) { hashmap_drop(&pool->ht); }