Re: allocating stl containers in memory buffer

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 01/01/05


Date: Sat, 1 Jan 2005 16:30:08 -0500


"Tomasz Grobelny" <grotk@poczta.onet.pl> wrote...
> Is it possible to create for example stl queue in previously allocated
> memory buffer (memory shared between two processes)? I thought of sth like
> that:
> queue<int>* q=new(buffer) queue<int>;
> but won't integer variables (and possible others needed by queue object)
> be
> allocated using default allocator outside the buffer? How to force
> allocation in specified buffer? Access to shared memory is protected by
> semaphores so the should be no problem with amount of requested memory.
> And
> how do I force the second program to view the buffer as queue<int>? Is
> casting enough?

With standard containers there are two allocations you need to concern
yourself with. One is the object of the type 'queue<int>' and the other
is for every contained 'int'. First one you definitely can allocate in
whatever buffer by using the method you showed (placement new). To use
any special memory for the other allocation, you need to supply your
[custom] allocator to your queue object.

Implementing custom allocators is a subject for a chapter in a book, so do
look it up in your favourite C++ book that deals with standard containers.

Good luck!

V



Relevant Pages

  • Re: Wait free queue
    ... dealloc, lock-free remote memory dealloc among producers, wait-free ... memory allocator blows the msvcrt away--why bother with it, ... Writing a Generalized Concurrent Queue ... struct queue_node* next; ...
    (comp.programming.threads)
  • Re: Benchmark: STLs list vs. hand-coded one
    ... And having 64MB buffer allocated you will need to write your own new, ... or malloc() implementations to manage memory in the buffer and do ... unlikely that a custom memory allocator can be any faster than a generic ...
    (comp.arch.embedded)
  • Re: Critique on my solution for an exercise. (check if a date is valid)
    ... simplify anything, when done in Forth. ... N times the allocation of one fixed sized object through dynamic memory. ... Thus you rely on general memory allocator, which isn't written in Forth way ... scalability problems, which is bullshit. ...
    (comp.lang.forth)
  • Re: Benchmark: STLs list vs. hand-coded one
    ... And having 64MB buffer allocated you will need to write your own new, ... or malloc() implementations to manage memory in the buffer and do ... unlikely that a custom memory allocator can be any faster than a generic ...
    (comp.arch.embedded)
  • Re: [PATCH] change gen_pool allocator to not touch managed memory
    ... The following patch modifies the gen_pool allocator to ... change is to eliminate the touching of the actual memory being allocated. ... + * Add a new chunk of memory to the specified pool. ... starting address of memory chunk to add to pool ...
    (Linux-Kernel)

Loading