Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / interprocess / example / comp_doc_anonymous_semaphoreB.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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_anonymous_semaphoreB
12 #include <boost/interprocess/shared_memory_object.hpp>
13 #include <boost/interprocess/mapped_region.hpp>
14 #include <iostream>
15 #include "doc_anonymous_semaphore_shared_data.hpp"
16
17 using namespace boost::interprocess;
18
19 int main ()
20 {
21    //Remove shared memory on destruction
22    struct shm_remove
23    {
24       ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
25    } remover;
26
27    //Create a shared memory object.
28    shared_memory_object shm
29       (open_only                    //only create
30       ,"MySharedMemory"              //name
31       ,read_write  //read-write mode
32       );
33
34    //Map the whole shared memory in this process
35    mapped_region region
36       (shm                       //What to map
37       ,read_write //Map it as read-write
38       );
39
40    //Get the address of the mapped region
41    void * addr       = region.get_address();
42
43    //Obtain the shared structure
44    shared_memory_buffer * data = static_cast<shared_memory_buffer*>(addr);
45
46    const int NumMsg = 100;
47
48    int extracted_data [NumMsg];
49
50    //Extract the data
51    for(int i = 0; i < NumMsg; ++i){
52       data->nstored.wait();
53       data->mutex.wait();
54       extracted_data[i] = data->items[i % shared_memory_buffer::NumItems];
55       data->mutex.post();
56       data->nempty.post();
57    }
58    return 0;
59 }
60 //]
61 #include <boost/interprocess/detail/config_end.hpp>