blob: 4922b63a53191d70432eeae184ff86477feb009b [file] [log] [blame]
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05001#include "aos/testing/test_shm.h"
2
Stephan Pleinesad3085f2024-05-30 10:50:50 -07003#include <stddef.h>
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -05004#include <sys/mman.h>
5
Stephan Pleinesad3085f2024-05-30 10:50:50 -07006#include "aos/ipc_lib/shared_mem.h"
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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080010namespace aos::testing {
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050011namespace {
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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080037} // namespace aos::testing