This commit is contained in:
2026-05-19 11:49:22 +08:00
commit add7af4222
96 changed files with 8635 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
#include <os_align.h>
+25
View File
@@ -0,0 +1,25 @@
#ifndef INCLUDED_OS_ALIGN_H
#define INCLUDED_OS_ALIGN_H
#ifndef OS_ALIGN_SIZE
#define OS_ALIGN_SIZE sizeof(os_align_t)
#endif
typedef union {
#if defined(OS_ALIGN_MAX)
unsigned char pad[OS_ALIGN_MAX];
#else
int i;
long l;
long long ll;
long* lp;
void* p;
void (*fp)(void);
float f;
double d;
long double ld;
#endif
}os_align_t;
#endif /*INCLUDED_OS_ALIGN_H*/
+1
View File
@@ -0,0 +1 @@
#include <os_compiler.h>
+35
View File
@@ -0,0 +1,35 @@
#ifndef INCLUDED_OS_COMPILER_H
#define INCLUDED_OS_COMPILER_H
#if defined(__GNUC__)
#define OS_STATIC_FORCE_INLINE static inline __attribute__((always_inline))
#define OS_ALIGNED(x) __attribute__((aligned(x)))
#define OS_WEAK __attribute__((weak))
#define OS_PACKED_STRUCT(x) struct __attribute__((packed)) x
#define OS_SECTION(n) __attribute__((section(#n)))
#define OS_NORETURN __attribute__((noreturn))
#define OS_PACKED __attribute__((packed))
#endif /*defined(__GNUC__)*/
#if defined(__CC_ARM)
#define OS_STATIC_FORCE_INLINE static inline __attribute__((always_inline))
#define OS_ALIGNED(x) __attribute__((aligned(x)))
#define OS_WEAK __attribute__((weak))
#define OS_PACKED_STRUCT(x) struct __attribute__((packed)) x
#define OS_SECTION(n) __attribute__((section(#n)))
#define OS_NORETURN __attribute__((noreturn))
#define OS_PACKED __attribute__((packed))
#endif /*defined(__CC_ARM)*/
#if defined(__IAR_SYSTEMS_ICC__)
#define OS_STATIC_FORCE_INLINE static inline
#define OS_ALIGNED(x) #pragma data_alignment(x)
#define OS_WEAK __weak
#define OS_PACKED_STRUCT(x) __packed struct x
#define OS_SECTION(n) #pragma location=#n
#define OS_NORETURN __noreturn
#define OS_PACKED __packed
#endif /*defined(__CC_ARM)*/
#endif /*INCLUDED_OS_COMPILER_H*/
+13
View File
@@ -0,0 +1,13 @@
#include <os_endian.h>
#include "os_types.h"
int os_endian_is_little_endian(void){
union {
uint32_t i;
uint8_t c;
} test;
test.i = 0x12345678;
return (test.c == 0x78); // 如果取到的是低位字节,则是小端
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef INCLUDED_OS_ENDIAN_H
#define INCLUDED_OS_ENDIAN_H
#define OS_BYTE_ORDER_LITTLE_ENDIAN 0
#define OS_BYTE_ORDER_BIG_ENDIAN 1
#if defined(OS_BYTE_ORDER)
#undef OS_BYTE_ORDER
#endif /*!defined(OS_BYTE_ORDER)*/
#if defined(__BYTE_ORDER__)
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define OS_BYTE_ORDER OS_BYTE_ORDER_BIG_ENDIAN
#else
#define OS_BYTE_ORDER OS_BYTE_ORDER_LITTLE_ENDIAN
#endif /* (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) */
#endif /* defined(__BYTE_ORDER__) */
int os_endian_is_little_endian(void);
#endif /*INCLUDED_OS_ENDIAN_H*/
+1
View File
@@ -0,0 +1 @@
#include <os_list.h>
+79
View File
@@ -0,0 +1,79 @@
#ifndef INCLUDED_OS_LIST_H
#define INCLUDED_OS_LIST_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct os_list_node_t{
struct os_list_node_t* prev;
struct os_list_node_t* next;
}os_list_node_t;
typedef os_list_node_t os_list_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#define os_list_prev(L) (L)->prev
#define os_list_next(L) (L)->next
#define os_list_prev_next(L) os_list_next(os_list_prev(L))
#define os_list_next_prev(L) os_list_prev(os_list_next(L))
#define os_list_next_next(L) os_list_next(os_list_next(L))
#define os_list_init(L) (os_list_prev(L)=os_list_next(L)=(os_list_node_t*)(L))
#define os_list_is_empty(L) ((os_list_prev(L)==(L))?OS_TRUE:OS_FALSE)
#define os_list_is_one(L) (os_list_next_next(L)==(L))
// Lp -- N -- L
#define os_list_insert_before(L, N) \
do{ \
os_list_prev_next(L) = (os_list_node_t*)(N); \
os_list_prev(N) = os_list_prev(L); \
os_list_prev(L) = (os_list_node_t*)(N); \
os_list_next(N) = (os_list_node_t*)(L); \
}while(0)
// L -- N -- Ln
#define os_list_insert_after(L, N) \
do{ \
os_list_next_prev(L) = (os_list_node_t*)(N); \
os_list_next(N) = os_list_next(L); \
os_list_next(L) = (os_list_node_t*)(N); \
os_list_prev(N) = (os_list_node_t*)(L); \
}while(0)
#define os_list_remove(N) \
do{ \
os_list_prev_next(N) = os_list_next(N); \
os_list_next_prev(N) = os_list_prev(N); \
os_list_init(N); \
}while(0)
// Lp -- L -- Ln -- Lp
// Np -- N -- Nn -- Np
// 1. Lp.next = Nn : L --> Ln --> Lp -next-> Nn -next-> Np --> N --> Nn
// 2. N.head.prev = L.tail: Lp <-prev- Nn
// 3. N.tail.next = L Np -next-> L
// 4. L.prev = N.prev Np <-prev- L
#define os_list_join(L, N) \
do{ \
if(os_list_is_empty(N)){ \
break; \
} \
os_list_prev_next(L) = os_list_next(N); \
os_list_next_prev(N) = os_list_prev(L); \
os_list_prev_next(N) = (L); \
os_list_prev(L) = os_list_prev(N); \
os_list_init(N); \
}while(0)
#define os_list_member_of(ptr, type, member) \
((type *)( (char *)ptr - ((os_size_t)&((type *)0)->member) ))
#endif /*INCLUDED_OS_LIST_H*/
+5
View File
@@ -0,0 +1,5 @@
#include <os_macros.h>
void about(void){
while(1){}
}
+161
View File
@@ -0,0 +1,161 @@
#ifndef INCLUDED_OS_MACROS_H
#define INCLUDED_OS_MACROS_H
#ifndef INCLUDED_STDIO_H
#define INCLUDED_STDIO_H
#include <stdio.h>
#endif /*INCLUDED_STDIO_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#define OS_STR_HELPER(x) #x
#define OS_STR(x) OS_STR_HELPER(x)
/* BITS */
#define OS_BIT(n) (1<<(n))
#define OS_BIT_SET(x,p) ((x)|(1<<(p)))
#define OS_BIT_CLEAR(x,p) ((x)&(~(1<<(p))))
#define OS_BIT_GET(x,p) (((x)>>(p))&1)
#define OS_BIT_TOGGLE(x,p) ((x)^(1<<(p)))
/* ARRAYS */
#define OS_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define OS_SET(d, n, v) do{ int i_, n_; \
for ( n_ = (n), i_ = 0; n_ > 0; --n_, ++i_) \
(d)[i_] = (v); } while(0)
#define OS_ZERO(d, n) OS_SET(d, n, 0)
#define OS_COLUMNS(S,E) ( (E) - (S) + 1 )
#define OS_IS_ARRAY(a) ((void *)&a == (void *)a)
/* DEBUG */
#ifndef NDEBUG
extern void about(void);
#define OS_ASSERT(expr, ...) \
do { \
if (!(expr)) { \
if(sizeof(#__VA_ARGS__) > 1){ \
fprintf(stderr, "[FAILED] Exp: %s - ", #expr); \
fprintf(stderr, "Msg: " __VA_ARGS__); \
fprintf(stderr, " - "__FILE__":%d - " __DATE__ " " __TIME__"\n", __LINE__); \
}else{ \
fprintf(stderr, "[FAILED] Exp: %s - "__FILE__":%d - " __DATE__ " " __TIME__ "\n", #expr, __LINE__); \
} \
about(); \
} \
} while (0)
#define OS_SAFE_ASSERT(expr, ...) \
do { \
if (!(expr)) { \
if(sizeof(#__VA_ARGS__) > 1){ \
fprintf(stderr, "[FAILED] Exp: %s - ", #expr); \
fprintf(stderr, "Msg: " __VA_ARGS__); \
fprintf(stderr, " - "__FILE__":%d - " __DATE__ " " __TIME__"\n", __LINE__); \
}else{ \
fprintf(stderr, "[FAILED] Exp: %s - "__FILE__":%d - " __DATE__ " " __TIME__ "\n", #expr, __LINE__); \
} \
} \
} while (0)
#else
#define OS_ASSERT(expr, ...)
#define OS_SAFE_ASSERT(expr, ...)
#endif
/* ALIGN */
#define OS_ALIGN_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
#define OS_ALIGN_DOWN(x, align) ((x) & ~((align) - 1))
/* MATH */
#define OS_MIN(x, y) (((x) < (y)) ? (x) : (y))
#define OS_MAX(x, y) (((x) > (y)) ? (x) : (y))
#define OS_IS_NAN(x) ((x) != (x))
#define OS_COMPARE(x, y) (((x) > (y)) - ((x) < (y)))
#define OS_SIGN(x) OS_COMPARE(x, 0)
#define OS_IS_ODD( num ) ((num) & 1)
#define OS_IS_EVEN( num ) (!OS_IS_ODD( (num) ))
#define OS_IS_BETWEEN(n,L,H) ((unsigned char)((n) >= (L) && (n) <= (H)))
#define OS_CONSTRAIN(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define OS_IS_POWER_OF_2(x) (((x) & ((x)-1))==0)
/* UNIQUE */
#define OS_CONCAT_INNER(a, b) a ## b
#define OS_CONCAT(a, b) OS_CONCAT_INNER(a, b)
#define OS_UNIQUE(prefix) OS_CONCAT(prefix, __LINE__)
/* DEFER, useful to init and clean a block of scoped code */
/* Please see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2895.htm for a more in depth guide explaining non-standard cleanup functions */
#define OS_DEFER2(head, tail, i) for(int i=(head,0);!i;tail,i++)
#define OS_DEFER(head, tail) OS_DEFER2(head, tail, OS_UNIQUE(__deferVar__))
/* STMT, useful for creating multiple statements macros */
#define OS_STMT( stuff ) do { stuff } while (0)
/* ONCE */
#define OS_ONCE2(stmts, i) {static int i = 1;\
if(i){stmts\
i = 0;}}
#define OS_ONCE(stmts) OS_ONCE2(stmts, OS_UNIQUE(__onceVar__))
/* C in C++ Environment */
#ifdef __cplusplus
# define OS_EXTERN_C_START extern "C" {
# define OS_EXTERN_C_END }
#else
# define OS_EXTERN_C_START
# define OS_EXTERN_C_END
#endif
/* MARKS */
#define OS_PRIVILEGE
#define OS_NONE_PRIVILEGE
#define OS_OPTIONAL
#define OS_UNUSED(x) ((void)(x))
#ifndef OS_I
#define OS_I volatile const
#endif
#ifndef OS_O
#define OS_O volatile
#endif
#ifndef OS_IO
#define OS_IO volatile
#endif
#define OS_OFFSETOF(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
#define OS_CONTAINER_OF(ptr, type, member) ((type *)( (unsigned char *)(ptr) - OS_OFFSETOF(type,member) ))
#define OS_BLOCK_DEF(__Name, nBytes, nCount) static uint8_t __Name[nBytes * nCount]
#endif /*INCLUDED_OS_MACROS_H*/
+1
View File
@@ -0,0 +1 @@
#include "os_memory.h"
+31
View File
@@ -0,0 +1,31 @@
#ifndef INCLUDED_OS_MEMORY_H
#define INCLUDED_OS_MEMORY_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
void os_memory_init(void* block, os_size_t block_size);
void* os_memory_alloc(os_size_t nBytes, const char* file, os_size_t line);
void* os_memory_realloc(void* ptr, os_size_t nBytes, const char* file, os_size_t line);
void* os_memory_calloc(os_size_t nCount, os_size_t nBytes, const char* file, os_size_t line);
void os_memory_free(void* ptr, const char* file, os_size_t line);
#define OS_ALLOC(n) os_memory_alloc((n), __FILE__, __LINE__)
#define OS_CALLOC(x, n) os_memory_calloc((x), (n), __FILE__, __LINE__)
#define OS_REALLOC(p, n) os_memory_realloc((p), (n), __FILE__, __LINE__)
#define OS_FREE(p) ({os_memory_free((p), __FILE__, __LINE__); (p)=NULL;})
#define OS_RESIZE(p, n) (p) = OS_REALLOC(p, n)
#define OS_NEW(p) (p) = OS_ALLOC(sizeof(*(p)))
#define OS_NEW0(p) (p) = OS_CALLOC(1, sizeof(*(p)))
#endif /*INCLUDED_OS_MEMORY_H*/
+1
View File
@@ -0,0 +1 @@
#include <os_stack.h>
+25
View File
@@ -0,0 +1,25 @@
#ifndef INCLUDED_OS_STACK_H
#define INCLUDED_OS_STACK_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef enum{
kOsStackType_MinToMax,
kOsStackType_MaxToMin,
}os_stack_type_t;
typedef struct {
void* sp;
os_uintptr_t min;
os_uintptr_t max;
os_stack_type_t type;
}os_stack_t;
#endif /*INCLUDED_OS_STACK_H*/
+1
View File
@@ -0,0 +1 @@
#include "os_types.h"
+107
View File
@@ -0,0 +1,107 @@
#ifndef INCLUDED_OS_TYPES_H
#define INCLUDED_OS_TYPES_H
#ifndef INCLUDED_OS_CONFIG_H
#include <os_config.h>
#endif /*INCLUDED_OS_CONFIG_H*/
#ifndef INCLUDED_STDINT_H
#define INCLUDED_STDINT_H
#include <stdint.h>
#endif /*INCLUDED_STDINT_H*/
#ifndef INCLUDED_STDBOOL_H
#define INCLUDED_STDBOOL_H
#include <stdbool.h>
#endif /*INCLUDED_STDBOOL_H*/
#ifndef INCLUDED_STRING_H
#define INCLUDED_STRING_H
#include <string.h>
#endif /*INCLUDED_STRING_H*/
#ifndef INCLUDED_STDDEF_H
#define INCLUDED_STDDEF_H
#include <stddef.h>
#endif /*INCLUDED_STDDEF_H*/
#ifndef INCLUDED_INTTYPES_H
#define INCLUDED_INTTYPES_H
#include <inttypes.h>
#endif /*INCLUDED_INTTYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#if defined(OS_CFG_CPU_INT_NBITS)
#if (OS_CFG_CPU_INT_NBITS==32)
typedef int32_t os_int_t;
typedef uint32_t os_uint_t;
typedef uint32_t os_size_t;
typedef int32_t os_intptr_t;
typedef uint32_t os_uintptr_t;
#define OS_INT_MIN INT32_MIN
#define OS_INT_MAX INT32_MAX
#define OS_UINT_MAX UINT32_MAX
#define OS_SIZE_MAX UINT32_MAX
#define OS_PRId PRId32
#define OS_PRIi PRIi32
#define OS_PRIo PRIo32
#define OS_PRIu PRIu32
#define OS_PRIx PRIx32
#define OS_PRIX PRIX32
#define OS_SCNd SCNd32
#define OS_SCNi SCNi32
#define OS_SCNo SCNo32
#define OS_SCNu SCNu32
#define OS_SCNx SCNx32
#endif /*(OS_CFG_CPU_INT_NBITS==32)*/
#if (OS_CFG_CPU_INT_NBITS==64)
typedef int64_t os_int_t;
typedef uint64_t os_uint_t;
typedef uint64_t os_size_t;
typedef int64_t os_intptr_t;
typedef uint64_t os_uintptr_t;
#define OS_INT_MIN INT64_MIN
#define OS_INT_MAX INT64_MAX
#define OS_UINT_MAX UINT64_MAX
#define OS_SIZE_MAX UINT64_MAX
#define OS_PRId PRId64
#define OS_PRIi PRIi64
#define OS_PRIo PRIo64
#define OS_PRIu PRIu64
#define OS_PRIx PRIx64
#define OS_PRIX PRIX64
#define OS_SCNd SCNd64
#define OS_SCNi SCNi64
#define OS_SCNo SCNo64
#define OS_SCNu SCNu64
#define OS_SCNx SCNx64
#endif /* (OS_CFG_CPU_INT_NBITS==64) */
#else
#error "Missing OS_CFG_CPU_INT_NBITS in os_config.h"
#endif /*OS_CFG_CPU_INT_NBITS*/
#define os_bool_t bool
#define OS_TRUE true
#define OS_FALSE false
typedef os_int_t os_err_t;
typedef os_uint_t os_tick_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#define OS_ERR_OK (0)
#define OS_ERR_FAIL (-1)
#define OS_ERR_TIMEOUT (-2)
#define OS_ERR_ARGS (-3)
#define OS_ERR_BUSY (-4)
#define OS_ERR_FULL (-5)
#define OS_ERR_EMPTY (-6)
#define OS_ERR_EXIST (-7)
#define OS_WAIT_INFINITY ((os_tick_t)(-1))
#endif /*INCLUDED_OS_TYPES_H*/