Compare commits

..
11 Commits
Author SHA1 Message Date
pchen 30cd757f09 OS_CFG_SYSCALL_PRIORITY 默认为 0x05 2026-08-06 22:45:58 +08:00
pchen 3fbd4d34cd refine 2026-07-28 10:51:28 +08:00
pchen 6d57f4f1ce fifo 支持水位监测和溢出次数统计 2026-07-20 10:53:37 +08:00
pchen 065420db36 stopwatch 计时 2026-07-19 17:56:30 +08:00
pchen b9eb6a2319 stopwatch 计时 2026-07-19 17:55:03 +08:00
pchen b21f91fd61 时间轮TWN3_MAX值错误 2026-07-15 22:23:40 +08:00
pchen 86e685b263 优化 2026-07-09 10:43:36 +08:00
pchen c358c790f9 优化 2026-07-09 10:21:02 +08:00
pchen 4cb5880258 优化 2026-07-09 10:16:24 +08:00
pchen 946ae6ac9a 优化 2026-07-09 10:00:59 +08:00
pchen e27272c36d 优化 2026-07-09 09:57:00 +08:00
16 changed files with 193 additions and 37 deletions
+3 -3
View File
@@ -111,15 +111,15 @@ void arean_destroy(arena_t* self){
/* */
temp_arena_memory_t temp_arena_memory_begin(arena_t *a) {
temp_arena_memory_t temp;
arena_temp_memory_t arena_temp_memory_begin(arena_t *a) {
arena_temp_memory_t temp;
temp.arena = a;
temp.prev_offset = a->prev_offset;
temp.curr_offset = a->curr_offset;
return temp;
}
void temp_arena_memory_end(temp_arena_memory_t temp) {
void arena_temp_memory_end(arena_temp_memory_t temp) {
temp.arena->prev_offset = temp.prev_offset;
temp.arena->curr_offset = temp.curr_offset;
}
+4 -4
View File
@@ -15,11 +15,11 @@ typedef struct arena_t{
os_size_t curr_offset;
}arena_t;
typedef struct temp_arena_memory_t {
typedef struct arena_temp_memory_t {
arena_t *arena;
size_t prev_offset;
size_t curr_offset;
}temp_arena_memory_t;
}arena_temp_memory_t;
/* ------------------------------------------------------------------------------------------------------------------ */
@@ -39,8 +39,8 @@ void arean_destroy(arena_t* self);
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
temp_arena_memory_t temp_arena_memory_begin(arena_t *a);
arena_temp_memory_t arena_temp_memory_begin(arena_t *a);
void temp_arena_memory_end(temp_arena_memory_t temp);
void arena_temp_memory_end(arena_temp_memory_t temp);
#endif /*INCLUDED_ARENA_H*/
+1 -1
View File
@@ -16,7 +16,7 @@ typedef struct buddy_block_t{
typedef struct buddy_t{
buddy_block_t* head;
buddy_block_t* tail;
int alignment;
os_size_t alignment;
}buddy_t;
/* ------------------------------------------------------------------------------------------------------------------ */
+16 -4
View File
@@ -9,18 +9,28 @@
// 在尾部写入数据
os_err_t fifo_put(fifo_t* self, uint8_t data){
os_size_t write_idx = self->write_idx;
os_size_t next_write_idx = fifo_next(self, self->write_idx);
if(next_write_idx == self->read_idx){
#if defined(OS_CFG_FIFO_DEBUG_ENABLE) && (OS_CFG_FIFO_DEBUG_ENABLE==1)
self->overflow_count++;
#endif
return OS_ERR_FULL;
}
self->buffer[write_idx] = data;
self->write_idx = next_write_idx;
#if defined(OS_CFG_FIFO_DEBUG_ENABLE) && (OS_CFG_FIFO_DEBUG_ENABLE==1)
os_size_t used = fifo_count(self);
if (used > self->high_water) {
self->high_water = used;
}
#endif
return OS_ERR_OK;
}
@@ -35,8 +45,7 @@ os_err_t fifo_get(fifo_t* self, uint8_t* data){
*data = self->buffer[read_idx];
}
os_size_t next_read_idx = fifo_next(self, self->read_idx);
self->read_idx = next_read_idx;
self->read_idx = fifo_next(self, self->read_idx);
return OS_ERR_OK;
}
@@ -393,6 +402,9 @@ unsigned long fifo_strtoul(fifo_t* self, os_size_t* endptr /*从哪里开始转
// Returns the virtual index relative to tail where the match starts, or -1 if not found
os_size_t fifo_strstr(fifo_t *self, const char *search) {
if(self==NULL || search==NULL || search[0]=='\0') return FIFO_INVALID_POS;
os_size_t search_len = strlen(search);
os_size_t count = fifo_count(self);
if (search_len == 0 || count < search_len) return FIFO_INVALID_POS;
+10
View File
@@ -13,7 +13,12 @@
#include <os_macros.h>
#endif /*INCLUDED_OS_MACROS_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
#ifndef OS_CFG_FIFO_DEBUG_ENABLE
#define OS_CFG_FIFO_DEBUG_ENABLE 1
#endif
/* ==================================================================================================== */
/* 类型 */
@@ -24,6 +29,11 @@ typedef struct fifo_t{
os_size_t buffer_size;
volatile os_size_t write_idx;
volatile os_size_t read_idx;
#if defined(OS_CFG_FIFO_DEBUG_ENABLE) && (OS_CFG_FIFO_DEBUG_ENABLE==1)
volatile os_size_t high_water; /* 顶峰时期使用的字节数 */
volatile os_size_t overflow_count; /* 请求遇到溢出的次数 */
#endif
}fifo_t;
#define FIFO_INVALID_POS ((os_size_t)(-1u))
+10 -7
View File
@@ -1,5 +1,6 @@
#include <fixed_rbtree_pool.h>
#include "os_macros.h"
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
@@ -28,7 +29,7 @@ void node_init(rbtree_t* tree, rbtree_node_t* self, os_size_t obj_size, void* bl
static
int node_cmp(const os_size_t obj_size, const rbtree_node_t* x, void* args){
return (obj_size == x->pool.obj_size)?0:((obj_size > x->pool.obj_size)?1:-1);
return OS_COMPARE(obj_size, x->pool.obj_size);
}
/* ------------------------------------------------------------------------------------------------------------------ */
@@ -427,32 +428,35 @@ void fixed_rbtree_pool_init(fixed_rbtree_pool_t* self, void* node_block, os_size
pool_add_block(&self->node_pool, node_block, node_block_size);
}
void fixed_rbtree_pool_add_node_pool(fixed_rbtree_pool_t* self, void* node_block, os_size_t node_block_size){
pool_add_block(&self->node_pool, node_block, node_block_size);
os_bool_t fixed_rbtree_pool_add_node_pool(fixed_rbtree_pool_t* self, void* node_block, os_size_t node_block_size){
return pool_add_block(&self->node_pool, node_block, node_block_size);
}
void fixed_rbtree_pool_destroy(fixed_rbtree_pool_t* self){
while(self->root){
rbtree_node_t * p = self->root;
pool_destroy(&p->pool);
node_remove(self, p);
node_destroy(self, p);
}
pool_destroy(&self->node_pool);
}
void fixed_rbtree_pool_add(fixed_rbtree_pool_t* self, os_size_t obj_size, void* block, os_size_t block_size){
os_bool_t fixed_rbtree_pool_add(fixed_rbtree_pool_t* self, os_size_t obj_size, void* block, os_size_t block_size){
rbtree_node_t* x = find(self, obj_size, self->root);
if(x==NULL){
x = pool_alloc(&self->node_pool);
if(!x) return OS_FALSE;
node_init(self, x, obj_size, block, block_size);
node_insert(self, x);
}else{
pool_add_block(&x->pool, block, block_size);
}
return OS_TRUE;
}
void* fixed_rbtree_pool_alloc(fixed_rbtree_pool_t* self, os_size_t obj_size){
rbtree_node_t * x = find(self, obj_size, self->root);
OS_ASSERT(x, "No pool exist for obj_size: %"OS_PRIu, obj_size);
if(x==NULL){
return NULL;
}
@@ -462,10 +466,9 @@ void* fixed_rbtree_pool_alloc(fixed_rbtree_pool_t* self, os_size_t obj_size){
void fixed_rbtree_pool_free(fixed_rbtree_pool_t* self, os_size_t obj_size, void* ptr){
if(!ptr) return;
rbtree_node_t * x = find(self, obj_size, self->root);
OS_ASSERT(x, "No pool exist for obj_size: %"OS_PRIu, obj_size);
if(x==NULL){
return;
}
pool_free(&self->node_pool, ptr);
pool_free(&x->pool, ptr);
}
+2 -2
View File
@@ -33,10 +33,10 @@ typedef struct {
/* */
void fixed_rbtree_pool_init(fixed_rbtree_pool_t* self, void* node_block, os_size_t node_block_size);
void fixed_rbtree_pool_add_node_pool(fixed_rbtree_pool_t* self, void* node_block, os_size_t node_block_size);
os_bool_t fixed_rbtree_pool_add_node_pool(fixed_rbtree_pool_t* self, void* node_block, os_size_t node_block_size);
void fixed_rbtree_pool_destroy(fixed_rbtree_pool_t* self);
void fixed_rbtree_pool_add(fixed_rbtree_pool_t* self, os_size_t obj_size, void* block, os_size_t block_size);
os_bool_t fixed_rbtree_pool_add(fixed_rbtree_pool_t* self, os_size_t obj_size, void* block, os_size_t block_size);
void* fixed_rbtree_pool_alloc(fixed_rbtree_pool_t* self, os_size_t obj_size);
void fixed_rbtree_pool_free(fixed_rbtree_pool_t* self, os_size_t obj_size, void* ptr);
+6 -2
View File
@@ -2,13 +2,15 @@
#include "os_macros.h"
#include "os_align.h"
void pool_add_block(pool_t* self, void* block, os_size_t block_size){
os_bool_t pool_add_block(pool_t* self, void* block, os_size_t block_size){
// 对齐
os_uintptr_t init_start = (os_uintptr_t)block; // 初始的位置
os_uintptr_t start = OS_ALIGN_UP(init_start, OS_ALIGN_SIZE); // 向上对齐后的位置
block_size -= (start - init_start); // 实际使用的块大小
OS_ASSERT(block_size > self->obj_size);
if(block_size < self->obj_size){
return OS_FALSE;
}
// 将内存块拆成 obj_size 大小
os_size_t count = block_size/self->obj_size;
@@ -19,5 +21,7 @@ void pool_add_block(pool_t* self, void* block, os_size_t block_size){
((pool_link_t*)last)->next = 0;
self->free_list_p = (pool_link_t*)start;
self->capacity += count;
return OS_TRUE;
}
+9 -1
View File
@@ -30,7 +30,7 @@ typedef struct {
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
void pool_add_block(pool_t* self, void* block, os_size_t block_size);
os_bool_t pool_add_block(pool_t* self, void* block, os_size_t block_size);
/*
* pool_t pool;
@@ -57,6 +57,14 @@ void pool_init(pool_t* self, os_size_t obj_size){
self->capacity = 0;
}
OS_STATIC_FORCE_INLINE
void pool_destroy(pool_t* self){
self->free_list_p = 0;
self->obj_size = 0;
self->count = 0;
self->capacity = 0;
}
OS_STATIC_FORCE_INLINE
void* pool_alloc(pool_t* self){
if(!self->free_list_p){
+1
View File
@@ -0,0 +1 @@
#include <stopwatch.h>
+39
View File
@@ -0,0 +1,39 @@
#ifndef INCLUDED_STOPWATCH_H
#define INCLUDED_STOPWATCH_H
#ifndef INCLUDED_OS_SYSTICK_H
#include <os_systick.h>
#endif /*INCLUDED_OS_SYSTICK_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
volatile os_tick_t start_tick;
volatile os_tick_t end_tick;
}stopwatch_t;
OS_STATIC_FORCE_INLINE
void stopwatch_init(stopwatch_t* self) {
self->start_tick = os_systick_safe_get();
self->end_tick = 0;
}
OS_STATIC_FORCE_INLINE
void stopwatch_start(stopwatch_t* self) {
self->start_tick = os_systick_safe_get();
}
OS_STATIC_FORCE_INLINE
void stopwatch_stop(stopwatch_t* self) {
self->end_tick = os_systick_safe_get();
}
OS_STATIC_FORCE_INLINE
os_tick_t stopwatch_elapsed_ticks(stopwatch_t* self) {
return self->end_tick - self->start_tick;
}
#endif /*INCLUDED_STOPWATCH_H*/
+33
View File
@@ -0,0 +1,33 @@
#include <str_util.h>
const char* str_util_strnstr(const char* haystack, const char* needle, os_size_t needle_len) {
if (needle_len == 0) return (char*)haystack;
if (!haystack || !needle || haystack[0]=='\0') return NULL;
os_size_t haystack_len = strlen(haystack);
if (haystack_len < needle_len) return NULL;
for (os_size_t i = 0; i <= haystack_len - needle_len; i++) {
if (memcmp(haystack + i, needle, needle_len) == 0) {
return haystack + i;
}
}
return NULL;
}
const char* str_util_strstr(const char* haystack, const char* needle) {
if (!haystack || !needle || haystack[0]=='\0' || needle[0]=='\0') return NULL;
os_size_t needle_len = strlen(needle);
os_size_t haystack_len = strlen(haystack);
if (haystack_len < needle_len) return NULL;
for (os_size_t i = 0; i <= haystack_len - needle_len; i++) {
if (memcmp(haystack + i, needle, needle_len) == 0) {
return haystack + i;
}
}
return NULL;
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef INCLUDED_STR_UTIL_H
#define INCLUDED_STR_UTIL_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/**
*
* @param haystack
* @param needle
* @return
*/
const char* str_util_strstr(const char* haystack, const char* needle);
/**
*
* @param haystack
* @param needle
* @param needle_len
* @return
*/
const char* str_util_strnstr(const char* haystack, const char* needle, os_size_t needle_len);
#endif /*INCLUDED_STR_UTIL_H*/
+5 -2
View File
@@ -22,7 +22,7 @@
#cmakedefine OS_CFG_CPU_FPU_PRESENT @OS_CFG_CPU_FPU_PRESENT@
#cmakedefine OS_CFG_CPU_MPU_PRESENT @OS_CFG_CPU_MPU_PRESENT@
#cmakedefine OS_CFG_FIFO_DEBUG_ENABLE @OS_CFG_FIFO_DEBUG_ENABLE@
/* ==================================================================================================== */
/* DEFAULT */
@@ -36,7 +36,7 @@
#endif /* OS_CFG_TICKS_PER_SECOND */
#ifndef OS_CFG_SYSCALL_PRIORITY
#define OS_CFG_SYSCALL_PRIORITY 0x4
#define OS_CFG_SYSCALL_PRIORITY 0x05
#endif /* OS_CFG_SYSCALL_PRIORITY */
#ifndef OS_CFG_PRIORITY_MAX
@@ -71,5 +71,8 @@
#define OS_CFG_CPU_MPU_PRESENT 0
#endif /* OS_CFG_CPU_MPU_PRESENT */
#ifndef OS_CFG_FIFO_DEBUG_ENABLE
#define OS_CFG_FIFO_DEBUG_ENABLE 0
#endif /* OS_CFG_FIFO_DEBUG_ENABLE */
#endif /*INCLUDED_OS_CONFIG_H*/
+21 -6
View File
@@ -1,5 +1,6 @@
#ifndef INCLUDED_OS_SYSTICK_H
#define INCLUDED_OS_SYSTICK_H
#include <stdatomic.h>
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
@@ -69,15 +70,29 @@ void os_systick_tick(void){
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_get(void){
#if 0
os_critical_enter();
os_tick_t ticks = g_os_systick_ticks;
os_critical_leave();
return ticks;
#endif
return g_os_systick_ticks;
}
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_safe_get(void) {
os_critical_enter();
const os_tick_t ticks = g_os_systick_ticks;
os_critical_leave();
return ticks;
}
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_safe_get_in_isr(void) {
os_critical_enter_in_isr();
const os_tick_t ticks = g_os_systick_ticks;
os_critical_leave_in_isr();
return ticks;
}
OS_STATIC_FORCE_INLINE
os_bool_t os_systick_is_timeout(os_tick_t start_tick, os_tick_t timeout_ticks) {
volatile os_tick_t end_ticks = g_os_systick_ticks;
return (end_ticks - start_tick) >= timeout_ticks;
}
#endif /*INCLUDED_OS_SYSTICK_H*/
+5 -5
View File
@@ -35,11 +35,11 @@
#define TWR_SIZE (1<<TWR_BITS)
#define TWN_SIZE (1<<TWN_BITS)
#define TWR_MAX (0xFFu)
#define TWN0_MAX (0x3FFFu) /*11 1111 1111 1111*/
#define TWN1_MAX (0xFFFFFu) /*1111 1111 1111 1111 1111*/
#define TWN2_MAX (0x3FFFFFFu) /*11 1111 1111 1111 1111 1111 1111*/
#define TWN3_MAX (0xFFFFFFFu) /*1111 1111 1111 1111 1111 1111 1111 1111*/
#define TWR_MAX (0xFFu) /* 1111 1111 */
#define TWN0_MAX (0x3FFFu) /* 11 1111 1111 1111 */
#define TWN1_MAX (0xFFFFFu) /* 1111 1111 1111 1111 1111 */
#define TWN2_MAX (0x3FFFFFFu) /* 11 1111 1111 1111 1111 1111 1111 */
#define TWN3_MAX (0xFFFFFFFFu) /* 1111 1111 1111 1111 1111 1111 1111 1111 */
#define TWN_IDX(T, N) (((T)>>(TWR_BITS + (N)*TWN_BITS)) & TWN_MASK)