| 1 | #include <unistd.h>
|
|---|
| 2 | #include <sys/types.h>
|
|---|
| 3 | #include <cstdint>
|
|---|
| 4 | #include <cstddef>
|
|---|
| 5 | #include <fstream>
|
|---|
| 6 | #include <boost/interprocess/offset_ptr.hpp>
|
|---|
| 7 | #include <boost/interprocess/file_mapping.hpp>
|
|---|
| 8 | #include <boost/interprocess/mapped_region.hpp>
|
|---|
| 9 | #include <boost/interprocess/managed_external_buffer.hpp>
|
|---|
| 10 |
|
|---|
| 11 | const char file_name[] = "mapped_file";
|
|---|
| 12 |
|
|---|
| 13 | const std::size_t header_size = 16;
|
|---|
| 14 | const std::size_t storage_size = 4096;
|
|---|
| 15 |
|
|---|
| 16 | // Unsigned offset type doesn't work because of incorrect casts in offset_ptr
|
|---|
| 17 | // typedef boost::interprocess::offset_ptr< void, std::ptrdiff_t, std::uint32_t > void_pointer;
|
|---|
| 18 | typedef boost::interprocess::offset_ptr< void, std::ptrdiff_t, std::int32_t > void_pointer;
|
|---|
| 19 |
|
|---|
| 20 | typedef boost::interprocess::basic_managed_external_buffer<
|
|---|
| 21 | char,
|
|---|
| 22 | boost::interprocess::rbtree_best_fit< boost::interprocess::null_mutex_family, void_pointer >,
|
|---|
| 23 | boost::interprocess::iset_index
|
|---|
| 24 | > managed_external_buffer;
|
|---|
| 25 |
|
|---|
| 26 | int main()
|
|---|
| 27 | {
|
|---|
| 28 | {
|
|---|
| 29 | std::ofstream file(file_name, std::ios::out | std::ios::trunc | std::ios::binary);
|
|---|
| 30 | }
|
|---|
| 31 | truncate(file_name, header_size + storage_size);
|
|---|
| 32 |
|
|---|
| 33 | boost::interprocess::file_mapping file_mapping(file_name, boost::interprocess::read_write);
|
|---|
| 34 | boost::interprocess::mapped_region file_region(file_mapping, boost::interprocess::read_write, header_size, storage_size);
|
|---|
| 35 |
|
|---|
| 36 | managed_external_buffer buffer(boost::interprocess::create_only, file_region.get_address(), file_region.get_size());
|
|---|
| 37 |
|
|---|
| 38 | return 0;
|
|---|
| 39 | }
|
|---|