本文整理汇总了C++中MemoryManager::alloc方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryManager::alloc方法的具体用法?C++ MemoryManager::alloc怎么用?C++ MemoryManager::alloc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryManager
的用法示例。
在下文中一共展示了MemoryManager::alloc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: MemoryPoolLite
TEST(TestMemoryManager, Allocations)
{
ScopedMemoryPoolLite smpl;
MemoryManager mm = MemoryPoolLite(smpl);
void* p;
char* c;
ASSERT_TRUE(mm);
p = NULL;
p = mm.alloc(10);
EXPECT_TRUE(p);
p = NULL;
p = mm.allocate<int>();
EXPECT_TRUE(p);
c = NULL;
c = reinterpret_cast<char*>(mm.calloc(10));
EXPECT_EQ(10L, count(c, c+10, '\0'));
c = NULL;
c = reinterpret_cast<char*>(mm.calloc(5, 7));
EXPECT_EQ(35L, count(c, c+35, '\0'));
static const string c_example = "Hello World";
c = NULL;
c = mm.strdup("Hello World");
EXPECT_EQ(c_example, c);
c = NULL;
c = reinterpret_cast<char*>(
mm.memdup(c_example.data(), c_example.size())
);
EXPECT_EQ(c_example, string(c, c_example.size()));
c = NULL;
c = mm.memdup_to_str(c_example.data(), c_example.size());
EXPECT_EQ(c_example, c);
}
示例2: execute
void execute(string line, MemoryManager &memManager) {
string funcName = parseFunction(line);
if (funcName.compare("alloc") == 0) {
int pid;
UNIT size = getAllocParam(line, &pid);
if (!size) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.alloc(pid, size)) {
cout << "No space for process " << pid << endl;
}
}
} else if (funcName.compare("realloc") == 0) {
int pid;
UNIT size = getReallocParam(line, &pid);
if (!size) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.realloc(pid, size)) {
cout << "No space for process " << pid << endl;
}
}
} else if (funcName.compare("free") == 0) {
int pid = getFreeParam(line);
if (pid == -1) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.free(pid)) {
cout << "Free: couldn't find process " << pid << endl;
}
}
} else if (funcName.compare("dump") == 0) {
memManager.dump();
} else {
cout << "Can not parse command: " << line << endl;
}
}