Example #1
0
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);
}
Example #2
0
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;
  }
}