feat 新的运行时环境
This commit is contained in:
95
runtime/libcore/src/cfg.std_impl.c
Normal file
95
runtime/libcore/src/cfg.std_impl.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifdef _MSC_VER
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
#include <core_impl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/* ====== 内存管理核心接口实现 ====== */
|
||||
|
||||
void* smcc_malloc(usize size) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void* smcc_calloc(usize count, usize size) {
|
||||
return calloc(count, size);
|
||||
}
|
||||
|
||||
void* smcc_realloc(void *ptr, usize new_size) {
|
||||
return realloc(ptr, new_size);
|
||||
}
|
||||
|
||||
void smcc_free(void *ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
/* ====== 文件系统核心接口实现 ====== */
|
||||
|
||||
static const char* get_file_mode_string(smcc_file_mode_t mode) {
|
||||
switch (mode) {
|
||||
case SMCC_FILE_READ: return "rb";
|
||||
case SMCC_FILE_WRITE: return "wb";
|
||||
case SMCC_FILE_APPEND: return "ab";
|
||||
default: return "rb";
|
||||
}
|
||||
}
|
||||
|
||||
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode) {
|
||||
const char* mode_str = get_file_mode_string(mode);
|
||||
return (smcc_file_t)fopen(path, mode_str);
|
||||
}
|
||||
|
||||
void smcc_fclose(smcc_file_t file) {
|
||||
if (file) {
|
||||
fclose((FILE*)file);
|
||||
}
|
||||
}
|
||||
|
||||
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
|
||||
if (!file || !buffer) return 0;
|
||||
return fread(buffer, 1, size, (FILE*)file);
|
||||
}
|
||||
|
||||
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size) {
|
||||
if (!file || !buffer) return 0;
|
||||
return fwrite(buffer, 1, size, (FILE*)file);
|
||||
}
|
||||
|
||||
cbool smcc_fexists(const char *path) {
|
||||
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
|
||||
if (!fp) return false;
|
||||
smcc_fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ====== 输入输出核心接口实现 ====== */
|
||||
|
||||
void smcc_snprintf(char *buf, usize size, const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, size, format, args); // NOLINT
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void smcc_printf(const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stdout, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void smcc_eprintf(const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/* ====== 系统核心接口实现 ====== */
|
||||
|
||||
void smcc_exit(int code) {
|
||||
exit(code);
|
||||
}
|
||||
224
runtime/libcore/src/core_mem.c
Normal file
224
runtime/libcore/src/core_mem.c
Normal file
@@ -0,0 +1,224 @@
|
||||
#include <core_mem.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// 判断是否支持非对齐访问(x86/x64 支持)
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
|
||||
#define UNALIGNED_ACCESS_ALLOWED 1
|
||||
#else
|
||||
#define UNALIGNED_ACCESS_ALLOWED 0
|
||||
#endif
|
||||
|
||||
void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
|
||||
char* d = (char*)dest;
|
||||
const char* s = (const char*)src;
|
||||
|
||||
// 快速路径:小内存拷贝
|
||||
if (n <= 16) {
|
||||
switch(n) {
|
||||
case 16: d[15] = s[15]; /* fall through */
|
||||
case 15: d[14] = s[14]; /* fall through */
|
||||
case 14: d[13] = s[13]; /* fall through */
|
||||
case 13: d[12] = s[12]; /* fall through */
|
||||
case 12: d[11] = s[11]; /* fall through */
|
||||
case 11: d[10] = s[10]; /* fall through */
|
||||
case 10: d[9] = s[9]; /* fall through */
|
||||
case 9: d[8] = s[8]; /* fall through */
|
||||
case 8: d[7] = s[7]; /* fall through */
|
||||
case 7: d[6] = s[6]; /* fall through */
|
||||
case 6: d[5] = s[5]; /* fall through */
|
||||
case 5: d[4] = s[4]; /* fall through */
|
||||
case 4: d[3] = s[3]; /* fall through */
|
||||
case 3: d[2] = s[2]; /* fall through */
|
||||
case 2: d[1] = s[1]; /* fall through */
|
||||
case 1: d[0] = s[0]; /* fall through */
|
||||
default: break;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
#if UNALIGNED_ACCESS_ALLOWED
|
||||
// 按8字节批量复制(适用于支持非对齐访问的平台)
|
||||
uint64_t* d64 = (uint64_t*)d;
|
||||
const uint64_t* s64 = (const uint64_t*)s;
|
||||
while (n >= 8) {
|
||||
*d64++ = *s64++;
|
||||
n -= 8;
|
||||
}
|
||||
d = (char*)d64;
|
||||
s = (const char*)s64;
|
||||
#endif
|
||||
|
||||
// 处理剩余字节
|
||||
while (n--) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void *smcc_memmove(void *dest, const void *src, usize n)
|
||||
{
|
||||
char* d = (char*)dest;
|
||||
const char* s = (const char*)src;
|
||||
|
||||
// 地址相同直接返回
|
||||
if (d == s) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
// 内存区域无重叠或前向拷贝
|
||||
if (d < s || d >= s + n) {
|
||||
return smcc_memcpy(d, s, n);
|
||||
} else {
|
||||
// 后向拷贝处理重叠情况
|
||||
d += n;
|
||||
s += n;
|
||||
while (n--) {
|
||||
*(--d) = *(--s);
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void* smcc_memset(void *s, int c, usize n) {
|
||||
unsigned char* p = (unsigned char*)s;
|
||||
unsigned char byte_val = (unsigned char)c;
|
||||
|
||||
// 快速设置小块内存
|
||||
if (n <= 16) {
|
||||
switch(n) {
|
||||
case 16: p[15] = byte_val; /* fall through */
|
||||
case 15: p[14] = byte_val; /* fall through */
|
||||
case 14: p[13] = byte_val; /* fall through */
|
||||
case 13: p[12] = byte_val; /* fall through */
|
||||
case 12: p[11] = byte_val; /* fall through */
|
||||
case 11: p[10] = byte_val; /* fall through */
|
||||
case 10: p[9] = byte_val; /* fall through */
|
||||
case 9: p[8] = byte_val; /* fall through */
|
||||
case 8: p[7] = byte_val; /* fall through */
|
||||
case 7: p[6] = byte_val; /* fall through */
|
||||
case 6: p[5] = byte_val; /* fall through */
|
||||
case 5: p[4] = byte_val; /* fall through */
|
||||
case 4: p[3] = byte_val; /* fall through */
|
||||
case 3: p[2] = byte_val; /* fall through */
|
||||
case 2: p[1] = byte_val; /* fall through */
|
||||
case 1: p[0] = byte_val; /* fall through */
|
||||
default: break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#if UNALIGNED_ACCESS_ALLOWED
|
||||
// 构造一个8字节值用于批量填充
|
||||
uint64_t fill_val = ((uint64_t)byte_val << 56) |
|
||||
((uint64_t)byte_val << 48) |
|
||||
((uint64_t)byte_val << 40) |
|
||||
((uint64_t)byte_val << 32) |
|
||||
((uint64_t)byte_val << 24) |
|
||||
((uint64_t)byte_val << 16) |
|
||||
((uint64_t)byte_val << 8) |
|
||||
(uint64_t)byte_val;
|
||||
|
||||
uint64_t* p64 = (uint64_t*)p;
|
||||
while (n >= 8) {
|
||||
*p64++ = fill_val;
|
||||
n -= 8;
|
||||
}
|
||||
p = (unsigned char*)p64;
|
||||
#endif
|
||||
|
||||
// 设置剩余字节
|
||||
while (n--) {
|
||||
*p++ = byte_val;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int smcc_memcmp(const void *s1, const void *s2, usize n) {
|
||||
const unsigned char* p1 = (const unsigned char*)s1;
|
||||
const unsigned char* p2 = (const unsigned char*)s2;
|
||||
|
||||
// 快速比较小块内存
|
||||
if (n <= 16) {
|
||||
unsigned char diff = 0;
|
||||
switch(n) {
|
||||
case 16: diff |= (p1[15] ^ p2[15]); /* fall through */
|
||||
case 15: diff |= (p1[14] ^ p2[14]); /* fall through */
|
||||
case 14: diff |= (p1[13] ^ p2[13]); /* fall through */
|
||||
case 13: diff |= (p1[12] ^ p2[12]); /* fall through */
|
||||
case 12: diff |= (p1[11] ^ p2[11]); /* fall through */
|
||||
case 11: diff |= (p1[10] ^ p2[10]); /* fall through */
|
||||
case 10: diff |= (p1[9] ^ p2[9]); /* fall through */
|
||||
case 9: diff |= (p1[8] ^ p2[8]); /* fall through */
|
||||
case 8: diff |= (p1[7] ^ p2[7]); /* fall through */
|
||||
case 7: diff |= (p1[6] ^ p2[6]); /* fall through */
|
||||
case 6: diff |= (p1[5] ^ p2[5]); /* fall through */
|
||||
case 5: diff |= (p1[4] ^ p2[4]); /* fall through */
|
||||
case 4: diff |= (p1[3] ^ p2[3]); /* fall through */
|
||||
case 3: diff |= (p1[2] ^ p2[2]); /* fall through */
|
||||
case 2: diff |= (p1[1] ^ p2[1]); /* fall through */
|
||||
case 1: diff |= (p1[0] ^ p2[0]); /* fall through */
|
||||
default: break;
|
||||
}
|
||||
// 只有当所有字节都相等时diff才为0
|
||||
if (!diff) return 0;
|
||||
|
||||
// 找到第一个不同的字节并返回差值
|
||||
size_t i = 0;
|
||||
switch(n) {
|
||||
case 16: if(p1[15] != p2[15]) {i=15;break;}
|
||||
case 15: if(p1[14] != p2[14]) {i=14;break;}
|
||||
case 14: if(p1[13] != p2[13]) {i=13;break;}
|
||||
case 13: if(p1[12] != p2[12]) {i=12;break;}
|
||||
case 12: if(p1[11] != p2[11]) {i=11;break;}
|
||||
case 11: if(p1[10] != p2[10]) {i=10;break;}
|
||||
case 10: if(p1[9] != p2[9]) {i=9;break;}
|
||||
case 9: if(p1[8] != p2[8]) {i=8;break;}
|
||||
case 8: if(p1[7] != p2[7]) {i=7;break;}
|
||||
case 7: if(p1[6] != p2[6]) {i=6;break;}
|
||||
case 6: if(p1[5] != p2[5]) {i=5;break;}
|
||||
case 5: if(p1[4] != p2[4]) {i=4;break;}
|
||||
case 4: if(p1[3] != p2[3]) {i=3;break;}
|
||||
case 3: if(p1[2] != p2[2]) {i=2;break;}
|
||||
case 2: if(p1[1] != p2[1]) {i=1;break;}
|
||||
case 1: if(p1[0] != p2[0]) {i=0;break;}
|
||||
default: break;
|
||||
}
|
||||
return p1[i] - p2[i];
|
||||
}
|
||||
|
||||
#if UNALIGNED_ACCESS_ALLOWED
|
||||
// 按8字节批量比较
|
||||
const uint64_t* p1_64 = (const uint64_t*)p1;
|
||||
const uint64_t* p2_64 = (const uint64_t*)p2;
|
||||
while (n >= 8) {
|
||||
if (*p1_64 != *p2_64) {
|
||||
// 发现不同,在8字节内定位具体位置
|
||||
const unsigned char* b1 = (const unsigned char*)p1_64;
|
||||
const unsigned char* b2 = (const unsigned char*)p2_64;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (b1[j] != b2[j])
|
||||
return b1[j] - b2[j];
|
||||
}
|
||||
}
|
||||
p1_64++;
|
||||
p2_64++;
|
||||
n -= 8;
|
||||
}
|
||||
p1 = (const unsigned char*)p1_64;
|
||||
p2 = (const unsigned char*)p2_64;
|
||||
#endif
|
||||
|
||||
// 比较剩余字节
|
||||
while (n--) {
|
||||
if (*p1 != *p2) {
|
||||
return *p1 - *p2;
|
||||
}
|
||||
p1++;
|
||||
p2++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user