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__

View File

@@ -4,69 +4,68 @@
#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 log_snprintf smcc_snprintf
#define log_printf smcc_printf
#define log_exit smcc_exit
#include <log.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/* ====== 内存管理核心接口实现 ====== */
void* smcc_malloc(usize size) {
return malloc(size);
}
void *smcc_malloc(usize size) { return malloc(size); }
void* smcc_calloc(usize count, usize size) {
return calloc(count, 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_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
void smcc_free(void *ptr) {
free(ptr);
}
void smcc_free(void *ptr) { free(ptr); }
/* ====== 文件系统核心接口实现 ====== */
static const char* get_file_mode_string(smcc_file_mode_t mode) {
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";
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);
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);
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);
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);
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;
if (!fp)
return false;
smcc_fclose(fp);
return true;
}
@@ -96,6 +95,4 @@ void smcc_eprintf(const char *format, ...) {
/* ====== 系统核心接口实现 ====== */
void smcc_exit(int code) {
exit(code);
}
void smcc_exit(int code) { exit(code); }

View File

@@ -2,50 +2,68 @@
#include <stdint.h>
// 判断是否支持非对齐访问x86/x64 支持)
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_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;
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;
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;
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;
d = (char *)d64;
s = (const char *)s64;
#endif
// 处理剩余字节
@@ -56,10 +74,9 @@ void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
return dest;
}
void *smcc_memmove(void *dest, const void *src, usize n)
{
char* d = (char*)dest;
const char* s = (const char*)src;
void *smcc_memmove(void *dest, const void *src, usize n) {
char *d = (char *)dest;
const char *s = (const char *)src;
// 地址相同直接返回
if (d == s) {
@@ -81,51 +98,65 @@ void *smcc_memmove(void *dest, const void *src, usize n)
return dest;
}
void* smcc_memset(void *s, int c, usize n) {
unsigned char* p = (unsigned char*)s;
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;
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 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;
uint64_t *p64 = (uint64_t *)p;
while (n >= 8) {
*p64++ = fill_val;
n -= 8;
}
p = (unsigned char*)p64;
p = (unsigned char *)p64;
#endif
// 设置剩余字节
@@ -137,67 +168,150 @@ void* smcc_memset(void *s, int c, usize n) {
}
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;
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;
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;
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;
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;
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;
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];
@@ -207,8 +321,8 @@ int smcc_memcmp(const void *s1, const void *s2, usize n) {
p2_64++;
n -= 8;
}
p1 = (const unsigned char*)p1_64;
p2 = (const unsigned char*)p2_64;
p1 = (const unsigned char *)p1_64;
p2 = (const unsigned char *)p2_64;
#endif
// 比较剩余字节

View File

@@ -2,9 +2,9 @@
#include <core_stream.h>
// 内存流的具体实现结构
static usize read_buf(core_stream_t* _stream, char* buffer, usize count) {
static usize read_buf(core_stream_t *_stream, char *buffer, usize count) {
Assert(buffer != null && buffer != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
usize remaining = stream->data_length - stream->curr_pos;
usize to_read = (remaining < count) ? remaining : count;
@@ -13,15 +13,16 @@ static usize read_buf(core_stream_t* _stream, char* buffer, usize count) {
smcc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
stream->curr_pos += to_read;
} else {
LOG_WARN("Reading past end of stream [maybe count is too large or negative?]");
LOG_WARN("Reading past end of stream "
"[maybe count is too large or negative?]");
}
return to_read;
}
static int peek_char(core_stream_t* _stream) {
static int peek_char(core_stream_t *_stream) {
Assert(_stream != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
// 如果已经到达末尾返回EOF
if (stream->peek_pos >= stream->data_length) {
@@ -31,15 +32,15 @@ static int peek_char(core_stream_t* _stream) {
return (int)(unsigned char)stream->data[stream->peek_pos++];
}
static int next_char(core_stream_t* _stream) {
static int next_char(core_stream_t *_stream) {
Assert(_stream != NULL);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
// 如果已经到达末尾返回EOF
if (stream->curr_pos >= stream->data_length) {
return core_stream_eof; // EOF
}
unsigned char ch = stream->data[stream->curr_pos++];
if (stream->peek_pos < stream->curr_pos) {
stream->peek_pos = stream->curr_pos;
@@ -47,26 +48,27 @@ static int next_char(core_stream_t* _stream) {
return (int)ch;
}
static void reset_char(core_stream_t* _stream) {
static void reset_char(core_stream_t *_stream) {
Assert(_stream != NULL);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
stream->peek_pos = stream->curr_pos;
}
static void free_stream(core_stream_t* _stream) {
static void free_stream(core_stream_t *_stream) {
Assert(_stream != null);
core_mem_stream_t* stream = (core_mem_stream_t*)_stream;
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
// FIXME maybe double free?
cstring_free(&stream->stream.name);
if (stream->owned) {
smcc_free((void*)stream->data);
smcc_free((void *)stream->data);
}
}
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) {
if (stream == null || data == NULL || length == 0) {
LOG_ERROR("param error");
return null;
@@ -74,7 +76,7 @@ core_stream_t* core_mem_stream_init(core_mem_stream_t* stream, const char* data,
stream->owned = need_copy;
if (need_copy) {
char* buf = (char*)smcc_malloc(length);
char *buf = (char *)smcc_malloc(length);
if (buf == null) {
LOG_ERROR("malloc error");
return null;
@@ -90,12 +92,12 @@ core_stream_t* core_mem_stream_init(core_mem_stream_t* stream, const char* data,
stream->peek_pos = 0;
stream->stream.name = cstring_from_cstr("mem_stream");
stream->stream.read_buf = read_buf;
stream->stream.peek_char = peek_char;
stream->stream.next_char = next_char;
stream->stream.reset_char = reset_char;
stream->stream.free_stream = free_stream;
return (void*)stream;
return (void *)stream;
}