|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r53857 - in sandbox/monotonic: boost/monotonic libs/monotonic/doc libs/monotonic/test
From: christian.schladetsch_at_[hidden]
Date: 2009-06-13 02:36:10
Author: cschladetsch
Date: 2009-06-13 02:36:09 EDT (Sat, 13 Jun 2009)
New Revision: 53857
URL: http://svn.boost.org/trac/boost/changeset/53857
Log:
added failed attempt to use boost::auto_buffer for inline_storage
Text files modified:
sandbox/monotonic/boost/monotonic/inline_storage.h | 24 +++++++++++++++++++-----
sandbox/monotonic/libs/monotonic/doc/index.html | 3 ++-
sandbox/monotonic/libs/monotonic/test/main.cpp | 27 +++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 6 deletions(-)
Modified: sandbox/monotonic/boost/monotonic/inline_storage.h
==============================================================================
--- sandbox/monotonic/boost/monotonic/inline_storage.h (original)
+++ sandbox/monotonic/boost/monotonic/inline_storage.h 2009-06-13 02:36:09 EDT (Sat, 13 Jun 2009)
@@ -8,6 +8,15 @@
#include <cassert>
#include <boost/array.hpp>
+// define this to use boost::auto_buffer<> rather than boost::array for monotonic::inline_storage
+//#define BOOST_MONOTONIC_USE_AUTOBUFFER
+// this currently does not work, because resizing the underlying buffer breaks
+// containers that may reference the previously used memory
+
+#ifdef BOOST_MONOTONIC_USE_AUTOBUFFER
+# include <boost/auto_buffer.hpp>
+#endif
+
namespace boost
{
namespace monotonic
@@ -16,12 +25,15 @@
template <size_t N>
struct inline_storage : storage_base
{
- // how to determine alignment requirements?
- BOOST_STATIC_CONSTANT(size_t, Alignment = 16);
+#ifdef BOOST_MONOTONIC_USE_AUTOBUFFER
+ typedef boost::auto_buffer<char, boost::store_n_bytes<N> > buffer_type;
+#else
+ typedef boost::array<char, N> buffer_type;
+#endif
private:
- boost::array<char, N> buffer; ///< the storage
- size_t cursor; ///< pointer to current index within storage for next allocation
+ buffer_type buffer; ///< the storage
+ size_t cursor; ///< pointer to current index within storage for next allocation
public:
inline_storage() : cursor(0)
@@ -47,9 +59,11 @@
void *allocate(size_t num_bytes, void const * = 0)
{
// ensure we return a point on an aligned boundary
- //size_t extra = num_bytes % Alignment;
size_t extra = num_bytes & 15;
size_t required = num_bytes + extra;
+#ifdef BOOST_MONOTONIC_USE_AUTOBUFFER
+ buffer.uninitialized_resize(buffer.size() + required);
+#endif
char *ptr = &buffer[cursor];
cursor += required;
return ptr + extra;
Modified: sandbox/monotonic/libs/monotonic/doc/index.html
==============================================================================
--- sandbox/monotonic/libs/monotonic/doc/index.html (original)
+++ sandbox/monotonic/libs/monotonic/doc/index.html 2009-06-13 02:36:09 EDT (Sat, 13 Jun 2009)
@@ -315,7 +315,8 @@
References</h2>
<ul class="simple">
<li>Boost.AlignedMemory</li>
- <li>Boost.AutoBuffer (Proposed)</li>
+ <li>Boost.AutoBuffer. A related service. Attempts to integrate this with STL has so
+ far been unsuccessful. See libs/monotonic/test/main.cpp#test_auto_buffer</li>
<li>Boost.IntrusiveContainer, for an alternative means of creating efficient
containers.</li>
</ul>
Modified: sandbox/monotonic/libs/monotonic/test/main.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/main.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/main.cpp 2009-06-13 02:36:09 EDT (Sat, 13 Jun 2009)
@@ -5,6 +5,7 @@
#include <boost/monotonic/list.h>
#include <boost/monotonic/map.h>
#include <boost/monotonic/set.h>
+#include <boost/auto_buffer.hpp>
#include <boost/timer.hpp>
#include <boost/foreach.hpp>
@@ -13,6 +14,31 @@
using namespace std;
using namespace boost;
+// #define BOOST_MONOTONIC_USE_AUTOBUFFER before including <boost/monotonic/inline_storage.h> to
+// try this at home.
+void test_auto_buffer()
+{
+ monotonic::inline_storage<10> storage;
+
+ // this fails because the storage that the vector uses
+ // will be moved when the buffer resizes...
+ //monotonic::vector<int> vec(storage);
+ //for (size_t n = 0; n < 100; ++n)
+ //{
+ // vec.push_back(n);
+ //}
+
+ // this fails because at the end of the buffer, just before
+ // it may be resized and possibly put onto heap, the following asserts
+ // on MSVC in <xtree>:
+ // if (max_size() - 1 <= _Mysize)
+ // _THROW(length_error, "map/set<T> too long");
+ //
+ //monotonic::map<int, int> map(storage);
+ //for (int n = 0; n < 100; ++n)
+ // map[n] = n;
+}
+
void test_basic()
{
monotonic::inline_storage<1000*sizeof(int)> storage1; // create local storage on the stack
@@ -359,6 +385,7 @@
int main()
{
+ test_auto_buffer();
test_alignment();
test_speed();
test_speed_heap();
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk