refactor(lex_parser): 重命名libcore为scc_core并重构头文件包含

- 将依赖项从libcore重命名为scc_core
- 更新头文件包含路径从<libcore.h>到<scc_core.h>
- 保持原有功能不变

refactor(lexer): 重命名libcore为scc_core并添加词法流式解析功能

- 将依赖项从libcore重命名为scc_core
- 移除不再需要的scc_lexer_token结构体定义
- 重命名struct cc_lexer为struct scc_lexer
- 添加scc_lexer_stream_t流式解析器相关定义和实现
- 新增lexer_stream.c文件实现流式token缓冲功能

refactor(lexer_log): 重命名logger变量和头文件定义

- 将头文件保护宏从__SMCC_LEXER_LOG_H__改为__SCC_LEXER_LOG_H__
- 将logger变量从__smcc_lexer_log改为__scc_lexer_log
- 更新头文件包含从<libcore.h>到<scc_core.h>

refactor(lexer_token): 重新组织token头文件结构

- 将头文件保护宏从__SMCC_CC_TOKEN_H__改为__SCC_LEXER_TOKEN_H__
- 更新头文件包含从<libcore.h>到<scc_core.h>
- 将scc_lexer_token结构体定义移至该文件

refactor(lexer): 简化token匹配代码格式

- 移除LCC相关的注释内容
- 优化括号符号的token匹配代码格式,使用clang-format控制

refactor(pprocessor): 更新依赖项名称和头文件包含

- 将libcore重命名为scc_core
- 将libutils重命名为scc_utils
- 更新头文件包含路径

refactor(runtime): 重命名libcore为scc_core并重构目录结构

- 将libcore目录重命名为scc_core
- 将libutils目录重命名为scc_utils
- 更新所有相关的头文件包含路径
- 修改cbuild.toml中的包名称
- 更新core_vec.h中的宏定义以支持标准库模式
This commit is contained in:
zzy
2026-01-08 11:22:27 +08:00
parent 09f4ac8de0
commit b753ae0911
40 changed files with 345 additions and 150 deletions

View File

@@ -0,0 +1,11 @@
[package]
name = "scc_core"
version = "0.1.0"
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,14 @@
#ifndef __SCC_CORE_H__
#define __SCC_CORE_H__
#include <scc_core_log.h>
#include <scc_core_impl.h>
#include <scc_core_macro.h>
#include <scc_core_mem.h>
#include <scc_core_pos.h>
#include <scc_core_str.h>
#include <scc_core_stream.h>
#include <scc_core_vec.h>
#endif // __SCC_CORE_H__

View File

@@ -0,0 +1,47 @@
#ifndef __SCC_CORE_IMPL_H__
#define __SCC_CORE_IMPL_H__
#include "scc_core_type.h"
/* ====== 内存管理核心接口 ====== */
void *scc_malloc(usize size);
void *scc_calloc(usize count, usize size);
void *scc_realloc(void *ptr, usize new_size);
void scc_free(void *ptr);
/* ====== 文件系统核心接口 ====== */
/* 文件句柄 - 不透明指针 */
typedef struct scc_file *scc_file_t;
/* 文件打开模式 - 只保留编译器真正需要的 */
typedef enum {
SCC_FILE_READ, /* 读取源文件、头文件 */
SCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SCC_FILE_APPEND /* 日志、调试输出 */
} scc_fmode_t;
/* 核心文件操作 */
scc_file_t scc_fopen(const char *path, scc_fmode_t mode);
void scc_fclose(scc_file_t file);
usize scc_fread(scc_file_t file, void *buffer, usize size);
usize scc_fwrite(scc_file_t file, const void *buffer, usize size);
cbool scc_fexists(const char *path);
/* ====== 输入输出核心接口 ====== */
void scc_snprintf(char *buf, usize size, const char *format, ...);
/* 标准输出 - 用于编译进度、结果 */
void scc_printf(const char *format, ...);
/* 错误输出 - 用于错误信息、警告 */
void scc_eprintf(const char *format, ...);
/* ====== 系统核心接口 ====== */
/* 程序控制 */
void scc_exit(int code);
#endif /* __SCC_CORE_IMPL_H__ */

View File

@@ -0,0 +1,17 @@
#ifndef __SCC_CORE_LOG_H__
#define __SCC_CORE_LOG_H__
#ifndef log_snprintf
#define log_snprintf scc_snprintf
#endif
#ifndef log_printf
#define log_printf scc_printf
#endif
#ifndef log_exit
#define log_exit scc_exit
#endif
#include <log.h>
#endif /* __SCC_CORE_LOG_H__ */

View File

@@ -0,0 +1,11 @@
#ifndef __SCC_CORE_MACRO_H__
#define __SCC_CORE_MACRO_H__
#define _SCC_STR(str) #str
#define SCC_STR(str) _SCC_STR(str)
#define SCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define SCC_FUNC
#endif // __SCC_CORE_MACRO_H__

View File

@@ -0,0 +1,56 @@
#ifndef __SCC_CORE_MEM_H__
#define __SCC_CORE_MEM_H__
#include "scc_core_type.h"
void *scc_memcpy(void *dest, const void *src, usize n);
void *scc_memmove(void *dest, const void *src, usize n);
void *scc_memset(void *s, int c, usize n);
int scc_memcmp(const void *s1, const void *s2, usize n);
/**
* @brief 使用FNV-1a进行 C 字符串哈希
*
* @param s
* @return u32
*/
static inline u32 scc_strhash32(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
hash *= 16777619u;
}
return hash;
}
/**
* @brief 获取 C 字符串长度
*
* @param str
* @return usize
*/
static inline usize scc_strlen(const char *str) {
usize len = 0;
while (*str) {
len++;
str++;
}
return len;
}
/**
* @brief 比较两个 C 字符串
*
* @param s1
* @param s2
* @return int
*/
static inline int scc_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
#endif /* __SCC_CORE_MEM_H__ */

View File

@@ -0,0 +1,28 @@
#ifndef __SCC_CORE_POS_H__
#define __SCC_CORE_POS_H__
#include "scc_core_str.h"
#include "scc_core_type.h"
typedef struct scc_pos {
scc_cstring_t name;
usize line;
usize col;
usize offset;
} scc_pos_t;
static inline scc_pos_t scc_pos_create() {
return (scc_pos_t){scc_cstring_create(), 1, 1, 0};
}
static inline void scc_pos_next(scc_pos_t *pos) {
pos->offset++;
pos->col++;
}
static inline void scc_pos_next_line(scc_pos_t *pos) {
pos->offset++;
pos->line++;
pos->col = 1;
}
#endif /* __SCC_CORE_POS_H__ */

View File

@@ -0,0 +1,208 @@
#ifndef __SCC_CORE_STR_H__
#define __SCC_CORE_STR_H__
#include "scc_core_impl.h"
#include "scc_core_log.h"
#include "scc_core_mem.h"
#include "scc_core_type.h"
/**
* @brief 动态字符串结构体
* @attention 创建的字符串对象需要使用 scc_cstring_free 释放
*/
typedef struct scc_cstring {
usize size; /**< 字符串当前大小(包括结尾的'\0'*/
usize cap; /**< 分配的容量 */
char *data; /**< 实际存储数据的指针 */
} scc_cstring_t;
/**
* @brief 创建一个新的空动态字符串对象
*
* @return cstring_t 初始化后的对象
*/
static inline scc_cstring_t scc_cstring_create(void) {
return (scc_cstring_t){.data = null, .size = 0, .cap = 0};
}
static inline void scc_cstring_init(scc_cstring_t *string) {
Assert(string != null);
string->data = null;
string->size = 0;
string->cap = 0;
}
/**
* @brief 从 C 风格字符串创建一个动态字符串副本
*
* @param s 输入的 C 风格字符串
* @return cstring_t 新建对象,包含输入字符串的副本
*/
static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
if (s == null) {
return scc_cstring_create();
}
usize len = 0;
const char *p = s;
while (*p++)
len++;
char *data = (char *)scc_malloc(len + 1);
Assert(data != null);
scc_memcpy(data, s, len);
data[len] = '\0';
return (scc_cstring_t){.size = len + 1, .cap = len + 1, .data = data};
}
static inline scc_cstring_t scc_cstring_copy(const scc_cstring_t *s) {
return scc_cstring_from_cstr(s->data);
}
/**
* @brief 释放动态字符串占用的内存资源
*
* @param str 要被释放的字符串指针
*/
static inline void scc_cstring_free(scc_cstring_t *str) {
if (str == null) {
return;
}
if (str->data != null) {
scc_free(str->data);
str->data = null;
}
str->size = 0;
str->cap = 0;
}
/**
* @brief 向动态字符串末尾追加一段 C 风格字符串
*
* @param str 目标动态字符串指针
* @param data 要追加的 C 字符串指针
* @param len 要追加的 C 字符串长度
*/
static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
if (str->cap == 0) {
// add '\0' to str
str->size = 1;
}
// 如果需要扩容
if (str->size + len > str->cap) {
usize new_cap = str->cap;
while (new_cap < str->size + len) {
if (new_cap == 0) {
new_cap = str->size + len;
break;
} else {
new_cap *= 2;
}
// FIXME 处理溢出情况
}
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != null);
str->data = new_data;
str->cap = new_cap;
}
scc_memcpy(str->data + str->size - 1, data, len);
str->size += len;
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
/**
* @brief 向动态字符串末尾追加另一个动态字符串
*
* @param str 目标动态字符串指针
* @param other 要追加的动态字符串指针
*/
static inline void scc_cstring_append(scc_cstring_t *str,
const scc_cstring_t *other) {
scc_cstring_append_cstr(str, other->data, other->size - 1);
}
/**
* @brief 向动态字符串末尾追加一个字符
*
* @param str 目标动态字符串指针
* @param ch 要追加的字符
*/
static inline void scc_cstring_append_ch(scc_cstring_t *str, char ch) {
scc_cstring_append_cstr(str, &ch, 1);
}
/**
* @brief 获取动态字符串的实际内容长度 不包括结尾的'\0'
*
* @param str 动态字符串指针
* @return usize 字符串实际长度
*/
static inline usize scc_cstring_len(const scc_cstring_t *str) {
if (str == null) {
return 0;
}
if (str->size == 0) {
return 0;
}
return str->size - 1;
}
/**
* @brief 判断动态字符串是否为空
*
* @param str 动态字符串指针
* @return cbool
*/
static inline cbool scc_cstring_is_empty(const scc_cstring_t *str) {
return str == null || str->size == 0;
}
/**
* @brief 清空动态字符串内容但保留已分配的内存空间
*
* @param str 动态字符串指针
*/
static inline void scc_cstring_clear(scc_cstring_t *str) {
if (str) {
str->size = 1;
if (str->data) {
str->data[0] = '\0';
}
}
}
/**
* @brief 将动态字符串转换为 C 风格字符串
*
* @param str 动态字符串指针
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
*/
static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
if (str == null || str->data == null) {
return null;
}
return str->data;
}
static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
if (str == null || str->data == null) {
return null;
}
char *ret = str->data;
str->data = null;
str->cap = 0;
str->size = 0;
return ret;
}
#endif /* __SCC_CORE_STR_H__ */

View File

@@ -0,0 +1,130 @@
#ifndef __SMCC_CORE_PROBE_STREAM_H__
#define __SMCC_CORE_PROBE_STREAM_H__
#include "scc_core_impl.h"
#include "scc_core_macro.h"
#include "scc_core_mem.h"
#include "scc_core_str.h"
struct scc_probe_stream;
typedef struct scc_probe_stream scc_probe_stream_t;
#define scc_stream_eof (-1)
/**
* @brief 带探针的流接口
*
* 这个流提供了双指针机制:当前读取位置(头指针)和探针位置(尾指针)。
* 尾指针只能向前移动,用于查看而不消费。
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
*/
struct scc_probe_stream {
scc_cstring_t name;
/// @brief 消费头指针处的字符(移动头指针)
int (*consume)(scc_probe_stream_t *stream);
/// @brief 查看当前探针位置的字符,不移动任何指针
int (*peek)(scc_probe_stream_t *stream);
/// @brief 移动探针位置并返回字符
int (*next)(scc_probe_stream_t *stream);
/// @brief 移动头指针到探针位置
void (*sync)(scc_probe_stream_t *stream);
/// @brief 重置探针位置到头指针位置
void (*reset)(scc_probe_stream_t *stream);
/// @brief 回退一个字符(单次后退,头指针后退一步)
cbool (*back)(scc_probe_stream_t *stream);
/// @brief 读取指定数量的字符到缓冲区
usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count);
/// @brief 检查是否到达流末尾
cbool (*is_at_end)(scc_probe_stream_t *stream);
/// @brief 销毁流并释放资源
void (*drop)(scc_probe_stream_t *stream);
};
static inline int scc_probe_stream_consume(scc_probe_stream_t *self) {
return self->consume(self);
}
static inline int scc_probe_stream_peek(scc_probe_stream_t *self) {
return self->peek(self);
}
static inline int scc_probe_stream_next(scc_probe_stream_t *self) {
return self->next(self);
}
static inline void scc_probe_stream_sync(scc_probe_stream_t *self) {
self->sync(self);
}
static inline cbool scc_probe_stream_back(scc_probe_stream_t *self) {
return self->back(self);
}
static inline void scc_probe_stream_reset(scc_probe_stream_t *self) {
self->reset(self);
}
static inline usize scc_probe_stream_read_buf(scc_probe_stream_t *self,
char *buffer, usize count) {
return self->read_buf(self, buffer, count);
}
static inline cbool scc_probe_stream_is_at_end(scc_probe_stream_t *self) {
return self->is_at_end(self);
}
static inline cbool scc_probe_stream_has_more(scc_probe_stream_t *self) {
return !self->is_at_end(self);
}
static inline void scc_probe_stream_drop(scc_probe_stream_t *self) {
self->drop(self);
}
#ifndef __SCC_NO_MEM_PROBE_STREAM__
/**
* @brief 内存探针流结构
*/
typedef struct scc_mem_probe_stream {
scc_probe_stream_t stream;
const char *data;
usize data_length;
usize curr_pos; // 当前读取位置
usize probe_pos; // 探针位置用于peek
cbool owned; // 是否拥有数据(如果拥有将会自动释放)
} scc_mem_probe_stream_t;
/**
* @brief 初始化内存探针流(由你负责scc_mem_probe_stream_t的释放)
*
* @param stream 流结构指针
* @param data 数据指针
* @param length 数据长度
* @param owned 是否拥有数据(如果拥有将会自动释放)
* @return core_probe_stream_t* 成功返回流指针失败返回NULL
*/
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool owned);
/**
* @brief 构造内存探针流(其中drop会自动释放内存)
*
* @param data
* @param length
* @param owned 是否拥有数据(如果拥有将会自动释放)
* @return scc_probe_stream_t*
*/
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
cbool owned);
#endif
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */

View File

@@ -0,0 +1,73 @@
#ifndef __SCC_CORE_TYPE_H__
#define __SCC_CORE_TYPE_H__
#ifndef __SCC_BUILTIN_TYPE__
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* clang-format off */
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
/* clang-format on */
static_assert(sizeof(cbool) == 1, "cbool size must 1");
#else
#define __scc_i8
#define __scc_i16
#define __scc_i32
#define __scc_i64
#define __scc_u8
#define __scc_u16
#define __scc_u32
#define __scc_u64
#define __scc_f32
#define __scc_f64
#define __scc_bool
#define __scc_char
#define __scc_void
#define __scc_null
#define __scc_isize
#define __scc_usize
#endif
typedef union scc_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];
} scc_cvalue_t;
#endif /* __SCC_CORE_TYPE_H__ */

View File

@@ -0,0 +1,153 @@
/**
* @file vec.h
* @brief 动态数组Dynamic Array实现
*
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
*/
#ifndef __SCC_CORE_VEC_H__
#define __SCC_CORE_VEC_H__
#ifndef __SCC_CORE_VEC_USE_STD__
#include "scc_core_impl.h"
#include "scc_core_type.h"
#define __scc_vec_realloc scc_realloc
#define __scc_vec_free scc_free
#else
#include <stddef.h>
#include <stdlib.h>
typedef size_t usize;
#define __scc_vec_realloc realloc
#define __scc_vec_free free
#ifndef LOG_FATAL
#include <stdio.h>
#define LOG_FATAL(...) \
do { \
printf(__VA_ARGS__); \
exit(1); \
} while (0)
#endif
#ifndef Assert
#include <assert.h>
#define Assert(cond) assert(cond)
#endif
#endif
/** @defgroup vec_struct 数据结构定义 */
/**
* @def SCC_VEC(type)
* @brief 声明向量结构体
* @param type 存储的数据类型
*
* 生成包含size/cap/data三个字段的结构体定义
* - size: 当前元素数量
* - cap: 数组容量
* - data: 存储数组指针
* @example
* SCC_VEC(char) string; <=> char[dynamic_array] string;
* struct people { SCC_VEC(char) name; int age; SCC_VEC(struct people) children;
* };
*/
#define SCC_VEC(type) \
struct { \
usize size; \
usize cap; \
type *data; \
}
/** @defgroup vec_operations 动态数组操作宏 */
/**
* @def scc_vec_init(vec)
* @brief 初始化向量结构体
* @param vec 要初始化的向量结构体变量
*
* @note 此宏不会分配内存,仅做零初始化
*/
#define scc_vec_init(vec) \
do { \
(vec).size = 0, (vec).cap = 0, (vec).data = 0; \
} while (0)
#define scc_vec_realloc(vec, new_cap) \
do { \
void *data = \
__scc_vec_realloc((vec).data, new_cap * sizeof(*(vec).data)); \
if (!data) { \
LOG_FATAL("vector_push: realloc failed\n"); \
} \
(vec).cap = new_cap; \
(vec).data = data; \
} while (0)
#define scc_vec_size(vec) ((vec).size)
#define scc_vec_cap(vec) ((vec).cap)
#define scc_vec_foreach(vec, idx) \
for (usize idx = 0; idx < scc_vec_size(vec); ++idx)
/**
* @def scc_vec_push(vec, value)
* @brief 添加元素到向量末尾
* @param vec 目标向量结构体
* @param value 要添加的值(需匹配存储类型)
*
* @note 当容量不足时自动扩容为2倍初始容量为4
* @warning 内存分配失败时会触发LOG_FATAL
*/
#define scc_vec_push(vec, value) \
do { \
if ((vec).size >= (vec).cap) { \
int cap = (vec).cap ? (vec).cap * 2 : 4; \
scc_vec_realloc(vec, cap); \
} \
Assert((vec).data != null); \
(vec).data[(vec).size++] = value; \
} while (0)
/**
* @def scc_vec_pop(vec)
* @brief 弹出最后一个元素
* @param vec 目标向量结构体
* @return 最后元素的引用
* @warning 需确保size > 0时使用
*/
#define scc_vec_pop(vec) ((vec).data[--(vec).size])
/**
* @def scc_vec_at(vec, idx)
* @brief 获取指定索引元素
* @param vec 目标向量结构体
* @param idx 元素索引0 <= idx < size
* @return 对应元素的引用
*/
#define scc_vec_at(vec, idx) (((vec).data)[idx])
/**
* @def scc_vec_idx(vec, ptr)
* @brief 获取元素指针对应的索引
* @param vec 目标向量结构体
* @param ptr 元素指针需在data数组范围内
* @return 元素索引值
*/
#define scc_vec_idx(vec, ptr) ((ptr) - (vec).data)
/**
* @def scc_vec_free(vec)
* @brief 释放向量内存
* @param vec 目标向量结构体
*
* @note 释放后需重新初始化才能再次使用
*/
#define scc_vec_free(vec) \
do { \
__scc_vec_free((vec).data); \
(vec).data = NULL; \
(vec).size = (vec).cap = 0; \
} while (0)
#endif /* __SCC_CORE_VEC_H__ */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,99 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <scc_core_impl.h>
#define __SCC_LOG_IMPORT_SRC__
#define log_snprintf scc_snprintf
#define log_printf scc_printf
#define log_exit scc_exit
#include <log.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ====== 内存管理核心接口实现 ====== */
void *scc_malloc(usize size) { return malloc(size); }
void *scc_calloc(usize count, usize size) { return calloc(count, size); }
void *scc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
void scc_free(void *ptr) { free(ptr); }
/* ====== 文件系统核心接口实现 ====== */
static const char *get_file_mode_string(scc_fmode_t mode) {
switch (mode) {
case SCC_FILE_READ:
return "rb";
case SCC_FILE_WRITE:
return "wb";
case SCC_FILE_APPEND:
return "ab";
default:
return "rb";
}
}
scc_file_t scc_fopen(const char *path, scc_fmode_t mode) {
const char *mode_str = get_file_mode_string(mode);
return (scc_file_t)fopen(path, mode_str);
}
void scc_fclose(scc_file_t file) {
if (file) {
fclose((FILE *)file);
}
}
usize scc_fread(scc_file_t file, void *buffer, usize size) {
if (!file || !buffer)
return 0;
return fread(buffer, 1, size, (FILE *)file);
}
usize scc_fwrite(scc_file_t file, const void *buffer, usize size) {
if (!file || !buffer)
return 0;
return fwrite(buffer, 1, size, (FILE *)file);
}
cbool scc_fexists(const char *path) {
scc_file_t fp = scc_fopen(path, SCC_FILE_READ);
if (!fp)
return false;
scc_fclose(fp);
return true;
}
/* ====== 输入输出核心接口实现 ====== */
void scc_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 scc_printf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void scc_eprintf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
/* ====== 系统核心接口实现 ====== */
void scc_exit(int code) { exit(code); }

View File

@@ -0,0 +1,353 @@
#include <scc_core_mem.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 *scc_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 *scc_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 scc_memcpy(d, s, n);
} else {
// 后向拷贝处理重叠情况
d += n;
s += n;
while (n--) {
*(--d) = *(--s);
}
}
return dest;
}
void *scc_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 scc_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;
}
/* fall through */
case 15:
if (p1[14] != p2[14]) {
i = 14;
break;
}
/* fall through */
case 14:
if (p1[13] != p2[13]) {
i = 13;
break;
}
/* fall through */
case 13:
if (p1[12] != p2[12]) {
i = 12;
break;
}
/* fall through */
case 12:
if (p1[11] != p2[11]) {
i = 11;
break;
}
/* fall through */
case 11:
if (p1[10] != p2[10]) {
i = 10;
break;
}
/* fall through */
case 10:
if (p1[9] != p2[9]) {
i = 9;
break;
}
/* fall through */
case 9:
if (p1[8] != p2[8]) {
i = 8;
break;
}
/* fall through */
case 8:
if (p1[7] != p2[7]) {
i = 7;
break;
}
/* fall through */
case 7:
if (p1[6] != p2[6]) {
i = 6;
break;
}
/* fall through */
case 6:
if (p1[5] != p2[5]) {
i = 5;
break;
}
/* fall through */
case 5:
if (p1[4] != p2[4]) {
i = 4;
break;
}
/* fall through */
case 4:
if (p1[3] != p2[3]) {
i = 3;
break;
}
/* fall through */
case 3:
if (p1[2] != p2[2]) {
i = 2;
break;
}
/* fall through */
case 2:
if (p1[1] != p2[1]) {
i = 1;
break;
}
/* fall through */
case 1:
if (p1[0] != p2[0]) {
i = 0;
break;
}
/* fall through */
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,182 @@
#include <scc_core_log.h>
#include <scc_core_stream.h>
#ifndef __SCC_CORE_NO_MEM_PROBE_STREAM__
static int mem_probe_stream_consume(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->curr_pos >= stream->data_length) {
return scc_stream_eof;
}
unsigned char ch = stream->data[stream->curr_pos++];
// 如果探针位置落后于当前读取位置,则更新探针位置
if (stream->probe_pos < stream->curr_pos) {
stream->probe_pos = stream->curr_pos;
}
return (int)ch;
}
static int mem_probe_stream_peek(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return scc_stream_eof;
}
// 只查看而不移动探针位置
return (int)(unsigned char)stream->data[stream->probe_pos];
}
static int mem_probe_stream_next(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return scc_stream_eof;
}
// 返回探针位置的字符,并将探针位置向前移动
int ch = (int)(unsigned char)stream->data[stream->probe_pos];
stream->probe_pos++;
return ch;
}
static void mem_probe_stream_sync(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 移动头指针到探针位置(消费已查看的字符)
if (stream->probe_pos > stream->curr_pos) {
stream->curr_pos = stream->probe_pos;
}
}
static cbool mem_probe_stream_back(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 只能回退一个字符,且不能回退到探针位置之前
if (stream->curr_pos == 0 || stream->curr_pos <= stream->probe_pos) {
return false;
}
stream->curr_pos--;
return true;
}
static usize mem_probe_stream_read_buf(scc_probe_stream_t *_stream,
char *buffer, usize count) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (buffer == null) {
LOG_WARN("Buffer is null");
return 0;
}
usize remaining = stream->data_length - stream->curr_pos;
usize to_read = (remaining < count) ? remaining : count;
if (to_read > 0) {
scc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
stream->curr_pos += to_read;
// 更新探针位置
if (stream->probe_pos < stream->curr_pos) {
stream->probe_pos = stream->curr_pos;
}
} else {
LOG_WARN("Reading past end of stream [maybe count is too large or "
"negative?]");
}
return to_read;
}
static void mem_probe_stream_reset(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 重置探针位置到头指针位置
stream->probe_pos = stream->curr_pos;
}
static cbool mem_probe_stream_is_at_end(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
return stream->curr_pos >= stream->data_length;
}
static void mem_probe_stream_drop(scc_probe_stream_t *_stream) {
Assert(_stream != null);
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
scc_cstring_free(&stream->stream.name);
if (stream->owned) {
scc_free((void *)stream->data);
stream->data = null;
}
}
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool owned) {
if (stream == null || data == null) {
LOG_ERROR("param error");
return null;
}
if (length == 0) {
LOG_WARN("input memory is empty");
owned = false;
}
stream->owned = owned;
stream->data = data;
stream->data_length = length;
stream->curr_pos = 0;
stream->probe_pos = 0;
stream->stream.name = scc_cstring_from_cstr("mem_probe_stream");
// 设置函数指针
stream->stream.consume = mem_probe_stream_consume;
stream->stream.peek = mem_probe_stream_peek;
stream->stream.next = mem_probe_stream_next;
stream->stream.sync = mem_probe_stream_sync;
stream->stream.back = mem_probe_stream_back;
stream->stream.read_buf = mem_probe_stream_read_buf;
stream->stream.reset = mem_probe_stream_reset;
stream->stream.is_at_end = mem_probe_stream_is_at_end;
stream->stream.drop = mem_probe_stream_drop;
return (scc_probe_stream_t *)stream;
}
static void scc_owned_mem_stream_drop(scc_probe_stream_t *_stream) {
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
mem_probe_stream_drop(_stream);
scc_free(stream);
}
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
cbool owned) {
scc_mem_probe_stream_t *stream =
(scc_mem_probe_stream_t *)scc_malloc(sizeof(scc_mem_probe_stream_t));
if (stream == null) {
return null;
}
scc_probe_stream_t *ret =
scc_mem_probe_stream_init(stream, data, length, owned);
stream->stream.drop = scc_owned_mem_stream_drop;
Assert(ret != null);
return ret;
}
#endif /* __SCC_CORE_NO_MEM_PROBE_STREAM__ */

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");
}