Re: C++ Memory Management Innovation: GC Allocator
On Apr 22, 4:13 am, xushiwei <xushiwe...@gmail.com> wrote:
To obtain a copy of this paper in pdf format click here (http://
xushiwei.com/local--files/gc-allocator/GCAllocator.pdf or from google
code:http://code.google.com/p/stdext/downloads/list). Another copy is
also available on google docs (http://docs.google.com/View?
docid=dds5zgx6_353dc5k4fcq) in html format.
[ the rest skipped]
Had a look at your code... Looks unnecessarily complicated to me. Here
is the result of about 15 minutes of work:
[code]
#include <deque>
#include <cassert>
using namespace std;
struct FastAlloc
{
struct Block
{
unsigned char memory[1024];
};
deque<Block> m_pool;
size_t m_left;
FastAlloc() : m_left(0) {}
void* allocate(size_t size)
{
assert(size <= 1024);
if (size > m_left)
{
m_pool.resize(m_pool.size() + 1);
m_left = 1024;
}
void* res = m_pool.back().memory + 1024 - m_left;
m_left -= size;
return res;
}
};
template<class T>
class my_alloc
{
public:
FastAlloc* m_alloc;
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef value_type const* const_pointer;
typedef value_type const& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<class _Other>
struct rebind
{ // convert an my_alloc<_Ty> to an my_alloc <_Other>
typedef my_alloc<_Other> other;
};
pointer address(reference _Val) const
{ // return address of mutable _Val
return pointer(&_Val);
}
const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return const_pointer(&_Val);
}
/*
my_alloc()
{ // construct default my_alloc (do nothing)
}
*/
my_alloc(FastAlloc& alloc) : m_alloc(&alloc)
{ // construct default my_alloc (do nothing)
}
my_alloc(const my_alloc<T>& o)
{ // construct by copying (do nothing)
m_alloc = o.m_alloc;
}
template<class _Other>
my_alloc(const my_alloc<_Other>& o)
{ // construct from a related my_alloc (do nothing)
m_alloc = o.m_alloc;
}
template<class _Other>
my_alloc<T>& operator=(const my_alloc<_Other>&)
{ // assign from a related my_alloc (do nothing)
m_alloc = o.m_alloc;
return (*this);
}
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
}
pointer allocate(size_type _Count, const void _FARQ * = 0)
{ // allocate array of _Count elements, ignore hint
return pointer(m_alloc->allocate(_Count*sizeof(T)));
}
void construct(pointer _Ptr, T const& _Val)
{ // construct object at _Ptr with value _Val
new (_Ptr) T(_Val);
}
void destroy(pointer _Ptr)
{ // destroy object at _Ptr
(*_Ptr).~T();
}
std::size_t max_size() const
{ // estimate maximum array size
std::size_t _Count = (std::size_t)(-1) / sizeof (T);
return (0 < _Count ? _Count : 1);
}
};
int main()
{
FastAlloc alloc;
deque<int, my_alloc<int> > q(alloc);
q.push_back(1024);
return 1;
}
[/code]
add STD_NEW/STD_DELETE support, parametrize my_alloc with allocator
type, write few different allocator classes and that's it. Looks much
more simple.
Sincerely yours,
Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]