views:

1123

answers:

5

Hi,

We are using c++ to develop an application that runs in Windows CE 4 on an embedded system.

One of our constraint is that all the memory used by the application shall be allocated during startup only. We wrote a lot of containers and algorithms that are using only preallocated memory instead of allocating new one.

Do you think it is possible for us to use the boost libraries instead of our own containers in these conditions?

Any comments and/or advice are welcomed!

Thanks a lot,

Nic

+4  A: 

You could write your own allocator for the container, which allocates from a fixed size static buffer. Depending on the usage patterns of the container the allocator could be as simple as incrementing a pointer (e.g. when you only insert stuff into the container once at app startup, and don't continuously add/remove elements.)

Tobi
In my experience, some Boost libraries will use a user-specified STL-style allocator and some won't. Some allow user-defined memory allocation using some other API. And I suspect some even do all of the above. Unfortunately, I have yet to find a reliable list of which is which.
jwfearn
+1  A: 

Boost is a set of libraries. Some of them are focussed on template metaprogramming. Those don't even use any memory at runtime. But your question seems to be about replacing your containers. I'd doubt that is possible except using custom allocators. But even then, it's most likely you would be using plain STL containers and not boost. Boost only provides the TR1 containers, for those compilers that do not yet include TR1.

MSalters
A: 

Do not use Boost.

It is a big library and your basic memory allocation requirements are very different from those of the libraries designers.

Even if you can get a current version of Boost to work according to your requirements with custom allocators it may break with a new version of Boost.

Feel free to look at the Boost source code though for some useful ideas but use your own implementation for what you need.

James Dean
New versions of Boost won't necessarily break on user-supplied allocators. Unfortunately, this totally depends on which of the many Boost libraries we're talking about and which, if any, API they use for user-defined allocation.
jwfearn
+5  A: 

We use boost for embedded systems. With boost you can pick and choose what you use. We use smart_ptr and boost::bind all of our projects. We write software for cheap cell phones. And if Windows CE can run on your hardware I would expect that parts of boost would be applicable. There are parts of boost that have no allocation and you might find them useful.

I would pick and choose based on your requirements.

Like anything that you use, you need to know where the costs are.

witkamp
A: 

Replacing your containers with Boost containers is NOT a good idea. The work to make appropriate custom allocators wouldn't be that bad, but you'd be violating the spirit of your 'allocate at startup' rule. The idea behind this rule (in my experience) is generally to make sure that you don't have to deal with out of memory type situations at run-time. The idea is to make sure that you have all the memory you could possibly need RIGHT AT THE START, so that there's no possibility of any part of the system coming up short of memory later on.

If you used the Boost containers with a custom allocator, you'd suddenly have to deal with the possibility that the pool the container is allocating from could go empty, thus eliminating the purpose of the 'allocate at startup' rule.

In the situation of a limited memory device, I would avoid any kind of container more complex than a statically allocated array.

Michael Kohne