Memory server is a library for the memory management of C++ programs. The idea is to instantiate a server class that provide a secure and controlled access to the real memory.

Image 1


Same features of the library are:

- Auto-deallocation of the unused memory.
- Resolve the problem of multiple deallocation and invalid pointer free.
- Trace all the blocks and his data.
- Template class for the secure array.
- The block can contain info with line and filename of the source file.
- Auto-deallocation of class.
- Tested with Linux , Win32 , Generic unix




QUICK TUTORIAL


This example show the auto-deallocation of unused memory.
The pointer P1 is auto-deallocated when the function F1 end because the client C2 is a local client and it is destroyed when the function end. The pointer p is persistent because it exist into a global client.

// Get the global client
MEM_CLIENT &C0 = Default_ms->Get_global_client();

// P is deallocated when the function end
// p1 is allocated in CO client (global client)
void F1(void)
{
MEM_CLIENT C2("Function_F1");
char *p,*p1;

  p = (char *) C2.Alloc(300);
  p1 = (char *) C0.Alloc(1000);
}




In this example a block is moved from a local client to a global client. In this case th memory pointed by p1 is valid after the end of the function F2.

// P is deallocated when the function end
// p1 is allocated in C3 and moved in CO
// P2 is allocated in C0
void F2(void)
{
MEM_CLIENT C3("Function_F2");
char *p,*p1,*p2;

  p = (char *) C3.Alloc(100);
  p1 = (char *) C3.Alloc(200);

  C3.Change(&C0,(void *) p1);

  p2 = (char *) C0.Alloc(2000);
}




This example show a class management.
You can use the macro NEW_MEM_CLASS or NEW_MEM_CLASS_P to allocate a class into the MEM_CLIENT. Now you can use the p->get() function to retrieve the pointer to the allocated class or can call a function of your class directly with p->Test(). In this example the class pointed to p1 is auto-deallocated at the exit of the function.

// The test class
class F3_TEST
{
public:
  F3_TEST(void) {cout << "F3_TEST::F3_TEST" << endl;}
  ~F3_TEST(void) {cout << "F3_TEST::~F3_TEST" << endl;}

  void Test(void) {cout << "F3_TEST::test" << endl;}
};

// p1 is destroyed when the function end
void F3(void)
{
MEM_CLIENT C3("Function_F3");
MEM_CLASS < F3_TEST > *p;
F3_TEST *p1,*p2;

  p = NEW_MEM_CLASS(C3,F3_TEST);
  NEW_MEM_CLASS_P(C3,F3_TEST,p1);

  NEW_MEM_CLASS_P(C0,F3_TEST,p2);

  p->get()->Test();
  p1->Test();

  delete p;
}


This is an example of the debug output.

-- Debug_server_block --
  [Global client] Pos:0x14630b50 bytes:1000
  [Global client] Pos:0x14631010 bytes:200
  [Global client] Pos:0x146310e0 bytes:2000
  [Global client] Pos:0x14630f90 bytes:12 MEM_CLASS
  [Global client] Pos:0x14630fe0 bytes:40 test1.cc:131 int
  [Global client] Pos:0x14630a58 bytes:10 test1.cc:124
  [Global client] Pos:0x14630f40 bytes:5 test1.cc:128
  [Main] Pos:0x14630f50 bytes:20 test1.cc:124
  [Main] Pos:0x14630f68 bytes:30 test1.cc:124
  [Main] Pos:0x14630fb0 bytes:40 test1.cc:124
  [Main] Pos:0x14630a10 bytes:50 test1.cc:124
  [Volatile client] Pos:0x14631e10 bytes:1024 test1.cc:150

-- Debug_server_memory --
Server name : Default server
Total client connected : 3
[Global client ]
[Main ]
[Volatile client ]
Total block : 12
Total bytes allocated : 4431 (4 Kb)

-- Debug_client --
Name : Main
Number of block : 4
Bytes allocated : 140

-- Dump_server_block --
[ Global client ] pos:0x14630f40 bytes:5

Test - 54 65 73 74 0


Secure array example.


MEM_BLOB B1(&C0);
int i;
char tmp;


// Allocate 10 char
B1.Alloc(10,0);

// OK
B1[0] = 1;
B1[9] = 1;

// Out of memory bound ! (No core file)
B1[10] = 1;

cout << " B1 : ";
for (i=0;i<10;i++)
{
cout << (int) B1[i] << " ";
}