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

posted @ 2018-01-24 17:57  larkin-cn  阅读(521)  评论(0编辑  收藏  举报