1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 //[doc_managed_raw_allocation
12 #include <boost/interprocess/managed_shared_memory.hpp>
13 //<-
14 #include "../test/get_process_id_name.hpp"
15 //->
16 
main()17 int main()
18 {
19    using namespace boost::interprocess;
20 
21    //Remove shared memory on construction and destruction
22    struct shm_remove
23    {
24    //<-
25    #if 1
26       shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
27       ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
28    #else
29    //->
30       shm_remove() { shared_memory_object::remove("MySharedMemory"); }
31       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
32    //<-
33    #endif
34    //->
35    } remover;
36    //<-
37    (void)remover;
38    //->
39 
40    //Managed memory segment that allocates portions of a shared memory
41    //segment with the default management algorithm
42    //<-
43    #if 1
44    managed_shared_memory managed_shm(create_only,test::get_process_id_name(), 65536);
45    #else
46    //->
47    managed_shared_memory managed_shm(create_only,"MySharedMemory", 65536);
48    //<-
49    #endif
50    //->
51 
52    //Allocate 100 bytes of memory from segment, throwing version
53    void *ptr = managed_shm.allocate(100);
54 
55    //Deallocate it
56    managed_shm.deallocate(ptr);
57 
58    //Non throwing version
59    ptr = managed_shm.allocate(100, std::nothrow);
60 
61    //Deallocate it
62    managed_shm.deallocate(ptr);
63    return 0;
64 }
65 //]
66 #include <boost/interprocess/detail/config_end.hpp>
67