feat: rename core types to scc prefix for consistency

Updated type names from `core_*` to `scc_*` across lex_parser and stream modules to maintain naming consistency within the SCC codebase. This includes changes to function signatures and internal usage of types like `core_probe_stream_t`, `core_pos_t`, and `cstring_t` to their `scc_*` counterparts.
This commit is contained in:
zzy
2025-12-11 13:00:29 +08:00
parent 35c13ee30a
commit d88fa3b8d3
33 changed files with 741 additions and 745 deletions

View File

@@ -1,47 +1,47 @@
#ifndef __SMCC_CORE_IMPL_H__
#define __SMCC_CORE_IMPL_H__
#ifndef __SCC_CORE_IMPL_H__
#define __SCC_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);
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 smcc_file *smcc_file_t;
typedef struct scc_file *scc_file_t;
/* 文件打开模式 - 只保留编译器真正需要的 */
typedef enum {
SMCC_FILE_READ, /* 读取源文件、头文件 */
SMCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SMCC_FILE_APPEND /* 日志、调试输出 */
} smcc_file_mode_t;
SCC_FILE_READ, /* 读取源文件、头文件 */
SCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SCC_FILE_APPEND /* 日志、调试输出 */
} scc_fmode_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);
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 smcc_snprintf(char *buf, usize size, const char *format, ...);
void scc_snprintf(char *buf, usize size, const char *format, ...);
/* 标准输出 - 用于编译进度、结果 */
void smcc_printf(const char *format, ...);
void scc_printf(const char *format, ...);
/* 错误输出 - 用于错误信息、警告 */
void smcc_eprintf(const char *format, ...);
void scc_eprintf(const char *format, ...);
/* ====== 系统核心接口 ====== */
/* 程序控制 */
void smcc_exit(int code);
void scc_exit(int code);
#endif // __SMCC_CORE_IMPL_H__
#endif /* __SCC_CORE_IMPL_H__ */

View File

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

View File

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

View File

@@ -1,12 +1,12 @@
#ifndef __SMCC_CORE_MEM_H__
#define __SMCC_CORE_MEM_H__
#ifndef __SCC_CORE_MEM_H__
#define __SCC_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);
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 字符串哈希
@@ -14,7 +14,7 @@ int smcc_memcmp(const void *s1, const void *s2, usize n);
* @param s
* @return u32
*/
static inline u32 smcc_strhash32(const char *s) {
static inline u32 scc_strhash32(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
@@ -29,7 +29,7 @@ static inline u32 smcc_strhash32(const char *s) {
* @param str
* @return usize
*/
static inline usize smcc_strlen(const char *str) {
static inline usize scc_strlen(const char *str) {
usize len = 0;
while (*str) {
len++;
@@ -45,7 +45,7 @@ static inline usize smcc_strlen(const char *str) {
* @param s2
* @return int
*/
static inline int smcc_strcmp(const char *s1, const char *s2) {
static inline int scc_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
@@ -53,4 +53,4 @@ static inline int smcc_strcmp(const char *s1, const char *s2) {
return *s1 - *s2;
}
#endif /* __SMCC_CORE_MEM_H__ */
#endif /* __SCC_CORE_MEM_H__ */

View File

@@ -1,28 +1,28 @@
#ifndef __SMCC_CORE_POS_H__
#define __SMCC_CORE_POS_H__
#ifndef __SCC_CORE_POS_H__
#define __SCC_CORE_POS_H__
#include "core_str.h"
#include "core_type.h"
typedef struct {
cstring_t name;
typedef struct scc_pos {
scc_cstring_t name;
usize line;
usize col;
usize offset;
} core_pos_t;
} scc_pos_t;
static inline core_pos_t core_pos_init() {
return (core_pos_t){cstring_new(), 1, 1, 0};
static inline scc_pos_t scc_pos_init() {
return (scc_pos_t){scc_cstring_new(), 1, 1, 0};
}
static inline void core_pos_next(core_pos_t *pos) {
static inline void core_pos_next(scc_pos_t *pos) {
pos->offset++;
pos->col++;
}
static inline void core_pos_next_line(core_pos_t *pos) {
static inline void core_pos_next_line(scc_pos_t *pos) {
pos->offset++;
pos->line++;
pos->col = 1;
}
#endif /* __SMCC_CORE_POS_H__ */
#endif /* __SCC_CORE_POS_H__ */

View File

@@ -1,5 +1,5 @@
#ifndef __SMCC_CORE_STR_H__
#define __SMCC_CORE_STR_H__
#ifndef __SCC_CORE_STR_H__
#define __SCC_CORE_STR_H__
#include "core_impl.h"
#include "core_log.h"
@@ -7,21 +7,21 @@
/**
* @brief 动态字符串结构体
* @attention 创建的字符串对象需要使用 cstring_free 释放
* @attention 创建的字符串对象需要使用 scc_cstring_free 释放
*/
typedef struct cstring {
typedef struct scc_cstring {
usize size; /**< 字符串当前大小(包括结尾的'\0'*/
usize cap; /**< 分配的容量 */
char *data; /**< 实际存储数据的指针 */
} cstring_t;
} scc_cstring_t;
/**
* @brief 创建一个新的空动态字符串对象
*
* @return cstring_t 初始化后的对象
*/
static inline cstring_t cstring_new(void) {
return (cstring_t){.data = null, .size = 0, .cap = 0};
static inline scc_cstring_t scc_cstring_new(void) {
return (scc_cstring_t){.data = null, .size = 0, .cap = 0};
}
/**
@@ -30,9 +30,9 @@ static inline cstring_t cstring_new(void) {
* @param s 输入的 C 风格字符串
* @return cstring_t 新建对象,包含输入字符串的副本
*/
static inline cstring_t cstring_from_cstr(const char *s) {
static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
if (s == null) {
return cstring_new();
return scc_cstring_new();
}
usize len = 0;
@@ -40,12 +40,12 @@ static inline cstring_t cstring_from_cstr(const char *s) {
while (*p++)
len++;
char *data = (char *)smcc_malloc(len + 1);
char *data = (char *)scc_malloc(len + 1);
Assert(data != null);
smcc_memcpy(data, s, len);
scc_memcpy(data, s, len);
data[len] = '\0';
return (cstring_t){.size = len + 1, .cap = len + 1, .data = data};
return (scc_cstring_t){.size = len + 1, .cap = len + 1, .data = data};
}
/**
@@ -53,12 +53,12 @@ static inline cstring_t cstring_from_cstr(const char *s) {
*
* @param str 要被释放的字符串指针
*/
static inline void cstring_free(cstring_t *str) {
static inline void scc_cstring_free(scc_cstring_t *str) {
if (str == null) {
return;
}
if (str->cap != 0 && str->data != null) {
smcc_free(str->data);
scc_free(str->data);
str->data = null;
}
str->size = 0;
@@ -72,8 +72,8 @@ static inline void cstring_free(cstring_t *str) {
* @param data 要追加的 C 字符串指针
* @param len 要追加的 C 字符串长度
*/
static inline void cstring_append_cstr(cstring_t *str, const char *data,
usize len) {
static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
@@ -96,14 +96,14 @@ static inline void cstring_append_cstr(cstring_t *str, const char *data,
// FIXME 处理溢出情况
}
char *new_data = (char *)smcc_realloc(str->data, new_cap);
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != null);
str->data = new_data;
str->cap = new_cap;
}
smcc_memcpy(str->data + str->size - 1, data, len);
scc_memcpy(str->data + str->size - 1, data, len);
str->size += len;
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
@@ -114,8 +114,9 @@ static inline void cstring_append_cstr(cstring_t *str, const char *data,
* @param str 目标动态字符串指针
* @param other 要追加的动态字符串指针
*/
static inline void cstring_append(cstring_t *str, const cstring_t *other) {
cstring_append_cstr(str, other->data, other->size - 1);
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);
}
/**
@@ -124,8 +125,8 @@ static inline void cstring_append(cstring_t *str, const cstring_t *other) {
* @param str 目标动态字符串指针
* @param ch 要追加的字符
*/
static inline void cstring_append_ch(cstring_t *str, char ch) {
cstring_append_cstr(str, &ch, 1);
static inline void scc_cstring_append_ch(scc_cstring_t *str, char ch) {
scc_cstring_append_cstr(str, &ch, 1);
}
/**
@@ -134,7 +135,7 @@ static inline void cstring_append_ch(cstring_t *str, char ch) {
* @param str 动态字符串指针
* @return usize 字符串实际长度
*/
static inline usize cstring_len(const cstring_t *str) {
static inline usize scc_cstring_len(const scc_cstring_t *str) {
return str ? str->size - 1 : 0;
}
@@ -144,7 +145,7 @@ static inline usize cstring_len(const cstring_t *str) {
* @param str 动态字符串指针
* @return cbool
*/
static inline cbool cstring_is_empty(const cstring_t *str) {
static inline cbool scc_cstring_is_empty(const scc_cstring_t *str) {
return str == null || str->size == 0;
}
@@ -153,7 +154,7 @@ static inline cbool cstring_is_empty(const cstring_t *str) {
*
* @param str 动态字符串指针
*/
static inline void cstring_clear(cstring_t *str) {
static inline void scc_cstring_clear(scc_cstring_t *str) {
if (str) {
str->size = 1;
if (str->data) {
@@ -168,11 +169,11 @@ static inline void cstring_clear(cstring_t *str) {
* @param str 动态字符串指针
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
*/
static inline char *cstring_as_cstr(const cstring_t *str) {
static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
if (str == null || str->data == null) {
return "";
}
return str->data;
}
#endif /* __SMCC_CORE_STR_H__ */
#endif /* __SCC_CORE_STR_H__ */

View File

@@ -6,8 +6,8 @@
#include "core_mem.h"
#include "core_str.h"
struct core_probe_stream;
typedef struct core_probe_stream core_probe_stream_t;
struct scc_probe_stream;
typedef struct scc_probe_stream scc_probe_stream_t;
#define core_stream_eof (-1)
@@ -18,90 +18,90 @@ typedef struct core_probe_stream core_probe_stream_t;
* 尾指针只能向前移动,用于查看而不消费。
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
*/
struct core_probe_stream {
cstring_t name;
struct scc_probe_stream {
scc_cstring_t name;
/// @brief 消费头指针处的字符(移动头指针)
int (*consume)(core_probe_stream_t *stream);
int (*consume)(scc_probe_stream_t *stream);
/// @brief 查看当前探针位置的字符,不移动任何指针
int (*peek)(core_probe_stream_t *stream);
int (*peek)(scc_probe_stream_t *stream);
/// @brief 移动探针位置并返回字符
int (*next)(core_probe_stream_t *stream);
int (*next)(scc_probe_stream_t *stream);
/// @brief 移动头指针到探针位置
void (*sync)(core_probe_stream_t *stream);
void (*sync)(scc_probe_stream_t *stream);
/// @brief 重置探针位置到头指针位置
void (*reset)(core_probe_stream_t *stream);
void (*reset)(scc_probe_stream_t *stream);
/// @brief 回退一个字符(单次后退,头指针后退一步)
cbool (*back)(core_probe_stream_t *stream);
cbool (*back)(scc_probe_stream_t *stream);
/// @brief 读取指定数量的字符到缓冲区
usize (*read_buf)(core_probe_stream_t *stream, char *buffer, usize count);
usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count);
/// @brief 检查是否到达流末尾
cbool (*is_at_end)(core_probe_stream_t *stream);
cbool (*is_at_end)(scc_probe_stream_t *stream);
/// @brief 销毁流并释放资源
void (*drop)(core_probe_stream_t *stream);
void (*drop)(scc_probe_stream_t *stream);
};
static inline int core_probe_stream_consume(core_probe_stream_t *self) {
static inline int scc_probe_stream_consume(scc_probe_stream_t *self) {
return self->consume(self);
}
static inline int core_probe_stream_peek(core_probe_stream_t *self) {
static inline int scc_probe_stream_peek(scc_probe_stream_t *self) {
return self->peek(self);
}
static inline int core_probe_stream_next(core_probe_stream_t *self) {
static inline int scc_probe_stream_next(scc_probe_stream_t *self) {
return self->next(self);
}
static inline void core_probe_stream_sync(core_probe_stream_t *self) {
static inline void scc_probe_stream_sync(scc_probe_stream_t *self) {
self->sync(self);
}
static inline cbool core_probe_stream_back(core_probe_stream_t *self) {
static inline cbool scc_probe_stream_back(scc_probe_stream_t *self) {
return self->back(self);
}
static inline void core_probe_stream_reset(core_probe_stream_t *self) {
static inline void scc_probe_stream_reset(scc_probe_stream_t *self) {
self->reset(self);
}
static inline usize core_probe_stream_read_buf(core_probe_stream_t *self,
char *buffer, usize count) {
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 core_probe_stream_is_at_end(core_probe_stream_t *self) {
static inline cbool scc_probe_stream_is_at_end(scc_probe_stream_t *self) {
return self->is_at_end(self);
}
static inline cbool core_probe_stream_has_more(core_probe_stream_t *self) {
static inline cbool scc_probe_stream_has_more(scc_probe_stream_t *self) {
return !self->is_at_end(self);
}
static inline void core_probe_stream_drop(core_probe_stream_t *self) {
static inline void scc_probe_stream_drop(scc_probe_stream_t *self) {
self->drop(self);
}
#ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__
#ifndef __SCC_NO_MEM_PROBE_STREAM__
/**
* @brief 内存探针流结构
*/
typedef struct core_mem_probe_stream {
core_probe_stream_t stream;
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; // 是否拥有数据(需要释放)
} core_mem_probe_stream_t;
} scc_mem_probe_stream_t;
/**
* @brief 初始化内存探针流
@@ -112,9 +112,9 @@ typedef struct core_mem_probe_stream {
* @param need_copy 是否需要复制数据
* @return core_probe_stream_t* 成功返回流指针失败返回NULL
*/
core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy);
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy);
#endif
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */

View File

@@ -1,7 +1,7 @@
#ifndef __SMCC_CORE_TYPE_H__
#define __SMCC_CORE_TYPE_H__
#ifndef __SCC_CORE_TYPE_H__
#define __SCC_CORE_TYPE_H__
#ifndef __SMCC_BUILTIN_TYPE__
#ifndef __SCC_BUILTIN_TYPE__
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
@@ -32,25 +32,25 @@ typedef bool cbool;
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
#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 core_cvalue {
typedef union scc_cvalue {
long double ld;
double d;
float f;
@@ -68,6 +68,6 @@ typedef union core_cvalue {
/* 16 byte == 128 bit */
char val[16];
} core_cvalue_t;
} scc_cvalue_t;
#endif
#endif /* __SCC_CORE_TYPE_H__ */

View File

@@ -5,19 +5,19 @@
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
*/
#ifndef __SMCC_CORE_VEC_H__
#define __SMCC_CORE_VEC_H__
#ifndef __SCC_CORE_VEC_H__
#define __SCC_CORE_VEC_H__
#include "core_impl.h"
#include "core_type.h"
#define __vec_realloc smcc_realloc
#define __vec_free smcc_free
#define __scc_vec_realloc scc_realloc
#define __scc_vec_free scc_free
/** @defgroup vec_struct 数据结构定义 */
/**
* @def VEC(type)
* @def SCC_VEC(type)
* @brief 声明向量结构体
* @param type 存储的数据类型
*
@@ -26,10 +26,11 @@
* - cap: 数组容量
* - data: 存储数组指针
* @example
* VEC(char) string; <=> char[dynamic_array] string;
* struct people { VEC(char) name; int age; VEC(struct people) children; };
* SCC_VEC(char) string; <=> char[dynamic_array] string;
* struct people { SCC_VEC(char) name; int age; SCC_VEC(struct people) children;
* };
*/
#define VEC(type) \
#define SCC_VEC(type) \
struct { \
usize size; \
usize cap; \
@@ -39,19 +40,19 @@
/** @defgroup vec_operations 动态数组操作宏 */
/**
* @def vec_init(vec)
* @def scc_vec_init(vec)
* @brief 初始化向量结构体
* @param vec 要初始化的向量结构体变量
*
* @note 此宏不会分配内存,仅做零初始化
*/
#define vec_init(vec) \
#define scc_vec_init(vec) \
do { \
(vec).size = 0, (vec).cap = 0, (vec).data = 0; \
} while (0)
/**
* @def vec_push(vec, value)
* @def scc_vec_push(vec, value)
* @brief 添加元素到向量末尾
* @param vec 目标向量结构体
* @param value 要添加的值(需匹配存储类型)
@@ -59,11 +60,12 @@
* @note 当容量不足时自动扩容为2倍初始容量为4
* @warning 内存分配失败时会触发LOG_FATAL
*/
#define vec_push(vec, value) \
#define scc_vec_push(vec, value) \
do { \
if ((vec).size >= (vec).cap) { \
int cap = (vec).cap ? (vec).cap * 2 : 4; \
void *data = __vec_realloc((vec).data, cap * sizeof(*(vec).data)); \
void *data = \
__scc_vec_realloc((vec).data, cap * sizeof(*(vec).data)); \
if (!data) { \
LOG_FATAL("vector_push: realloc failed\n"); \
} \
@@ -75,44 +77,44 @@
} while (0)
/**
* @def vec_pop(vec)
* @def scc_vec_pop(vec)
* @brief 弹出最后一个元素
* @param vec 目标向量结构体
* @return 最后元素的引用
* @warning 需确保size > 0时使用
*/
#define vec_pop(vec) ((vec).data[--(vec).size])
#define scc_vec_pop(vec) ((vec).data[--(vec).size])
/**
* @def vec_at(vec, idx)
* @def scc_vec_at(vec, idx)
* @brief 获取指定索引元素
* @param vec 目标向量结构体
* @param idx 元素索引0 <= idx < size
* @return 对应元素的引用
*/
#define vec_at(vec, idx) (((vec).data)[idx])
#define scc_vec_at(vec, idx) (((vec).data)[idx])
/**
* @def vec_idx(vec, ptr)
* @def scc_vec_idx(vec, ptr)
* @brief 获取元素指针对应的索引
* @param vec 目标向量结构体
* @param ptr 元素指针需在data数组范围内
* @return 元素索引值
*/
#define vec_idx(vec, ptr) ((ptr) - (vec).data)
#define scc_vec_idx(vec, ptr) ((ptr) - (vec).data)
/**
* @def vec_free(vec)
* @def scc_vec_free(vec)
* @brief 释放向量内存
* @param vec 目标向量结构体
*
* @note 释放后需重新初始化才能再次使用
*/
#define vec_free(vec) \
#define scc_vec_free(vec) \
do { \
__vec_free((vec).data); \
__scc_vec_free((vec).data); \
(vec).data = NULL; \
(vec).size = (vec).cap = 0; \
} while (0)
#endif // __SMCC_CORE_VEC_H__
#endif /* __SCC_CORE_VEC_H__ */

View File

@@ -1,29 +1,14 @@
#ifndef __SMCC_CORE_H__
#define __SMCC_CORE_H__
#ifndef __SCC_CORE_H__
#define __SCC_CORE_H__
#include <core_log.h>
#include <core_impl.h>
#include <core_macro.h>
#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
#define SMCC_STR(str) _SMCC_STR(str)
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#include <core_pos.h>
#include <core_str.h>
#include <core_stream.h>
#include <core_vec.h>
#endif // __SMCC_CORE_H__
#endif // __SCC_CORE_H__

View File

@@ -3,12 +3,13 @@
#endif
#include <core_impl.h>
#define __SMCC_LOG_IMPORT_SRC__
#define log_snprintf smcc_snprintf
#define log_printf smcc_printf
#define log_exit smcc_exit
#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>
@@ -16,77 +17,77 @@
/* ====== 内存管理核心接口实现 ====== */
void *smcc_malloc(usize size) { return malloc(size); }
void *scc_malloc(usize size) { return malloc(size); }
void *smcc_calloc(usize count, usize size) { return calloc(count, size); }
void *scc_calloc(usize count, usize size) { return calloc(count, size); }
void *smcc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
void *scc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
void smcc_free(void *ptr) { free(ptr); }
void scc_free(void *ptr) { free(ptr); }
/* ====== 文件系统核心接口实现 ====== */
static const char *get_file_mode_string(smcc_file_mode_t mode) {
static const char *get_file_mode_string(scc_fmode_t mode) {
switch (mode) {
case SMCC_FILE_READ:
case SCC_FILE_READ:
return "rb";
case SMCC_FILE_WRITE:
case SCC_FILE_WRITE:
return "wb";
case SMCC_FILE_APPEND:
case SCC_FILE_APPEND:
return "ab";
default:
return "rb";
}
}
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode) {
scc_file_t scc_fopen(const char *path, scc_fmode_t mode) {
const char *mode_str = get_file_mode_string(mode);
return (smcc_file_t)fopen(path, mode_str);
return (scc_file_t)fopen(path, mode_str);
}
void smcc_fclose(smcc_file_t file) {
void scc_fclose(scc_file_t file) {
if (file) {
fclose((FILE *)file);
}
}
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
usize scc_fread(scc_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) {
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 smcc_fexists(const char *path) {
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
cbool scc_fexists(const char *path) {
scc_file_t fp = scc_fopen(path, SCC_FILE_READ);
if (!fp)
return false;
smcc_fclose(fp);
scc_fclose(fp);
return true;
}
/* ====== 输入输出核心接口实现 ====== */
void smcc_snprintf(char *buf, usize size, const char *format, ...) {
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 smcc_printf(const char *format, ...) {
void scc_printf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void smcc_eprintf(const char *format, ...) {
void scc_eprintf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
@@ -95,4 +96,4 @@ void smcc_eprintf(const char *format, ...) {
/* ====== 系统核心接口实现 ====== */
void smcc_exit(int code) { exit(code); }
void scc_exit(int code) { exit(code); }

View File

@@ -1,5 +1,4 @@
#include <core_mem.h>
#include <stdint.h>
// 判断是否支持非对齐访问x86/x64 支持)
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
@@ -9,7 +8,7 @@
#define UNALIGNED_ACCESS_ALLOWED 0
#endif
void *smcc_memcpy(void *dest, const void *restrict src, usize n) {
void *scc_memcpy(void *dest, const void *restrict src, usize n) {
char *d = (char *)dest;
const char *s = (const char *)src;
@@ -74,7 +73,7 @@ void *smcc_memcpy(void *dest, const void *restrict src, usize n) {
return dest;
}
void *smcc_memmove(void *dest, const void *src, usize n) {
void *scc_memmove(void *dest, const void *src, usize n) {
char *d = (char *)dest;
const char *s = (const char *)src;
@@ -85,7 +84,7 @@ void *smcc_memmove(void *dest, const void *src, usize n) {
// 内存区域无重叠或前向拷贝
if (d < s || d >= s + n) {
return smcc_memcpy(d, s, n);
return scc_memcpy(d, s, n);
} else {
// 后向拷贝处理重叠情况
d += n;
@@ -98,7 +97,7 @@ void *smcc_memmove(void *dest, const void *src, usize n) {
return dest;
}
void *smcc_memset(void *s, int c, usize n) {
void *scc_memset(void *s, int c, usize n) {
unsigned char *p = (unsigned char *)s;
unsigned char byte_val = (unsigned char)c;
@@ -167,7 +166,7 @@ void *smcc_memset(void *s, int c, usize n) {
return s;
}
int smcc_memcmp(const void *s1, const void *s2, usize n) {
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;

View File

@@ -1,11 +1,11 @@
#include <core_log.h>
#include <core_stream.h>
#ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__
#ifndef __SCC_CORE_NO_MEM_PROBE_STREAM__
static int mem_probe_stream_consume(core_probe_stream_t *_stream) {
static int mem_probe_stream_consume(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->curr_pos >= stream->data_length) {
return core_stream_eof;
@@ -19,9 +19,9 @@ static int mem_probe_stream_consume(core_probe_stream_t *_stream) {
return (int)ch;
}
static int mem_probe_stream_peek(core_probe_stream_t *_stream) {
static int mem_probe_stream_peek(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return core_stream_eof;
@@ -31,9 +31,9 @@ static int mem_probe_stream_peek(core_probe_stream_t *_stream) {
return (int)(unsigned char)stream->data[stream->probe_pos];
}
static int mem_probe_stream_next(core_probe_stream_t *_stream) {
static int mem_probe_stream_next(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return core_stream_eof;
@@ -45,9 +45,9 @@ static int mem_probe_stream_next(core_probe_stream_t *_stream) {
return ch;
}
static void mem_probe_stream_sync(core_probe_stream_t *_stream) {
static void mem_probe_stream_sync(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 移动头指针到探针位置(消费已查看的字符)
if (stream->probe_pos > stream->curr_pos) {
@@ -55,9 +55,9 @@ static void mem_probe_stream_sync(core_probe_stream_t *_stream) {
}
}
static cbool mem_probe_stream_back(core_probe_stream_t *_stream) {
static cbool mem_probe_stream_back(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
// 只能回退一个字符,且不能回退到探针位置之前
if (stream->curr_pos == 0 || stream->curr_pos <= stream->probe_pos) {
@@ -68,10 +68,10 @@ static cbool mem_probe_stream_back(core_probe_stream_t *_stream) {
return true;
}
static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream,
static usize mem_probe_stream_read_buf(scc_probe_stream_t *_stream,
char *buffer, usize count) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
if (buffer == null) {
LOG_WARN("Buffer is null");
@@ -82,7 +82,7 @@ static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream,
usize to_read = (remaining < count) ? remaining : count;
if (to_read > 0) {
smcc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
scc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
stream->curr_pos += to_read;
// 更新探针位置
if (stream->probe_pos < stream->curr_pos) {
@@ -96,36 +96,36 @@ static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream,
return to_read;
}
static void mem_probe_stream_reset(core_probe_stream_t *_stream) {
static void mem_probe_stream_reset(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
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(core_probe_stream_t *_stream) {
static cbool mem_probe_stream_is_at_end(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
return stream->curr_pos >= stream->data_length;
}
static void mem_probe_stream_destroy(core_probe_stream_t *_stream) {
static void mem_probe_stream_destroy(scc_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
cstring_free(&stream->stream.name);
scc_cstring_free(&stream->stream.name);
if (stream->owned) {
smcc_free((void *)stream->data);
scc_free((void *)stream->data);
stream->data = null;
}
}
core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy) {
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy) {
if (stream == null || data == null) {
LOG_ERROR("param error");
return null;
@@ -138,13 +138,13 @@ core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
stream->owned = need_copy;
if (need_copy) {
char *buf = (char *)smcc_malloc(length);
char *buf = (char *)scc_malloc(length);
if (buf == null) {
LOG_ERROR("malloc error");
return null;
}
smcc_memcpy(buf, data, length);
scc_memcpy(buf, data, length);
stream->data = buf;
} else {
stream->data = data;
@@ -153,7 +153,7 @@ core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
stream->curr_pos = 0;
stream->probe_pos = 0;
stream->stream.name = cstring_from_cstr("mem_probe_stream");
stream->stream.name = scc_cstring_from_cstr("mem_probe_stream");
// 设置函数指针
stream->stream.consume = mem_probe_stream_consume;
@@ -166,7 +166,7 @@ core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
stream->stream.is_at_end = mem_probe_stream_is_at_end;
stream->stream.drop = mem_probe_stream_destroy;
return (core_probe_stream_t *)stream;
return (scc_probe_stream_t *)stream;
}
#endif /* __SMCC_CORE_NO_MEM_PROBE_STREAM__ */
#endif /* __SCC_CORE_NO_MEM_PROBE_STREAM__ */