format using clang-format to formate code

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

View File

@@ -5,21 +5,21 @@
/* ====== 内存管理核心接口 ====== */
void* smcc_malloc(usize size);
void* smcc_calloc(usize count, usize size);
void* smcc_realloc(void *ptr, usize new_size);
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 struct smcc_file *smcc_file_t;
/* 文件打开模式 - 只保留编译器真正需要的 */
typedef enum {
SMCC_FILE_READ, /* 读取源文件、头文件 */
SMCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SMCC_FILE_APPEND /* 日志、调试输出 */
SMCC_FILE_READ, /* 读取源文件、头文件 */
SMCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
SMCC_FILE_APPEND /* 日志、调试输出 */
} smcc_file_mode_t;
/* 核心文件操作 */

View File

@@ -2,17 +2,16 @@
#define __SMCC_CORE_LOG_H__
#ifndef log_snprintf
#define log_snprintf smcc_snprintf
#define log_snprintf smcc_snprintf
#endif
#ifndef log_printf
#define log_printf smcc_printf
#define log_printf smcc_printf
#endif
#ifndef log_exit
#define log_exit smcc_exit
#define log_exit smcc_exit
#endif
#include <log.h>
#endif /* __SMCC_CORE_LOG_H__ */

View File

@@ -1,14 +1,14 @@
#ifndef __SMCC_CORE_MEM_H__
#define __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);
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) {
static inline u32 smcc_strhash32(const char *s) {
u32 hash = 2166136261u; // FNV-1a偏移基础值
while (*s) {
hash ^= *s++;
@@ -17,7 +17,7 @@ static inline u32 smcc_strhash32(const char* s) {
return hash;
}
static inline int smcc_strcmp(const char* s1, const char* s2) {
static inline int smcc_strcmp(const char *s1, const char *s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;

View File

@@ -1,63 +1,52 @@
#ifndef __CORE_STR_H__
#define __CORE_STR_H__
#define __CORE_STR_H__
#include "core_type.h"
#include "core_impl.h"
#include "core_log.h"
#include "core_type.h"
typedef struct cstring {
char* data;
usize len;
usize size;
usize cap;
char *data;
} 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 };
return (cstring_t){.data = null, .size = 0, .cap = 0};
}
/**
* 从 C 字符串创建 Rust 风格字符串
*/
static inline cstring_t cstring_from_cstr(const char* s) {
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++;
const char *p = s;
while (*p++)
len++;
char* data = (char*)smcc_malloc(len + 1);
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 };
return (cstring_t){.data = data, .size = len + 1, .cap = len + 1};
}
/**
* 释放字符串资源
*/
static inline void cstring_free(cstring_t* str) {
static inline void cstring_free(cstring_t *str) {
if (str && str->data && str->cap != 0) {
smcc_free(str->data);
str->data = null;
str->len = 0;
str->size = 0;
str->cap = 0;
}
}
@@ -65,64 +54,67 @@ static inline void cstring_free(cstring_t* str) {
/**
* 向字符串追加内容
*/
static inline void cstring_push_cstr(cstring_t* str, const char* data, usize len) {
static inline void cstring_push_cstr(cstring_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
if (str->cap == 0) {
str->size = 1;
}
// 如果需要扩容
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) {
if (str->size + len > str->cap) {
usize new_cap = str->cap;
while (new_cap < str->size + len) {
new_cap *= 2;
if (new_cap == 0) { // 处理溢出情况
new_cap = str->len + len + 1;
// FIXME write by AI 处理溢出情况
if (new_cap == 0) {
new_cap = str->size + len;
break;
}
}
char* new_data = str->data ?
(char*)smcc_realloc(str->data, new_cap) :
(char*)smcc_malloc(new_cap);
char *new_data = (char *)smcc_realloc(str->data, 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 字符串兼容性
smcc_memcpy(str->data + str->size - 1, data, len);
str->size += len;
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
/**
* 向字符串追加单个字符
*/
static inline void cstring_push(cstring_t* str, char ch) {
static inline void cstring_push(cstring_t *str, char ch) {
cstring_push_cstr(str, &ch, 1);
}
/**
* 获取字符串长度
*/
static inline usize cstring_len(const cstring_t* str) {
return str ? str->len : 0;
static inline usize cstring_len(const cstring_t *str) {
return str ? str->size - 1 : 0;
}
/**
* 检查字符串是否为空
*/
static inline cbool cstring_is_empty(const cstring_t* str) {
return str == null || str->len == 0;
static inline cbool cstring_is_empty(const cstring_t *str) {
return str == null || str->size == 0;
}
/**
* 清空字符串内容但保留分配的内存
*/
static inline void cstring_clear(cstring_t* str) {
static inline void cstring_clear(cstring_t *str) {
if (str) {
str->len = 0;
str->size = 1;
if (str->data) {
str->data[0] = '\0';
}
@@ -132,7 +124,7 @@ static inline void cstring_clear(cstring_t* str) {
/**
* 获取 C 风格字符串
*/
static inline const char* cstring_as_cstr(const cstring_t* str) {
static inline char *cstring_as_cstr(const cstring_t *str) {
if (str == null || str->data == null) {
return "";
}

View File

@@ -2,9 +2,9 @@
#define __SMCC_CORE_STREAM_H__
#include "core_impl.h"
#include "core_macro.h"
#include "core_mem.h"
#include "core_str.h"
#include "core_macro.h"
typedef struct core_stream core_stream_t;
@@ -14,52 +14,53 @@ struct core_stream {
cstring_t name;
/// @brief 读取指定数量的字符到缓冲区
usize (*read_buf)(core_stream_t* stream, char* buffer, usize count);
usize (*read_buf)(core_stream_t *stream, char *buffer, usize count);
/// @brief 获取下一个字符
int (*peek_char)(core_stream_t* stream);
int (*peek_char)(core_stream_t *stream);
/// @brief 重置字符流位置
void (*reset_char) (core_stream_t* stream);
void (*reset_char)(core_stream_t *stream);
/// @brief 读取并消费下一个字符(移动流位置)
int (*next_char)(core_stream_t* stream);
int (*next_char)(core_stream_t *stream);
/// @brief 释放资源
void (*free_stream) (core_stream_t* steam);
void (*free_stream)(core_stream_t *steam);
};
static inline usize core_stream_read_buf(core_stream_t* self, char* buffer, usize count) {
static inline usize core_stream_read_buf(core_stream_t *self, char *buffer,
usize count) {
return self->read_buf(self, buffer, count);
}
static inline int core_stream_peek_char(core_stream_t* self) {
static inline int core_stream_peek_char(core_stream_t *self) {
return self->peek_char(self);
}
static inline void core_stream_reset_char(core_stream_t* self) {
static inline void core_stream_reset_char(core_stream_t *self) {
self->reset_char(self);
}
static inline int core_stream_next_char(core_stream_t* self) {
static inline int core_stream_next_char(core_stream_t *self) {
return self->next_char(self);
}
static inline void core_stream_free_stream(core_stream_t* self) {
static inline void core_stream_free_stream(core_stream_t *self) {
self->free_stream(self);
}
#ifndef __SMCC_CORE_NO_MEM_STREAM__
typedef struct core_mem_stream {
core_stream_t stream;
const char* data;
const char *data;
usize data_length;
usize curr_pos;
usize peek_pos;
cbool owned;
} core_mem_stream_t;
core_stream_t* core_mem_stream_init(core_mem_stream_t* stream, const char* data, usize length, cbool need_copy);
core_stream_t *core_mem_stream_init(core_mem_stream_t *stream, const char *data,
usize length, cbool need_copy);
#endif
#endif /* __SMCC_CORE_STREAM_H__ */

View File

@@ -1,12 +1,13 @@
#ifndef __SMCC_CORE_TYPE_H__
#define __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>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* clang-format off */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
@@ -27,6 +28,7 @@ typedef bool cbool;
/// void / null
#define null NULL
/* clang-format on */
static_assert(sizeof(cbool) == 1, "cbool size must 1");
#else
@@ -60,7 +62,7 @@ typedef union core_cvalue {
/* string value */
struct {
char* data;
char *data;
uintptr_t len;
} cstr;

View File

@@ -0,0 +1,115 @@
/**
* @file vec.h
* @brief 动态数组Dynamic Array实现
*
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
*/
#ifndef __SMCC_CORE_DARRAY_H__
#define __SMCC_CORE_DARRAY_H__
#include "core_impl.h"
#include "core_type.h"
#define __vec_realloc smcc_realloc
#define __vec_free smcc_free
/** @defgroup vec_struct 数据结构定义 */
/**
* @def VEC(name, type)
* @brief 声明向量结构体
* @param name 结构体变量名
* @param type 存储的数据类型
*
* 生成包含size/cap/data三个字段的结构体定义
* - size: 当前元素数量
* - cap: 数组容量
* - data: 存储数组指针
*/
#define VEC(name, type) \
struct { \
usize size; \
usize cap; \
type *data; \
} name
/** @defgroup vec_operations 动态数组操作宏 */
/**
* @def vec_init(vec)
* @brief 初始化向量结构体
* @param vec 要初始化的向量结构体变量
*
* @note 此宏不会分配内存,仅做零初始化
*/
#define vec_init(vec) \
do { \
(vec).size = 0, (vec).cap = 0, (vec).data = 0; \
} while (0)
/**
* @def vec_push(vec, value)
* @brief 添加元素到向量末尾
* @param vec 目标向量结构体
* @param value 要添加的值(需匹配存储类型)
*
* @note 当容量不足时自动扩容为2倍初始容量为4
* @warning 内存分配失败时会触发LOG_FATAL
*/
#define 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)); \
if (!data) { \
LOG_FATAL("vector_push: realloc failed\n"); \
} \
(vec).cap = cap; \
(vec).data = data; \
} \
(vec).data[(vec).size++] = value; \
} while (0)
/**
* @def vec_pop(vec)
* @brief 弹出最后一个元素
* @param vec 目标向量结构体
* @return 最后元素的引用
* @warning 需确保size > 0时使用
*/
#define vec_pop(vec) ((vec).data[--(vec).size])
/**
* @def vec_at(vec, idx)
* @brief 获取指定索引元素
* @param vec 目标向量结构体
* @param idx 元素索引0 <= idx < size
* @return 对应元素的引用
*/
#define vec_at(vec, idx) (((vec).data)[idx])
/**
* @def vec_idx(vec, ptr)
* @brief 获取元素指针对应的索引
* @param vec 目标向量结构体
* @param ptr 元素指针需在data数组范围内
* @return 元素索引值
*/
#define vec_idx(vec, ptr) ((ptr) - (vec).data)
/**
* @def vec_free(vec)
* @brief 释放向量内存
* @param vec 目标向量结构体
*
* @note 释放后需重新初始化才能再次使用
*/
#define vec_free(vec) \
do { \
__vec_free((vec).data); \
(vec).data = NULL; \
(vec).size = (vec).cap = 0; \
} while (0)
#endif // __SMCC_CORE_DARRAY_H__

View File

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