Boost logo

Boost Users :

Subject: [Boost-users] Using boost::serialization in real-time without allocating memory
From: Peter Soetens (peter.soetens_at_[hidden])
Date: 2009-09-17 09:10:11


Hi,

I'm trying to find out if boost::serialization can be used in
real-time applications to stream data into a fifo to another process.
It is mandatory that no memory allocations happen during the
serialization. I tested this with a std::vector<double> of 10 elements
in combination with the boost::iostreams library.

The aim is that all memory allocations happen during construction of
the archive object, while the serialisation itself causes none.

<code>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>

namespace io = boost::iostreams;
int main(int argc, char *argv[])
{
  vector<double> d(10, 1.1);
  char sink[1000];
  memset( sink, 0, 1000);
  io::stream<io::array_sink> out(sink,1000);

  boost::archive::binary_oarchive oa(out);
  //oa << d; // should not allocate
  return 0;
}
</code>

The setup code does 10 memory allocations according to valgrind:
==12995== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
==12995== malloc/free: in use at exit: 0 bytes in 0 blocks.
==12995== malloc/free: 10 allocs, 10 frees, 913 bytes allocated.
==12995== For counts of detected errors, rerun with: -v
==12995== All heap blocks were freed -- no leaks are possible.

If we uncomment 'oa << d' we get 2 more (unwanted) allocations:
==13010== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
==13010== malloc/free: in use at exit: 0 bytes in 0 blocks.
==13010== malloc/free: 12 allocs, 12 frees, 1,001 bytes allocated.
==13010== For counts of detected errors, rerun with: -v
==13010== All heap blocks were freed -- no leaks are possible

I'm guessing that the 2 allocations in the serialisation path come
from a temporay std::string object, when writing the
'serialization::archive' string into the archive. Wouldn't it be
possible to rewrite this library code as such that there are no
allocations / strings created ? A similar pattern is observed for
deserializing: 2 allocs in the serialisation path.

Peter

PS: I tried to find out who defines the macro
BOOST_ARCHIVE_SIGNATURE() but I couldn't find the definition/#define
!?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net