Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: C++ Memory ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 37 of 41 Topic 9525 of 9775
Post > Topic >>

Re: C++ Memory Management Innovation: GC Allocator

by Michael Kilburn <crusader.mike@[EMAIL PROTECTED] > May 6, 2008 at 08:22 AM

On Apr 22, 4:13 am, xu****wei <xu****we...@[EMAIL PROTECTED]
> wrote:
> To obtain a copy of this paper in pdf format click here (http://
> xu****wei.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 sup****t, 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! ]
 




 41 Posts in Topic:
[Proposal for ISO C/C++] C++ Memory Management Innovation: GC A
xushiwei <xushiweizh@[  2008-04-21 12:13:18 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-21 15:39:15 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-21 16:13:16 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
Alberto Ganesh Barbati &l  2008-04-21 20:43:17 
Re: C++ Memory Management Innovation: GC Allocator
marlow.andrew@[EMAIL PROT  2008-04-21 20:43:18 
Re: C++ Memory Management Innovation: GC Allocator
Lance Diduck <lancedid  2008-04-21 20:43:17 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 00:08:55 
Re: [Proposal for ISO C/C++] C++ Memory Management Innovation: G
"Chris Thomasson&quo  2008-04-22 10:52:14 
Re: C++ Memory Management Innovation: GC Allocator
Lance Diduck <lancedid  2008-04-22 11:17:48 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 11:17:25 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 11:13:27 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 15:19:26 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 15:19:18 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 15:22:50 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 15:43:17 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 22:26:21 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-22 22:25:42 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-22 22:32:36 
Re: C++ Memory Management Innovation: GC Allocator
Francis Glassborow <fr  2008-04-23 14:13:19 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-23 14:43:18 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-24 01:43:35 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-25 03:42:49 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:47:43 
Re: C++ Memory Management Innovation: GC Allocator
"Stephen Howe"   2008-04-25 10:48:18 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:47:13 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:45:25 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-25 10:44:50 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-25 16:15:02 
Re: C++ Memory Management Innovation: GC Allocator
Yongwei Wu <wuyongwei@  2008-04-28 15:58:48 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-28 23:26:31 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-28 23:27:55 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-29 17:43:31 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 04:13:27 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 04:13:33 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-04-30 17:03:54 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-04-30 21:55:29 
Re: C++ Memory Management Innovation: GC Allocator
Michael Kilburn <crusa  2008-05-06 08:22:59 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-05-06 08:20:19 
Re: C++ Memory Management Innovation: GC Allocator
"Chris Thomasson&quo  2008-05-07 00:31:29 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-05-07 11:03:52 
Re: C++ Memory Management Innovation: GC Allocator
xushiwei <xushiweizh@[  2008-05-08 03:28:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:26:05 CDT 2008.