本文整理汇总了C++中SharedMemory::get_size方法的典型用法代码示例。如果您正苦于以下问题:C++ SharedMemory::get_size方法的具体用法?C++ SharedMemory::get_size怎么用?C++ SharedMemory::get_size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SharedMemory
的用法示例。
在下文中一共展示了SharedMemory::get_size方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(SharedMemoryTest, Alone) {
SharedMemory memory;
EXPECT_TRUE(memory.get_block() == nullptr);
EXPECT_TRUE(memory.is_null());
std::string meta_path = base_path() + std::string("_alone");
COERCE_ERROR(memory.alloc(meta_path, 1ULL << 21, 0));
EXPECT_TRUE(memory.get_block() != nullptr);
EXPECT_EQ(1ULL << 21, memory.get_size());
EXPECT_EQ(meta_path, memory.get_meta_path());
EXPECT_NE(0, memory.get_shmid());
EXPECT_NE(0, memory.get_shmkey());
EXPECT_EQ(0, memory.get_numa_node());
EXPECT_FALSE(memory.is_null());
EXPECT_TRUE(memory.is_owned());
EXPECT_EQ(::getpid(), memory.get_owner_pid());
memory.release_block();
EXPECT_TRUE(memory.get_block() == nullptr);
EXPECT_TRUE(memory.is_null());
}
示例2: meta_path
TEST(SharedMemoryTest, ShareSpawn) {
const char* env_meta_path = ::getenv("test_meta_path");
if (env_meta_path) {
// child process!
{
std::string meta_path(env_meta_path);
std::cout << "I'm a child process(" << ::getpid()
<< "). meta_path=" << meta_path << std::endl;
SharedMemory memory;
memory.attach(meta_path);
EXPECT_FALSE(memory.is_null());
EXPECT_EQ(1ULL << 21, memory.get_size());
EXPECT_EQ(meta_path, memory.get_meta_path());
EXPECT_NE(0, memory.get_shmid());
EXPECT_NE(0, memory.get_shmkey());
EXPECT_EQ(0, memory.get_numa_node());
EXPECT_EQ(42, memory.get_block()[3]);
EXPECT_FALSE(memory.is_owned());
memory.get_block()[5] = 67;
}
::_exit(0); // for the same reason, this should terminate now.
return;
} else {
std::cout << "I'm a master process(" << ::getpid() << ")" << std::endl;
}
SharedMemory memory;
EXPECT_TRUE(memory.get_block() == nullptr);
EXPECT_TRUE(memory.is_null());
std::string meta_path = base_path() + std::string("_share_spawn");
COERCE_ERROR(memory.alloc(meta_path, 1ULL << 21, 0));
memory.get_block()[3] = 42;
posix_spawn_file_actions_t file_actions;
posix_spawnattr_t attr;
::posix_spawn_file_actions_init(&file_actions);
::posix_spawnattr_init(&attr);
std::string path = assorted::get_current_executable_path();
// execute this test
char* const new_argv[] = {
const_cast<char*>(path.c_str()),
const_cast<char*>("--gtest_filter=SharedMemoryTest.ShareSpawn"),
nullptr};
// with meta_path in environment variable
std::string meta("test_meta_path=");
meta += meta_path;
char* const new_envp[] = {
const_cast<char*>(meta.c_str()),
nullptr};
pid_t child_pid;
std::cout << "spawning: " << path << std::endl;
int ret = ::posix_spawn(&child_pid, path.c_str(), &file_actions, &attr, new_argv, new_envp);
EXPECT_EQ(0, ret);
if (ret == 0) {
EXPECT_NE(0, child_pid);
int status = 0;
pid_t result = ::waitpid(child_pid, &status, 0);
std::cout << "child process(" << child_pid << ") died. result=" << result << std::endl;
EXPECT_EQ(child_pid, result);
EXPECT_EQ(0, status);
}
assorted::memory_fence_acquire();
EXPECT_EQ(67, memory.get_block()[5]);
memory.release_block();
}