feat(core): 添加字符串长度计算函数并优化数据结构定义
- 在 `core_mem.h` 中新增 `smcc_strlen` 函数,用于计算字符串长度 - 调整 `VEC` 宏定义参数,移除冗余的 name 参数,增强结构体声明一致性 - 修改 `cstring_from_cstr` 返回值字段顺序,保持代码风格统一 - 在 `libcore.h` 中增加日志相关宏定义的保护判断,防止重复定义冲突
This commit is contained in:
@@ -17,6 +17,15 @@ static inline u32 smcc_strhash32(const char *s) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
static inline usize smcc_strlen(const char *str) {
|
||||
usize len = 0;
|
||||
while (*str) {
|
||||
len++;
|
||||
str++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int smcc_strcmp(const char *s1, const char *s2) {
|
||||
while (*s1 && *s2 && *s1 == *s2) {
|
||||
s1++;
|
||||
|
||||
@@ -36,7 +36,7 @@ static inline cstring_t cstring_from_cstr(const char *s) {
|
||||
smcc_memcpy(data, s, len);
|
||||
data[len] = '\0';
|
||||
|
||||
return (cstring_t){.data = data, .size = len + 1, .cap = len + 1};
|
||||
return (cstring_t){.size = len + 1, .cap = len + 1, .data = data};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,22 +17,24 @@
|
||||
/** @defgroup vec_struct 数据结构定义 */
|
||||
|
||||
/**
|
||||
* @def VEC(name, type)
|
||||
* @def VEC(type)
|
||||
* @brief 声明向量结构体
|
||||
* @param name 结构体变量名
|
||||
* @param type 存储的数据类型
|
||||
*
|
||||
* 生成包含size/cap/data三个字段的结构体定义:
|
||||
* - size: 当前元素数量
|
||||
* - cap: 数组容量
|
||||
* - data: 存储数组指针
|
||||
* @example
|
||||
* VEC(char) string; <=> char[dynamic_array] string;
|
||||
* struct people { VEC(char) name; int age; VEC(struct people) children; };
|
||||
*/
|
||||
#define VEC(name, type) \
|
||||
#define VEC(type) \
|
||||
struct { \
|
||||
usize size; \
|
||||
usize cap; \
|
||||
type *data; \
|
||||
} name
|
||||
}
|
||||
|
||||
/** @defgroup vec_operations 动态数组操作宏 */
|
||||
|
||||
|
||||
@@ -6,9 +6,15 @@
|
||||
#include <core_mem.h>
|
||||
|
||||
#define __SMCC_LOG_NO_STD_IMPL__
|
||||
#ifndef log_snprintf
|
||||
#define log_snprintf smcc_snprintf
|
||||
#endif
|
||||
#ifndef log_printf
|
||||
#define log_printf smcc_eprintf
|
||||
#endif
|
||||
#ifndef log_exit
|
||||
#define log_exit smcc_exit
|
||||
#endif
|
||||
#include <log.h>
|
||||
|
||||
#define _SMCC_STR(str) #str
|
||||
|
||||
1994
runtime/libcore/include/utest/acutest.h
Normal file
1994
runtime/libcore/include/utest/acutest.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,48 +1,48 @@
|
||||
/**
|
||||
* @file hashtable.h
|
||||
* @file hashmap.h
|
||||
* @brief 开放寻址法哈希表实现
|
||||
*
|
||||
* 提供基于向量容器的哈希表实现,支持动态扩容和墓碑机制
|
||||
*/
|
||||
|
||||
#ifndef __SMCC_HASHTABLE_H__
|
||||
#define __SMCC_HASHTABLE_H__
|
||||
#ifndef __SMCC_HASHMAP_H__
|
||||
#define __SMCC_HASHMAP_H__
|
||||
|
||||
#include <libcore.h>
|
||||
|
||||
/**
|
||||
* @enum ht_entry_state_t
|
||||
* @enum hp_entry_state_t
|
||||
* @brief 哈希表条目状态标识
|
||||
*/
|
||||
typedef enum hash_table_entry_state {
|
||||
typedef enum hashmap_entry_state {
|
||||
ENTRY_EMPTY, /**< 空槽位(从未使用过) */
|
||||
ENTRY_ACTIVE, /**< 有效条目(包含键值对) */
|
||||
ENTRY_TOMBSTONE /**< 墓碑标记(已删除条目) */
|
||||
} ht_entry_state_t;
|
||||
} hp_entry_state_t;
|
||||
|
||||
/**
|
||||
* @struct hash_entry_t
|
||||
* @struct hashmap_entry_t
|
||||
* @brief 哈希表条目结构
|
||||
*
|
||||
* @note key/value内存由调用者管理,哈希表不负责其生命周期
|
||||
*/
|
||||
typedef struct hash_entry {
|
||||
typedef struct hashmap_entry {
|
||||
const void *key; /**< 键指针(不可变) */
|
||||
void *value; /**< 值指针 */
|
||||
u32 hash; /**< 预计算的哈希值(避免重复计算) */
|
||||
ht_entry_state_t state; /**< 当前条目状态 */
|
||||
} hash_entry_t;
|
||||
hp_entry_state_t state; /**< 当前条目状态 */
|
||||
} hashmap_entry_t;
|
||||
|
||||
/**
|
||||
* @struct hash_table_t
|
||||
* @struct hashmap_t
|
||||
* @brief 哈希表主体结构
|
||||
*
|
||||
* 使用开放寻址法实现,采用墓碑标记处理删除操作
|
||||
*/
|
||||
typedef struct hash_table {
|
||||
VECTOR_HEADER(entries, hash_entry_t); /**< 条目存储容器 */
|
||||
u32 count; /**< 有效条目数量(不含墓碑) */
|
||||
u32 tombstone_count; /**< 墓碑条目数量 */
|
||||
typedef struct smcc_hashmap {
|
||||
VEC(hashmap_entry_t) entries; /**< 条目存储容器 */
|
||||
u32 count; /**< 有效条目数量(不含墓碑) */
|
||||
u32 tombstone_count; /**< 墓碑条目数量 */
|
||||
/**
|
||||
* @brief 哈希函数指针
|
||||
* @param key 键指针
|
||||
@@ -56,7 +56,7 @@ typedef struct hash_table {
|
||||
* @return 相同返回0,不同返回非0
|
||||
*/
|
||||
int (*key_cmp)(const void *key1, const void *key2);
|
||||
} hash_table_t;
|
||||
} hashmap_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化哈希表结构
|
||||
@@ -64,7 +64,7 @@ typedef struct hash_table {
|
||||
*
|
||||
* @warning 必须设置hash_func和key_cmp后才能使用
|
||||
*/
|
||||
void init_hashtable(hash_table_t *ht);
|
||||
void hashmap_init(hashmap_t *ht);
|
||||
|
||||
/**
|
||||
* @brief 插入/更新键值对
|
||||
@@ -73,7 +73,7 @@ void init_hashtable(hash_table_t *ht);
|
||||
* @param value 值指针
|
||||
* @return 被替换的旧值指针(无替换返回NULL)
|
||||
*/
|
||||
void *hashtable_set(hash_table_t *ht, const void *key, void *value);
|
||||
void *hashmap_set(hashmap_t *ht, const void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief 查找键对应值
|
||||
@@ -81,7 +81,7 @@ void *hashtable_set(hash_table_t *ht, const void *key, void *value);
|
||||
* @param key 查找键指针
|
||||
* @return 找到返回值指针,未找到返回NULL
|
||||
*/
|
||||
void *hashtable_get(hash_table_t *ht, const void *key);
|
||||
void *hashmap_get(hashmap_t *ht, const void *key);
|
||||
|
||||
/**
|
||||
* @brief 删除键值对
|
||||
@@ -91,7 +91,7 @@ void *hashtable_get(hash_table_t *ht, const void *key);
|
||||
*
|
||||
* @note 实际采用墓碑标记方式删除
|
||||
*/
|
||||
void *hashtable_del(hash_table_t *ht, const void *key);
|
||||
void *hashmap_del(hashmap_t *ht, const void *key);
|
||||
|
||||
/**
|
||||
* @brief 销毁哈希表
|
||||
@@ -99,18 +99,17 @@ void *hashtable_del(hash_table_t *ht, const void *key);
|
||||
*
|
||||
* @note 仅释放哈希表内部内存,不会释放key/value内存
|
||||
*/
|
||||
void hashtable_destory(hash_table_t *ht);
|
||||
void hashmap_drop(hashmap_t *ht);
|
||||
|
||||
/**
|
||||
* @typedef hash_table_iter_func
|
||||
* @typedef hashmap_iter_fn
|
||||
* @brief 哈希表迭代回调函数类型
|
||||
* @param key 当前键指针
|
||||
* @param value 当前值指针
|
||||
* @param context 用户上下文指针
|
||||
* @return 返回非0停止迭代
|
||||
*/
|
||||
typedef int (*hash_table_iter_func)(const void *key, void *value,
|
||||
void *context);
|
||||
typedef int (*hashmap_iter_fn)(const void *key, void *value, void *context);
|
||||
|
||||
/**
|
||||
* @brief 遍历哈希表所有有效条目
|
||||
@@ -118,7 +117,6 @@ typedef int (*hash_table_iter_func)(const void *key, void *value,
|
||||
* @param iter_func 迭代回调函数
|
||||
* @param context 用户上下文指针
|
||||
*/
|
||||
void hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
|
||||
void *context);
|
||||
void hashmap_foreach(hashmap_t *ht, hashmap_iter_fn iter_func, void *context);
|
||||
|
||||
#endif // __SMCC_HASHTABLE_H__
|
||||
#endif // __SMCC_HASHMAP_H__
|
||||
@@ -1,9 +1,8 @@
|
||||
#ifndef __SMCC_UTILS_H__
|
||||
#define __SMCC_UTILS_H__
|
||||
|
||||
#include "hashtable.h"
|
||||
#include "hashmap.h"
|
||||
#include "kllist.h"
|
||||
#include "string.h"
|
||||
#include "strpool.h"
|
||||
#include <libcore.h>
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@
|
||||
#ifndef __SMCC_STRPOOL_H__
|
||||
#define __SMCC_STRPOOL_H__
|
||||
|
||||
#include "hashtable.h"
|
||||
#include "string.h"
|
||||
#include "hashmap.h"
|
||||
#include <libcore.h>
|
||||
|
||||
/**
|
||||
@@ -19,7 +18,7 @@
|
||||
* 组合哈希表和专用内存分配器实现的高效字符串存储池
|
||||
*/
|
||||
typedef struct strpool {
|
||||
hash_table_t ht; /**< 哈希表用于快速查找已存储字符串 */
|
||||
hashmap_t ht; /**< 哈希表用于快速查找已存储字符串 */
|
||||
} strpool_t;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#include <hashtable.h>
|
||||
#include <hashmap.h>
|
||||
|
||||
#define INIT_HASH_TABLE_SIZE (32)
|
||||
#ifndef SMCC_INIT_HASHMAP_SIZE
|
||||
#define SMCC_INIT_HASHMAP_SIZE (32)
|
||||
#endif
|
||||
|
||||
void init_hashtable(hash_table_t *ht) {
|
||||
vector_init(ht->entries);
|
||||
void hashmap_init(hashmap_t *ht) {
|
||||
vec_init(ht->entries);
|
||||
ht->count = 0;
|
||||
ht->tombstone_count = 0;
|
||||
// Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
|
||||
Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
|
||||
}
|
||||
|
||||
static int next_power_of_two(int n) {
|
||||
@@ -19,17 +21,17 @@ static int next_power_of_two(int n) {
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
static hash_entry_t *find_entry(hash_table_t *ht, const void *key, u32 hash) {
|
||||
static hashmap_entry_t *find_entry(hashmap_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;
|
||||
|
||||
hash_entry_t *tombstone = NULL;
|
||||
hashmap_entry_t *tombstone = NULL;
|
||||
|
||||
while (1) {
|
||||
hash_entry_t *entry = &vector_at(ht->entries, index);
|
||||
hashmap_entry_t *entry = &vec_at(ht->entries, index);
|
||||
if (entry->state == ENTRY_EMPTY) {
|
||||
return tombstone ? tombstone : entry;
|
||||
}
|
||||
@@ -51,43 +53,43 @@ static hash_entry_t *find_entry(hash_table_t *ht, const void *key, u32 hash) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void adjust_capacity(hash_table_t *ht, int new_cap) {
|
||||
static void adjust_capacity(hashmap_t *ht, int new_cap) {
|
||||
new_cap = next_power_of_two(new_cap);
|
||||
Assert(new_cap >= ht->entries.cap);
|
||||
|
||||
VECTOR_HEADER(old_entries, hash_entry_t);
|
||||
VEC(hashmap_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 = smcc_realloc(NULL, new_cap * sizeof(hash_entry_t));
|
||||
smcc_memset(ht->entries.data, 0, new_cap * sizeof(hash_entry_t));
|
||||
ht->entries.data = smcc_realloc(NULL, new_cap * sizeof(hashmap_entry_t));
|
||||
smcc_memset(ht->entries.data, 0, new_cap * sizeof(hashmap_entry_t));
|
||||
|
||||
// rehash the all of the old data
|
||||
for (usize i = 0; i < old_entries.cap; i++) {
|
||||
hash_entry_t *entry = &vector_at(old_entries, i);
|
||||
hashmap_entry_t *entry = &vec_at(old_entries, i);
|
||||
if (entry->state == ENTRY_ACTIVE) {
|
||||
hash_entry_t *dest = find_entry(ht, entry->key, entry->hash);
|
||||
hashmap_entry_t *dest = find_entry(ht, entry->key, entry->hash);
|
||||
*dest = *entry;
|
||||
}
|
||||
}
|
||||
|
||||
vector_free(old_entries);
|
||||
vec_free(old_entries);
|
||||
ht->tombstone_count = 0;
|
||||
}
|
||||
|
||||
void *hashtable_set(hash_table_t *ht, const void *key, void *value) {
|
||||
void *hashmap_set(hashmap_t *ht, const void *key, void *value) {
|
||||
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
|
||||
int new_cap = ht->entries.cap < INIT_HASH_TABLE_SIZE
|
||||
? INIT_HASH_TABLE_SIZE
|
||||
int new_cap = ht->entries.cap < SMCC_INIT_HASHMAP_SIZE
|
||||
? SMCC_INIT_HASHMAP_SIZE
|
||||
: ht->entries.cap * 2;
|
||||
adjust_capacity(ht, new_cap);
|
||||
}
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hash_entry_t *entry = find_entry(ht, key, hash);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
void *old_value = NULL;
|
||||
if (entry->state == ENTRY_ACTIVE) {
|
||||
@@ -105,21 +107,21 @@ void *hashtable_set(hash_table_t *ht, const void *key, void *value) {
|
||||
return old_value;
|
||||
}
|
||||
|
||||
void *hashtable_get(hash_table_t *ht, const void *key) {
|
||||
void *hashmap_get(hashmap_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return NULL;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hash_entry_t *entry = find_entry(ht, key, hash);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : NULL;
|
||||
}
|
||||
|
||||
void *hashtable_del(hash_table_t *ht, const void *key) {
|
||||
void *hashmap_del(hashmap_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return NULL;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
hash_entry_t *entry = find_entry(ht, key, hash);
|
||||
hashmap_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
if (entry == NULL || entry->state != ENTRY_ACTIVE)
|
||||
return NULL;
|
||||
@@ -131,16 +133,15 @@ void *hashtable_del(hash_table_t *ht, const void *key) {
|
||||
return value;
|
||||
}
|
||||
|
||||
void hashtable_destory(hash_table_t *ht) {
|
||||
vector_free(ht->entries);
|
||||
void hashmap_drop(hashmap_t *ht) {
|
||||
vec_free(ht->entries);
|
||||
ht->count = 0;
|
||||
ht->tombstone_count = 0;
|
||||
}
|
||||
|
||||
void hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
|
||||
void *context) {
|
||||
void hashmap_foreach(hashmap_t *ht, hashmap_iter_fn iter_func, void *context) {
|
||||
for (usize i = 0; i < ht->entries.cap; i++) {
|
||||
hash_entry_t *entry = &vector_at(ht->entries, i);
|
||||
hashmap_entry_t *entry = &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
|
||||
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user