format using clang-format to formate code
This commit is contained in:
@@ -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); }
|
||||
@@ -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
|
||||
|
||||
// 比较剩余字节
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user