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