ProudNet includes a high performance memory manager, which developer can use to accelerate the operating performance of applications.
Here are the memory managers that ProudNet provide.
2.1Lookaside allocator
Lookaside Allocator uses generic memory pool method. If memories of the same size get allocated and released too often then we recommend you to use Lookaside Allocator.
The main mechanism of Lookaside Allocator is as followed.
When allocating a new memory, it allocates a fresh system memory.
When releasing a memory block, the release block gets returned to Lookaside Allocator.
When re-allocating a memory block, then the memory block returned to Lookaside Allocator will be recycled.
This process runs very quickly even faster than the speed of memory allocation from OS. But, there are disadvantages too.
Lookaside Allocator can only allocate memories of the same size.
The memory blocks allocated to Lookaside Allocator must be released before Lookaside Allocator itself gets destroyed.
Here guides how you can use Lookaside Allocator.
First, create an object with Proud.CLookasideAllocator.New method. You can create it as global object if you want.
Allocate a memory block with Proud.CLookasideAllocator.Alloc method.
You can release it with Proud.CLookasideAllocator.Free. There is no a separated method for re-allocation.
Release all memory blocks and destroy Proud.CLookasideAllocator object.
More details are guided in Proud.CLookasideAllocator.
Using 2.3 Designating as the default allocator of C++ class is another good way to use this module.
2.2Fast heap
Fast heap of ProudNet is a bit slower than Lookaside Allocator but still it is much faster than OS as allocating and releasing memory block. And unlike Lookaside Allocator, it can allocate and release memory block in various sizes.
Implementing class of Fast heap is Proud.CFastHeap. Just like Lookaside Allocation, Proud.CFastHeap also can remove Proud.CFastHeap object after destroying all memory blocks first.
You can use Fast heap as followed.
First, create a fast heap object by Proud.CFastHeap.New method.
Allocate a memory block with Proud.CFastHeap.Alloc method.
You can release the memory block by Proud.CFastHeap.Free. To re-allocate, use CFastHeap.Realloc method.
Destroy Proud.CFastHeap object after releasing all memory blocks.
More details are guided in Proud.CFastHeap.
Using 2.3 Designating as the default allocator of C++ class is another good way to use this module.
2.3Designating as the default allocator of C++ class
Am easier way to accelerate Fast heap or Lookaside Allocator is to override operator new and delete methods of C++ class. By doing this, you can fast heap or Lookaside Allocator instead of system memory heap when creating or deleting C++ class with new or delete operator.
The below example shows how the high performance memory manager allocates and releases memory instead when instancing class with new or delete operation.
class Example { static CLookasideAllocatorPtr gAlloc; // Or use CFastHeap. public: void* operator new(size_t size) { return gAlloc->Alloc(size); } void operator delete(void* ptr, size_t size) { gAlloc->Free(ptr); } }; CLookasideAllocatorPtr Example::gAlloc(CLookasideAllocator::New()); // Or use CFastHeap.