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

转载于:https://www.cnblogs.com/larkin/p/8342887.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值