内存池申请的内存如何释放
c++吧
全部回复
仅看楼主
level 11
whatofor 楼主
下面这个看起来是个内存池的程序。
将 小于 128bytes的 堆内存申请进行管理。
可是,我不知道在程序运行结束之前怎么释放 内存池进行管理的内存。
求解
2011年08月08日 15点08分 1
level 11
whatofor 楼主
#include<stdlib.h>
#include<stddef.h>
#include<string.h>
#include<stdio.h>
#define ALIGN 8
#define MAX_BYTES 128
#define NFREELISTS (MAX_BYTES/ALIGN) //16
typedef union obj
{
    union obj *next_free_link;
    char clint[1];
}obj;
static obj *free_list[NFREELISTS]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
static char *start_free=0,*end_free=0;
static size_t heap_size=0;
static inline size_t ROUND_UP(size_t bytes)
{
    return ( ((bytes)+ALIGN-1) & ~(ALIGN-1) );
}
static inline size_t FREELIST_INDEX(size_t bytes)
{
    return (((bytes)+ALIGN-1)/ALIGN-1);
}
static void * refill(size_t );
static void * chunk_alloc(size_t,int *);
static void *allocate(size_t n)
{
    obj **my_free_list;
    obj *result;
    if(n>MAX_BYTES)return malloc(n);
    my_free_list=free_list+FREELIST_INDEX(n);
    result=*my_free_list;
    if(0==result)
    {
        void *re=refill(ROUND_UP(n));
        return re;
    }
    *my_free_list=result->next_free_link;
    return (result);
}
static void deallocate(void *p,size_t n)
{
    obj *q=(obj *)p;
    obj **my_free_list;
    if(n>(size_t)MAX_BYTES)
    {
        free(p);
        return ;
    }
    my_free_list=free_list+FREELIST_INDEX(n);
    q->next_free_link=*my_free_list;
    *my_free_list=q;
}
static void *reallocate(void *p,size_t old_size,size_t new_size)
{
    obj *result;
    size_t copy_size;
    if(new_size>(size_t)MAX_BYTES && old_size>(size_t)MAX_BYTES)
    {
        return realloc(p,new_size);
    }
    if(ROUND_UP(old_size)==ROUND_UP(new_size))return p;
    result=allocate(new_size);
    copy_size = new_size<old_size ? new_size : old_size;
    memcpy(result,p,copy_size);
    deallocate(p,old_size);
}
void *chunk_alloc(size_t size,int *n)
{
    char *result;
    size_t total_bytes=*n*size;
    size_t heap_left=end_free-start_free;
    obj **my_free_list,*p;

2011年08月08日 15点08分 2
level 11
whatofor 楼主
    int i;
    // 剩余内存完全够用
    if(heap_left>total_bytes)
    {
        result=start_free;
        start_free+=total_bytes;
        return (result);
    }
    else if(heap_left>size) //剩余内存 够一部分。
    {
        result=start_free;
        *n=heap_left/size;
        start_free+=*n*size;
        return result;
    }
    else //一个都不够。
    {
        size_t heap_to_get=2*total_bytes+ROUND_UP(heap_size>>4);
        //将堆中残留内存 链接进入维护的链表
        if(heap_left>0)
        {
            my_free_list=free_list+FREELIST_INDEX(heap_left);
            ((obj *)start_free)->next_free_link=*my_free_list;
            *my_free_list=(obj *)start_free;
        }
        start_free=(char *)malloc(heap_to_get);
        //堆中分配内存。
        if(0==start_free)
        {
            // 从其余的 链表中 寻找 空余内存,释放以便使用
            for(i=size;i<MAX_BYTES;i+=ALIGN)
            {
                my_free_list=free_list+FREELIST_INDEX(i);
                p=*my_free_list;
                if(p!=0)
                {
                    *my_free_list=p->next_free_link;
                    start_free=(char *)p;
                    end_free=start_free+i;

2011年08月08日 15点08分 3
level 11
whatofor 楼主
                    return (chunk_alloc(size,n));
                }
            }
            end_free=0;
            // 再次尝试申请。
            start_free=(char *)malloc(heap_to_get);
            //如果依然无法申请成功。
            if(start_free==0)
            {
                fprintf(stderr,"Out Of Memory!!!\n file:%s \nline:%s",__FILE__,__LINE__);
                abort();
            }
        }
        //申请成功。
        heap_size+=heap_to_get;
        end_free=start_free+heap_to_get;
        return chunk_alloc(size,n);
    }
}
// 假定n为8的倍数。
void *refill(size_t n)
{
    int nobj=20;
    char * chunk=chunk_alloc(n,&nobj);
    obj **my_free_list;
    obj *result;
    obj *current_obj,*next_obj;
    int i;
    if(nobj==1)return (chunk);
    my_free_list=free_list+FREELIST_INDEX(n);
    result=(obj *)chunk;
    *my_free_list=next_obj=(obj *)(chunk+n);
    for(i=1;;i++)
    {
        current_obj=next_obj;
        next_obj=(obj *)((char *)next_obj+n);
        if(i==nobj-1)
        {
            current_obj->next_free_link=0;
            break;
        }
        else
        {
            current_obj->next_free_link=next_obj;
        }
    }
    return (result);
}
int main()
{
    void * p=allocate(100);
    dealloc(p,100);
    return 0;
}
2011年08月08日 15点08分 4
level 11
whatofor 楼主
main函数无视。
deallocate。。。
2011年08月08日 15点08分 5
level 11
whatofor 楼主
SOS
2011年08月08日 15点08分 6
level 12
把要释放的chunk的结点放到free_list去
代码乱先不看
2011年08月08日 16点08分 7
level 11
whatofor 楼主
deallocate 的释放 是把 要释放的 放回 free_list.
可关键是 在 chunk_alloc这个函数中用malloc申请的内存,在程序结束的时候怎么释放。。。
2011年08月09日 00点08分 8
level 11
那是操作系统自动完成的了
2011年08月09日 00点08分 9
level 11
whatofor 楼主
难道要我去指望OS?
2011年08月09日 00点08分 10
level 11
你程序都结束了、关闭了,它占的内存还不能自动释放吗?
2011年08月09日 00点08分 11
level 11
whatofor 楼主
怎么确保没有内存泄露?
2011年08月09日 00点08分 12
level 11
在程序执行的时候不泄露就行了
2011年08月09日 00点08分 13
level 11
whatofor 楼主
第一次听到这种观点,不放心。。。
2011年08月09日 00点08分 14
level 11
whatofor 楼主
@幻の上帝
求助
2011年08月09日 00点08分 15
level 11
whatofor 楼主
2011年08月09日 00点08分 16
level 11
whatofor 楼主
你的意思是这段代码浪费了?
2011年08月09日 01点08分 18
level 12
把全代码发上来,我用高级玩意检测一下[揉脸]
2011年08月09日 01点08分 19
1 2 3 4 5 尾页