• 内存池的C实现


    mempool.h

    #ifndef _MEMPOOL_H
    #define _MEMPOOL_H
    
    #include "sys.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "string.h"
    #include "delay.h"
    
    /* 将内存设置为0 */
    #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"
    
    //>全部消息池:msg_pool[MAX_MSG][MAX_MSG_LEN]
    #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();\
    }
    
    //申请消息,并清0
    void* _alloc_msg(uint16_t size){
        uint32_t i = 0;
    	uint32_t state_value = 0;
        uint8_t* ret = NULL;
    
    //_again:
    	
        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{
            // 不应该出现大于256的消息
            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();
            //goto _again;  
        }
        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
  • 相关阅读:
    如何将图片进行竖直拼接(全景拼接)
    【开题报告】基于SpringBoot的乡村文旅平台的设计与实现
    prometheus监控mysql主从
    maven推送本地jar包到nexus仓库遇到的问题
    商城免费搭建之java商城 java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    sscanf提取相应字符到数组
    2022亚太杯数学建模竞赛C题思路解析
    智能晾衣架(一)--设计
    .NET餐厅管理系统sql数据帮助类执行SQL返回DataReader数据集、执行SQL语句,返回影响的记录数、执行多条SQL语句,实现数据库事务。
    (附源码)ssm智慧社区管理系统 毕业设计 101635
  • 原文地址:https://blog.csdn.net/u010650845/article/details/126345606