custom container allocators and custom memory allocators 
Author Message
 custom container allocators and custom memory allocators

I have a custom memory allocator with handles for different memory
pools.  I can create a custom allocator template by simply starting
with the Josuttis example code.  But I do not see how to get our
custom memory allocator with handles working with custom STL
allocators - how do I get my handle information to the allocator so
that the handle may be used in the calls in the template to the
overridden operator new?  I must be overlooking something simple...


Sun, 17 Jul 2005 07:25:03 GMT  
 custom container allocators and custom memory allocators

Quote:
>I have a custom memory allocator with handles for different memory
>pools.  I can create a custom allocator template by simply starting
>with the Josuttis example code.  But I do not see how to get our
>custom memory allocator with handles working with custom STL
>allocators - how do I get my handle information to the allocator so
>that the handle may be used in the calls in the template to the
>overridden operator new?  I must be overlooking something simple...

Add the handle as a member variable of your custom allocator, and pass
it into the constructor. e.g.

explicit custom_allocator(handle_type h)
        :m_h(h)
{

Quote:
}

bool operator==(custom_allocator const& other)
{
        return m_h == other.m_h;

Quote:
}

//etc

Now, you can construct a vector like so:

HANDLE h = whatever;

std::list<int, custom_allocator<int> > l(
        custom_allocator(h)
);

A well defined implementation will use the value of the allocator you
use to construct it. Be careful copying elements between containers,
etc, though.

Tom



Sun, 17 Jul 2005 20:08:13 GMT  
 custom container allocators and custom memory allocators

Quote:

> Add the handle as a member variable of your custom allocator, and pass
> it into the constructor. e.g.

> explicit custom_allocator(handle_type h)
>    :m_h(h)
>  {
> }

> bool operator==(custom_allocator const& other)
> {
>    return m_h == other.m_h;
> }

> //etc

> Now, you can construct a vector like so:

> HANDLE h = whatever;

> std::list<int, custom_allocator<int> > l(
>    custom_allocator(h)
> );

> A well defined implementation will use the value of the allocator you
> use to construct it. Be careful copying elements between containers,
> etc, though.

> Tom

Thanks for responding.  I tried the member variable route and couldn't
figure out how to initialize the value in the constructor because the
allocator constructor is called with no parameters within the
container template.   I tried what you suggested and VC7 says
custom_allocator(h)
error C2955: 'custom_allocator' : use of class template requires
template argument list     : see declaration of 'custom_allocator'


Mon, 18 Jul 2005 02:28:45 GMT  
 custom container allocators and custom memory allocators



Quote:
> > Add the handle as a member variable of your custom allocator, and pass
> > it into the constructor. e.g.

> > explicit custom_allocator(handle_type h)
> > :m_h(h)
> >  {
> > }

> > bool operator==(custom_allocator const& other)
> > {
> > return m_h == other.m_h;
> > }

> > //etc

> > Now, you can construct a vector like so:

> > HANDLE h = whatever;

> > std::list<int, custom_allocator<int> > l(
> > custom_allocator(h)
> > );

> > A well defined implementation will use the value of the allocator you
> > use to construct it. Be careful copying elements between containers,
> > etc, though.

> > Tom

> Thanks for responding.  I tried the member variable route and couldn't
> figure out how to initialize the value in the constructor because the
> allocator constructor is called with no parameters within the
> container template.   I tried what you suggested and VC7 says
> custom_allocator(h)
> error C2955: 'custom_allocator' : use of class template requires
> template argument list     : see declaration of 'custom_allocator'

I think your problem is that you need to write the constructor call like this:
std::list<int, custom_allocator<int> > l(
 custom_allocator<int>(h)
);

Notice the "<int>" part where int  matches the value_type of your list..

/Magnus Lidbom



Mon, 18 Jul 2005 02:59:17 GMT  
 custom container allocators and custom memory allocators

Quote:

>> Add the handle as a member variable of your custom allocator, and pass
>> it into the constructor. e.g.

>> explicit custom_allocator(handle_type h)
>>        :m_h(h)
>>  {
>> }

>> bool operator==(custom_allocator const& other)
>> {
>>        return m_h == other.m_h;
>> }

>> //etc

>> Now, you can construct a vector like so:

>> HANDLE h = whatever;

>> std::list<int, custom_allocator<int> > l(
>>        custom_allocator(h)
>> );

>> A well defined implementation will use the value of the allocator you
>> use to construct it. Be careful copying elements between containers,
>> etc, though.

>> Tom

>Thanks for responding.  I tried the member variable route and couldn't
>figure out how to initialize the value in the constructor because the
>allocator constructor is called with no parameters within the
>container template.

Not if you pass it to the constructor of the container - in that case
the allocator should be copied into the one held in the container.

   I tried what you suggested and VC7 says

Quote:
>custom_allocator(h)
>error C2955: 'custom_allocator' : use of class template requires
>template argument list     : see declaration of 'custom_allocator'

Post some code...

Tom



Mon, 18 Jul 2005 03:49:57 GMT  
 custom container allocators and custom memory allocators

Quote:
> Not if you pass it to the constructor of the container - in that case
> the allocator should be copied into the one held in the container.

>    I tried what you suggested and VC7 says
> >custom_allocator(h)
> >error C2955: 'custom_allocator' : use of class template requires
> >template argument list     : see declaration of 'custom_allocator'

> Post some code...

> Tom

Thank you to you and Magnus - there was some formatting stuff my
reader didn't show or didn't show up in your response!
For anyone doing a usenet search later...

std::list< int, Myalloc< int > > TestContainer( MyAlloc< int >(
HeapHandle ) );



Mon, 18 Jul 2005 13:01:17 GMT  
 custom container allocators and custom memory allocators

Quote:
>> Not if you pass it to the constructor of the container - in that case
>> the allocator should be copied into the one held in the container.

>>    I tried what you suggested and VC7 says
>> >custom_allocator(h)
>> >error C2955: 'custom_allocator' : use of class template requires
>> >template argument list     : see declaration of 'custom_allocator'

>> Post some code...

>> Tom

>Thank you to you and Magnus - there was some formatting stuff my
>reader didn't show or didn't show up in your response!
>For anyone doing a usenet search later...

>std::list< int, Myalloc< int > > TestContainer( MyAlloc< int >(
>HeapHandle ) );

If only I could blame the newsreader. But it was a typo on my part
missing the second <int>. Magnus kindly corrected it!

Tom



Mon, 18 Jul 2005 20:59:13 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. STL custom allocator using memory mapped file

2. problem with custom allocator in VC7

3. Profiling with custom allocator library

4. problem with custom allocator in VC7

5. copy constructor for vector with a custom allocator

6. stlport4 Has anyone written a custom allocator?

7. Trouble with type conversion in custom allocator

8. Does MS implementation support custom allocators?

9. Custom Allocator class for SGI STL

10. Custom Allocator

11. STL custom allocator

12. syntax for MyString with custom allocator

 

 
Powered by phpBB® Forum Software