blob: 0787e5ac820a17559100cb1fe6f08462b4e74280 [file] [log] [blame]
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05001#include "aos/testing/test_shm.h"
2
3#include <sys/mman.h>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/logging.h"
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05006#include "aos/testing/test_logging.h"
7
8namespace aos {
9namespace testing {
10namespace {
11
12const size_t kCoreSize = 0x100000;
13
14} // namespace
15
16TestSharedMemory::TestSharedMemory() {
17 global_core = &global_core_data_;
18 global_core->owner = true;
19 // Use mmap(2) manually instead of through malloc(3) so that we can pass
20 // MAP_SHARED which allows forked processes to communicate using the
21 // "shared" memory.
22 void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
23 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070024 AOS_CHECK_NE(memory, MAP_FAILED);
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050025
26 aos_core_use_address_as_shared_mem(memory, kCoreSize);
27
28 ::aos::testing::EnableTestLogging();
29}
30
31TestSharedMemory::~TestSharedMemory() {
Austin Schuhf257f3c2019-10-27 21:00:43 -070032 AOS_PCHECK(munmap(global_core->mem_struct, kCoreSize));
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050033 global_core = NULL;
34}
35
36} // namespace testing
37} // namespace aos