format using clang-format to formate code
This commit is contained in:
@@ -6,8 +6,8 @@
|
|||||||
#ifndef __SMCC_CC_LEXER_H__
|
#ifndef __SMCC_CC_LEXER_H__
|
||||||
#define __SMCC_CC_LEXER_H__
|
#define __SMCC_CC_LEXER_H__
|
||||||
|
|
||||||
#include <libcore.h>
|
|
||||||
#include "lexer_token.h"
|
#include "lexer_token.h"
|
||||||
|
#include <libcore.h>
|
||||||
|
|
||||||
typedef struct lexer_loc {
|
typedef struct lexer_loc {
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ typedef enum ckeyword {
|
|||||||
} ckeyword_t;
|
} ckeyword_t;
|
||||||
|
|
||||||
// Using Binary Search To Fast Find Keyword
|
// Using Binary Search To Fast Find Keyword
|
||||||
|
/* clang-format off */
|
||||||
#define KEYWORD_TABLE \
|
#define KEYWORD_TABLE \
|
||||||
X(asm , TK_BASIC_KEYWORD , TOKEN_ASM , CEXT_ASM) \
|
X(asm , TK_BASIC_KEYWORD , TOKEN_ASM , CEXT_ASM) \
|
||||||
X(break , TK_BASIC_KEYWORD , TOKEN_BREAK , CSTD_C89) \
|
X(break , TK_BASIC_KEYWORD , TOKEN_BREAK , CSTD_C89) \
|
||||||
@@ -105,6 +106,7 @@ typedef enum ckeyword {
|
|||||||
X(char_literal , TK_BASIC_LITERAL, TOKEN_CHAR_LITERAL ) \
|
X(char_literal , TK_BASIC_LITERAL, TOKEN_CHAR_LITERAL ) \
|
||||||
X(string_literal , TK_BASIC_LITERAL, TOKEN_STRING_LITERAL ) \
|
X(string_literal , TK_BASIC_LITERAL, TOKEN_STRING_LITERAL ) \
|
||||||
// END
|
// END
|
||||||
|
/* clang-format on */
|
||||||
|
|
||||||
// 定义TokenType枚举
|
// 定义TokenType枚举
|
||||||
typedef enum cc_tktype {
|
typedef enum cc_tktype {
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ the distribution and installation instructions.
|
|||||||
Chris Fraser / cwf@aya.yale.edu
|
Chris Fraser / cwf@aya.yale.edu
|
||||||
David Hanson / drh@drhanson.net
|
David Hanson / drh@drhanson.net
|
||||||
*/
|
*/
|
||||||
#include <lexer_log.h>
|
|
||||||
#include <lexer.h>
|
#include <lexer.h>
|
||||||
|
#include <lexer_log.h>
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
@@ -54,12 +54,14 @@ static inline int keyword_cmp(const char* name, int len) {
|
|||||||
cmp = (unsigned char)name[i] - (unsigned char)key[i];
|
cmp = (unsigned char)name[i] - (unsigned char)key[i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (name[i] == '\0') break; // 遇到终止符提前结束
|
if (name[i] == '\0')
|
||||||
|
break; // 遇到终止符提前结束
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmp == 0) {
|
if (cmp == 0) {
|
||||||
// 完全匹配检查(长度相同)
|
// 完全匹配检查(长度相同)
|
||||||
if (key[len] == '\0') return mid;
|
if (key[len] == '\0')
|
||||||
|
return mid;
|
||||||
cmp = -1; // 当前关键词比输入长
|
cmp = -1; // 当前关键词比输入长
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,19 +165,31 @@ static void skip_block_comment(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
// TODO escape character not enough
|
// TODO escape character not enough
|
||||||
static inline int got_slash(int peek) {
|
static inline int got_slash(int peek) {
|
||||||
switch (peek) {
|
switch (peek) {
|
||||||
case '\\': return '\\';
|
case '\\':
|
||||||
case '\'': return '\'';
|
return '\\';
|
||||||
case '\"': return '\"';
|
case '\'':
|
||||||
case '\?': return '\?';
|
return '\'';
|
||||||
case '0': return '\0';
|
case '\"':
|
||||||
|
return '\"';
|
||||||
|
case '\?':
|
||||||
|
return '\?';
|
||||||
|
case '0':
|
||||||
|
return '\0';
|
||||||
|
|
||||||
case 'b': return '\b';
|
case 'b':
|
||||||
case 'f': return '\f';
|
return '\b';
|
||||||
case 'n': return '\n';
|
case 'f':
|
||||||
case 'r': return '\r';
|
return '\f';
|
||||||
case 't': return '\t';
|
case 'n':
|
||||||
case 'v': return '\v';
|
return '\n';
|
||||||
default: break;
|
case 'r':
|
||||||
|
return '\r';
|
||||||
|
case 't':
|
||||||
|
return '\t';
|
||||||
|
case 'v':
|
||||||
|
return '\v';
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -374,7 +388,8 @@ static void parse_line(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
ch = stream_next_char(stream);
|
ch = stream_next_char(stream);
|
||||||
lexer_next_pos(lexer);
|
lexer_next_pos(lexer);
|
||||||
if (ch != line[i]) {
|
if (ch != line[i]) {
|
||||||
LEX_WARN("Maroc does not support in lexer rather in preprocessor, it will be ignored");
|
LEX_WARN("Maroc does not support in lexer rather in preprocessor, "
|
||||||
|
"it will be ignored");
|
||||||
goto SKIP_LINE;
|
goto SKIP_LINE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -427,59 +442,120 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '=':
|
case '=':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_EQ; goto double_char;
|
case '=':
|
||||||
default: stream_reset_char(stream), type = TOKEN_ASSIGN; break;
|
type = TOKEN_EQ;
|
||||||
} break;
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_ASSIGN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '+':
|
case '+':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '+': type = TOKEN_ADD_ADD; goto double_char;
|
case '+':
|
||||||
case '=': type = TOKEN_ASSIGN_ADD; goto double_char;
|
type = TOKEN_ADD_ADD;
|
||||||
default: stream_reset_char(stream), type = TOKEN_ADD; break;
|
goto double_char;
|
||||||
} break;
|
case '=':
|
||||||
|
type = TOKEN_ASSIGN_ADD;
|
||||||
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_ADD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '-': type = TOKEN_SUB_SUB; goto double_char;
|
case '-':
|
||||||
case '=': type = TOKEN_ASSIGN_SUB; goto double_char;
|
type = TOKEN_SUB_SUB;
|
||||||
case '>': type = TOKEN_DEREF; goto double_char;
|
goto double_char;
|
||||||
default: stream_reset_char(stream), type = TOKEN_SUB; break;
|
case '=':
|
||||||
} break;
|
type = TOKEN_ASSIGN_SUB;
|
||||||
|
goto double_char;
|
||||||
|
case '>':
|
||||||
|
type = TOKEN_DEREF;
|
||||||
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_SUB;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_ASSIGN_MUL; goto double_char;
|
case '=':
|
||||||
default: stream_reset_char(stream), type = TOKEN_MUL; break;
|
type = TOKEN_ASSIGN_MUL;
|
||||||
} break;
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_MUL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_ASSIGN_DIV; goto double_char;
|
case '=':
|
||||||
case '/': skip_newline(lexer, token); goto END;
|
type = TOKEN_ASSIGN_DIV;
|
||||||
case '*': skip_block_comment(lexer, token); goto END;
|
goto double_char;
|
||||||
default: stream_reset_char(stream), type = TOKEN_DIV; break;
|
case '/':
|
||||||
} break;
|
skip_newline(lexer, token);
|
||||||
|
goto END;
|
||||||
|
case '*':
|
||||||
|
skip_block_comment(lexer, token);
|
||||||
|
goto END;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_DIV;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_ASSIGN_MOD; goto double_char;
|
case '=':
|
||||||
default: stream_reset_char(stream), type = TOKEN_MOD; break;
|
type = TOKEN_ASSIGN_MOD;
|
||||||
} break;
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_MOD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '&': type = TOKEN_AND_AND; goto double_char;
|
case '&':
|
||||||
case '=': type = TOKEN_ASSIGN_AND; goto double_char;
|
type = TOKEN_AND_AND;
|
||||||
default: stream_reset_char(stream), type = TOKEN_AND; break;
|
goto double_char;
|
||||||
} break;
|
case '=':
|
||||||
|
type = TOKEN_ASSIGN_AND;
|
||||||
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_AND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '|':
|
case '|':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '|': type = TOKEN_OR_OR; goto double_char;
|
case '|':
|
||||||
case '=': type = TOKEN_ASSIGN_OR; goto double_char;
|
type = TOKEN_OR_OR;
|
||||||
default: stream_reset_char(stream), type = TOKEN_OR; break;
|
goto double_char;
|
||||||
} break;
|
case '=':
|
||||||
|
type = TOKEN_ASSIGN_OR;
|
||||||
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_OR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '^':
|
case '^':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_ASSIGN_XOR; goto double_char;
|
case '=':
|
||||||
default: stream_reset_char(stream), type = TOKEN_XOR; break;
|
type = TOKEN_ASSIGN_XOR;
|
||||||
} break;
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_XOR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '<':
|
case '<':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_LE; goto double_char;
|
case '=':
|
||||||
|
type = TOKEN_LE;
|
||||||
|
goto double_char;
|
||||||
case '<': {
|
case '<': {
|
||||||
if (stream_peek_char(stream) == '=') {
|
if (stream_peek_char(stream) == '=') {
|
||||||
type = TOKEN_ASSIGN_L_SH;
|
type = TOKEN_ASSIGN_L_SH;
|
||||||
@@ -490,11 +566,16 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: stream_reset_char(stream), type = TOKEN_LT; break;
|
default:
|
||||||
} break;
|
stream_reset_char(stream), type = TOKEN_LT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_GE; goto double_char;
|
case '=':
|
||||||
|
type = TOKEN_GE;
|
||||||
|
goto double_char;
|
||||||
case '>': {
|
case '>': {
|
||||||
if (stream_peek_char(stream) == '=') {
|
if (stream_peek_char(stream) == '=') {
|
||||||
type = TOKEN_ASSIGN_R_SH;
|
type = TOKEN_ASSIGN_R_SH;
|
||||||
@@ -505,44 +586,69 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: stream_reset_char(stream), type = TOKEN_GT; break;
|
default:
|
||||||
} break;
|
stream_reset_char(stream), type = TOKEN_GT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '~':
|
case '~':
|
||||||
type = TOKEN_BIT_NOT; break;
|
type = TOKEN_BIT_NOT;
|
||||||
|
break;
|
||||||
case '!':
|
case '!':
|
||||||
switch (stream_peek_char(stream)) {
|
switch (stream_peek_char(stream)) {
|
||||||
case '=': type = TOKEN_NEQ; goto double_char;
|
case '=':
|
||||||
default: stream_reset_char(stream), type = TOKEN_NOT; break;
|
type = TOKEN_NEQ;
|
||||||
} break;
|
goto double_char;
|
||||||
|
default:
|
||||||
|
stream_reset_char(stream), type = TOKEN_NOT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
type = TOKEN_L_BRACKET; break;
|
type = TOKEN_L_BRACKET;
|
||||||
|
break;
|
||||||
case ']':
|
case ']':
|
||||||
type = TOKEN_R_BRACKET; break;
|
type = TOKEN_R_BRACKET;
|
||||||
|
break;
|
||||||
case '(':
|
case '(':
|
||||||
type = TOKEN_L_PAREN; break;
|
type = TOKEN_L_PAREN;
|
||||||
|
break;
|
||||||
case ')':
|
case ')':
|
||||||
type = TOKEN_R_PAREN; break;
|
type = TOKEN_R_PAREN;
|
||||||
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
type = TOKEN_L_BRACE; break;
|
type = TOKEN_L_BRACE;
|
||||||
|
break;
|
||||||
case '}':
|
case '}':
|
||||||
type = TOKEN_R_BRACE; break;
|
type = TOKEN_R_BRACE;
|
||||||
|
break;
|
||||||
case ';':
|
case ';':
|
||||||
type = TOKEN_SEMICOLON; break;
|
type = TOKEN_SEMICOLON;
|
||||||
|
break;
|
||||||
case ',':
|
case ',':
|
||||||
type = TOKEN_COMMA; break;
|
type = TOKEN_COMMA;
|
||||||
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
type = TOKEN_COLON; break;
|
type = TOKEN_COLON;
|
||||||
|
break;
|
||||||
case '.':
|
case '.':
|
||||||
if (stream_peek_char(stream) == '.' && stream_peek_char(stream) == '.') {
|
if (stream_peek_char(stream) == '.' &&
|
||||||
|
stream_peek_char(stream) == '.') {
|
||||||
type = TOKEN_ELLIPSIS;
|
type = TOKEN_ELLIPSIS;
|
||||||
goto triple_char;
|
goto triple_char;
|
||||||
}
|
}
|
||||||
type = TOKEN_DOT; break;
|
type = TOKEN_DOT;
|
||||||
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
type = TOKEN_COND; break;
|
type = TOKEN_COND;
|
||||||
case '\v': case '\r': case '\f':
|
break;
|
||||||
case ' ': case '\t':
|
case '\v':
|
||||||
type = TOKEN_BLANK; break;
|
case '\r':
|
||||||
|
case '\f':
|
||||||
|
case ' ':
|
||||||
|
case '\t':
|
||||||
|
type = TOKEN_BLANK;
|
||||||
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
// you need to flush a newline or blank
|
// you need to flush a newline or blank
|
||||||
stream_next_char(stream);
|
stream_next_char(stream);
|
||||||
@@ -565,19 +671,22 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
case '"':
|
case '"':
|
||||||
parse_string(lexer, token);
|
parse_string(lexer, token);
|
||||||
goto END;
|
goto END;
|
||||||
|
/* clang-format off */
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
case '5': case '6': case '7': case '8': case '9':
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
/* clang-format on */
|
||||||
parse_number(lexer, token);
|
parse_number(lexer, token);
|
||||||
goto END;
|
goto END;
|
||||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
/* clang-format off */
|
||||||
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
|
||||||
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
|
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
|
||||||
case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
|
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
|
||||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
case 'v': case 'w': case 'x': case 'y': case 'z':
|
||||||
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
|
||||||
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
|
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
|
||||||
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':case 'Y': case 'Z':
|
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
|
||||||
case '_':
|
case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
|
||||||
|
/* clang-format on */
|
||||||
// TOKEN_IDENT
|
// TOKEN_IDENT
|
||||||
// TODO
|
// TODO
|
||||||
// if ((ch == 'L' && ch == '\'') || (ch == 'L' && ch == '"')) {
|
// if ((ch == 'L' && ch == '\'') || (ch == 'L' && ch == '"')) {
|
||||||
@@ -596,13 +705,15 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = keyword_cmp((const char*)str.data, str.len);
|
int res = keyword_cmp((const char *)str.data, str.size - 1);
|
||||||
if (res == -1) {
|
if (res == -1) {
|
||||||
token->value.cstr.data = (char *)cstring_as_cstr(&str);
|
token->value.cstr.data = (char *)cstring_as_cstr(&str);
|
||||||
token->value.cstr.len = cstring_len(&str);
|
token->value.cstr.len = cstring_len(&str);
|
||||||
type = TOKEN_IDENT; break;
|
type = TOKEN_IDENT;
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
type = keywords[res].tok; break;
|
type = keywords[res].tok;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
LEX_ERROR("unsupport char in sourse code `%c`", ch);
|
LEX_ERROR("unsupport char in sourse code `%c`", ch);
|
||||||
@@ -631,6 +742,7 @@ void lexer_get_valid_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
|||||||
lexer_get_token(lexer, token);
|
lexer_get_token(lexer, token);
|
||||||
type = get_tok_subtype(token->type);
|
type = get_tok_subtype(token->type);
|
||||||
AssertFmt(type != TK_BASIC_INVALID, "Invalid token: `%s` at %s:%d:%d",
|
AssertFmt(type != TK_BASIC_INVALID, "Invalid token: `%s` at %s:%d:%d",
|
||||||
get_tok_name(token->type), token->loc.name, token->loc.line, token->loc.column);
|
get_tok_name(token->type), token->loc.name, token->loc.line,
|
||||||
|
token->loc.column);
|
||||||
} while (type == TK_BASIC_EMPTYSPACE || type == TK_BASIC_COMMENT);
|
} while (type == TK_BASIC_EMPTYSPACE || type == TK_BASIC_COMMENT);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,2 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {}
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <lexer.h>
|
#include <lexer.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
/// gcc -g ../lexer.c ../token.c test_lexer.c -o test_lexer
|
/// gcc -g ../lexer.c ../token.c test_lexer.c -o test_lexer
|
||||||
/*
|
/*
|
||||||
@@ -23,9 +23,7 @@ int g_num_arr[3];
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// int num = 0;
|
// int num = 0;
|
||||||
if (argc == 3 && strcmp(argv[2], "-nodebug") == 0) {
|
if (argc == 3 && strcmp(argv[2], "-nodebug") == 0) {
|
||||||
log_set_level(NULL, LOG_LEVEL_INFO
|
log_set_level(NULL, LOG_LEVEL_INFO | LOG_LEVEL_WARN | LOG_LEVEL_ERROR);
|
||||||
| LOG_LEVEL_WARN
|
|
||||||
| LOG_LEVEL_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *file_name = __FILE__;
|
const char *file_name = __FILE__;
|
||||||
@@ -62,7 +60,8 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
smcc_lexer_t lexer;
|
smcc_lexer_t lexer;
|
||||||
core_mem_stream_t mem_stream = {0};
|
core_mem_stream_t mem_stream = {0};
|
||||||
core_stream_t* stream = core_mem_stream_init(&mem_stream, buffer, fsize, false);
|
core_stream_t *stream =
|
||||||
|
core_mem_stream_init(&mem_stream, buffer, fsize, false);
|
||||||
Assert(stream != null);
|
Assert(stream != null);
|
||||||
cstring_clear(&stream->name);
|
cstring_clear(&stream->name);
|
||||||
cstring_push_cstr(&stream->name, file_name, strlen(file_name));
|
cstring_push_cstr(&stream->name, file_name, strlen(file_name));
|
||||||
@@ -74,7 +73,8 @@ int main(int argc, char* argv[]) {
|
|||||||
if (tok.type == TOKEN_EOF) {
|
if (tok.type == TOKEN_EOF) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
LOG_DEBUG("token `%s` at %s:%u:%u", get_tok_name(tok.type), tok.loc.name, tok.loc.line, tok.loc.column);
|
LOG_DEBUG("token `%s` at %s:%u:%u", get_tok_name(tok.type),
|
||||||
|
tok.loc.name, tok.loc.line, tok.loc.column);
|
||||||
Assert(tok.loc.offset <= fsize);
|
Assert(tok.loc.offset <= fsize);
|
||||||
// LOG_DEBUG("%s", tok.val.str);
|
// LOG_DEBUG("%s", tok.val.str);
|
||||||
// printf("line: %d, column: %d, type: %3d, typename: %s\n",
|
// printf("line: %d, column: %d, type: %3d, typename: %s\n",
|
||||||
|
|||||||
@@ -14,5 +14,4 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <log.h>
|
#include <log.h>
|
||||||
|
|
||||||
|
|
||||||
#endif /* __SMCC_CORE_LOG_H__ */
|
#endif /* __SMCC_CORE_LOG_H__ */
|
||||||
|
|||||||
@@ -1,33 +1,21 @@
|
|||||||
#ifndef __CORE_STR_H__
|
#ifndef __CORE_STR_H__
|
||||||
#define __CORE_STR_H__
|
#define __CORE_STR_H__
|
||||||
|
|
||||||
#include "core_type.h"
|
|
||||||
#include "core_impl.h"
|
#include "core_impl.h"
|
||||||
#include "core_log.h"
|
#include "core_log.h"
|
||||||
|
#include "core_type.h"
|
||||||
|
|
||||||
typedef struct cstring {
|
typedef struct cstring {
|
||||||
char* data;
|
usize size;
|
||||||
usize len;
|
|
||||||
usize cap;
|
usize cap;
|
||||||
|
char *data;
|
||||||
} cstring_t;
|
} cstring_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的空字符串
|
* 创建一个新的空字符串
|
||||||
*/
|
*/
|
||||||
static inline cstring_t cstring_new(void) {
|
static inline cstring_t cstring_new(void) {
|
||||||
return (cstring_t) { .data = null, .len = 0, .cap = 0 };
|
return (cstring_t){.data = null, .size = 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 };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,14 +28,15 @@ static inline cstring_t cstring_from_cstr(const char* s) {
|
|||||||
|
|
||||||
usize len = 0;
|
usize len = 0;
|
||||||
const char *p = s;
|
const char *p = s;
|
||||||
while (*p++) len++;
|
while (*p++)
|
||||||
|
len++;
|
||||||
|
|
||||||
char *data = (char *)smcc_malloc(len + 1);
|
char *data = (char *)smcc_malloc(len + 1);
|
||||||
Assert(data != null);
|
Assert(data != null);
|
||||||
smcc_memcpy(data, s, len);
|
smcc_memcpy(data, s, len);
|
||||||
data[len] = '\0';
|
data[len] = '\0';
|
||||||
|
|
||||||
return (cstring_t) { .data = data, .len = len, .cap = len };
|
return (cstring_t){.data = data, .size = len + 1, .cap = len + 1};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,7 +46,7 @@ static inline void cstring_free(cstring_t* str) {
|
|||||||
if (str && str->data && str->cap != 0) {
|
if (str && str->data && str->cap != 0) {
|
||||||
smcc_free(str->data);
|
smcc_free(str->data);
|
||||||
str->data = null;
|
str->data = null;
|
||||||
str->len = 0;
|
str->size = 0;
|
||||||
str->cap = 0;
|
str->cap = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,35 +54,38 @@ 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) {
|
if (str == null || data == null || len == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (str->cap == 0) {
|
||||||
|
str->size = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// 如果需要扩容
|
// 如果需要扩容
|
||||||
if (str->len + len + 1 > str->cap) {
|
if (str->size + len > str->cap) {
|
||||||
// FIXME c string 兼容性问题 bad practice a lot of `+ 1`
|
usize new_cap = str->cap;
|
||||||
usize new_cap = str->cap == 0 ? len + 1 : str->cap;
|
while (new_cap < str->size + len) {
|
||||||
while (new_cap < str->len + len + 1) {
|
|
||||||
new_cap *= 2;
|
new_cap *= 2;
|
||||||
if (new_cap == 0) { // 处理溢出情况
|
// FIXME write by AI 处理溢出情况
|
||||||
new_cap = str->len + len + 1;
|
if (new_cap == 0) {
|
||||||
|
new_cap = str->size + len;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char* new_data = str->data ?
|
char *new_data = (char *)smcc_realloc(str->data, new_cap);
|
||||||
(char*)smcc_realloc(str->data, new_cap) :
|
|
||||||
(char*)smcc_malloc(new_cap);
|
|
||||||
Assert(new_data != null);
|
Assert(new_data != null);
|
||||||
|
|
||||||
str->data = new_data;
|
str->data = new_data;
|
||||||
str->cap = new_cap;
|
str->cap = new_cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
smcc_memcpy(str->data + str->len, data, len);
|
smcc_memcpy(str->data + str->size - 1, data, len);
|
||||||
str->len += len;
|
str->size += len;
|
||||||
str->data[str->len] = '\0'; // 保证 C 字符串兼容性
|
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,14 +99,14 @@ static inline void cstring_push(cstring_t* str, char ch) {
|
|||||||
* 获取字符串长度
|
* 获取字符串长度
|
||||||
*/
|
*/
|
||||||
static inline usize cstring_len(const cstring_t *str) {
|
static inline usize cstring_len(const cstring_t *str) {
|
||||||
return str ? str->len : 0;
|
return str ? str->size - 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查字符串是否为空
|
* 检查字符串是否为空
|
||||||
*/
|
*/
|
||||||
static inline cbool cstring_is_empty(const cstring_t *str) {
|
static inline cbool cstring_is_empty(const cstring_t *str) {
|
||||||
return str == null || str->len == 0;
|
return str == null || str->size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -122,7 +114,7 @@ static inline cbool cstring_is_empty(const cstring_t* str) {
|
|||||||
*/
|
*/
|
||||||
static inline void cstring_clear(cstring_t *str) {
|
static inline void cstring_clear(cstring_t *str) {
|
||||||
if (str) {
|
if (str) {
|
||||||
str->len = 0;
|
str->size = 1;
|
||||||
if (str->data) {
|
if (str->data) {
|
||||||
str->data[0] = '\0';
|
str->data[0] = '\0';
|
||||||
}
|
}
|
||||||
@@ -132,7 +124,7 @@ static inline void cstring_clear(cstring_t* str) {
|
|||||||
/**
|
/**
|
||||||
* 获取 C 风格字符串
|
* 获取 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) {
|
if (str == null || str->data == null) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
#define __SMCC_CORE_STREAM_H__
|
#define __SMCC_CORE_STREAM_H__
|
||||||
|
|
||||||
#include "core_impl.h"
|
#include "core_impl.h"
|
||||||
|
#include "core_macro.h"
|
||||||
#include "core_mem.h"
|
#include "core_mem.h"
|
||||||
#include "core_str.h"
|
#include "core_str.h"
|
||||||
#include "core_macro.h"
|
|
||||||
|
|
||||||
typedef struct core_stream core_stream_t;
|
typedef struct core_stream core_stream_t;
|
||||||
|
|
||||||
@@ -29,7 +29,8 @@ struct core_stream {
|
|||||||
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);
|
return self->read_buf(self, buffer, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +59,8 @@ typedef struct core_mem_stream {
|
|||||||
usize peek_pos;
|
usize peek_pos;
|
||||||
cbool owned;
|
cbool owned;
|
||||||
} core_mem_stream_t;
|
} 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
|
||||||
|
|
||||||
|
|
||||||
#endif /* __SMCC_CORE_STREAM_H__ */
|
#endif /* __SMCC_CORE_STREAM_H__ */
|
||||||
|
|||||||
@@ -2,11 +2,12 @@
|
|||||||
#define __SMCC_CORE_TYPE_H__
|
#define __SMCC_CORE_TYPE_H__
|
||||||
|
|
||||||
#ifndef __SMCC_BUILTIN_TYPE__
|
#ifndef __SMCC_BUILTIN_TYPE__
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* clang-format off */
|
||||||
typedef int8_t i8;
|
typedef int8_t i8;
|
||||||
typedef int16_t i16;
|
typedef int16_t i16;
|
||||||
typedef int32_t i32;
|
typedef int32_t i32;
|
||||||
@@ -27,6 +28,7 @@ typedef bool cbool;
|
|||||||
/// void / null
|
/// void / null
|
||||||
#define null NULL
|
#define null NULL
|
||||||
|
|
||||||
|
/* clang-format on */
|
||||||
static_assert(sizeof(cbool) == 1, "cbool size must 1");
|
static_assert(sizeof(cbool) == 1, "cbool size must 1");
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|||||||
115
runtime/libcore/include/core_vec.h
Normal file
115
runtime/libcore/include/core_vec.h
Normal 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__
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef __SMCC_CORE_H__
|
#ifndef __SMCC_CORE_H__
|
||||||
#define __SMCC_CORE_H__
|
#define __SMCC_CORE_H__
|
||||||
|
|
||||||
#include <core_mem.h>
|
|
||||||
#include <core_impl.h>
|
#include <core_impl.h>
|
||||||
#include <core_macro.h>
|
#include <core_macro.h>
|
||||||
|
#include <core_mem.h>
|
||||||
|
|
||||||
#define __SMCC_LOG_NO_STD_IMPL__
|
#define __SMCC_LOG_NO_STD_IMPL__
|
||||||
#define log_snprintf smcc_snprintf
|
#define log_snprintf smcc_snprintf
|
||||||
@@ -17,5 +17,6 @@
|
|||||||
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||||
#include <core_str.h>
|
#include <core_str.h>
|
||||||
#include <core_stream.h>
|
#include <core_stream.h>
|
||||||
|
#include <core_vec.h>
|
||||||
|
|
||||||
#endif // __SMCC_CORE_H__
|
#endif // __SMCC_CORE_H__
|
||||||
|
|||||||
@@ -9,37 +9,33 @@
|
|||||||
#define log_exit smcc_exit
|
#define log_exit smcc_exit
|
||||||
#include <log.h>
|
#include <log.h>
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* ====== 内存管理核心接口实现 ====== */
|
/* ====== 内存管理核心接口实现 ====== */
|
||||||
|
|
||||||
void* smcc_malloc(usize size) {
|
void *smcc_malloc(usize size) { return malloc(size); }
|
||||||
return malloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void* smcc_calloc(usize count, usize size) {
|
void *smcc_calloc(usize count, usize size) { return calloc(count, size); }
|
||||||
return calloc(count, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void* smcc_realloc(void *ptr, usize new_size) {
|
void *smcc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); }
|
||||||
return realloc(ptr, new_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void smcc_free(void *ptr) {
|
void smcc_free(void *ptr) { free(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) {
|
switch (mode) {
|
||||||
case SMCC_FILE_READ: return "rb";
|
case SMCC_FILE_READ:
|
||||||
case SMCC_FILE_WRITE: return "wb";
|
return "rb";
|
||||||
case SMCC_FILE_APPEND: return "ab";
|
case SMCC_FILE_WRITE:
|
||||||
default: return "rb";
|
return "wb";
|
||||||
|
case SMCC_FILE_APPEND:
|
||||||
|
return "ab";
|
||||||
|
default:
|
||||||
|
return "rb";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,18 +51,21 @@ void smcc_fclose(smcc_file_t file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
|
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
|
||||||
if (!file || !buffer) return 0;
|
if (!file || !buffer)
|
||||||
|
return 0;
|
||||||
return fread(buffer, 1, size, (FILE *)file);
|
return fread(buffer, 1, size, (FILE *)file);
|
||||||
}
|
}
|
||||||
|
|
||||||
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size) {
|
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size) {
|
||||||
if (!file || !buffer) return 0;
|
if (!file || !buffer)
|
||||||
|
return 0;
|
||||||
return fwrite(buffer, 1, size, (FILE *)file);
|
return fwrite(buffer, 1, size, (FILE *)file);
|
||||||
}
|
}
|
||||||
|
|
||||||
cbool smcc_fexists(const char *path) {
|
cbool smcc_fexists(const char *path) {
|
||||||
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
|
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
|
||||||
if (!fp) return false;
|
if (!fp)
|
||||||
|
return false;
|
||||||
smcc_fclose(fp);
|
smcc_fclose(fp);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -96,6 +95,4 @@ void smcc_eprintf(const char *format, ...) {
|
|||||||
|
|
||||||
/* ====== 系统核心接口实现 ====== */
|
/* ====== 系统核心接口实现 ====== */
|
||||||
|
|
||||||
void smcc_exit(int code) {
|
void smcc_exit(int code) { exit(code); }
|
||||||
exit(code);
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
// 判断是否支持非对齐访问(x86/x64 支持)
|
// 判断是否支持非对齐访问(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
|
#define UNALIGNED_ACCESS_ALLOWED 1
|
||||||
#else
|
#else
|
||||||
#define UNALIGNED_ACCESS_ALLOWED 0
|
#define UNALIGNED_ACCESS_ALLOWED 0
|
||||||
@@ -15,23 +16,40 @@ void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
|
|||||||
// 快速路径:小内存拷贝
|
// 快速路径:小内存拷贝
|
||||||
if (n <= 16) {
|
if (n <= 16) {
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 16: d[15] = s[15]; /* fall through */
|
case 16:
|
||||||
case 15: d[14] = s[14]; /* fall through */
|
d[15] = s[15]; /* fall through */
|
||||||
case 14: d[13] = s[13]; /* fall through */
|
case 15:
|
||||||
case 13: d[12] = s[12]; /* fall through */
|
d[14] = s[14]; /* fall through */
|
||||||
case 12: d[11] = s[11]; /* fall through */
|
case 14:
|
||||||
case 11: d[10] = s[10]; /* fall through */
|
d[13] = s[13]; /* fall through */
|
||||||
case 10: d[9] = s[9]; /* fall through */
|
case 13:
|
||||||
case 9: d[8] = s[8]; /* fall through */
|
d[12] = s[12]; /* fall through */
|
||||||
case 8: d[7] = s[7]; /* fall through */
|
case 12:
|
||||||
case 7: d[6] = s[6]; /* fall through */
|
d[11] = s[11]; /* fall through */
|
||||||
case 6: d[5] = s[5]; /* fall through */
|
case 11:
|
||||||
case 5: d[4] = s[4]; /* fall through */
|
d[10] = s[10]; /* fall through */
|
||||||
case 4: d[3] = s[3]; /* fall through */
|
case 10:
|
||||||
case 3: d[2] = s[2]; /* fall through */
|
d[9] = s[9]; /* fall through */
|
||||||
case 2: d[1] = s[1]; /* fall through */
|
case 9:
|
||||||
case 1: d[0] = s[0]; /* fall through */
|
d[8] = s[8]; /* fall through */
|
||||||
default: break;
|
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;
|
return dest;
|
||||||
}
|
}
|
||||||
@@ -56,8 +74,7 @@ void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
|
|||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *smcc_memmove(void *dest, const void *src, usize n)
|
void *smcc_memmove(void *dest, const void *src, usize n) {
|
||||||
{
|
|
||||||
char *d = (char *)dest;
|
char *d = (char *)dest;
|
||||||
const char *s = (const char *)src;
|
const char *s = (const char *)src;
|
||||||
|
|
||||||
@@ -88,37 +105,51 @@ void* smcc_memset(void *s, int c, usize n) {
|
|||||||
// 快速设置小块内存
|
// 快速设置小块内存
|
||||||
if (n <= 16) {
|
if (n <= 16) {
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 16: p[15] = byte_val; /* fall through */
|
case 16:
|
||||||
case 15: p[14] = byte_val; /* fall through */
|
p[15] = byte_val; /* fall through */
|
||||||
case 14: p[13] = byte_val; /* fall through */
|
case 15:
|
||||||
case 13: p[12] = byte_val; /* fall through */
|
p[14] = byte_val; /* fall through */
|
||||||
case 12: p[11] = byte_val; /* fall through */
|
case 14:
|
||||||
case 11: p[10] = byte_val; /* fall through */
|
p[13] = byte_val; /* fall through */
|
||||||
case 10: p[9] = byte_val; /* fall through */
|
case 13:
|
||||||
case 9: p[8] = byte_val; /* fall through */
|
p[12] = byte_val; /* fall through */
|
||||||
case 8: p[7] = byte_val; /* fall through */
|
case 12:
|
||||||
case 7: p[6] = byte_val; /* fall through */
|
p[11] = byte_val; /* fall through */
|
||||||
case 6: p[5] = byte_val; /* fall through */
|
case 11:
|
||||||
case 5: p[4] = byte_val; /* fall through */
|
p[10] = byte_val; /* fall through */
|
||||||
case 4: p[3] = byte_val; /* fall through */
|
case 10:
|
||||||
case 3: p[2] = byte_val; /* fall through */
|
p[9] = byte_val; /* fall through */
|
||||||
case 2: p[1] = byte_val; /* fall through */
|
case 9:
|
||||||
case 1: p[0] = byte_val; /* fall through */
|
p[8] = byte_val; /* fall through */
|
||||||
default: break;
|
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;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNALIGNED_ACCESS_ALLOWED
|
#if UNALIGNED_ACCESS_ALLOWED
|
||||||
// 构造一个8字节值用于批量填充
|
// 构造一个8字节值用于批量填充
|
||||||
uint64_t fill_val = ((uint64_t)byte_val << 56) |
|
uint64_t fill_val =
|
||||||
((uint64_t)byte_val << 48) |
|
((uint64_t)byte_val << 56) | ((uint64_t)byte_val << 48) |
|
||||||
((uint64_t)byte_val << 40) |
|
((uint64_t)byte_val << 40) | ((uint64_t)byte_val << 32) |
|
||||||
((uint64_t)byte_val << 32) |
|
((uint64_t)byte_val << 24) | ((uint64_t)byte_val << 16) |
|
||||||
((uint64_t)byte_val << 24) |
|
((uint64_t)byte_val << 8) | (uint64_t)byte_val;
|
||||||
((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) {
|
while (n >= 8) {
|
||||||
@@ -144,47 +175,130 @@ int smcc_memcmp(const void *s1, const void *s2, usize n) {
|
|||||||
if (n <= 16) {
|
if (n <= 16) {
|
||||||
unsigned char diff = 0;
|
unsigned char diff = 0;
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 16: diff |= (p1[15] ^ p2[15]); /* fall through */
|
case 16:
|
||||||
case 15: diff |= (p1[14] ^ p2[14]); /* fall through */
|
diff |= (p1[15] ^ p2[15]); /* fall through */
|
||||||
case 14: diff |= (p1[13] ^ p2[13]); /* fall through */
|
case 15:
|
||||||
case 13: diff |= (p1[12] ^ p2[12]); /* fall through */
|
diff |= (p1[14] ^ p2[14]); /* fall through */
|
||||||
case 12: diff |= (p1[11] ^ p2[11]); /* fall through */
|
case 14:
|
||||||
case 11: diff |= (p1[10] ^ p2[10]); /* fall through */
|
diff |= (p1[13] ^ p2[13]); /* fall through */
|
||||||
case 10: diff |= (p1[9] ^ p2[9]); /* fall through */
|
case 13:
|
||||||
case 9: diff |= (p1[8] ^ p2[8]); /* fall through */
|
diff |= (p1[12] ^ p2[12]); /* fall through */
|
||||||
case 8: diff |= (p1[7] ^ p2[7]); /* fall through */
|
case 12:
|
||||||
case 7: diff |= (p1[6] ^ p2[6]); /* fall through */
|
diff |= (p1[11] ^ p2[11]); /* fall through */
|
||||||
case 6: diff |= (p1[5] ^ p2[5]); /* fall through */
|
case 11:
|
||||||
case 5: diff |= (p1[4] ^ p2[4]); /* fall through */
|
diff |= (p1[10] ^ p2[10]); /* fall through */
|
||||||
case 4: diff |= (p1[3] ^ p2[3]); /* fall through */
|
case 10:
|
||||||
case 3: diff |= (p1[2] ^ p2[2]); /* fall through */
|
diff |= (p1[9] ^ p2[9]); /* fall through */
|
||||||
case 2: diff |= (p1[1] ^ p2[1]); /* fall through */
|
case 9:
|
||||||
case 1: diff |= (p1[0] ^ p2[0]); /* fall through */
|
diff |= (p1[8] ^ p2[8]); /* fall through */
|
||||||
default: break;
|
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
|
// 只有当所有字节都相等时diff才为0
|
||||||
if (!diff) return 0;
|
if (!diff)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// 找到第一个不同的字节并返回差值
|
// 找到第一个不同的字节并返回差值
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
switch (n) {
|
switch (n) {
|
||||||
case 16: if(p1[15] != p2[15]) {i=15;break;}
|
case 16:
|
||||||
case 15: if(p1[14] != p2[14]) {i=14;break;}
|
if (p1[15] != p2[15]) {
|
||||||
case 14: if(p1[13] != p2[13]) {i=13;break;}
|
i = 15;
|
||||||
case 13: if(p1[12] != p2[12]) {i=12;break;}
|
break;
|
||||||
case 12: if(p1[11] != p2[11]) {i=11;break;}
|
}
|
||||||
case 11: if(p1[10] != p2[10]) {i=10;break;}
|
case 15:
|
||||||
case 10: if(p1[9] != p2[9]) {i=9;break;}
|
if (p1[14] != p2[14]) {
|
||||||
case 9: if(p1[8] != p2[8]) {i=8;break;}
|
i = 14;
|
||||||
case 8: if(p1[7] != p2[7]) {i=7;break;}
|
break;
|
||||||
case 7: if(p1[6] != p2[6]) {i=6;break;}
|
}
|
||||||
case 6: if(p1[5] != p2[5]) {i=5;break;}
|
case 14:
|
||||||
case 5: if(p1[4] != p2[4]) {i=4;break;}
|
if (p1[13] != p2[13]) {
|
||||||
case 4: if(p1[3] != p2[3]) {i=3;break;}
|
i = 13;
|
||||||
case 3: if(p1[2] != p2[2]) {i=2;break;}
|
break;
|
||||||
case 2: if(p1[1] != p2[1]) {i=1;break;}
|
}
|
||||||
case 1: if(p1[0] != p2[0]) {i=0;break;}
|
case 13:
|
||||||
default: break;
|
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];
|
return p1[i] - p2[i];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ static usize read_buf(core_stream_t* _stream, char* buffer, usize count) {
|
|||||||
smcc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
|
smcc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
|
||||||
stream->curr_pos += to_read;
|
stream->curr_pos += to_read;
|
||||||
} else {
|
} 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;
|
return to_read;
|
||||||
@@ -66,7 +67,8 @@ static void free_stream(core_stream_t* _stream) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
if (stream == null || data == NULL || length == 0) {
|
||||||
LOG_ERROR("param error");
|
LOG_ERROR("param error");
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#define __SMCC_HASHTABLE_H__
|
#define __SMCC_HASHTABLE_H__
|
||||||
|
|
||||||
#include <libcore.h>
|
#include <libcore.h>
|
||||||
#include "vector.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @enum ht_entry_state_t
|
* @enum ht_entry_state_t
|
||||||
@@ -110,7 +109,8 @@ void hashtable_destory(hash_table_t* ht);
|
|||||||
* @param context 用户上下文指针
|
* @param context 用户上下文指针
|
||||||
* @return 返回非0停止迭代
|
* @return 返回非0停止迭代
|
||||||
*/
|
*/
|
||||||
typedef int (*hash_table_iter_func)(const void* key, void* value, void* context);
|
typedef int (*hash_table_iter_func)(const void *key, void *value,
|
||||||
|
void *context);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 遍历哈希表所有有效条目
|
* @brief 遍历哈希表所有有效条目
|
||||||
@@ -118,6 +118,7 @@ typedef int (*hash_table_iter_func)(const void* key, void* value, void* context)
|
|||||||
* @param iter_func 迭代回调函数
|
* @param iter_func 迭代回调函数
|
||||||
* @param context 用户上下文指针
|
* @param context 用户上下文指针
|
||||||
*/
|
*/
|
||||||
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context);
|
void hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
|
||||||
|
void *context);
|
||||||
|
|
||||||
#endif // __SMCC_HASHTABLE_H__
|
#endif // __SMCC_HASHTABLE_H__
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
* @link https://njusecourse.feishu.cn/wiki/I8vkw2zkwiEInUkujTJc7zzOnwf
|
* @link https://njusecourse.feishu.cn/wiki/I8vkw2zkwiEInUkujTJc7zzOnwf
|
||||||
* @link https://kernelnewlbies.org/FAQ/LinkedLists
|
* @link https://kernelnewlbies.org/FAQ/LinkedLists
|
||||||
* @link https://lwn.net/Articles/887097/
|
* @link https://lwn.net/Articles/887097/
|
||||||
* @link https://liuluheng.github.io/wiki/public_html/Embedded-System/kernel/list-and-hlist.html
|
* @link
|
||||||
|
* https://liuluheng.github.io/wiki/public_html/Embedded-System/kernel/list-and-hlist.html
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __KLLIST_H__
|
#ifndef __KLLIST_H__
|
||||||
@@ -18,13 +19,17 @@
|
|||||||
// Magic: https://radek.io/posts/magical-container_of-macro/
|
// Magic: https://radek.io/posts/magical-container_of-macro/
|
||||||
// StackOverflow: https://stackoverflow.com/q/15832301/1833118
|
// StackOverflow: https://stackoverflow.com/q/15832301/1833118
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#define container_of(ptr, type, member) ({ \
|
#define container_of(ptr, type, member) \
|
||||||
|
({ \
|
||||||
const typeof(((type *)0)->member) *__mptr = (ptr); \
|
const typeof(((type *)0)->member) *__mptr = (ptr); \
|
||||||
(type *)( (char *)__mptr - offsetof(type,member) );})
|
(type *)((char *)__mptr - offsetof(type, member)); \
|
||||||
|
})
|
||||||
#else
|
#else
|
||||||
#define container_of(ptr, type, member) ({ \
|
#define container_of(ptr, type, member) \
|
||||||
|
({ \
|
||||||
const void *__mptr = (ptr); \
|
const void *__mptr = (ptr); \
|
||||||
(type *)( (char *)__mptr - offsetof(type,member) );})
|
(type *)((char *)__mptr - offsetof(type, member)); \
|
||||||
|
})
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -40,7 +45,8 @@ struct list_head {
|
|||||||
* @example
|
* @example
|
||||||
* 1. struct list_head your_list = LIST_HEAD_INIT(your_list);
|
* 1. struct list_head your_list = LIST_HEAD_INIT(your_list);
|
||||||
* 2. struct list_head your_list; INIT_LIST_HEAD(&your_list);
|
* 2. struct list_head your_list; INIT_LIST_HEAD(&your_list);
|
||||||
* 3. LIST_HEAD(your_list); => struct your_list = { &(your_list), &(your_list) };
|
* 3. LIST_HEAD(your_list); => struct your_list = { &(your_list), &(your_list)
|
||||||
|
* };
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define LIST_HEAD_INIT(name) {&(name), &(name)}
|
#define LIST_HEAD_INIT(name) {&(name), &(name)}
|
||||||
@@ -48,15 +54,13 @@ static inline void INIT_LIST_HEAD(struct list_head *list) {
|
|||||||
list->next = list;
|
list->next = list;
|
||||||
list->prev = list;
|
list->prev = list;
|
||||||
}
|
}
|
||||||
#define LIST_HEAD(name) \
|
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
|
||||||
struct list_head name = LIST_HEAD_INIT(name)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* list add
|
* list add
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static inline void __list_add(struct list_head *newl,
|
static inline void __list_add(struct list_head *newl, struct list_head *prev,
|
||||||
struct list_head *prev,
|
|
||||||
struct list_head *next) {
|
struct list_head *next) {
|
||||||
next->prev = newl;
|
next->prev = newl;
|
||||||
newl->next = next;
|
newl->next = next;
|
||||||
@@ -68,7 +72,8 @@ static inline void list_add(struct list_head *newl, struct list_head *head) {
|
|||||||
__list_add(newl, head, head->next);
|
__list_add(newl, head, head->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void list_add_tail(struct list_head *newl, struct list_head *head) {
|
static inline void list_add_tail(struct list_head *newl,
|
||||||
|
struct list_head *head) {
|
||||||
__list_add(newl, head->prev, head);
|
__list_add(newl, head->prev, head);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +97,8 @@ static inline void list_del(struct list_head *entry) {
|
|||||||
* @list: the entry to test
|
* @list: the entry to test
|
||||||
* @head: the head of the list
|
* @head: the head of the list
|
||||||
*/
|
*/
|
||||||
static inline int list_is_first(const struct list_head *list, const struct list_head *head) {
|
static inline int list_is_first(const struct list_head *list,
|
||||||
|
const struct list_head *head) {
|
||||||
return list->prev == head;
|
return list->prev == head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +107,8 @@ static inline int list_is_first(const struct list_head *list, const struct list_
|
|||||||
* @list: the entry to test
|
* @list: the entry to test
|
||||||
* @head: the head of the list
|
* @head: the head of the list
|
||||||
*/
|
*/
|
||||||
static inline int list_is_last(const struct list_head *list, const struct list_head *head) {
|
static inline int list_is_last(const struct list_head *list,
|
||||||
|
const struct list_head *head) {
|
||||||
return list->next == head;
|
return list->next == head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +117,8 @@ static inline int list_is_last(const struct list_head *list, const struct list_h
|
|||||||
* @list: the entry to test
|
* @list: the entry to test
|
||||||
* @head: the head of the list
|
* @head: the head of the list
|
||||||
*/
|
*/
|
||||||
static inline int list_is_head(const struct list_head *list, const struct list_head *head) {
|
static inline int list_is_head(const struct list_head *list,
|
||||||
|
const struct list_head *head) {
|
||||||
return list == head;
|
return list == head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,8 +154,8 @@ static inline int list_empty(const struct list_head *head) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_KLIST_SORT
|
#ifdef HAVE_KLIST_SORT
|
||||||
typedef int (*list_cmp_func_t)(void *,
|
typedef int (*list_cmp_func_t)(void *, const struct list_head *,
|
||||||
const struct list_head *, const struct list_head *);
|
const struct list_head *);
|
||||||
static void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
|
static void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
#ifndef __SMCC_UTILS_H__
|
#ifndef __SMCC_UTILS_H__
|
||||||
#define __SMCC_UTILS_H__
|
#define __SMCC_UTILS_H__
|
||||||
|
|
||||||
#include <libcore.h>
|
|
||||||
#include "vector.h"
|
|
||||||
#include "kllist.h"
|
|
||||||
#include "hashtable.h"
|
#include "hashtable.h"
|
||||||
|
#include "kllist.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "strpool.h"
|
#include "strpool.h"
|
||||||
|
#include <libcore.h>
|
||||||
|
|
||||||
#endif // __SMCC_UTILS_H__
|
#endif // __SMCC_UTILS_H__
|
||||||
|
|||||||
@@ -8,9 +8,9 @@
|
|||||||
#ifndef __SMCC_STRPOOL_H__
|
#ifndef __SMCC_STRPOOL_H__
|
||||||
#define __SMCC_STRPOOL_H__
|
#define __SMCC_STRPOOL_H__
|
||||||
|
|
||||||
#include <libcore.h>
|
|
||||||
#include "hashtable.h"
|
#include "hashtable.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include <libcore.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @struct strpool_t
|
* @struct strpool_t
|
||||||
|
|||||||
@@ -1,119 +0,0 @@
|
|||||||
/**
|
|
||||||
* @file vector.h
|
|
||||||
* @brief 动态数组(Vector)实现
|
|
||||||
*
|
|
||||||
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __SMCC_DS_VECTOR_H__
|
|
||||||
#define __SMCC_DS_VECTOR_H__
|
|
||||||
|
|
||||||
#include <libcore.h>
|
|
||||||
|
|
||||||
#define __vec_realloc smcc_realloc
|
|
||||||
#define __vec_free smcc_free
|
|
||||||
|
|
||||||
/** @defgroup vector_struct 数据结构定义 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def VECTOR_HEADER(name, type)
|
|
||||||
* @brief 声明向量结构体
|
|
||||||
* @param name 结构体变量名
|
|
||||||
* @param type 存储的数据类型
|
|
||||||
*
|
|
||||||
* 生成包含size/cap/data三个字段的结构体定义:
|
|
||||||
* - size: 当前元素数量
|
|
||||||
* - cap: 数组容量
|
|
||||||
* - data: 存储数组指针
|
|
||||||
*/
|
|
||||||
#define VECTOR_HEADER(name, type) \
|
|
||||||
struct { \
|
|
||||||
isize size; /**< 当前元素数量 */ \
|
|
||||||
isize cap; /**< 数组容量 */ \
|
|
||||||
type *data; /**< 数据存储指针 */ \
|
|
||||||
} name
|
|
||||||
|
|
||||||
/** @defgroup vector_operations 向量操作宏 */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_init(vec)
|
|
||||||
* @brief 初始化向量结构体
|
|
||||||
* @param vec 要初始化的向量结构体变量
|
|
||||||
*
|
|
||||||
* @note 此宏不会分配内存,仅做零初始化
|
|
||||||
*/
|
|
||||||
#define vector_init(vec) \
|
|
||||||
do { \
|
|
||||||
(vec).size = 0, \
|
|
||||||
(vec).cap = 0, \
|
|
||||||
(vec).data = 0; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_push(vec, value)
|
|
||||||
* @brief 添加元素到向量末尾
|
|
||||||
* @param vec 目标向量结构体
|
|
||||||
* @param value 要添加的值(需匹配存储类型)
|
|
||||||
*
|
|
||||||
* @note 当容量不足时自动扩容为2倍(初始容量为8)
|
|
||||||
* @warning 内存分配失败时会触发LOG_FATAL
|
|
||||||
*/
|
|
||||||
#define vector_push(vec, value) \
|
|
||||||
do { \
|
|
||||||
if (vec.size >= vec.cap) { \
|
|
||||||
int cap = vec.cap ? vec.cap * 2 : 8; \
|
|
||||||
void* data = __vec_realloc(vec.data, cap * sizeof(*vec.data)); \
|
|
||||||
if (!data) { \
|
|
||||||
LOG_FATAL("vector_push: rt_realloc failed\n"); \
|
|
||||||
} \
|
|
||||||
(vec).cap = cap; \
|
|
||||||
(vec).data = data; \
|
|
||||||
} \
|
|
||||||
(vec).data[(vec).size++] = value; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_pop(vec)
|
|
||||||
* @brief 弹出最后一个元素
|
|
||||||
* @param vec 目标向量结构体
|
|
||||||
* @return 最后元素的引用
|
|
||||||
* @warning 需确保size > 0时使用
|
|
||||||
*/
|
|
||||||
#define vector_pop(vec) \
|
|
||||||
((vec).data[--(vec).size])
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_at(vec, idx)
|
|
||||||
* @brief 获取指定索引元素
|
|
||||||
* @param vec 目标向量结构体
|
|
||||||
* @param idx 元素索引(0 <= idx < size)
|
|
||||||
* @return 对应元素的引用
|
|
||||||
*/
|
|
||||||
#define vector_at(vec, idx) \
|
|
||||||
(((vec).data)[idx])
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_idx(vec, ptr)
|
|
||||||
* @brief 获取元素指针对应的索引
|
|
||||||
* @param vec 目标向量结构体
|
|
||||||
* @param ptr 元素指针(需在data数组范围内)
|
|
||||||
* @return 元素索引值
|
|
||||||
*/
|
|
||||||
#define vector_idx(vec, ptr) \
|
|
||||||
((ptr) - (vec).data)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @def vector_free(vec)
|
|
||||||
* @brief 释放向量内存
|
|
||||||
* @param vec 目标向量结构体
|
|
||||||
*
|
|
||||||
* @note 释放后需重新初始化才能再次使用
|
|
||||||
*/
|
|
||||||
#define vector_free(vec) \
|
|
||||||
do { \
|
|
||||||
__vec_free((vec).data); \
|
|
||||||
(vec).data = NULL; \
|
|
||||||
(vec).size = (vec).cap = 0; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#endif // __SMCC_DS_VECTOR_H__
|
|
||||||
@@ -20,7 +20,8 @@ static int next_power_of_two(int n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static hash_entry_t *find_entry(hash_table_t *ht, const void *key, u32 hash) {
|
static hash_entry_t *find_entry(hash_table_t *ht, const void *key, u32 hash) {
|
||||||
if (ht->entries.cap == 0) return NULL;
|
if (ht->entries.cap == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
|
u32 index = hash & (ht->entries.cap - 1); // 容量是2的幂
|
||||||
u32 probe = 0;
|
u32 probe = 0;
|
||||||
@@ -34,7 +35,8 @@ static hash_entry_t* find_entry(hash_table_t* ht, const void* key, u32 hash) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (entry->state == ENTRY_TOMBSTONE) {
|
if (entry->state == ENTRY_TOMBSTONE) {
|
||||||
if (!tombstone) tombstone = entry;
|
if (!tombstone)
|
||||||
|
tombstone = entry;
|
||||||
} else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) {
|
} else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) {
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
@@ -42,7 +44,8 @@ static hash_entry_t* find_entry(hash_table_t* ht, const void* key, u32 hash) {
|
|||||||
// Liner finding
|
// Liner finding
|
||||||
index = (index + 1) & (ht->entries.cap - 1);
|
index = (index + 1) & (ht->entries.cap - 1);
|
||||||
probe++;
|
probe++;
|
||||||
if (probe >= ht->entries.cap) break;
|
if (probe >= ht->entries.cap)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
LOG_ERROR("hashset_find: hash table is full");
|
LOG_ERROR("hashset_find: hash table is full");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -77,7 +80,9 @@ static void adjust_capacity(hash_table_t* ht, int new_cap) {
|
|||||||
|
|
||||||
void *hashtable_set(hash_table_t *ht, const void *key, void *value) {
|
void *hashtable_set(hash_table_t *ht, const void *key, void *value) {
|
||||||
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
|
if (ht->count + ht->tombstone_count >= ht->entries.cap * 0.75) {
|
||||||
int new_cap = ht->entries.cap < INIT_HASH_TABLE_SIZE ? INIT_HASH_TABLE_SIZE : ht->entries.cap * 2;
|
int new_cap = ht->entries.cap < INIT_HASH_TABLE_SIZE
|
||||||
|
? INIT_HASH_TABLE_SIZE
|
||||||
|
: ht->entries.cap * 2;
|
||||||
adjust_capacity(ht, new_cap);
|
adjust_capacity(ht, new_cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +93,8 @@ void* hashtable_set(hash_table_t* ht, const void* key, void* value) {
|
|||||||
if (entry->state == ENTRY_ACTIVE) {
|
if (entry->state == ENTRY_ACTIVE) {
|
||||||
old_value = entry->value;
|
old_value = entry->value;
|
||||||
} else {
|
} else {
|
||||||
if (entry->state == ENTRY_TOMBSTONE) ht->tombstone_count--;
|
if (entry->state == ENTRY_TOMBSTONE)
|
||||||
|
ht->tombstone_count--;
|
||||||
ht->count++;
|
ht->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +106,8 @@ void* hashtable_set(hash_table_t* ht, const void* key, void* value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *hashtable_get(hash_table_t *ht, const void *key) {
|
void *hashtable_get(hash_table_t *ht, const void *key) {
|
||||||
if (ht->entries.cap == 0) return NULL;
|
if (ht->entries.cap == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
u32 hash = ht->hash_func(key);
|
u32 hash = ht->hash_func(key);
|
||||||
hash_entry_t *entry = find_entry(ht, key, hash);
|
hash_entry_t *entry = find_entry(ht, key, hash);
|
||||||
@@ -108,12 +115,14 @@ void* hashtable_get(hash_table_t* ht, const void* key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *hashtable_del(hash_table_t *ht, const void *key) {
|
void *hashtable_del(hash_table_t *ht, const void *key) {
|
||||||
if (ht->entries.cap == 0) return NULL;
|
if (ht->entries.cap == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
u32 hash = ht->hash_func(key);
|
u32 hash = ht->hash_func(key);
|
||||||
hash_entry_t *entry = find_entry(ht, key, hash);
|
hash_entry_t *entry = find_entry(ht, key, hash);
|
||||||
|
|
||||||
if (entry == NULL || entry->state != ENTRY_ACTIVE) return NULL;
|
if (entry == NULL || entry->state != ENTRY_ACTIVE)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
void *value = entry->value;
|
void *value = entry->value;
|
||||||
entry->state = ENTRY_TOMBSTONE;
|
entry->state = ENTRY_TOMBSTONE;
|
||||||
@@ -128,7 +137,8 @@ void hashtable_destory(hash_table_t* ht) {
|
|||||||
ht->tombstone_count = 0;
|
ht->tombstone_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context) {
|
void hashtable_foreach(hash_table_t *ht, hash_table_iter_func iter_func,
|
||||||
|
void *context) {
|
||||||
for (usize i = 0; i < ht->entries.cap; i++) {
|
for (usize i = 0; i < ht->entries.cap; i++) {
|
||||||
hash_entry_t *entry = &vector_at(ht->entries, i);
|
hash_entry_t *entry = &vector_at(ht->entries, i);
|
||||||
if (entry->state == ENTRY_ACTIVE) {
|
if (entry->state == ENTRY_ACTIVE) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#ifndef __SMCC_TERMINAL_COLOR_H__
|
#ifndef __SMCC_TERMINAL_COLOR_H__
|
||||||
#define __SMCC_TERMINAL_COLOR_H__
|
#define __SMCC_TERMINAL_COLOR_H__
|
||||||
|
|
||||||
|
/* clang-format off */
|
||||||
/// @name 前景色控制码
|
/// @name 前景色控制码
|
||||||
/// @{
|
/// @{
|
||||||
#define ANSI_FG_BLACK "\33[30m" ///< 黑色前景
|
#define ANSI_FG_BLACK "\33[30m" ///< 黑色前景
|
||||||
@@ -38,6 +39,7 @@
|
|||||||
#define ANSI_BOLD "\33[1m" ///< 粗体样式
|
#define ANSI_BOLD "\33[1m" ///< 粗体样式
|
||||||
#define ANSI_NONE "\33[0m" ///< 重置所有样式
|
#define ANSI_NONE "\33[0m" ///< 重置所有样式
|
||||||
/// @}
|
/// @}
|
||||||
|
/* clang-format on */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @def ANSI_FMT
|
* @def ANSI_FMT
|
||||||
|
|||||||
@@ -1,35 +1,62 @@
|
|||||||
#include <log.h>
|
#include <log.h>
|
||||||
|
|
||||||
void log_default_handler(log_level_t level, const char* module, const char* file, int line, const char* message) {
|
void log_default_handler(log_level_t level, const char *module,
|
||||||
|
const char *file, int line, const char *message) {
|
||||||
const char *level_str;
|
const char *level_str;
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case LOG_LEVEL_DEBUG: level_str = "DEBUG"; break;
|
case LOG_LEVEL_DEBUG:
|
||||||
case LOG_LEVEL_INFO: level_str = "INFO "; break;
|
level_str = "DEBUG";
|
||||||
case LOG_LEVEL_WARN: level_str = "WARN "; break;
|
break;
|
||||||
case LOG_LEVEL_ERROR: level_str = "ERROR"; break;
|
case LOG_LEVEL_INFO:
|
||||||
case LOG_LEVEL_FATAL: level_str = "FATAL"; break;
|
level_str = "INFO ";
|
||||||
case LOG_LEVEL_TRACE: level_str = "TRACE"; break;
|
break;
|
||||||
default: level_str = "NOTSET"; break;
|
case LOG_LEVEL_WARN:
|
||||||
|
level_str = "WARN ";
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_ERROR:
|
||||||
|
level_str = "ERROR";
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_FATAL:
|
||||||
|
level_str = "FATAL";
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_TRACE:
|
||||||
|
level_str = "TRACE";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
level_str = "NOTSET";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出
|
/// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出
|
||||||
#ifndef __LOG_NO_COLOR__
|
#ifndef __LOG_NO_COLOR__
|
||||||
const char *color_code;
|
const char *color_code;
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case LOG_LEVEL_DEBUG: color_code = ANSI_FG_CYAN; break;
|
case LOG_LEVEL_DEBUG:
|
||||||
case LOG_LEVEL_INFO: color_code = ANSI_FG_GREEN; break;
|
color_code = ANSI_FG_CYAN;
|
||||||
case LOG_LEVEL_TRACE: color_code = ANSI_FG_BLUE; break;
|
break;
|
||||||
case LOG_LEVEL_WARN: color_code = ANSI_FG_YELLOW; break;
|
case LOG_LEVEL_INFO:
|
||||||
case LOG_LEVEL_ERROR: color_code = ANSI_FG_RED; break;
|
color_code = ANSI_FG_GREEN;
|
||||||
case LOG_LEVEL_FATAL: color_code = ANSI_FG_RED ANSI_UNDERLINED; break; // 增强对比度
|
break;
|
||||||
default: color_code = ANSI_NONE;
|
case LOG_LEVEL_TRACE:
|
||||||
|
color_code = ANSI_FG_BLUE;
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_WARN:
|
||||||
|
color_code = ANSI_FG_YELLOW;
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_ERROR:
|
||||||
|
color_code = ANSI_FG_RED;
|
||||||
|
break;
|
||||||
|
case LOG_LEVEL_FATAL:
|
||||||
|
color_code = ANSI_FG_RED ANSI_UNDERLINED;
|
||||||
|
break; // 增强对比度
|
||||||
|
default:
|
||||||
|
color_code = ANSI_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
log_printf(ANSI_BOLD "%s[%s] - %s - %s:%d | %s" ANSI_NONE "\n", color_code,
|
log_printf(ANSI_BOLD "%s[%s] - %s - %s:%d | %s" ANSI_NONE "\n", color_code,
|
||||||
level_str, module, file, line, message);
|
level_str, module, file, line, message);
|
||||||
#else
|
#else
|
||||||
log_printf("[%s] %s:%d | %s: %s\n",
|
log_printf("[%s] %s:%d | %s: %s\n", level_str, file, line, module, message);
|
||||||
level_str, file, line, module, message);
|
|
||||||
#endif
|
#endif
|
||||||
// for clangd warning
|
// for clangd warning
|
||||||
// clang-analyzer-deadcode.DeadStores
|
// clang-analyzer-deadcode.DeadStores
|
||||||
@@ -52,20 +79,20 @@ void init_logger(logger_t* logger, const char* name) {
|
|||||||
log_set_level(logger, LOG_LEVEL_ALL);
|
log_set_level(logger, LOG_LEVEL_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger_t* log_get(const char* name) {
|
logger_t *log_get(const char *name) { return &logger_root; }
|
||||||
return &logger_root;
|
|
||||||
}
|
|
||||||
|
|
||||||
void log_set_level(logger_t *logger, int level) {
|
void log_set_level(logger_t *logger, int level) {
|
||||||
if (logger) logger->level = level;
|
if (logger)
|
||||||
else logger_root.level = level;
|
logger->level = level;
|
||||||
|
else
|
||||||
|
logger_root.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_set_handler(logger_t *logger, log_handler handler) {
|
void log_set_handler(logger_t *logger, log_handler handler) {
|
||||||
if (logger) logger->handler = handler;
|
if (logger)
|
||||||
else logger_root.handler = handler;
|
logger->handler = handler;
|
||||||
|
else
|
||||||
|
logger_root.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_destroy(logger_t* logger) {
|
void logger_destroy(logger_t *logger) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -56,13 +56,8 @@ typedef enum log_level {
|
|||||||
* @param message 格式化后的日志消息
|
* @param message 格式化后的日志消息
|
||||||
* @todo 待实现模块名称,输入的模块名称,都将被忽略
|
* @todo 待实现模块名称,输入的模块名称,都将被忽略
|
||||||
*/
|
*/
|
||||||
typedef void (*log_handler)(
|
typedef void (*log_handler)(log_level_t level, const char *module,
|
||||||
log_level_t level,
|
const char *file, int line, const char *message);
|
||||||
const char* module,
|
|
||||||
const char* file,
|
|
||||||
int line,
|
|
||||||
const char* message
|
|
||||||
);
|
|
||||||
|
|
||||||
#ifndef LOGGER_MAX_BUF_SIZE
|
#ifndef LOGGER_MAX_BUF_SIZE
|
||||||
#define LOGGER_MAX_BUF_SIZE 512 ///< 单条日志最大缓冲区尺寸
|
#define LOGGER_MAX_BUF_SIZE 512 ///< 单条日志最大缓冲区尺寸
|
||||||
@@ -80,7 +75,8 @@ typedef struct logger {
|
|||||||
char buf[LOGGER_MAX_BUF_SIZE]; ///< 格式化缓冲区
|
char buf[LOGGER_MAX_BUF_SIZE]; ///< 格式化缓冲区
|
||||||
} logger_t;
|
} logger_t;
|
||||||
|
|
||||||
void log_default_handler(log_level_t level, const char* module, const char* file, int line, const char* message);
|
void log_default_handler(log_level_t level, const char *module,
|
||||||
|
const char *file, int line, const char *message);
|
||||||
extern logger_t logger_root;
|
extern logger_t logger_root;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,34 +132,50 @@ void logger_destroy(logger_t* logger);
|
|||||||
logger_t *_logger; \
|
logger_t *_logger; \
|
||||||
if (!_module_) { \
|
if (!_module_) { \
|
||||||
_logger = log_get(NULL); \
|
_logger = log_get(NULL); \
|
||||||
} \
|
} else \
|
||||||
else _logger = _module_; \
|
_logger = _module_; \
|
||||||
if (_logger && _logger->handler && (_logger->level & (_level_))) { \
|
if (_logger && _logger->handler && (_logger->level & (_level_))) { \
|
||||||
log_snprintf(_logger->buf, sizeof(_logger->buf), (_msg_), ##__VA_ARGS__); \
|
log_snprintf(_logger->buf, sizeof(_logger->buf), (_msg_), \
|
||||||
_logger->handler((_level_), _logger->name, __FILE__, __LINE__, _logger->buf); \
|
##__VA_ARGS__); \
|
||||||
|
_logger->handler((_level_), _logger->name, __FILE__, __LINE__, \
|
||||||
|
_logger->buf); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/// @name 模块日志宏
|
/// @name 模块日志宏
|
||||||
/// @{
|
/// @{
|
||||||
#define MLOG_NOTSET(module, ...) _LOG(module, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
#define MLOG_NOTSET(module, ...) \
|
||||||
#define MLOG_DEBUG( module, ...) _LOG(module, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
_LOG(module, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
||||||
#define MLOG_INFO( module, ...) _LOG(module, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
#define MLOG_DEBUG(module, ...) \
|
||||||
#define MLOG_WARN( module, ...) _LOG(module, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
_LOG(module, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
||||||
#define MLOG_ERROR( module, ...) _LOG(module, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
#define MLOG_INFO(module, ...) \
|
||||||
#define MLOG_FATAL( module, ...) _LOG(module, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
_LOG(module, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
||||||
#define MLOG_TRACE( module, ...) _LOG(module, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
#define MLOG_WARN(module, ...) \
|
||||||
|
_LOG(module, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
||||||
|
#define MLOG_ERROR(module, ...) \
|
||||||
|
_LOG(module, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
||||||
|
#define MLOG_FATAL(module, ...) \
|
||||||
|
_LOG(module, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
||||||
|
#define MLOG_TRACE(module, ...) \
|
||||||
|
_LOG(module, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
/// @name 快捷日志宏
|
/// @name 快捷日志宏
|
||||||
/// @{
|
/// @{
|
||||||
#define LOG_NOTSET(...) _LOG(NULL, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
#define LOG_NOTSET(...) \
|
||||||
#define LOG_DEBUG(...) _LOG(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
_LOG(NULL, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
||||||
#define LOG_INFO(...) _LOG(NULL, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
#define LOG_DEBUG(...) \
|
||||||
#define LOG_WARN(...) _LOG(NULL, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
_LOG(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
||||||
#define LOG_ERROR(...) _LOG(NULL, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
#define LOG_INFO(...) \
|
||||||
#define LOG_FATAL(...) _LOG(NULL, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
_LOG(NULL, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
||||||
#define LOG_TRACE(...) _LOG(NULL, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
#define LOG_WARN(...) \
|
||||||
|
_LOG(NULL, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
||||||
|
#define LOG_ERROR(...) \
|
||||||
|
_LOG(NULL, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
||||||
|
#define LOG_FATAL(...) \
|
||||||
|
_LOG(NULL, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
||||||
|
#define LOG_TRACE(...) \
|
||||||
|
_LOG(NULL, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -183,13 +195,19 @@ void logger_destroy(logger_t* logger);
|
|||||||
/// @{
|
/// @{
|
||||||
#define __INNER_LOG_STR(str) #str
|
#define __INNER_LOG_STR(str) #str
|
||||||
#define __LOG_STR(str) __INNER_LOG_STR(str)
|
#define __LOG_STR(str) __INNER_LOG_STR(str)
|
||||||
#define AssertFmt(cond, format, ...) _Assert(cond, "Assertion Failure: " format, ## __VA_ARGS__) ///< 带格式的断言检查
|
#define AssertFmt(cond, format, ...) \
|
||||||
#define PanicFmt(format, ...) _Assert(0, "Panic: " format, ## __VA_ARGS__) ///< 立即触发致命错误
|
_Assert(cond, "Assertion Failure: " format, \
|
||||||
#define Assert(cond) AssertFmt(cond, "cond is `" __LOG_STR(cond) "`") ///< 基础断言检查
|
##__VA_ARGS__) ///< 带格式的断言检查
|
||||||
|
#define PanicFmt(format, ...) \
|
||||||
|
_Assert(0, "Panic: " format, ##__VA_ARGS__) ///< 立即触发致命错误
|
||||||
|
#define Assert(cond) \
|
||||||
|
AssertFmt(cond, "cond is `" __LOG_STR(cond) "`") ///< 基础断言检查
|
||||||
#define Panic(...) PanicFmt(__VA_ARGS__) ///< 触发致命错误(带自定义消息)
|
#define Panic(...) PanicFmt(__VA_ARGS__) ///< 触发致命错误(带自定义消息)
|
||||||
#define TODO() PanicFmt("TODO please implement me") ///< 标记未实现代码(触发致命错误)
|
#define TODO() \
|
||||||
|
PanicFmt("TODO please implement me") ///< 标记未实现代码(触发致命错误)
|
||||||
#define UNREACHABLE() PanicFmt("UNREACHABLE") ///< 触发致命错误(代码不可达)
|
#define UNREACHABLE() PanicFmt("UNREACHABLE") ///< 触发致命错误(代码不可达)
|
||||||
#define FIXME(str) PanicFmt("FIXME " __LOG_STR(str)) ///< 提醒开发者修改代码(触发致命错误)
|
#define FIXME(str) \
|
||||||
|
PanicFmt("FIXME " __LOG_STR(str)) ///< 提醒开发者修改代码(触发致命错误)
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
#ifdef __SMCC_LOG_IMPORT_SRC__
|
#ifdef __SMCC_LOG_IMPORT_SRC__
|
||||||
|
|||||||
Reference in New Issue
Block a user