feat 新的运行时环境

This commit is contained in:
zzy
2025-11-20 11:22:37 +08:00
parent e22811f2f5
commit 5c24f35c87
23 changed files with 1755 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
[package]
name = "libcore"
default_features = [
"std_impl",
]
features = [
"std_impl",
]
dependencies = [
# TODO define some to disable stdio for self-contained build
{ name = "log", path = "../log" }
]

View File

@@ -0,0 +1,47 @@
#ifndef __SMCC_CORE_IMPL_H__
#define __SMCC_CORE_IMPL_H__
#include "core_type.h"
/* ====== 内存管理核心接口 ====== */
void* smcc_malloc(usize size);
void* smcc_calloc(usize count, usize size);
void* smcc_realloc(void *ptr, usize new_size);
void smcc_free(void *ptr);
/* ====== 文件系统核心接口 ====== */
/* 文件句柄 - 不透明指针 */
typedef struct smcc_file* smcc_file_t;
/* 文件打开模式 - 只保留编译器真正需要的 */
typedef enum {
SMCC_FILE_READ, /* 读取源文件、头文件 */
SMCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SMCC_FILE_APPEND /* 日志、调试输出 */
} smcc_file_mode_t;
/* 核心文件操作 */
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode);
void smcc_fclose(smcc_file_t file);
usize smcc_fread(smcc_file_t file, void *buffer, usize size);
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size);
cbool smcc_fexists(const char *path);
/* ====== 输入输出核心接口 ====== */
void smcc_snprintf(char *buf, usize size, const char *format, ...);
/* 标准输出 - 用于编译进度、结果 */
void smcc_printf(const char *format, ...);
/* 错误输出 - 用于错误信息、警告 */
void smcc_eprintf(const char *format, ...);
/* ====== 系统核心接口 ====== */
/* 程序控制 */
void smcc_exit(int code);
#endif // __SMCC_CORE_IMPL_H__

View File

@@ -0,0 +1,9 @@
#ifndef __SMCC_CORE_MACRO_H__
#define __SMCC_CORE_MACRO_H__
#define _SMCC_STR(str) #str
#define SMCC_STR(str) _SMCC_STR(str)
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#endif // __SMCC_CORE_MACRO_H__

View File

@@ -0,0 +1,28 @@
#ifndef __SMCC_CORE_MEM_H__
#define __SMCC_CORE_MEM_H__
#include "core_type.h"
void* smcc_memcpy(void *dest, const void *src, usize n);
void* smcc_memmove(void *dest, const void *src, usize n);
void* smcc_memset(void *s, int c, usize n);
int smcc_memcmp(const void *s1, const void *s2, usize n);
static inline u32 smcc_strhash32(const char* s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
hash *= 16777619u;
}
return hash;
}
static inline int smcc_strcmp(const char* s1, const char* s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
#endif /* __SMCC_CORE_MEM_H__ */

View File

@@ -0,0 +1,142 @@
#ifndef __CORE_STR_H__
#define __CORE_STR_H__
#include "core_type.h"
#include "core_impl.h"
#include "log.h"
typedef struct cstring {
char* data;
usize len;
usize cap;
} cstring_t;
/**
* 创建一个新的空字符串
*/
static inline cstring_t cstring_new(void) {
return (cstring_t) { .data = null, .len = 0, .cap = 0 };
}
/**
* 使用指定容量创建字符串
*/
static inline cstring_t cstring_with_capacity(usize capacity) {
char* data = null;
if (capacity > 0) {
data = (char*)smcc_malloc(capacity);
Assert(data != null);
}
return (cstring_t) { .data = data, .len = 0, .cap = capacity };
}
/**
* 从 C 字符串创建 Rust 风格字符串
*/
static inline cstring_t cstring_from_cstr(const char* s) {
if (s == null) {
return cstring_new();
}
usize len = 0;
const char* p = s;
while (*p++) len++;
char* data = (char*)smcc_malloc(len + 1);
Assert(data != null);
smcc_memcpy(data, s, len);
data[len] = '\0';
return (cstring_t) { .data = data, .len = len, .cap = len };
}
/**
* 释放字符串资源
*/
static inline void cstring_free(cstring_t* str) {
if (str && str->data) {
smcc_free(str->data);
str->data = null;
str->len = 0;
str->cap = 0;
}
}
/**
* 向字符串追加内容
*/
static inline void cstring_push_str(cstring_t* str, const char* data, usize len) {
if (str == null || data == null || len == 0) {
return;
}
// 如果需要扩容
if (str->len + len + 1 > str->cap) {
// FIXME c string 兼容性问题 bad practice a lot of `+ 1`
usize new_cap = str->cap == 0 ? len + 1 : str->cap;
while (new_cap < str->len + len + 1) {
new_cap *= 2;
if (new_cap == 0) { // 处理溢出情况
new_cap = str->len + len + 1;
break;
}
}
char* new_data = str->data ?
(char*)smcc_realloc(str->data, new_cap) :
(char*)smcc_malloc(new_cap);
Assert(new_data != null);
str->data = new_data;
str->cap = new_cap;
}
smcc_memcpy(str->data + str->len, data, len);
str->len += len;
str->data[str->len] = '\0'; // 保证 C 字符串兼容性
}
/**
* 向字符串追加单个字符
*/
static inline void cstring_push(cstring_t* str, char ch) {
cstring_push_str(str, &ch, 1);
}
/**
* 获取字符串长度
*/
static inline usize cstring_len(const cstring_t* str) {
return str ? str->len : 0;
}
/**
* 检查字符串是否为空
*/
static inline cbool cstring_is_empty(const cstring_t* str) {
return str == null || str->len == 0;
}
/**
* 清空字符串内容但保留分配的内存
*/
static inline void cstring_clear(cstring_t* str) {
if (str) {
str->len = 0;
if (str->data) {
str->data[0] = '\0';
}
}
}
/**
* 获取 C 风格字符串
*/
static inline const char* cstring_as_cstr(const cstring_t* str) {
if (str == null || str->data == null) {
return "";
}
return str->data;
}
#endif /* __CORE_STR_H__ */

View File

@@ -0,0 +1,71 @@
#ifndef __SMCC_CORE_TYPE_H__
#define __SMCC_CORE_TYPE_H__
#ifndef __SMCC_BUILTIN_TYPE__
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef intptr_t isize;
typedef uintptr_t usize;
typedef ptrdiff_t pdiff;
typedef float f32;
typedef double f64;
typedef bool cbool;
/// void / null
#define null NULL
static_assert(sizeof(cbool) == 1, "cbool size must 1");
#else
#define __smcc_i8
#define __smcc_i16
#define __smcc_i32
#define __smcc_i64
#define __smcc_u8
#define __smcc_u16
#define __smcc_u32
#define __smcc_u64
#define __smcc_f32
#define __smcc_f64
#define __smcc_bool
#define __smcc_char
#define __smcc_void
#define __smcc_null
#define __smcc_isize
#define __smcc_usize
#endif
typedef union core_cvalue {
long double ld;
double d;
float f;
unsigned long long ull;
char ch;
/* number value */
uint64_t n;
/* string value */
struct {
char* data;
uintptr_t len;
} cstr;
/* 16 byte == 128 bit */
char val[16];
} core_cvalue_t;
#endif

View File

@@ -0,0 +1,20 @@
#ifndef __SMCC_CORE_H__
#define __SMCC_CORE_H__
#include <core_mem.h>
#include <core_impl.h>
#include <core_macro.h>
#define __SMCC_LOG_NO_STD_IMPL__
#define log_snprintf smcc_snprintf
#define log_printf smcc_eprintf
#define log_exit smcc_exit
#include <log.h>
#define _SMCC_STR(str) #str
#define SMCC_STR(str) _SMCC_STR(str)
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#include <core_str.h>
#endif // __SMCC_CORE_H__

View File

@@ -0,0 +1,95 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <core_impl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/* ====== 内存管理核心接口实现 ====== */
void* smcc_malloc(usize size) {
return malloc(size);
}
void* smcc_calloc(usize count, usize size) {
return calloc(count, size);
}
void* smcc_realloc(void *ptr, usize new_size) {
return realloc(ptr, new_size);
}
void smcc_free(void *ptr) {
free(ptr);
}
/* ====== 文件系统核心接口实现 ====== */
static const char* get_file_mode_string(smcc_file_mode_t mode) {
switch (mode) {
case SMCC_FILE_READ: return "rb";
case SMCC_FILE_WRITE: return "wb";
case SMCC_FILE_APPEND: return "ab";
default: return "rb";
}
}
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode) {
const char* mode_str = get_file_mode_string(mode);
return (smcc_file_t)fopen(path, mode_str);
}
void smcc_fclose(smcc_file_t file) {
if (file) {
fclose((FILE*)file);
}
}
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
if (!file || !buffer) return 0;
return fread(buffer, 1, size, (FILE*)file);
}
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size) {
if (!file || !buffer) return 0;
return fwrite(buffer, 1, size, (FILE*)file);
}
cbool smcc_fexists(const char *path) {
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
if (!fp) return false;
smcc_fclose(fp);
return true;
}
/* ====== 输入输出核心接口实现 ====== */
void smcc_snprintf(char *buf, usize size, const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(buf, size, format, args); // NOLINT
va_end(args);
}
void smcc_printf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void smcc_eprintf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
/* ====== 系统核心接口实现 ====== */
void smcc_exit(int code) {
exit(code);
}

View File

@@ -0,0 +1,224 @@
#include <core_mem.h>
#include <stdint.h>
// 判断是否支持非对齐访问x86/x64 支持)
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
#define UNALIGNED_ACCESS_ALLOWED 1
#else
#define UNALIGNED_ACCESS_ALLOWED 0
#endif
void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
char* d = (char*)dest;
const char* s = (const char*)src;
// 快速路径:小内存拷贝
if (n <= 16) {
switch(n) {
case 16: d[15] = s[15]; /* fall through */
case 15: d[14] = s[14]; /* fall through */
case 14: d[13] = s[13]; /* fall through */
case 13: d[12] = s[12]; /* fall through */
case 12: d[11] = s[11]; /* fall through */
case 11: d[10] = s[10]; /* fall through */
case 10: d[9] = s[9]; /* fall through */
case 9: d[8] = s[8]; /* fall through */
case 8: d[7] = s[7]; /* fall through */
case 7: d[6] = s[6]; /* fall through */
case 6: d[5] = s[5]; /* fall through */
case 5: d[4] = s[4]; /* fall through */
case 4: d[3] = s[3]; /* fall through */
case 3: d[2] = s[2]; /* fall through */
case 2: d[1] = s[1]; /* fall through */
case 1: d[0] = s[0]; /* fall through */
default: break;
}
return dest;
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量复制适用于支持非对齐访问的平台
uint64_t* d64 = (uint64_t*)d;
const uint64_t* s64 = (const uint64_t*)s;
while (n >= 8) {
*d64++ = *s64++;
n -= 8;
}
d = (char*)d64;
s = (const char*)s64;
#endif
// 处理剩余字节
while (n--) {
*d++ = *s++;
}
return dest;
}
void *smcc_memmove(void *dest, const void *src, usize n)
{
char* d = (char*)dest;
const char* s = (const char*)src;
// 地址相同直接返回
if (d == s) {
return dest;
}
// 内存区域无重叠或前向拷贝
if (d < s || d >= s + n) {
return smcc_memcpy(d, s, n);
} else {
// 后向拷贝处理重叠情况
d += n;
s += n;
while (n--) {
*(--d) = *(--s);
}
}
return dest;
}
void* smcc_memset(void *s, int c, usize n) {
unsigned char* p = (unsigned char*)s;
unsigned char byte_val = (unsigned char)c;
// 快速设置小块内存
if (n <= 16) {
switch(n) {
case 16: p[15] = byte_val; /* fall through */
case 15: p[14] = byte_val; /* fall through */
case 14: p[13] = byte_val; /* fall through */
case 13: p[12] = byte_val; /* fall through */
case 12: p[11] = byte_val; /* fall through */
case 11: p[10] = byte_val; /* fall through */
case 10: p[9] = byte_val; /* fall through */
case 9: p[8] = byte_val; /* fall through */
case 8: p[7] = byte_val; /* fall through */
case 7: p[6] = byte_val; /* fall through */
case 6: p[5] = byte_val; /* fall through */
case 5: p[4] = byte_val; /* fall through */
case 4: p[3] = byte_val; /* fall through */
case 3: p[2] = byte_val; /* fall through */
case 2: p[1] = byte_val; /* fall through */
case 1: p[0] = byte_val; /* fall through */
default: break;
}
return s;
}
#if UNALIGNED_ACCESS_ALLOWED
// 构造一个8字节值用于批量填充
uint64_t fill_val = ((uint64_t)byte_val << 56) |
((uint64_t)byte_val << 48) |
((uint64_t)byte_val << 40) |
((uint64_t)byte_val << 32) |
((uint64_t)byte_val << 24) |
((uint64_t)byte_val << 16) |
((uint64_t)byte_val << 8) |
(uint64_t)byte_val;
uint64_t* p64 = (uint64_t*)p;
while (n >= 8) {
*p64++ = fill_val;
n -= 8;
}
p = (unsigned char*)p64;
#endif
// 设置剩余字节
while (n--) {
*p++ = byte_val;
}
return s;
}
int smcc_memcmp(const void *s1, const void *s2, usize n) {
const unsigned char* p1 = (const unsigned char*)s1;
const unsigned char* p2 = (const unsigned char*)s2;
// 快速比较小块内存
if (n <= 16) {
unsigned char diff = 0;
switch(n) {
case 16: diff |= (p1[15] ^ p2[15]); /* fall through */
case 15: diff |= (p1[14] ^ p2[14]); /* fall through */
case 14: diff |= (p1[13] ^ p2[13]); /* fall through */
case 13: diff |= (p1[12] ^ p2[12]); /* fall through */
case 12: diff |= (p1[11] ^ p2[11]); /* fall through */
case 11: diff |= (p1[10] ^ p2[10]); /* fall through */
case 10: diff |= (p1[9] ^ p2[9]); /* fall through */
case 9: diff |= (p1[8] ^ p2[8]); /* fall through */
case 8: diff |= (p1[7] ^ p2[7]); /* fall through */
case 7: diff |= (p1[6] ^ p2[6]); /* fall through */
case 6: diff |= (p1[5] ^ p2[5]); /* fall through */
case 5: diff |= (p1[4] ^ p2[4]); /* fall through */
case 4: diff |= (p1[3] ^ p2[3]); /* fall through */
case 3: diff |= (p1[2] ^ p2[2]); /* fall through */
case 2: diff |= (p1[1] ^ p2[1]); /* fall through */
case 1: diff |= (p1[0] ^ p2[0]); /* fall through */
default: break;
}
// 只有当所有字节都相等时diff才为0
if (!diff) return 0;
// 找到第一个不同的字节并返回差值
size_t i = 0;
switch(n) {
case 16: if(p1[15] != p2[15]) {i=15;break;}
case 15: if(p1[14] != p2[14]) {i=14;break;}
case 14: if(p1[13] != p2[13]) {i=13;break;}
case 13: if(p1[12] != p2[12]) {i=12;break;}
case 12: if(p1[11] != p2[11]) {i=11;break;}
case 11: if(p1[10] != p2[10]) {i=10;break;}
case 10: if(p1[9] != p2[9]) {i=9;break;}
case 9: if(p1[8] != p2[8]) {i=8;break;}
case 8: if(p1[7] != p2[7]) {i=7;break;}
case 7: if(p1[6] != p2[6]) {i=6;break;}
case 6: if(p1[5] != p2[5]) {i=5;break;}
case 5: if(p1[4] != p2[4]) {i=4;break;}
case 4: if(p1[3] != p2[3]) {i=3;break;}
case 3: if(p1[2] != p2[2]) {i=2;break;}
case 2: if(p1[1] != p2[1]) {i=1;break;}
case 1: if(p1[0] != p2[0]) {i=0;break;}
default: break;
}
return p1[i] - p2[i];
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量比较
const uint64_t* p1_64 = (const uint64_t*)p1;
const uint64_t* p2_64 = (const uint64_t*)p2;
while (n >= 8) {
if (*p1_64 != *p2_64) {
// 发现不同在8字节内定位具体位置
const unsigned char* b1 = (const unsigned char*)p1_64;
const unsigned char* b2 = (const unsigned char*)p2_64;
for (int j = 0; j < 8; j++) {
if (b1[j] != b2[j])
return b1[j] - b2[j];
}
}
p1_64++;
p2_64++;
n -= 8;
}
p1 = (const unsigned char*)p1_64;
p2 = (const unsigned char*)p2_64;
#endif
// 比较剩余字节
while (n--) {
if (*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}

View File

@@ -0,0 +1,13 @@
#include <libcore.h>
#include <stdio.h>
int main(void) {
printf("test log...\n");
Assert(1 == 1);
LOG_TRACE("log trace");
LOG_NOTSET("log notset");
LOG_DEBUG("log debug");
LOG_INFO("log info");
LOG_WARN("log warn");
LOG_ERROR("log error");
}

View File

@@ -0,0 +1,7 @@
[package]
name = "libutils"
version = "0.1.0"
dependencies = [
{ name = "core", path = "../libcore" }
]

View File

@@ -0,0 +1,123 @@
/**
* @file hashtable.h
* @brief 开放寻址法哈希表实现
*
* 提供基于向量容器的哈希表实现,支持动态扩容和墓碑机制
*/
#ifndef __SMCC_HASHTABLE_H__
#define __SMCC_HASHTABLE_H__
#include <libcore.h>
#include "vector.h"
/**
* @enum ht_entry_state_t
* @brief 哈希表条目状态标识
*/
typedef enum hash_table_entry_state {
ENTRY_EMPTY, /**< 空槽位(从未使用过) */
ENTRY_ACTIVE, /**< 有效条目(包含键值对) */
ENTRY_TOMBSTONE /**< 墓碑标记(已删除条目) */
} ht_entry_state_t;
/**
* @struct hash_entry_t
* @brief 哈希表条目结构
*
* @note key/value内存由调用者管理哈希表不负责其生命周期
*/
typedef struct hash_entry {
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; /**< 墓碑条目数量 */
/**
* @brief 哈希函数指针
* @param key 键指针
* @return 32位无符号哈希值
*/
u32 (*hash_func)(const void* key);
/**
* @brief 键比较函数指针
* @param key1 第一个键指针
* @param key2 第二个键指针
* @return 相同返回0不同返回非0
*/
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);
/**
* @brief 插入/更新键值对
* @param ht 哈希表实例指针
* @param key 键指针
* @param value 值指针
* @return 被替换的旧值指针无替换返回NULL
*/
void* hashtable_set(hash_table_t* ht, const void* key, void* value);
/**
* @brief 查找键对应值
* @param ht 哈希表实例指针
* @param key 查找键指针
* @return 找到返回值指针未找到返回NULL
*/
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);
/**
* @brief 销毁哈希表
* @param ht 哈希表实例指针
*
* @note 仅释放哈希表内部内存不会释放key/value内存
*/
void hashtable_destory(hash_table_t* ht);
/**
* @typedef hash_table_iter_func
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*hash_table_iter_func)(const void* key, void* value, void* context);
/**
* @brief 遍历哈希表所有有效条目
* @param ht 哈希表实例指针
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context);
#endif // __SMCC_HASHTABLE_H__

View File

@@ -0,0 +1,158 @@
/**
* kllist.h is a list implement by linux kernel list
* @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
*/
#ifndef __KLLIST_H__
#define __KLLIST_H__
#ifndef NULL
#define NULL (0)
#define __NULL_KLIST_DEFINED__
#endif
#ifndef container_of
// 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) );})
#else
#define container_of(ptr, type, member) ({ \
const void *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
#endif
/**
* used by list
*/
struct list_head {
struct list_head *next, *prev;
};
/**
* list init
* @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) };
*/
#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)
/**
* list add
*/
static inline void __list_add(struct list_head *newl,
struct list_head *prev,
struct list_head *next) {
next->prev = newl;
newl->next = next;
newl->prev = prev;
prev->next = newl;
}
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) {
__list_add(newl, head->prev, head);
}
/**
* list delete
*/
static inline void __list_del(struct list_head * prev, struct list_head * next) {
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
/**
* list_is_first -- tests whether @list is the first entry in list @head
* @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;
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @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;
}
/**
* list_is_head - tests whether @list is the list @head
* @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;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head) {
return head->next == head;
}
/**
* list_for_each - iterate over a list
* @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)
/**
* 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)
/**
* list sort
* by linux kernel 6.3.1 /lib/list_sort.c
* it remain use sigle linked list to merge sort
* @link https://www.geeksforgeeks.org/merge-sort-for-linked-list/
*/
#ifdef HAVE_KLIST_SORT
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
#if defined(__NULL_KLIST_DEFINED__) && !defined(__NULL_KLIST_DEFINED_NOMOVE__)
#undef NULL
#endif
#endif

View File

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

View File

@@ -0,0 +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"
/**
* @struct strpool_t
* @brief 字符串池上下文
*
* 组合哈希表和专用内存分配器实现的高效字符串存储池
*/
typedef struct strpool {
hash_table_t ht; /**< 哈希表用于快速查找已存储字符串 */
} strpool_t;
/**
* @brief 初始化字符串池
* @param 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);
/**
* @brief 销毁字符串池
* @param pool 字符串池实例指针
*
* @warning 销毁后已获取的字符串指针将失效
* @note 会自动释放所有驻留字符串内存
*/
void strpool_destroy(strpool_t* pool);
#endif // __SMCC_STRPOOL_H__

View File

@@ -0,0 +1,119 @@
/**
* @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

@@ -0,0 +1,140 @@
#include <hashtable.h>
#define INIT_HASH_TABLE_SIZE (32)
void init_hashtable(hash_table_t* ht) {
vector_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
// Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
}
static int next_power_of_two(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
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;
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
u32 probe = 0;
hash_entry_t* tombstone = NULL;
while (1) {
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;
} 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;
}
LOG_ERROR("hashset_find: hash table is full");
return NULL;
}
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);
VECTOR_HEADER(old_entries, hash_entry_t);
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));
// 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);
if (entry->state == ENTRY_ACTIVE) {
hash_entry_t* dest = find_entry(ht, entry->key, entry->hash);
*dest = *entry;
}
}
vector_free(old_entries);
ht->tombstone_count = 0;
}
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;
adjust_capacity(ht, new_cap);
}
u32 hash = ht->hash_func(key);
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--;
ht->count++;
}
entry->key = key;
entry->value = value;
entry->hash = hash;
entry->state = ENTRY_ACTIVE;
return old_value;
}
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);
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;
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;
entry->state = ENTRY_TOMBSTONE;
ht->count--;
ht->tombstone_count++;
return value;
}
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) {
for (usize i = 0; i < ht->entries.cap; 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

@@ -0,0 +1,47 @@
#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);
}
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);
if (!new_str) {
LOG_ERROR("strpool: Failed to allocate memory for string");
return NULL;
}
rt_memcpy(new_str, str, len);
hashtable_set(&pool->ht, new_str, new_str);
return new_str;
}
void strpool_destroy(strpool_t* pool) {
hashtable_destory(&pool->ht);
lalloc_destroy(&pool->stralloc);
}

View File

@@ -0,0 +1,112 @@
# vector_gdb.py
import gdb # type: ignore
from gdb.printing import PrettyPrinter # type: ignore
class VectorPrinter:
"""兼容新旧注册方式的最终方案"""
def __init__(self, val: gdb.Value):
self.val:gdb.Value = val
def check_type(self) -> bool:
"""类型检查(兼容匿名结构体)"""
try:
if self.val.type.code != gdb.TYPE_CODE_STRUCT:
return False
fields = self.val.type.fields()
if not fields:
return False
exp = ['size', 'cap', 'data']
for t in fields:
if t.name in exp:
exp.remove(t.name)
else:
return False
return True
except gdb.error:
return False
def to_string(self):
if not self.check_type():
return "Not a vector"
return "vector({} size={}, cap={})".format(
self.val.address,
self.val['size'],
self.val['cap'],
)
def display_hint(self):
return 'array'
def children(self):
"""生成数组元素(关键改进点)"""
if not self.check_type():
return []
size = int(self.val['size'])
cap = int(self.val['cap'])
data_ptr = self.val['data']
if cap == 0 or data_ptr == 0:
return []
# 使用 GDB 内置数组转换
array = data_ptr.dereference()
array = array.cast(data_ptr.type.target().array(cap - 1))
for i in range(size):
# state = "<used>" if i < size else "<unused>"
try:
value = array[i]
yield (f"[{i}] {value.type} {value.address}", value)
except gdb.MemoryError:
yield (f"[{i}]", "<invalid>")
# 注册方式一传统append方法您之前有效的方式self
def append_printer():
gdb.pretty_printers.append(
lambda val: VectorPrinter(val) if VectorPrinter(val).check_type() else None
)
# 注册方式二:新版注册方法(备用方案)
def register_new_printer():
class VectorPrinterLocator(PrettyPrinter):
def __init__(self):
super().__init__("vector_printer")
def __call__(self, val):
ret = VectorPrinter(val).check_type()
print(f"ret {ret}, type {val.type}, {[(i.name, i.type) for i in val.type.fields()]}")
return None
gdb.printing.register_pretty_printer(
gdb.current_objfile(),
VectorPrinterLocator()
)
# 双重注册保证兼容性
append_printer() # 保留您原来有效的方式
# register_new_printer() # 添加新版注册
class VectorInfoCommand(gdb.Command):
"""保持原有命令不变"""
def __init__(self):
super().__init__("vector_info", gdb.COMMAND_USER)
def invoke(self, argument, from_tty):
val = gdb.parse_and_eval(argument)
printer = VectorPrinter(val)
if not printer.check_type():
print("Invalid vector")
return
print("=== Vector Details ===")
print("Size:", val['size'])
print("Capacity:", val['cap'])
print("Elements:")
for name, value in printer.children():
print(f" {name}: {value}")
VectorInfoCommand()

3
runtime/log/cbuild.toml Normal file
View File

@@ -0,0 +1,3 @@
[package]
name = "log"
version = "0.1.0"

View File

@@ -0,0 +1,59 @@
/**
* @file color.h
* @brief ANSI终端颜色控制码定义
*
* 提供跨平台的终端文本颜色和样式控制支持
*/
#ifndef __SMCC_TERMINAL_COLOR_H__
#define __SMCC_TERMINAL_COLOR_H__
/// @name 前景色控制码
/// @{
#define ANSI_FG_BLACK "\33[30m" ///< 黑色前景
#define ANSI_FG_RED "\33[31m" ///< 红色前景
#define ANSI_FG_GREEN "\33[32m" ///< 绿色前景
#define ANSI_FG_YELLOW "\33[33m" ///< 黄色前景
#define ANSI_FG_BLUE "\33[34m" ///< 蓝色前景
#define ANSI_FG_MAGENTA "\33[35m" ///< 品红色前景
#define ANSI_FG_CYAN "\33[36m" ///< 青色前景
#define ANSI_FG_WHITE "\33[37m" ///< 白色前景
/// @}
/// @name 背景色控制码
/// @{
#define ANSI_BG_BLACK "\33[40m" ///< 黑色背景
#define ANSI_BG_RED "\33[41m" ///< 红色背景
#define ANSI_BG_GREEN "\33[42m" ///< 绿色背景
#define ANSI_BG_YELLOW "\33[43m" ///< 黄色背景
#define ANSI_BG_BLUE "\33[44m" ///< 蓝色背景
#define ANSI_BG_MAGENTA "\33[45m" ///< 品红色背景原始代码此处应为45m
#define ANSI_BG_CYAN "\33[46m" ///< 青色背景
#define ANSI_BG_WHITE "\33[47m" ///< 白色背景
/// @}
/// @name 文字样式控制码
/// @{
#define ANSI_UNDERLINED "\33[4m" ///< 下划线样式
#define ANSI_BOLD "\33[1m" ///< 粗体样式
#define ANSI_NONE "\33[0m" ///< 重置所有样式
/// @}
/**
* @def ANSI_FMT
* @brief 安全文本格式化宏
* @param str 目标字符串
* @param fmt ANSI格式序列可组合多个样式
*
* @note 当定义ANSI_FMT_DISABLE时自动禁用颜色输出
* @code
* printf(ANSI_FMT("Warning!", ANSI_FG_YELLOW ANSI_BOLD));
* @endcode
*/
#ifndef ANSI_FMT_DISABLE
#define ANSI_FMT(str, fmt) fmt str ANSI_NONE ///< 启用样式包裹
#else
#define ANSI_FMT(str, fmt) str ///< 禁用样式输出
#endif
#endif // __SMCC_TERMINAL_COLOR_H__

195
runtime/log/include/log.h Normal file
View File

@@ -0,0 +1,195 @@
/**
* @file log.h
* @brief 日志系统核心模块(支持多级日志、断言和异常处理)
*/
#ifndef __SMCC_LOG_H__
#define __SMCC_LOG_H__
#include "color.h"
#ifndef __SMCC_LOG_NO_STD_IMPL__
#include <stdio.h>
#include <stdlib.h>
#define log_snprintf snprintf
#define log_printf printf
#define log_exit exit
#endif
#ifndef log_snprintf
#define log_snprintf(...)
#warning "log_snprintf not defined"
#endif
#ifndef log_printf
#define log_printf(...)
#warning "log_printf not defined"
#endif
#ifndef log_exit
#define log_exit(...)
#warning "log_exit not defined"
#endif
/**
* @brief 日志级别枚举
*
* 定义日志系统的输出级别和组合标志位
*/
typedef enum log_level {
LOG_LEVEL_NOTSET = 0, ///< 未设置级别(继承默认配置)
LOG_LEVEL_DEBUG = 1 << 0, ///< 调试信息(开发阶段详细信息)
LOG_LEVEL_INFO = 1 << 1, ///< 常规信息(系统运行状态)
LOG_LEVEL_WARN = 1 << 2, ///< 警告信息(潜在问题提示)
LOG_LEVEL_ERROR = 1 << 3, ///< 错误信息(可恢复的错误)
LOG_LEVEL_FATAL = 1 << 4, ///< 致命错误(导致程序终止的严重错误)
LOG_LEVEL_TRACE = 1 << 5, ///< 追踪(性能追踪或者栈帧追踪)
LOG_LEVEL_ALL = 0xFF, ///< 全级别标志(组合所有日志级别)
} log_level_t;
/**
* @brief 日志处理回调函数类型
* @param level 日志级别
* @param module 模块名称可为NULL
* @param file 源文件名
* @param line 代码行号
* @param message 格式化后的日志消息
* @todo 待实现模块名称,输入的模块名称,都将被忽略
*/
typedef void (*log_handler)(
log_level_t level,
const char* module,
const char* file,
int line,
const char* message
);
#ifndef LOGGER_MAX_BUF_SIZE
#define LOGGER_MAX_BUF_SIZE 512 ///< 单条日志最大缓冲区尺寸
#endif
/**
* @brief 日志器实例结构体
*
* 每个日志器实例维护独立的配置和缓冲区
*/
typedef struct logger {
const char* name; ///< 日志器名称(用于模块区分)
log_level_t level; ///< 当前设置的日志级别
log_handler handler; ///< 日志处理回调函数
char buf[LOGGER_MAX_BUF_SIZE]; ///< 格式化缓冲区
} logger_t;
void log_default_handler(log_level_t level, const char* module, const char* file, int line, const char* message);
extern logger_t logger_root;
/**
* @brief 初始化日志实例 其余参数设置为默认值
* @param[in] logger 日志器实例指针
* @param[in] name 日志器名称NULL表示获取默认日志器名称
*/
void init_logger(logger_t* logger, const char* name);
// TODO log_set(); 暂未实现 日志注册
/**
* @brief 获取或创建日志器实例
* @param[in] name 日志器名称NULL表示获取默认日志器
* @return 日志器实例指针
* @warning 若没有找到相应日志器则会返回根日志器
*/
logger_t* log_get(const char* name);
/**
* @brief 设置日志级别
* @param[in] logger 目标日志器实例
* @param[in] level 要设置的日志级别(可组合多个级别)
*/
void log_set_level(logger_t* logger, log_level_t level);
/**
* @brief 设置自定义日志处理器
* @param[in] logger 目标日志器实例
* @param[in] handler 自定义处理函数NULL恢复默认处理
*/
void log_set_handler(logger_t* logger, log_handler handler);
/**
* @todo TODO impliment
*/
void logger_destroy(logger_t* logger);
#ifndef LOG_MAX_MAROC_BUF_SIZE
#define LOG_MAX_MAROC_BUF_SIZE LOGGER_MAX_BUF_SIZE ///< 宏展开缓冲区尺寸
#endif
/**
* @def _LOG
* @brief 内部日志宏(供其他日志宏调用)
* @param _module_ 模块实例NULL表示使用默认日志器
* @param _level_ 日志级别
* @param _msg_ 格式字符串
* @param ... 可变参数列表
*/
#define _LOG(_module_, _level_, _msg_, ...) \
do { \
logger_t* _logger; \
if (!_module_) { \
_logger = log_get(NULL); \
} \
else _logger = _module_; \
if (_logger && _logger->handler && (_logger->level & (_level_))) { \
log_snprintf(_logger->buf, sizeof(_logger->buf), (_msg_), ##__VA_ARGS__); \
_logger->handler((_level_), _logger->name, __FILE__, __LINE__, _logger->buf); \
} \
} while(0)
/// @name 模块日志宏
/// @{
#define MLOG_NOTSET(module, ...) _LOG(module, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
#define MLOG_DEBUG( module, ...) _LOG(module, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志需启用DEBUG级别
#define MLOG_INFO( module, ...) _LOG(module, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
#define MLOG_WARN( module, ...) _LOG(module, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
#define MLOG_ERROR( module, ...) _LOG(module, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
#define MLOG_FATAL( module, ...) _LOG(module, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
#define MLOG_TRACE( module, ...) _LOG(module, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
/// @}
/// @name 快捷日志宏
/// @{
#define LOG_NOTSET(...) _LOG(NULL, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
#define LOG_DEBUG(...) _LOG(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志需启用DEBUG级别
#define LOG_INFO(...) _LOG(NULL, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
#define LOG_WARN(...) _LOG(NULL, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
#define LOG_ERROR(...) _LOG(NULL, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
#define LOG_FATAL(...) _LOG(NULL, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
#define LOG_TRACE(...) _LOG(NULL, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
/// @}
/**
* @def _Assert
* @brief 断言检查内部宏
* @param cond 检查条件表达式
* @param ... 错误信息参数(格式字符串+参数)
*/
#define _Assert(cond, ...) \
do { \
if (!(cond)) { \
LOG_FATAL(__VA_ARGS__); \
} \
} while (0)
/// @name 断言工具宏
/// @{
#define __INNER_LOG_STR(str) #str
#define __LOG_STR(str) __INNER_LOG_STR(str)
#define AssertFmt(cond, format, ...) _Assert(cond, "Assertion Failure: " format, ## __VA_ARGS__) ///< 带格式的断言检查
#define PanicFmt(format, ...) _Assert(0, "Panic: " format, ## __VA_ARGS__) ///< 立即触发致命错误
#define Assert(cond) AssertFmt(cond, "cond is `" __LOG_STR(cond) "`") ///< 基础断言检查
#define Panic(...) PanicFmt(__VA_ARGS__) ///< 触发致命错误(带自定义消息)
#define TODO() PanicFmt("TODO please implement me") ///< 标记未实现代码(触发致命错误)
#define UNREACHABLE() PanicFmt("UNREACHABLE") ///< 触发致命错误(代码不可达)
#define FIXME(str) PanicFmt("FIXME " __LOG_STR(str)) ///< 提醒开发者修改代码(触发致命错误)
/// @}
#endif // __SMCC_LOG_H__

67
runtime/log/src/log.c Normal file
View File

@@ -0,0 +1,67 @@
#include <log.h>
void log_default_handler(log_level_t level, const char* module, const char* file, int line, const char* message) {
const char* level_str;
switch (level) {
case LOG_LEVEL_DEBUG: level_str = "DEBUG"; break;
case LOG_LEVEL_INFO: level_str = "INFO "; break;
case LOG_LEVEL_WARN: level_str = "WARN "; break;
case LOG_LEVEL_ERROR: level_str = "ERROR"; break;
case LOG_LEVEL_FATAL: level_str = "FATAL"; break;
case LOG_LEVEL_TRACE: level_str = "TRACE"; break;
default: level_str = "NOTSET"; break;
}
/// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出
#ifndef __LOG_NO_COLOR__
const char* color_code;
switch (level) {
case LOG_LEVEL_DEBUG: color_code = ANSI_FG_CYAN; break;
case LOG_LEVEL_INFO: color_code = ANSI_FG_GREEN; break;
case LOG_LEVEL_TRACE: color_code = ANSI_FG_BLUE; break;
case LOG_LEVEL_WARN: color_code = ANSI_FG_YELLOW; break;
case LOG_LEVEL_ERROR: color_code = ANSI_FG_RED; break;
case LOG_LEVEL_FATAL: color_code = ANSI_FG_RED ANSI_UNDERLINED; break; // 增强对比度
default: color_code = ANSI_NONE;
}
log_printf(ANSI_BOLD "%s[%s] - %s - %s:%d | %s" ANSI_NONE "\n", color_code,
level_str, module, file, line, message);
#else
log_printf("[%s] %s:%d | %s: %s\n",
level_str, file, line, module, message);
#endif
if (level & LOG_LEVEL_FATAL) {
log_exit(-LOG_LEVEL_FATAL);
}
}
logger_t logger_root = {
.name = "root",
.level = LOG_LEVEL_ALL,
.handler = log_default_handler,
};
void init_logger(logger_t* logger, const char* name) {
logger->name = name;
logger->handler = log_default_handler;
log_set_level(logger, LOG_LEVEL_ALL);
}
logger_t* log_get(const char* name) {
return &logger_root;
}
void log_set_level(logger_t* logger, log_level_t level) {
if (logger) logger->level = level;
else logger_root.level = level;
}
void log_set_handler(logger_t* logger, log_handler handler) {
if (logger) logger->handler = handler;
else logger_root.handler = handler;
}
void logger_destroy(logger_t* logger) {
return;
}