zoukankan      html  css  js  c++  java
  • x memory pool c语言 内存池

    #ifndef X_MEMORY_H
    
    #define X_MEMORY_H
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <memory.h>
    
    typedef enum
    {
        ErrorUnknown,
        NoError,
        ErrorInit,            //无法申请系统内存
        ErrorOverBlockSize    //超过默认块大小
    }XMemErrorCode;
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
        void* xmalloc(int size, int* code);
    
        void xfree(void* data);
    
    
    #ifdef __cplusplus
    }
    #endif
    
    
    #endif



    #include "xmemory.h"
    
    #pragma pack(1)
    
    typedef struct  
    {
        void* pre;
        void* next;
        unsigned int size;
        unsigned char flag;
    }stBlock;
    
    #pragma pack()
    
    static stBlock* head = NULL;
    
    #define BLOCK_DEFAULT_SIZE    10485760    //1024*1024*10
    
    void xlock()
    {
    
    }
    
    void xunlock()
    {
    
    }
    
    int remalloc(stBlock* tail)
    {
        static unsigned char blockflag = 1;
        stBlock* temhead = NULL;
        stBlock* temtaill = NULL;
        char* nhead = (char*)malloc(BLOCK_DEFAULT_SIZE);
        if (NULL == nhead)
        {
            return ErrorInit;
        }
        memset(nhead, 0, BLOCK_DEFAULT_SIZE);
        temhead = (stBlock*)nhead;
        temhead->pre = tail;
        temhead->next = nhead + BLOCK_DEFAULT_SIZE - sizeof(stBlock);
        temhead->size = 0;
        temhead->flag = blockflag;
    
        temtaill = temhead->next;
        temtaill->pre = temhead;
        temtaill->next = NULL;
        temtaill->size = 0;
        temtaill->flag = blockflag;
    
        if (NULL == tail)
        {
            head = temhead;
        } 
        else
        {
            tail->next = temhead;
        }    
        blockflag++;
        return NoError;
    }
    
    void* xmalloc(int size, int* code)
    {
        stBlock* blk = head;
        stBlock* nblk = NULL;
        stBlock* blknext = NULL;
        char* ret = NULL;
        if (size >= BLOCK_DEFAULT_SIZE)
        {
            if (code){
                *code = ErrorOverBlockSize;
            }
            return NULL;
        }
        xlock();
        if (NULL == head)
        {
            if(ErrorInit == remalloc(NULL) /*initMemory(BLOCK_DEFAULT_SIZE)*/){
                xunlock();
                if (code){
                    *code = ErrorInit;
                }
                return NULL;
            }
            blk = head;
        }
        do 
        {
            
            int validsize = (char*)blk->next - (char*)blk - sizeof(stBlock)*2 - blk->size;
            if(validsize >= size){
                
                nblk = (stBlock*)((char*)blk+sizeof(stBlock)+blk->size);
                
                nblk->size = size;
                nblk->next = blk->next;
                nblk->pre = blk;
                nblk->flag = blk->flag;
                
                blk->next = nblk;
                break;
            }else{    
                blk = blk->next;
                if (NULL == blk->next)
                {
                    if (ErrorInit == remalloc(blk))
                    {
                        xunlock();
                        if (code){
                            *code = ErrorInit;
                        }
                        return NULL;
                    }
                }
                blknext = blk->next;
                if (blk->flag != blknext->flag)
                {
                    blk = blk->next;
                }
                
            }
            
        } while (1);
        ret = (char*)nblk+sizeof(stBlock);
        memset(ret, 0, size);
        xunlock();
        if (code){
            *code = NoError;
        }
        return ret;
    }
    
    void xfree(void* data)
    {
        stBlock* blk = head;
        stBlock* preblk = NULL;
        xlock();
        do 
        {
            
            if ((char*)blk+sizeof(stBlock) == data)
            {            
                preblk = blk->pre;
                preblk->next = blk->next;            
                break;
            }else
            {
                blk = blk->next;
            }
        } while (blk);
        xunlock();
    }

    源码地址:  https://gitee.com/larkin_xu/xmemory

  • 相关阅读:
    Atitit  技术经理职责与流程表总结
    Atitit.pagging  翻页功能解决方案专题 与 目录大纲 v3 r44.docx
    Atitit 翻页功能的解决方案与版本历史 v4 r49
    Atitit 数据库表文档生成解决方案
    Atiit 常见功能 常用功能与模块的最快速解决方案
    Atitit 列出wifi热点以及连接
    Atiitt 使用java语言编写sql函数或存储过程
    Atitit 数据库 标准库  sdk 函数库 编程语言 mysql oracle  attilax总结
    Atitit 纯java项目的提升进度大法通用json dsl接口
    Atitit 数据融合merge功能v3新特性.docx
  • 原文地址:https://www.cnblogs.com/larkin-cn/p/8342887.html
Copyright © 2011-2022 走看看