[pool] Definition of a static_pool ?

Greetings, The Boost.Pool class describes a pool of buffer, pool whose size grows as needed. This is very convenient but there usage cases where the pool should not perform any dynamic memory allocation, i.e. the maximum number of simultaneously required buffers is known at initialization. I have hence defined a simple class 'static_pool' (see below), which (i) allocate all requested buffers at initialization; (ii) fails (returns null) if more buffers are being asked for at runtime. Does such a class has its place within the Boost Pool library ? Regards, Étienne #ifndef BOOST_STATIC_POOL_HPP #define BOOST_STATIC_POOL_HPP #include <boost/pool/pool.hpp> namespace boost { template<typename UserAllocator> class static_pool : protected pool<UserAllocator> { public : // Overridden functions explicit static_pool(const size_type nrequested_size) : pool<UserAllocator>(nrequested_size, nrequested_size) { free(malloc()); } void * malloc() { return store().empty() ? NULL : store().malloc(); } public : // Inherited functions void free(void * const chunk) { return pool<UserAllocator>::free(chunk); } size_type get_requested_size() const { return pool<UserAllocator>::get_requestedSize(); } bool is_from(void * const chunk) const { return pool<UserAllocator>::is_from(chunk); } }; } #endif Ce message et toutes les pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute modification, édition, utilisation ou diffusion non autorisée est interdite. Si vous avez reçu ce message par erreur, merci de nous en avertir immédiatement. ATEME décline toute responsabilité au titre de ce message s'il a été altéré, déformé, falsifié ou encore édité ou diffusé sans autorisation. This message and any attachments are confidential and intended solely for the addressees. Any unauthorized modification, edition, use or dissemination is prohibited. If you have received this message by mistake, please notify us immediately. ATEME decline all responsibility for this message if it has been altered, deformed, falsified or even edited or disseminated without authorization. Note: To protect against computer viruses, e-mail programs may prevent sending or receiving certain types of file attachments.

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of DUPUIS Etienne Sent: Tuesday, April 24, 2012 11:36 AM To: boost@lists.boost.org Subject: [boost] [pool] Definition of a static_pool ?
The Boost.Pool class describes a pool of buffer, pool whose size grows as needed.
This is very convenient but there usage cases where the pool should not perform any dynamic memory allocation, i.e. the maximum number of simultaneously required buffers is known at initialization. I have hence defined a simple class 'static_pool' (see below), which (i) allocate all requested buffers at initialization; (ii) fails (returns null) if more buffers are being asked for at runtime.
Does such a class has its place within the Boost Pool library ?
I'm sure that there is some user for this (though unclear how popular or advantageous it would be - perhaps others can comment on this?). To add to Boost.Pool would need merging with the existing framework, providing tests, examples and updated documentation. If you are willing to contemplate undertaking this, you could start by setting up a trial project in your Boost-sandbox folder which starts with a copy of the existing trunk version, and see if you can get started doing this. If it look promising (and you are not daunted by the task ;-) you could ask for write access to boost-sandbox and expose it to public view and see if Boosters like it. HTH Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com

The Boost.Pool class describes a pool of buffer, pool whose size grows as needed.
This is very convenient but there usage cases where the pool should not perform any dynamic memory allocation, i.e. the maximum number of simultaneously required buffers is known at initialization. I have hence defined a simple class 'static_pool' (see below), which (i) allocate all requested buffers at initialization; (ii) fails (returns null) if more buffers are being asked for at runtime.
Does such a class has its place within the Boost Pool library ?
Paul A. Bristow wrote:
I'm sure that there is some user for this (though unclear how popular or advantageous it would be - perhaps others can comment on this?).
I also need entirely static pools that allocate memory at *compile* time via template parameter. I use this kind of pool as the basis for memory allocation in embedded microcontroller projects. So I would actually need one-shot allocation (as you describe) as well as compile-time allocation. I have always written my own pools for this. I might be nice to get one *off the rack* from boost. Good luck. Best regards, Chris.

On Thu, Apr 26, 2012 at 06:38, Christopher Kormanyos <e_float@yahoo.com>wrote:
I also need entirely static pools that allocate memory at *compile* time via template parameter. I use this kind of pool as the basis for memory allocation in embedded microcontroller projects.
So I would actually need one-shot allocation (as you describe) as well as compile-time allocation.
I have always written my own pools for this. I might be nice to get one *off the rack* from boost.
Same here, I use both runtime defined size pools and compile-time defined size pools. About singleton_pool, my understanding is that it provide a totally orthogonal feature to this static_pool...right? Joel Lamotte

-----Message d'origine----- De : boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] De la part de Klaim - Joël Lamotte Envoyé : jeudi 26 avril 2012 04:29 À : boost@lists.boost.org Objet : Re: [boost] [pool] Definition of a static_pool ? On Thu, Apr 26, 2012 at 06:38, Christopher Kormanyos <e_float@yahoo.com>wrote: Same here, I use both runtime defined size pools and compile-time defined size pools. Joel Lamotte Having both classes sounds good, perhaps I will start by trying to add the runtime pool correctly (with test cases and documentation...) and afterward perhaps if Christopher and/or Joël could send me their implementation of compile-time pools, I could also include it in my development. Regards, Étienne Ce message et toutes les pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute modification, édition, utilisation ou diffusion non autorisée est interdite. Si vous avez reçu ce message par erreur, merci de nous en avertir immédiatement. ATEME décline toute responsabilité au titre de ce message s'il a été altéré, déformé, falsifié ou encore édité ou diffusé sans autorisation. This message and any attachments are confidential and intended solely for the addressees. Any unauthorized modification, edition, use or dissemination is prohibited. If you have received this message by mistake, please notify us immediately. ATEME decline all responsibility for this message if it has been altered, deformed, falsified or even edited or disseminated without authorization. Note: To protect against computer viruses, e-mail programs may prevent sending or receiving certain types of file attachments.

On Fri, Apr 27, 2012 at 15:17, DUPUIS Etienne <e.dupuis@ateme.com> wrote:
Having both classes sounds good, perhaps I will start by trying to add the runtime pool correctly (with test cases and documentation...) and afterward perhaps if Christopher and/or Joël could send me their implementation of compile-time pools, I could also include it in my development.
My pool implementations are either boost pool or a class encapsulating a raw array... not really made to be generic in fact. But a generic solution is welcome.

Same here, I use both runtime defined size pools and compile-time defined size pools.
Joel Lamotte
Having both classes sounds good, perhaps I will start by trying to add the runtime pool correctly (with test cases and documentation...) and afterward perhaps if Christopher and/or Joël could send me their implementation of compile-time pools, I could also include it in my development. Regards, Étienne
I was wrong. It looks like boost's development and my own development simply overlap. It appears as though I merely reinvented the wheel on this one. I often make this kind of mistake---charge ahead with my own development before actually knowing what's there in the first place. My methods simply parallel those of boost's fast_pool_allocator (with a dummy Mutex template parameter) in combination with boost's singleton_pool. So my code is very similar to boost's. Sorry about that. One other idea that may be of interest, though, is the concept of "placing" a custom allocator and its associated pool at a known physical address and granting these the one-shot allocation character that you describe. I have used this technique in combination with various sets of microcontroller registers to overly STL containers like a vector on top of, say, the background readings from a hardware DMA. In this way the DMA "fills up" your vector in a CPU-independent fashion and the user can read the results from the underlying hardware layer---properly packed in the container---with essentially zero cost. I have not yet seen anyone else do this with the STL. Best regards, Chris.

On Sat, Apr 28, 2012 at 12:44 PM, Christopher Kormanyos <e_float@yahoo.com> wrote:
One other idea that may be of interest, though, is the concept of "placing" a custom allocator and its associated pool at a known physical address and granting these the one-shot allocation character that you describe. I have used this technique in combination with various sets of microcontroller registers to overly STL containers like a vector on top of, say, the background readings from a hardware DMA. In this way the DMA "fills up" your vector in a CPU-independent fashion and the user can read the results from the underlying hardware layer---properly packed in the container---with essentially zero cost. I have not yet seen anyone else do this with the STL.
I assume this vector can't be resized. Why use a vector instead of for example an array or ptr pair? -- Olaf

One other idea that may be of interest, though, is the concept
of "placing" a custom allocator and its associated pool at a known physical address and granting these the one-shot allocation character that you describe. I have used this technique in combination with various sets of microcontroller registers to overly STL containers like a vector on top of, say, the background readings from a hardware DMA. In this way the DMA "fills up" your vector in a CPU-independent fashion and the user can read the results from the underlying hardware layer---properly packed in the container---with essentially zero cost. I have not yet seen anyone else do this with the STL.
I assume this vector can't be resized. Why use a vector instead of for example an array or ptr pair? Olaf
Because I never figured out a way to *place* an array at the known physical address in memory where I want it, such as 0xFFE0, or whatever the address of, say, the ADC result registers may be in the microcontroller. Maybe there is a simpler way to do things like this. But I never got it. Any advice would be welcome. Best regards, Chris.

On Sat, Apr 28, 2012 at 2:48 PM, Christopher Kormanyos <e_float@yahoo.com> wrote:
I assume this vector can't be resized. Why use a vector instead of for example an array or ptr pair? Olaf
Because I never figured out a way to *place* an array at the known physical address in memory where I want it, such as 0xFFE0, or whatever the address of, say, the ADC result registers may be in the microcontroller.
Maybe there is a simpler way to do things like this. But I never got it. Any advice would be welcome.
What about placement new? Or an array_ref (not part of Boost). -- Olaf

Because I never figured out a way to *place* an array at
the known physical address in memory where I want it, such as 0xFFE0, or whatever the address of, say, the ADC result registers may be in the microcontroller.
Maybe there is a simpler way to do things like this. But I never got it. Any advice would be welcome.
What about placement new? Or an array_ref (not part of Boost). Olaf
Sorry, I was unclear. I meant that I never figured out how to put std::array at an address of my choice. Unfortunately, I have written countless custom arrays and custom allocators, etc. for this, constantly making and re-making the same mistakes over and over again. I generally use 3 methods for memory-mapped arrays. The methods 2 and 3 are related to Etienne's OP. But since I was wrong about the scope of Boost.Pool in the first place, it might be able to do it and I just don't know. 1) In C and C++ if I don't need size(), begin(), end(), etc., I use something like this #define adc_results ((volatile uint16_t*) 0xFFE0) 2) If I need size(), begin(), end() and also want to give the compiler freedom for loop unrolling, I use something like this template<typename address_type, const address_type addr, const unsigned N> class memory_mapped_array { // And basically write most of std::array, but without data, // only using the (volatile) address ... }; 3) If I really need the comfort of a container, I use something like this in combination with an STL container. template<typename address_type, const address_type addr> class memory_mapped_allocator { // And basically write std::allocator conforming allocator // but at a volatile address. }; I keep writing methods 2 and 3 over and over again, probably never really getting it right. I thought if Etienne wants to extend Boost.Pool, others might want memory-mapped pools and allocators. But we're getting too far off the OP. Thanks and best regards, Chris.

On Sat, Apr 28, 2012 at 9:16 PM, Christopher Kormanyos <e_float@yahoo.com> wrote:
Because I never figured out a way to *place* an array at
the known physical address in memory where I want it, such as 0xFFE0, or whatever the address of, say, the ADC result registers may be in the microcontroller.
Maybe there is a simpler way to do things like this. But I never got it. Any advice would be welcome.
What about placement new? Or an array_ref (not part of Boost). Olaf
Sorry, I was unclear. I meant that I never figured out how to put std::array at an address of my choice. Unfortunately,
Wouldn't placement new allow you to do just that? -- Olaf

What about placement new? Or an array_ref (not part of Boost). Olaf
Sorry, I was unclear. I meant that I never figured out how to put std::array at an address of my choice. Unfortunately,
Wouldn't placement new allow you to do just that?
That's what I thought to, assuming say you have 8 32-bit ints at address "myhardware", then something like: typedef std::array<volatile std::int32_t, 8> array_type; array_type* pa = new(myhardware) array_type; assert(&(*pa[0]) == myhardware); ?? HTH, John.

On Sun, Apr 29, 2012 at 2:44 PM, John Maddock <boost.regex@virgin.net> wrote:
Wouldn't placement new allow you to do just that?
That's what I thought to, assuming say you have 8 32-bit ints at address "myhardware", then something like:
typedef std::array<volatile std::int32_t, 8> array_type;
array_type* pa = new(myhardware) array_type;
assert(&(*pa[0]) == myhardware);
boost::iterator_range might work too, but unfortunately it doesn't provide operator[] and data(). -- Olaf

What about placement new?
Or an array_ref (not part of Boost). Olaf
Sorry, I was unclear. I meant that I never figured out how to put std::array at an address of my choice. Unfortunately,
Wouldn't placement new allow you to do just that?
That's what I thought to, assuming say you have 8 32-bit ints at address "myhardware", then something like:
typedef std::array<volatile std::int32_t, 8> array_type; array_type* pa = new(myhardware) array_type; assert(&(*pa[0]) == myhardware); ?? HTH, John.
Oh, that's easy. It seems obvious now, but I never got it. Thanks so much for the clarification, John and Olaf. It helps. Best regards, Chris.

typedef std::array<volatile std::int32_t, 8> array_type; array_type* pa = new(myhardware) array_type; assert(&(*pa[0]) == myhardware); ?? HTH, John.
Oh, that's easy. It seems obvious now, but I never got it. Thanks so much for the clarification, John and Olaf. It helps.
But did you spot the "deliberate" mistake? ;-) Should be assert(&(*pa)[0] == myhardware); John.

typedef std::array<volatile std::int32_t, 8> array_type;
array_type* pa = new(myhardware) array_type; assert(&(*pa[0]) == myhardware); ?? HTH, John.
Oh, that's easy. It seems obvious now, but I never got it. Thanks so much for the clarification, John and Olaf. It helps.
But did you spot the "deliberate" mistake? ;-) Only now, after you mentioned it. Actually, I'm relived that it can happen to you too.
Should be assert(&(*pa)[0] == myhardware); John.
Cool. Thanks, Chris.

-----Message d'origine----- De : boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] De la part de Paul A. Bristow Envoyé : mercredi 25 avril 2012 12:16 À : boost@lists.boost.org Objet : Re: [boost] [pool] Definition of a static_pool ? To add to Boost.Pool would need merging with the existing framework, providing tests, examples and updated documentation. If you are willing to contemplate undertaking this, you could start by setting up a trial project in your Boost-sandbox folder which starts with a copy of the existing trunk version, and see if you can get started doing this. If it look promising (and you are not daunted by the task ;-) you could ask for write access to boost-sandbox and expose it to public view and see if Boosters like it. No, I am not (yet) daunted, and I have thus requested an access to the boost sandbox to start adding the new class to boost.pool. Regards, Étienne Ce message et toutes les pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute modification, édition, utilisation ou diffusion non autorisée est interdite. Si vous avez reçu ce message par erreur, merci de nous en avertir immédiatement. ATEME décline toute responsabilité au titre de ce message s'il a été altéré, déformé, falsifié ou encore édité ou diffusé sans autorisation. This message and any attachments are confidential and intended solely for the addressees. Any unauthorized modification, edition, use or dissemination is prohibited. If you have received this message by mistake, please notify us immediately. ATEME decline all responsibility for this message if it has been altered, deformed, falsified or even edited or disseminated without authorization. Note: To protect against computer viruses, e-mail programs may prevent sending or receiving certain types of file attachments.

Étienne wrote:
This is very convenient but there usage cases where the pool should not perform any dynamic memory allocation, i.e. the maximum number of simultaneously required buffers is known at initialization. I have hence defined a simple class 'static_pool' (see below), which (i) allocate all requested buffers at initialization; (ii) fails (returns null) if more buffers are being asked for at runtime.
Does such a class has its place within the Boost Pool library ?
Sounds much like singleton_pool. Chris
participants (7)
-
Christopher Kormanyos
-
DUPUIS Etienne
-
Hite, Christopher
-
John Maddock
-
Klaim - Joël Lamotte
-
Olaf van der Spek
-
Paul A. Bristow