blob: b1c384895cd440cb99c82975285aefb9bf440c98 [file] [log] [blame]
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05001#include "aos/testing/test_shm.h"
2
3#include <sys/mman.h>
4
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include "aos/ipc_lib/queue.h"
John Park33858a32018-09-28 23:05:48 -07006#include "aos/logging/logging.h"
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05007#include "aos/testing/test_logging.h"
8
9namespace aos {
10namespace testing {
11namespace {
12
13const size_t kCoreSize = 0x100000;
14
15} // namespace
16
17TestSharedMemory::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 Schuhf257f3c2019-10-27 21:00:43 -070025 AOS_CHECK_NE(memory, MAP_FAILED);
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050026
27 aos_core_use_address_as_shared_mem(memory, kCoreSize);
28
29 ::aos::testing::EnableTestLogging();
30}
31
32TestSharedMemory::~TestSharedMemory() {
Austin Schuhf257f3c2019-10-27 21:00:43 -070033 AOS_PCHECK(munmap(global_core->mem_struct, kCoreSize));
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050034 global_core = NULL;
35}
36
37} // namespace testing
38} // namespace aos