Topic: Placement syntax and STL


Author: mangino@saturn.planet.net (Reed Mangino)
Date: 1997/12/17
Raw View
I have run into a situation where I need to specify the location of memory
utilization when using an STL list<>.

This would not normally be a problem if I were writing my own list:
I would just build my list and call new using 'placement syntax' so
that the allocation/deallocation occurs within my own memory block
(as opposed to the default heap).

For example:

void* operator new(size_t, void* p) { return p; }

void *buf = malloc ( sizeof( foo ) );    // allocate a buffer
foo *fp = new ( buf ) foo;               // force new to allocate memory
                                         // from my buffer


I have been looking at how to implement this mechanism when using
an STL list<>.  Do I have to define my own allocator and pass that
into the constructor of the list<>:

list < foo, MyAllocator() > SpecialList;

Or do I somehow derive a special class of list<> itself??

Any information you might want to pass along would be greatly appreciated.

Thanks,
   Reed

******************************************************
Reed Mangino
Software Engineer
Dialogic Corporation
     phone:  973-993-3000 ext. 6535
     mailto:r.mangino@dialogic.com
     Get the Dialogic Edge at: http://www.dialogic.com
******************************************************



--

    ^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
     Reed R. Mangino       |  ** Dialogic Corporation **
     manginor@dialogic.com | World leader in the design of
     mangino@planet.net    |  computer telephony systems
   -----------------------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "greg" <dont@spam.me>
Date: 1997/12/18
Raw View
Reed Mangino <mangino@saturn.planet.net> wrote in article
<679k4d$he3@jupiter.planet.net>...
> I have run into a situation where I need to specify the location of
memory
> utilization when using an STL list<>.
>
> This would not normally be a problem if I were writing my own list:
> I would just build my list and call new using 'placement syntax' so
> that the allocation/deallocation occurs within my own memory block
> (as opposed to the default heap).
>
> For example:
>
> void* operator new(size_t, void* p) { return p; }
>
> void *buf = malloc ( sizeof( foo ) );    // allocate a buffer
> foo *fp = new ( buf ) foo;               // force new to allocate memory
>                                          // from my buffer
>
>
> I have been looking at how to implement this mechanism when using
> an STL list<>.  Do I have to define my own allocator and pass that
> into the constructor of the list<>:
>
> list < foo, MyAllocator() > SpecialList;

Yes.  But be careful, because the Standard promises very little
about how containers work with non-standard allocators.  I
assume that you want do something like this:

 list< foo, MyAllocator(buffer,buffer_size) > SpecialList;

Where the allocator get its storage from buffer.  If you do, then
beware of splicing or swapping SpecialList nodes onto another list
with a different allocator.

> Or do I somehow derive a special class of list<> itself??

You might want to, but you shouldn't have to.

Greg Colvin
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/12/19
Raw View
Reed Mangino <mangino@saturn.planet.net> wrote:
: I have run into a situation where I need to specify the location of memory
: utilization when using an STL list<>.

: This would not normally be a problem if I were writing my own list:
: I would just build my list and call new using 'placement syntax' so
: that the allocation/deallocation occurs within my own memory block
: (as opposed to the default heap).

: For example:

: void* operator new(size_t, void* p) { return p; }

: void *buf = malloc ( sizeof( foo ) );    // allocate a buffer
: foo *fp = new ( buf ) foo;               // force new to allocate memory
:                                          // from my buffer


: I have been looking at how to implement this mechanism when using
: an STL list<>.  Do I have to define my own allocator and pass that
: into the constructor of the list<>:

That's the best approach.

: list < foo, MyAllocator() > SpecialList;

make it list<foo, MyAllocator> or list< foo, MyAllocator<foo> ,>
whichever is appropriate.

: Or do I somehow derive a special class of list<> itself??

No. list<> doesn't have a virtual destructor. You can't derive
from it.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/22
Raw View
greg <dont@spam.me> writes:

> Reed Mangino <mangino@saturn.planet.net> wrote in article
> <679k4d$he3@jupiter.planet.net>...
> > I have run into a situation where I need to specify the location of
> memory
> > utilization when using an STL list<>.

> I assume that you want do something like this:
>
>  list< foo, MyAllocator(buffer,buffer_size) > SpecialList;
>

Since two posts in this thread have used this ill-formed
syntax, I think it's better to post the correct one:

list <foo, MyAllocator> SpecialList (MyAllocator(buffer,buffer_size));
           ^^^^^^^^^^^               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            typename                        value of the type

All STL containners take template arguments which are types.
The ctor takes a value of the right type.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]