游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2313|回复: 6

云风: 内存优化程序

[复制链接]

80

主题

173

帖子

189

积分

注册会员

Rank: 2

积分
189
发表于 2006-5-16 08:40:00 | 显示全部楼层 |阅读模式
//smallalloc.h
#pragma once
#include <windows.h>
#include <assert.h>
class thead_guard
{
        long *_mutex;
public:
        thead_guard(long *m);
        ~thead_guard();

};
inline thead_guard ::thead_guard(long *m)
{
        _mutex =m;
        while (::InterlockedExchange(_mutex,1))
        {
                ::Sleep(0);
        }

}
inline thead_guard ::~thead_guard()
{
        ::InterlockedExchange(_mutex,0);
}
class smallobject_allocator
{
public:
        enum
        {
                chunk_limit = 16384,
                max_number = 64,
                agign_size = 8,
                chunk_number = chunk_limit / agign_size,
        };
private:
        struct memory_list
        {
                memory_list* _next;

        };
        struct chunk_list
        {
                chunk_list *_next;
                memory_list *_data;
        };
        memory_list *_free_list[chunk_number];
        long _guard[chunk_number];
        chunk_list *_chunk_list;
        long _chunk_guard;
        static smallobject_allocator *_instance;
        static long _singleton_guard;
        static bool _singleton_destroyed;
        static void create_instance();
        static size_t chunk_index(size_t bytes)
        {
                size_t idx = (bytes -1)/agign_size;
                assert(idx >= 0 && idx < chunk_number);
                return idx;
        }
        smallobject_allocator();
        memory_list* alloc_chunk(size_t idx);
public:
        ~smallobject_allocator();
        static smallobject_allocator& instance()
        {
                if(!_instance)
                {
                        create_instance();
                }
                return *_instance;
        }
        void* allocate(size_t size);
        void deallocate(void *p,size_t size);

};
//smallalloc.cpp
#include "smallalloc.h"
smallobject_allocator * smallobject_allocator::_instance = 0;
long smallobject_allocator::_singleton_guard = 0;
bool smallobject_allocator::_singleton_destroyed = false;
void smallobject_allocator::create_instance()
{
        thead_guard guard(&_singleton_guard);
        if(_instance)
                return;
        assert(!_singleton_destroyed);
        static smallobject_allocator obj;
        _instance = &obj;
}
smallobject_allocator ::smallobject_allocator()
{
        _chunk_list =0;
        _chunk_guard =0;
        ::memset(_free_list,0,sizeof(_free_list));
        ::memset(_guard,0,sizeof(_guard));

}
smallobject_allocator ::~smallobject_allocator()
{
        int s =0;
        chunk_list *temp = _chunk_list;
        while(temp)
        {
                ++s;
                temp = temp->_next;

        }
        void **chunk = reinterpret_cast<void**>(::malloc(s*sizeof(void*)));
        temp = _chunk_list;
        int i =0;
        while (temp)
        {
                chunk = temp->_data;
                ++i;
                temp = temp->_next;
        }
        for(i=0;i<s;i++)
        {
                ::free(chunk);
        }
        ::free(chunk);
        _singleton_destroyed = true;
        _instance = 0;
}

inline size_t _min(size_t a,size_t b)
{
        return a < b ? a : b;
}
smallobject_allocator ::memory_list*
smallobject_allocator ::alloc_chunk(size_t idx)
{
        const size_t node_size = (idx+1)*agign_size;
        const size_t chunk_size = _min(chunk_limit/node_size*node_size,node_size*max_number);
        thead_guard guard(&_chunk_guard);
        memory_list * &current_list = _free_list[idx];
        if(current_list)
                return current_list;
        memory_list *ret = current_list = reinterpret_cast<memory_list*>(::malloc(chunk_size));
        memory_list *iter = ret;
        for(size_t i = 0; i<= chunk_size - node_size*2;i+=node_size)
        {
                iter = iter->_next = iter + (idx+1)*agign_size/sizeof(*iter);
        }
        iter->_next = 0;
        return ret;
}
void* smallobject_allocator ::allocate(size_t size)
{
        size_t idx = chunk_index(size);
        assert(idx<chunk_number);
        thead_guard guard(&_guard[idx]);
        memory_list * &temp = _free_list[idx];
        if(!temp)
        {
                memory_list *new_chunk = alloc_chunk(idx);
                chunk_list *chunk_node;
                if(chunk_index(sizeof(chunk_list))==idx)
                {
                        chunk_node = reinterpret_cast<chunk_list *>(temp);
                        temp = temp ->_next;
                }
                else
                {
                        chunk_node = reinterpret_cast<chunk_list*>(allocate(sizeof(chunk_list)));
                }
                thead_guard guard(&_chunk_guard);
                chunk_node->_next = _chunk_list;
                chunk_node ->_data = new_chunk;
                _chunk_list = chunk_node;
        }
        void *ret = temp;
        temp =temp->_next;
        return ret;
}
void smallobject_allocator ::deallocate(void *p,size_t size)
{
        size_t idx = chunk_index(size);
        assert(idx < chunk_number);
        memory_list *free_block = reinterpret_cast<memory_list*>(p);
        thead_guard guard(&_guard[idx]);
        memory_list * &temp = _free_list[idx];
        free_block ->_next = temp;
        temp = free_block;
}


80

主题

173

帖子

189

积分

注册会员

Rank: 2

积分
189
 楼主| 发表于 2006-5-16 14:51:00 | 显示全部楼层

Re:云风: 内存优化程序

麻烦大牛们改改这个程序。肯定是有错误。

15

主题

79

帖子

79

积分

注册会员

Rank: 2

积分
79
发表于 2006-5-17 16:45:00 | 显示全部楼层

Re:云风: 内存优化程序

我想你这程序应该是给新手看的吧,如果我写韩语,再假如你不懂韩语,不知道你希不希望我解释给你听??怎么最起码的注释都没有?

82

主题

331

帖子

340

积分

中级会员

Rank: 3Rank: 3

积分
340
QQ
发表于 2006-5-17 17:21:00 | 显示全部楼层

Re:云风: 内存优化程序

现在感觉程序注释怎么越来越少。。。

13

主题

978

帖子

978

积分

高级会员

Rank: 4

积分
978
发表于 2006-5-17 22:27:00 | 显示全部楼层

Re:云风: 内存优化程序

先不说注释
光看那糟糕的缩进……

80

主题

173

帖子

189

积分

注册会员

Rank: 2

积分
189
 楼主| 发表于 2006-5-18 07:47:00 | 显示全部楼层

Re:云风: 内存优化程序

呵呵!这个是书上的例子程序。缩进还是有的,一贴就没了。

86

主题

2251

帖子

2386

积分

金牌会员

Rank: 6Rank: 6

积分
2386
QQ
发表于 2006-5-19 13:31:00 | 显示全部楼层

Re:云风: 内存优化程序

好象云风的站上有勘误,你去看看。
贴代码用代码标签
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 12:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表