Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53952 - in sandbox/monotonic: boost/monotonic libs/monotonic/test
From: christian.schladetsch_at_[hidden]
Date: 2009-06-15 21:05:06


Author: cschladetsch
Date: 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
New Revision: 53952
URL: http://svn.boost.org/trac/boost/changeset/53952

Log:
added chained_storage<N,M,Al>

Added:
   sandbox/monotonic/boost/monotonic/chained_storage.h (contents, props changed)
   sandbox/monotonic/boost/monotonic/storage.h
      - copied, changed from r53948, /sandbox/monotonic/boost/monotonic/inline_storage.h
   sandbox/monotonic/libs/monotonic/test/test_chained_storage.cpp
      - copied, changed from r53951, /sandbox/monotonic/libs/monotonic/test/test_chained.cpp
Removed:
   sandbox/monotonic/boost/monotonic/inline_storage.h
   sandbox/monotonic/libs/monotonic/test/test_chained.cpp
Text files modified:
   sandbox/monotonic/boost/monotonic/allocator.h | 2 +-
   sandbox/monotonic/boost/monotonic/storage.h | 5 ++++-
   sandbox/monotonic/libs/monotonic/test/main.cpp | 2 ++
   sandbox/monotonic/libs/monotonic/test/monotonic.vcproj | 28 ++++++++++++++++++++++++++--
   sandbox/monotonic/libs/monotonic/test/test_chained_storage.cpp | 9 +++++++--
   5 files changed, 40 insertions(+), 6 deletions(-)

Modified: sandbox/monotonic/boost/monotonic/allocator.h
==============================================================================
--- sandbox/monotonic/boost/monotonic/allocator.h (original)
+++ sandbox/monotonic/boost/monotonic/allocator.h 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -6,7 +6,7 @@
 #pragma once
 
 #include <boost/monotonic/storage_base.h>
-#include <boost/monotonic/inline_storage.h>
+#include <boost/monotonic/storage.h>
 #include <boost/assert.hpp>
 //#include <boost/swap.hpp>
 #include <boost/type_traits/has_trivial_constructor.hpp>

Added: sandbox/monotonic/boost/monotonic/chained_storage.h
==============================================================================
--- (empty file)
+++ sandbox/monotonic/boost/monotonic/chained_storage.h 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -0,0 +1,143 @@
+// Copyright (C) 2009 Christian Schladetsch
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#pragma once
+
+#include <boost/monotonic/storage.h>
+
+namespace boost
+{
+ namespace monotonic
+ {
+ /// storage that spans the stack/heap boundary.
+ ///
+ /// allocation requests first use inline storage of N bytes.
+ /// once that is exhausted, later requests are serviced from the heap.
+ ///
+ /// all allocations remain valid at all times.
+ template <size_t N, size_t MLS = N*2, class Al = std::allocator<char> >
+ struct chained_storage : storage_base
+ {
+ BOOST_STATIC_CONSTANT(size_t, MinLinkSize = MLS);
+ typedef Al Allocator;
+ typedef typename Allocator::rebind<char>::other CharAllocator;
+ struct Link
+ {
+ size_t capacity, cursor;
+ char *buffer;
+ CharAllocator alloc;
+ Link(Allocator const &al, size_t N)
+ : capacity(N), cursor(0), buffer(0), alloc(al)
+ {
+ }
+ void Construct()
+ {
+ buffer = alloc.allocate(capacity);
+ if (buffer == 0)
+ capacity = 0;
+ }
+ ~Link()
+ {
+ alloc.deallocate(buffer, 1);
+ }
+ bool CanAllocate(size_t num_bytes) const
+ {
+ return capacity - cursor >= num_bytes;
+ }
+ void *Allocate(size_t num_bytes, size_t alignment)
+ {
+ size_t extra = cursor & (alignment - 1);
+ if (extra > 0)
+ extra = alignment - extra;
+ size_t required = num_bytes + extra;
+ if (!CanAllocate(required))
+ {
+ return 0;
+ }
+ char *ptr = &buffer[cursor];
+ cursor += required;
+ return ptr + extra;
+ }
+ };
+ typedef std::list<Link, Al> Chain;
+
+ private:
+ storage<N> fixed; // the inline storage
+ Chain chain; // heap-based storage
+ Allocator alloc; // allocator for heap-based storage
+
+ public:
+ chained_storage()
+ {
+ }
+ chained_storage(Allocator const &A)
+ : alloc(A)
+ {
+ }
+
+ void reset()
+ {
+ fixed.reset();
+ chain.clear();
+ }
+
+ void *allocate(size_t num_bytes, size_t alignment)
+ {
+ if (void *ptr = fixed.allocate(num_bytes, alignment))
+ {
+ return ptr;
+ }
+ BOOST_FOREACH(Link &link, chain)
+ {
+ if (void *ptr = link.Allocate(num_bytes, alignment))
+ {
+ return ptr;
+ }
+ }
+ size_t size = max(MinLinkSize, num_bytes*2);
+ return AddLink(size).Allocate(num_bytes, alignment);
+ }
+
+ void deallocate(void *, size_t)
+ {
+ // do nothing
+ }
+
+ size_t max_size() const
+ {
+ return std::numeric_limits<size_t>::max(); //??
+ }
+
+ size_t fixed_remaining() const
+ {
+ return fixed.remaining();
+ }
+
+ size_t remaining() const
+ {
+ return max_size(); //??!!
+ }
+
+ size_t used() const
+ {
+ size_t count = fixed.used();
+ BOOST_FOREACH(Link const &link, chain)
+ count += link.used;
+ return count;
+ }
+
+ private:
+ Link &AddLink(size_t size)
+ {
+ chain.push_back(Link(alloc, size));
+ Link &link = chain.back();
+ link.Construct();
+ return link;
+ }
+ };
+ }
+}
+
+//EOF

Deleted: sandbox/monotonic/boost/monotonic/inline_storage.h
==============================================================================
--- sandbox/monotonic/boost/monotonic/inline_storage.h 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
+++ (empty file)
@@ -1,130 +0,0 @@
-// Copyright (C) 2009 Christian Schladetsch
-//
-// Distributed under the Boost Software License, Version 1.0. (See accompanying
-// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-
-#pragma once
-
-#include <cassert>
-#include <boost/array.hpp>
-#include <boost/aligned_storage.hpp>
-
-// define this to use boost::auto_buffer<> rather than boost::array for monotonic::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
- {
- /// storage for an allocator that is on the stack or heap
- template <size_t N>
- struct storage : storage_base
- {
-
-#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:
- buffer_type buffer; ///< the storage
- size_t cursor; ///< pointer to current index within storage for next allocation
-#ifndef NDEBUG
- size_t num_allocations;
-#endif
- public:
- storage()
- : cursor(0)
-#ifndef NDEBUG
- , num_allocations(0)
-#endif
- {
- }
-
- buffer_type const &get_buffer() const
- {
- return buffer;
- }
- const char *begin() const
- {
- return &buffer[0];
- }
- const char *end() const
- {
- return &buffer[N - 1];
- }
- void reset()
- {
- cursor = 0;
-#ifndef NDEBUG
- num_allocations = 0;
-#endif
- }
-
- size_t get_cursor() const
- {
- return cursor;
- }
-
- void set_cursor(size_t c)
- {
- cursor = c;
- }
-
- /// allocate storage, given alignment requirement
- void *allocate(size_t num_bytes, size_t alignment)
- {
-#ifndef NDEBUG
- ++num_allocations;
-#endif
- // ensure we return a point on an aligned boundary
- size_t extra = cursor & (alignment - 1);
- if (extra > 0)
- extra = alignment - extra;
- size_t required = num_bytes + extra;
- BOOST_ASSERT(cursor + required <= N);
-#ifdef BOOST_MONOTONIC_USE_AUTOBUFFER
- buffer.uninitialized_resize(buffer.size() + required);
-#endif
- char *ptr = &buffer[cursor];
- cursor += required;
- return ptr + extra;
- }
-
- /// no work is done to deallocate storage
- void deallocate(void * /*p*/, size_t /*n*/)
- {
- }
-
- /// the maximum size is determined at compile-time
- size_t max_size() const
- {
- return N;
- }
-
- size_t remaining() const
- {
- return N - cursor;
- }
- size_t used() const
- {
- return cursor;
- }
-#ifndef NDEBUG
- size_t get_num_allocs() const
- {
- return num_allocations;
- }
-#endif
- };
- }
-}
-
-//EOF

Copied: sandbox/monotonic/boost/monotonic/storage.h (from r53948, /sandbox/monotonic/boost/monotonic/inline_storage.h)
==============================================================================
--- /sandbox/monotonic/boost/monotonic/inline_storage.h (original)
+++ sandbox/monotonic/boost/monotonic/storage.h 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -89,7 +89,10 @@
                                 if (extra > 0)
                                         extra = alignment - extra;
                                 size_t required = num_bytes + extra;
- BOOST_ASSERT(cursor + required <= N);
+ if (cursor + required > N)
+ {
+ return 0;
+ }
 #ifdef BOOST_MONOTONIC_USE_AUTOBUFFER
                                 buffer.uninitialized_resize(buffer.size() + required);
 #endif

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-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -419,9 +419,11 @@
 
 #include "test_bubble_sort.cpp"
 #include "test_dupe.cpp"
+#include "test_chained_storage.cpp"
 
 int main()
 {
+ test_chained_storage();
         test_map_list_heap_stack();
         test_dupe();
         //graph_bubble_sort();

Modified: sandbox/monotonic/libs/monotonic/test/monotonic.vcproj
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/monotonic.vcproj (original)
+++ sandbox/monotonic/libs/monotonic/test/monotonic.vcproj 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -182,11 +182,11 @@
>
                                 </File>
                                 <File
- RelativePath="..\..\..\boost\monotonic\inline_clone_allocator.h"
+ RelativePath="..\..\..\boost\monotonic\chained_storage.h"
>
                                 </File>
                                 <File
- RelativePath="..\..\..\boost\monotonic\inline_storage.h"
+ RelativePath="..\..\..\boost\monotonic\inline_clone_allocator.h"
>
                                 </File>
                                 <File
@@ -206,6 +206,10 @@
>
                                 </File>
                                 <File
+ RelativePath="..\..\..\boost\monotonic\storage.h"
+ >
+ </File>
+ <File
                                         RelativePath="..\..\..\boost\monotonic\storage_base.h"
>
                                 </File>
@@ -284,6 +288,26 @@
                         </FileConfiguration>
                 </File>
                 <File
+ RelativePath=".\test_chained_storage.cpp"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ ExcludedFromBuild="true"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ ExcludedFromBuild="true"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ />
+ </FileConfiguration>
+ </File>
+ <File
                         RelativePath=".\test_dupe.cpp"
>
                         <FileConfiguration

Deleted: sandbox/monotonic/libs/monotonic/test/test_chained.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/test_chained.cpp 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
+++ (empty file)
@@ -1,9 +0,0 @@
-#include <boost/monotonic/changed_storage.h>
-
-void test_chained_storage()
-{
- monotonic::chained_storage<10> store;
- void *p = store.allocate(20);
-}
-
-//EOF

Copied: sandbox/monotonic/libs/monotonic/test/test_chained_storage.cpp (from r53951, /sandbox/monotonic/libs/monotonic/test/test_chained.cpp)
==============================================================================
--- /sandbox/monotonic/libs/monotonic/test/test_chained.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/test_chained_storage.cpp 2009-06-15 21:05:05 EDT (Mon, 15 Jun 2009)
@@ -1,9 +1,14 @@
-#include <boost/monotonic/changed_storage.h>
+
+#include <boost/monotonic/chained_storage.h>
 
 void test_chained_storage()
 {
         monotonic::chained_storage<10> store;
- void *p = store.allocate(20);
+ {
+ std::vector<char, monotonic::allocator<char> > vec(store);
+ vec.resize(5);
+ vec.resize(500);
+ }
 }
 
 //EOF


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