Update of /cvsroot/boost/boost/libs/interprocess/example
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv26630/example

Modified Files:
        doc_managed_aligned_allocation.cpp doc_named_allocA.cpp 
        doc_named_allocB.cpp 
Added Files:
        doc_managed_allocation_command.cpp 
        doc_managed_construction_info.cpp 
        doc_managed_multiple_allocation.cpp 
Log Message:
New Interprocess version

--- NEW FILE: doc_managed_allocation_command.cpp ---
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. 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)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
//[doc_managed_allocation_command
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cassert>

int main()
{
   using namespace boost::interprocess;

   //Managed memory segment that allocates portions of a shared memory
   //segment with the default management algorithm
   shared_memory_object::remove("MyManagedShm");

   try{
      managed_shared_memory managed_shm(create_only, "MyManagedShm", 
10000*sizeof(std::size_t));

      //Allocate at least 100 bytes, 1000 bytes if possible
      std::size_t received_size, min_size = 100, preferred_size = 1000;
      std::size_t *ptr = managed_shm.allocation_command<std::size_t>
         (allocate_new, min_size, preferred_size, received_size).first;

      //Received size must be bigger than min_size
      assert(received_size >= min_size);

      //Get free memory
      std::size_t free_memory_after_allocation = managed_shm.get_free_memory();

      //Now write the data
      for(std::size_t i = 0; i < received_size; ++i) ptr[i] = i;

      //Now try to triplicate the buffer. We won't admit an expansion
      //lower to the double of the original buffer.
      //This "should" be successful since no other class is allocating
      //memory from the segment
      std::size_t expanded_size;
      std::pair<std::size_t *, bool> ret = managed_shm.allocation_command
         (expand_fwd, received_size*2, received_size*3, expanded_size, ptr);

      //Check invariants
      assert(ret.second == true);
      assert(ret.first == ptr);
      assert(expanded_size >= received_size*2);

      //Get free memory and compare
      std::size_t free_memory_after_expansion = managed_shm.get_free_memory();
      assert(free_memory_after_expansion < free_memory_after_allocation);

      //Write new values
      for(std::size_t i = received_size; i < expanded_size; ++i)  ptr[i] = i;

      //Try to shrink approximately to min_size, but the new size
      //should be smaller than min_size*2.
      //This "should" be successful since no other class is allocating
      //memory from the segment
      std::size_t shrunk_size;
      ret = managed_shm.allocation_command
         (shrink_in_place, min_size*2, min_size, shrunk_size, ptr);

      //Check invariants
      assert(ret.second == true);
      assert(ret.first == ptr);
      assert(shrunk_size <= min_size*2);
      assert(shrunk_size >= min_size);

      //Get free memory and compare
      std::size_t free_memory_after_shrinking = managed_shm.get_free_memory();
      assert(free_memory_after_shrinking > free_memory_after_expansion);

      //Deallocate the buffer
      managed_shm.deallocate(ptr);
   }
   catch(...){
      shared_memory_object::remove("MyManagedShm");
      throw;
   }
   shared_memory_object::remove("MyManagedShm");
   return 0;
}
//]

--- NEW FILE: doc_managed_construction_info.cpp ---
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. 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)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
//[doc_managed_construction_info
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cassert>
#include <cstring>

class my_class
{
   //...
};

int main()
{
   using namespace boost::interprocess;
   typedef managed_shared_memory msm;
   shared_memory_object::remove("MyManagedShm");

   try{
      msm managed_shm(create_only, "MyManagedShm", 10000*sizeof(std::size_t));

      //Construct objects
      my_class *named_object  = managed_shm.construct<my_class>("Object 
name")[1]();
      my_class *unique_object = 
managed_shm.construct<my_class>(unique_instance)[2]();
      my_class *anon_object   = 
managed_shm.construct<my_class>(anonymous_instance)[3]();

      //Now test "get_instance_name" function.
      assert(0 == std::strcmp(msm::get_instance_name(named_object), "Object 
name"));
      assert(0 == msm::get_instance_name(unique_object));
      assert(0 == msm::get_instance_name(anon_object));

      //Now test "get_instance_type" function.
      assert(named_type     == msm::get_instance_type(named_object));
      assert(unique_type    == msm::get_instance_type(unique_object));
      assert(anonymous_type == msm::get_instance_type(anon_object));

      //Now test "get_instance_length" function.
      assert(1 == msm::get_instance_length(named_object));
      assert(2 == msm::get_instance_length(unique_object));
      assert(3 == msm::get_instance_length(anon_object));

      managed_shm.destroy_ptr(named_object);
      managed_shm.destroy_ptr(unique_object);
      managed_shm.destroy_ptr(anon_object);
   }
   catch(...){
      shared_memory_object::remove("MyManagedShm");
      throw;
   }
   shared_memory_object::remove("MyManagedShm");
   return 0;
}
//]

--- NEW FILE: doc_managed_multiple_allocation.cpp ---
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. 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)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
//[doc_managed_multiple_allocation
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cassert>//assert
#include <cstring>//std::memset
#include <new>    //std::nothrow

int main()
{
   using namespace boost::interprocess;
   typedef managed_shared_memory::multiallocation_iterator 
multiallocation_iterator;

   //Try to erase any previous managed segment with the same name
   shared_memory_object::remove("MyManagedShm");

   try{
      managed_shared_memory managed_shm(create_only, "MyManagedShm", 65536);
      std::size_t received_size;
      //Allocate 16 elements of 100 bytes in a single call. Non-throwing 
version.
      multiallocation_iterator beg_it = managed_shm.allocate_many(100, 16, 16, 
received_size, std::nothrow);

      //To check for an error, we can use a boolean expresssion
      //or compare it with a default constructed iterator
      assert(!beg_it == (beg_it == multiallocation_iterator()));
      
      //Check if the memory allocation was successful
      if(!beg_it)  return 1;

      //Initialize our data
      for( multiallocation_iterator it = beg_it, end_it; it != end_it; )
         std::memset(*it++, 0, 100);

      //Now deallocate
      for(multiallocation_iterator it = beg_it, end_it; it != end_it;)
         managed_shm.deallocate(*it++);

      //Allocate 10 buffers of different sizes in a single call. Throwing 
version
      std::size_t sizes[10];
      for(std::size_t i = 0; i < 10; ++i)
         sizes[i] = i*3;

      beg_it  = managed_shm.allocate_many(sizes, 10);

      //Iterate each allocated buffer and deallocate
      //The "end" condition can be also checked with operator!
      for(multiallocation_iterator it = beg_it; it;)
         managed_shm.deallocate(*it++);
   }
   catch(...){
      shared_memory_object::remove("MyManagedShm");
      throw;
   }
   shared_memory_object::remove("MyManagedShm");
   return 0;
}
//]

Index: doc_managed_aligned_allocation.cpp
===================================================================
RCS file: 
/cvsroot/boost/boost/libs/interprocess/example/doc_managed_aligned_allocation.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- doc_managed_aligned_allocation.cpp  12 May 2007 12:31:52 -0000      1.2
+++ doc_managed_aligned_allocation.cpp  22 Jul 2007 14:16:55 -0000      1.3
@@ -49,7 +49,7 @@
       //This allocation will maximize the size of the aligned memory
       //and will increase the possibility of finding more aligned memory
       ptr = managed_shm.allocate_aligned
-         (Alignment - managed_shared_memory::PayloadPerAllocation, Alignment);
+         (3*Alignment - managed_shared_memory::PayloadPerAllocation, 
Alignment);
 
       //Check alignment
       assert(((char*)ptr-(char*)0) % Alignment == 0);

Index: doc_named_allocA.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/interprocess/example/doc_named_allocA.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- doc_named_allocA.cpp        12 May 2007 12:31:52 -0000      1.2
+++ doc_named_allocA.cpp        22 Jul 2007 14:16:58 -0000      1.3
@@ -31,13 +31,24 @@
       //Create an object of MyType initialized to {0.0, 0}
       MyType *instance = segment.construct<MyType>
          ("MyType instance")  //name of the object
-         (0.0, 0);            //ctor second argument
+         (0.0, 0);            //ctor first argument
 
       //Create an array of 10 elements of MyType initialized to {0.0, 0}
       MyType *array = segment.construct<MyType>
          ("MyType array")     //name of the object
          [10]                 //number of elements
-         (0.0, 0);            //ctor second argument
+         (0.0, 0);            //Same two ctor arguments for all objects
+
+      //Create an array of 3 elements of MyType initializing each one
+      //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
+      float float_initializer[3] = { 0.0, 1.0, 2.0 };
+      int   int_initializer[3]   = { 0, 1, 2 };
+
+      MyType *array_it = segment.construct_it<MyType>
+         ("MyType array from it")   //name of the object
+         [3]                        //number of elements
+         ( &float_initializer[0]    //Iterator for the 1st ctor argument
+         , &int_initializer[0]);    //Iterator for the 2nd ctor argument
    }
    catch(...){
       shared_memory_object::remove("MySharedMemory");

Index: doc_named_allocB.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/interprocess/example/doc_named_allocB.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- doc_named_allocB.cpp        12 May 2007 12:31:52 -0000      1.2
+++ doc_named_allocB.cpp        22 Jul 2007 14:16:58 -0000      1.3
@@ -27,30 +27,30 @@
       //and initialize needed resources
       managed_shared_memory segment(open_only, "MySharedMemory");
 
-      //Find the array and object
       std::pair<MyType*, std::size_t> res;
-      res = segment.find<MyType> ("MyType array");   
 
-      std::size_t array_len   = res.second;
+      //Find the array
+      res = segment.find<MyType> ("MyType array");   
       //Length should be 10
-      assert(array_len == 10);
+      assert(res.second == 10);
 
-      //Find the array and the object
+      //Find the object
       res = segment.find<MyType> ("MyType instance");   
-
-      std::size_t len   = res.second;
-
       //Length should be 1
-      assert(len == 1);
+      assert(res.second == 1);
+
+      //Find the array constructed from iterators
+      res = segment.find<MyType> ("MyType array from it");
+      //Length should be 3
+      assert(res.second == 3);
 
       //Use data
       // . . . 
 
-      //We're done, delete array from memory
+      //We're done, delete all the objects
       segment.destroy<MyType>("MyType array");
-
-      //We're done, delete object from memory
       segment.destroy<MyType>("MyType instance");
+      segment.destroy<MyType>("MyType array from it");
    }
    catch(...){
       shared_memory_object::remove("MySharedMemory");


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs

Reply via email to