mempool.h
#ifndef _MEMPOOL_H
#define _MEMPOOL_H
#include "sys.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
#include "delay.h"
#define memset0(mem, size) memset((mem), 0, (size))
#define my_alloc_msg(_type) _alloc_msg(sizeof(_type))
#define my_free_msg(_msg) do {if(_msg==NULL) break;_free_msg(_msg); _msg = 0;}while(0);
void msg_pool_init(void);
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
mempool.c
#include "mempool.h"
#define MAX_MSG_2 32
#define MAX_MSG_4 64
#define MAX_MSG_8 128
#define MAX_MSG_32 128
#define MAX_MSG_64 64
#define MAX_MSG_128 8
#define MAX_MSG_256 4
uint8_t msg_pool_2[MAX_MSG_2][2];
uint8_t msg_pool_4[MAX_MSG_4][4];
uint8_t msg_pool_8[MAX_MSG_8][8];
uint8_t msg_pool_32[MAX_MSG_32][32];
uint8_t msg_pool_64[MAX_MSG_64][64];
uint8_t msg_pool_128[MAX_MSG_128][128];
uint8_t msg_pool_256[MAX_MSG_256][256];
void msg_pool_init(void){
memset0(msg_pool_2, MAX_MSG_2*2);
memset0(msg_pool_4, MAX_MSG_4*4);
memset0(msg_pool_8, MAX_MSG_8*8);
memset0(msg_pool_32, MAX_MSG_32*32);
memset0(msg_pool_64, MAX_MSG_64*64);
memset0(msg_pool_128, MAX_MSG_128*128);
memset0(msg_pool_256, MAX_MSG_256*256);
}
#define LOOP_POOL_REMOVE(MAX_MSG_x,msg_pool_x,tick_1s) \
for( i=0; i<MAX_MSG_x; ++i) {\
if(msg_pool_x[i][0] > 0 && tick_1s-msg_pool_x[i][1] > 15) {\
msg_pool_x[i][0] = 0;\
}\
}
#define LOOP_POOL(MAX_MSG_x,msg_pool_x) \
for( i=0; i<MAX_MSG_x; ++i) {\
if(msg_pool_x[i][0] == 0) {\
ret = (uint8_t*)(&msg_pool_x[i]);\
goto ok_end;\
}\
}
#define OS_ENTER_CRITICAL() \
if(0==__get_CONTROL()){ \
state_value = taskENTER_CRITICAL_FROM_ISR(); \
}else{ \
taskENTER_CRITICAL();\
}
#define OS_EXIT_CRITICAL() \
if(0==__get_CONTROL()){\
taskEXIT_CRITICAL_FROM_ISR(state_value);\
}else{\
taskEXIT_CRITICAL();\
}
void* _alloc_msg(uint16_t size){
uint32_t i = 0;
uint32_t state_value = 0;
uint8_t* ret = NULL;
OS_ENTER_CRITICAL();
if(size <=2 ){
LOOP_POOL(MAX_MSG_2, msg_pool_2);
LOOP_POOL(MAX_MSG_4, msg_pool_4);
LOOP_POOL(MAX_MSG_8, msg_pool_8);
LOOP_POOL(MAX_MSG_32, msg_pool_32);
LOOP_POOL(MAX_MSG_64, msg_pool_64);
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <=4 ){
LOOP_POOL(MAX_MSG_4, msg_pool_4);
LOOP_POOL(MAX_MSG_8, msg_pool_8);
LOOP_POOL(MAX_MSG_32, msg_pool_32);
LOOP_POOL(MAX_MSG_64, msg_pool_64);
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <= 8){
LOOP_POOL(MAX_MSG_8, msg_pool_8);
LOOP_POOL(MAX_MSG_32, msg_pool_32);
LOOP_POOL(MAX_MSG_64, msg_pool_64);
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <= 32){
LOOP_POOL(MAX_MSG_32, msg_pool_32);
LOOP_POOL(MAX_MSG_64, msg_pool_64);
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <= 64){
LOOP_POOL(MAX_MSG_64, msg_pool_64);
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <= 128){
LOOP_POOL(MAX_MSG_128, msg_pool_128);
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else if(size <= 256){
LOOP_POOL(MAX_MSG_256, msg_pool_256);
}
else{
ret = NULL;
}
ok_end:
if(ret != NULL) {
ret[0] = 0xff;
ret[1] = systemMs();
OS_EXIT_CRITICAL();
memset0(ret+2, size-2);
} else {
OS_EXIT_CRITICAL();
}
return ret;
}
void _free_msg(void* msg){
if(msg != NULL){
uint8_t* p = msg;
uint32_t state_value = 0;
OS_ENTER_CRITICAL();
p[0] = 0;
p[1] = 0;
OS_EXIT_CRITICAL();
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145