format using clang-format to formate code

This commit is contained in:
zzy
2025-11-20 17:55:08 +08:00
parent 9762cf8a2b
commit d1fafa830d
27 changed files with 1047 additions and 766 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file hashtable.h
* @brief 开放寻址法哈希表实现
*
*
* 提供基于向量容器的哈希表实现,支持动态扩容和墓碑机制
*/
@@ -9,7 +9,6 @@
#define __SMCC_HASHTABLE_H__
#include <libcore.h>
#include "vector.h"
/**
* @enum ht_entry_state_t
@@ -24,48 +23,48 @@ typedef enum hash_table_entry_state {
/**
* @struct hash_entry_t
* @brief 哈希表条目结构
*
*
* @note key/value内存由调用者管理哈希表不负责其生命周期
*/
typedef struct hash_entry {
const void* key; /**< 键指针(不可变) */
void* value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
const void *key; /**< 键指针(不可变) */
void *value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
ht_entry_state_t state; /**< 当前条目状态 */
} hash_entry_t;
/**
* @struct hash_table_t
* @brief 哈希表主体结构
*
*
* 使用开放寻址法实现,采用墓碑标记处理删除操作
*/
typedef struct hash_table {
VECTOR_HEADER(entries, hash_entry_t); /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
VECTOR_HEADER(entries, hash_entry_t); /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
/**
* @brief 哈希函数指针
* @param key 键指针
* @return 32位无符号哈希值
*/
u32 (*hash_func)(const void* key);
u32 (*hash_func)(const void *key);
/**
* @brief 键比较函数指针
* @param key1 第一个键指针
* @param key2 第二个键指针
* @return 相同返回0不同返回非0
*/
int(*key_cmp)(const void* key1, const void* key2);
int (*key_cmp)(const void *key1, const void *key2);
} hash_table_t;
/**
* @brief 初始化哈希表结构
* @param ht 哈希表实例指针
*
*
* @warning 必须设置hash_func和key_cmp后才能使用
*/
void init_hashtable(hash_table_t* ht);
void init_hashtable(hash_table_t *ht);
/**
* @brief 插入/更新键值对
@@ -74,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 *hashtable_set(hash_table_t *ht, const void *key, void *value);
/**
* @brief 查找键对应值
@@ -82,25 +81,25 @@ 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 *hashtable_get(hash_table_t *ht, const void *key);
/**
* @brief 删除键值对
* @param ht 哈希表实例指针
* @param key 要删除的键指针
* @return 被删除的值指针不存在返回NULL
*
*
* @note 实际采用墓碑标记方式删除
*/
void* hashtable_del(hash_table_t* ht, const void* key);
void *hashtable_del(hash_table_t *ht, const void *key);
/**
* @brief 销毁哈希表
* @param ht 哈希表实例指针
*
*
* @note 仅释放哈希表内部内存不会释放key/value内存
*/
void hashtable_destory(hash_table_t* ht);
void hashtable_destory(hash_table_t *ht);
/**
* @typedef hash_table_iter_func
@@ -110,7 +109,8 @@ void hashtable_destory(hash_table_t* ht);
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*hash_table_iter_func)(const void* key, void* value, void* context);
typedef int (*hash_table_iter_func)(const void *key, void *value,
void *context);
/**
* @brief 遍历哈希表所有有效条目
@@ -118,6 +118,7 @@ typedef int (*hash_table_iter_func)(const void* key, void* value, void* context)
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context);
void hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
void *context);
#endif // __SMCC_HASHTABLE_H__

View File

@@ -3,7 +3,8 @@
* @link https://njusecourse.feishu.cn/wiki/I8vkw2zkwiEInUkujTJc7zzOnwf
* @link https://kernelnewlbies.org/FAQ/LinkedLists
* @link https://lwn.net/Articles/887097/
* @link https://liuluheng.github.io/wiki/public_html/Embedded-System/kernel/list-and-hlist.html
* @link
* https://liuluheng.github.io/wiki/public_html/Embedded-System/kernel/list-and-hlist.html
*/
#ifndef __KLLIST_H__
@@ -18,13 +19,17 @@
// Magic: https://radek.io/posts/magical-container_of-macro/
// StackOverflow: https://stackoverflow.com/q/15832301/1833118
#ifdef __GNUC__
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define container_of(ptr, type, member) \
({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#else
#define container_of(ptr, type, member) ({ \
const void *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define container_of(ptr, type, member) \
({ \
const void *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); \
})
#endif
#endif
@@ -37,26 +42,25 @@ struct list_head {
/**
* list init
* @example
* @example
* 1. struct list_head your_list = LIST_HEAD_INIT(your_list);
* 2. struct list_head your_list; INIT_LIST_HEAD(&your_list);
* 3. LIST_HEAD(your_list); => struct your_list = { &(your_list), &(your_list) };
* 3. LIST_HEAD(your_list); => struct your_list = { &(your_list), &(your_list)
* };
*/
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD_INIT(name) {&(name), &(name)}
static inline void INIT_LIST_HEAD(struct list_head *list) {
list->next = list;
list->prev = list;
}
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
/**
* list add
*/
static inline void __list_add(struct list_head *newl,
struct list_head *prev,
static inline void __list_add(struct list_head *newl, struct list_head *prev,
struct list_head *next) {
next->prev = newl;
newl->next = next;
@@ -68,7 +72,8 @@ static inline void list_add(struct list_head *newl, struct list_head *head) {
__list_add(newl, head, head->next);
}
static inline void list_add_tail(struct list_head *newl, struct list_head *head) {
static inline void list_add_tail(struct list_head *newl,
struct list_head *head) {
__list_add(newl, head->prev, head);
}
@@ -76,7 +81,7 @@ static inline void list_add_tail(struct list_head *newl, struct list_head *head)
* list delete
*/
static inline void __list_del(struct list_head * prev, struct list_head * next) {
static inline void __list_del(struct list_head *prev, struct list_head *next) {
next->prev = prev;
prev->next = next;
}
@@ -84,7 +89,7 @@ static inline void __list_del(struct list_head * prev, struct list_head * next)
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
entry->prev = NULL;
}
/**
@@ -92,8 +97,9 @@ static inline void list_del(struct list_head *entry) {
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_first(const struct list_head *list, const struct list_head *head) {
return list->prev == head;
static inline int list_is_first(const struct list_head *list,
const struct list_head *head) {
return list->prev == head;
}
/**
@@ -101,8 +107,9 @@ static inline int list_is_first(const struct list_head *list, const struct list_
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list, const struct list_head *head) {
return list->next == head;
static inline int list_is_last(const struct list_head *list,
const struct list_head *head) {
return list->next == head;
}
/**
@@ -110,8 +117,9 @@ static inline int list_is_last(const struct list_head *list, const struct list_h
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_head(const struct list_head *list, const struct list_head *head) {
return list == head;
static inline int list_is_head(const struct list_head *list,
const struct list_head *head) {
return list == head;
}
/**
@@ -119,7 +127,7 @@ static inline int list_is_head(const struct list_head *list, const struct list_h
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head) {
return head->next == head;
return head->next == head;
}
/**
@@ -127,16 +135,16 @@ static inline int list_empty(const struct list_head *head) {
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
#define list_for_each(pos, head) \
for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
/**
* list sort
@@ -146,8 +154,8 @@ static inline int list_empty(const struct list_head *head) {
*/
#ifdef HAVE_KLIST_SORT
typedef int (*list_cmp_func_t)(void *,
const struct list_head *, const struct list_head *);
typedef int (*list_cmp_func_t)(void *, const struct list_head *,
const struct list_head *);
static void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
#endif

View File

@@ -1,11 +1,10 @@
#ifndef __SMCC_UTILS_H__
#define __SMCC_UTILS_H__
#define __SMCC_UTILS_H__
#include <libcore.h>
#include "vector.h"
#include "kllist.h"
#include "hashtable.h"
#include "kllist.h"
#include "string.h"
#include "strpool.h"
#include <libcore.h>
#endif // __SMCC_UTILS_H__

View File

@@ -1,51 +1,51 @@
/**
* @file strpool.h
* @brief 字符串池实现
*
*
* 提供字符串驻留String Interning功能保证相同字符串的唯一性存储
*/
#ifndef __SMCC_STRPOOL_H__
#define __SMCC_STRPOOL_H__
#include <libcore.h>
#include "hashtable.h"
#include "string.h"
#include <libcore.h>
/**
* @struct strpool_t
* @brief 字符串池上下文
*
*
* 组合哈希表和专用内存分配器实现的高效字符串存储池
*/
typedef struct strpool {
hash_table_t ht; /**< 哈希表用于快速查找已存储字符串 */
hash_table_t ht; /**< 哈希表用于快速查找已存储字符串 */
} strpool_t;
/**
* @brief 初始化字符串池
* @param pool 字符串池实例指针
*/
void init_strpool(strpool_t* pool);
void init_strpool(strpool_t *pool);
/**
* @brief 驻留字符串到池中
* @param pool 字符串池实例指针
* @param str 要驻留的 C 字符串
* @return 池中唯一字符串的持久指针
*
*
* @note 返回值生命周期与字符串池一致
* @note 重复插入相同字符串会返回已有指针
*/
const char* strpool_intern(strpool_t* pool, const char* str);
const char *strpool_intern(strpool_t *pool, const char *str);
/**
* @brief 销毁字符串池
* @param pool 字符串池实例指针
*
*
* @warning 销毁后已获取的字符串指针将失效
* @note 会自动释放所有驻留字符串内存
*/
void strpool_destroy(strpool_t* pool);
void strpool_destroy(strpool_t *pool);
#endif // __SMCC_STRPOOL_H__

View File

@@ -1,119 +0,0 @@
/**
* @file vector.h
* @brief 动态数组Vector实现
*
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
*/
#ifndef __SMCC_DS_VECTOR_H__
#define __SMCC_DS_VECTOR_H__
#include <libcore.h>
#define __vec_realloc smcc_realloc
#define __vec_free smcc_free
/** @defgroup vector_struct 数据结构定义 */
/**
* @def VECTOR_HEADER(name, type)
* @brief 声明向量结构体
* @param name 结构体变量名
* @param type 存储的数据类型
*
* 生成包含size/cap/data三个字段的结构体定义
* - size: 当前元素数量
* - cap: 数组容量
* - data: 存储数组指针
*/
#define VECTOR_HEADER(name, type) \
struct { \
isize size; /**< 当前元素数量 */ \
isize cap; /**< 数组容量 */ \
type *data; /**< 数据存储指针 */ \
} name
/** @defgroup vector_operations 向量操作宏 */
/**
* @def vector_init(vec)
* @brief 初始化向量结构体
* @param vec 要初始化的向量结构体变量
*
* @note 此宏不会分配内存,仅做零初始化
*/
#define vector_init(vec) \
do { \
(vec).size = 0, \
(vec).cap = 0, \
(vec).data = 0; \
} while(0)
/**
* @def vector_push(vec, value)
* @brief 添加元素到向量末尾
* @param vec 目标向量结构体
* @param value 要添加的值(需匹配存储类型)
*
* @note 当容量不足时自动扩容为2倍初始容量为8
* @warning 内存分配失败时会触发LOG_FATAL
*/
#define vector_push(vec, value) \
do { \
if (vec.size >= vec.cap) { \
int cap = vec.cap ? vec.cap * 2 : 8; \
void* data = __vec_realloc(vec.data, cap * sizeof(*vec.data)); \
if (!data) { \
LOG_FATAL("vector_push: rt_realloc failed\n"); \
} \
(vec).cap = cap; \
(vec).data = data; \
} \
(vec).data[(vec).size++] = value; \
} while(0)
/**
* @def vector_pop(vec)
* @brief 弹出最后一个元素
* @param vec 目标向量结构体
* @return 最后元素的引用
* @warning 需确保size > 0时使用
*/
#define vector_pop(vec) \
((vec).data[--(vec).size])
/**
* @def vector_at(vec, idx)
* @brief 获取指定索引元素
* @param vec 目标向量结构体
* @param idx 元素索引0 <= idx < size
* @return 对应元素的引用
*/
#define vector_at(vec, idx) \
(((vec).data)[idx])
/**
* @def vector_idx(vec, ptr)
* @brief 获取元素指针对应的索引
* @param vec 目标向量结构体
* @param ptr 元素指针需在data数组范围内
* @return 元素索引值
*/
#define vector_idx(vec, ptr) \
((ptr) - (vec).data)
/**
* @def vector_free(vec)
* @brief 释放向量内存
* @param vec 目标向量结构体
*
* @note 释放后需重新初始化才能再次使用
*/
#define vector_free(vec) \
do { \
__vec_free((vec).data); \
(vec).data = NULL; \
(vec).size = (vec).cap = 0; \
} while(0)
#endif // __SMCC_DS_VECTOR_H__

View File

@@ -2,7 +2,7 @@
#define INIT_HASH_TABLE_SIZE (32)
void init_hashtable(hash_table_t* ht) {
void init_hashtable(hash_table_t *ht) {
vector_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
@@ -19,36 +19,39 @@ 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) {
if (ht->entries.cap == 0) return NULL;
static hash_entry_t *find_entry(hash_table_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;
hash_entry_t *tombstone = NULL;
while (1) {
hash_entry_t* entry = &vector_at(ht->entries, index);
hash_entry_t *entry = &vector_at(ht->entries, index);
if (entry->state == ENTRY_EMPTY) {
return tombstone ? tombstone : entry;
}
if (entry->state == ENTRY_TOMBSTONE) {
if (!tombstone) tombstone = entry;
if (!tombstone)
tombstone = entry;
} else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) {
return entry;
}
// Liner finding
index = (index + 1) & (ht->entries.cap - 1);
probe++;
if (probe >= ht->entries.cap) break;
if (probe >= ht->entries.cap)
break;
}
LOG_ERROR("hashset_find: hash table is full");
return NULL;
}
static void adjust_capacity(hash_table_t* ht, int new_cap) {
static void adjust_capacity(hash_table_t *ht, int new_cap) {
new_cap = next_power_of_two(new_cap);
Assert(new_cap >= ht->entries.cap);
@@ -64,9 +67,9 @@ static void adjust_capacity(hash_table_t* ht, int new_cap) {
// 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);
hash_entry_t *entry = &vector_at(old_entries, i);
if (entry->state == ENTRY_ACTIVE) {
hash_entry_t* dest = find_entry(ht, entry->key, entry->hash);
hash_entry_t *dest = find_entry(ht, entry->key, entry->hash);
*dest = *entry;
}
}
@@ -75,23 +78,26 @@ static void adjust_capacity(hash_table_t* ht, int new_cap) {
ht->tombstone_count = 0;
}
void* hashtable_set(hash_table_t* ht, const void* key, void* value) {
void *hashtable_set(hash_table_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 : ht->entries.cap * 2;
int new_cap = ht->entries.cap < INIT_HASH_TABLE_SIZE
? INIT_HASH_TABLE_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);
void* old_value = NULL;
hash_entry_t *entry = find_entry(ht, key, hash);
void *old_value = NULL;
if (entry->state == ENTRY_ACTIVE) {
old_value = entry->value;
} else {
if (entry->state == ENTRY_TOMBSTONE) ht->tombstone_count--;
if (entry->state == ENTRY_TOMBSTONE)
ht->tombstone_count--;
ht->count++;
}
entry->key = key;
entry->value = value;
entry->hash = hash;
@@ -99,38 +105,42 @@ 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) {
if (ht->entries.cap == 0) return NULL;
void *hashtable_get(hash_table_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);
hash_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) {
if (ht->entries.cap == 0) return NULL;
void *hashtable_del(hash_table_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);
if (entry == NULL || entry->state != ENTRY_ACTIVE) return NULL;
void* value = entry->value;
hash_entry_t *entry = find_entry(ht, key, hash);
if (entry == NULL || entry->state != ENTRY_ACTIVE)
return NULL;
void *value = entry->value;
entry->state = ENTRY_TOMBSTONE;
ht->count--;
ht->tombstone_count++;
return value;
}
void hashtable_destory(hash_table_t* ht) {
void hashtable_destory(hash_table_t *ht) {
vector_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 hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
void *context) {
for (usize i = 0; i < ht->entries.cap; i++) {
hash_entry_t* entry = &vector_at(ht->entries, i);
hash_entry_t *entry = &vector_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,6 +1,6 @@
#include "strpool.h"
u32 rt_strhash(const char* s) {
u32 rt_strhash(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
@@ -9,7 +9,7 @@ u32 rt_strhash(const char* s) {
return hash;
}
int rt_strcmp(const char* s1, const char* s2) {
int rt_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
@@ -17,20 +17,20 @@ int rt_strcmp(const char* s1, const char* 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;
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);
}
const char* strpool_intern(strpool_t* pool, const char* str) {
void* existing = hashtable_get(&pool->ht, str);
const char *strpool_intern(strpool_t *pool, const char *str) {
void *existing = hashtable_get(&pool->ht, str);
if (existing) {
return existing;
}
rt_size_t len = rt_strlen(str) + 1;
char* new_str = lalloc_alloc(&pool->stralloc, len);
char *new_str = lalloc_alloc(&pool->stralloc, len);
if (!new_str) {
LOG_ERROR("strpool: Failed to allocate memory for string");
return NULL;
@@ -41,7 +41,7 @@ const char* strpool_intern(strpool_t* pool, const char* str) {
return new_str;
}
void strpool_destroy(strpool_t* pool) {
void strpool_destroy(strpool_t *pool) {
hashtable_destory(&pool->ht);
lalloc_destroy(&pool->stralloc);
}