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,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 00
*/
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__

View File

@@ -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>

View File

@@ -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;
/**