Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 1 | #include "aos/testing/test_shm.h" |
| 2 | |
| 3 | #include <sys/mman.h> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/ipc_lib/queue.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 7 | #include "aos/testing/test_logging.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | namespace { |
| 12 | |
| 13 | const size_t kCoreSize = 0x100000; |
| 14 | |
| 15 | } // namespace |
| 16 | |
| 17 | TestSharedMemory::TestSharedMemory() { |
| 18 | global_core = &global_core_data_; |
| 19 | global_core->owner = true; |
| 20 | // Use mmap(2) manually instead of through malloc(3) so that we can pass |
| 21 | // MAP_SHARED which allows forked processes to communicate using the |
| 22 | // "shared" memory. |
| 23 | void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE, |
| 24 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 25 | AOS_CHECK_NE(memory, MAP_FAILED); |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 26 | |
| 27 | aos_core_use_address_as_shared_mem(memory, kCoreSize); |
| 28 | |
| 29 | ::aos::testing::EnableTestLogging(); |
| 30 | } |
| 31 | |
| 32 | TestSharedMemory::~TestSharedMemory() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 33 | AOS_PCHECK(munmap(global_core->mem_struct, kCoreSize)); |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 34 | global_core = NULL; |
| 35 | } |
| 36 | |
| 37 | } // namespace testing |
| 38 | } // namespace aos |