owned this note
owned this note
Published
Linked with GitHub
Memory Allocation of WvsProj.
===
The [69817dc](https://github.com/tnsc4502/mnwvs196/commit/69817dcbf650dd84dbba33740806fd307006908e) version of project replaced most new/delete with AllocObj(AllocObjCtor)/FreeObj and AllocArray/FreeArray, the following shows their usage.
The [MemoryPool](https://github.com/cacay/MemoryPool) which is used in this project is an Node structure allocator that enable itself to perform constant time allocation.
First, the WvsSingleObjectAllocator uses five static MemoryPools to allocate small objects which have size 32bytes, 64bytes, 128bytes, 256bytes and 512bytes respectively.
For example, to create an object with 48bytes, the
__gsgMemPool64 is used to allocate memory.
For the requirement that the size is larger than 512bytes, new MemoryPool instance is created for specific data type of requirement and with size 128 * sizeof(T) for every block, and can only be accessed by calling GetInstance(), for example, to construct an object with 614Bytes:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
struct LARGE_OBJ {
char data[614];
}
auto pLObj = AllocObj(ST_OBJ);
//That instantiate a new WvsSingleObjectAllocator:
//The second template parament denotes if the data type requires more than 512bytes
template<>
class WvsSingleObjectAllocator<LARGE_OBJ, true>
{
MemoryPool<LARGE_OBJ> m;
static WvsSingleObjectAllocator* GetInstance()
{
static WvsSingleObjectAllocator* p = new WvsSingleObjectAllocator;
return p;
}
}
//so, the above code is same as:
auto pData = WvsSingleObjectAllocator<LARGE_OBJ, true>::GetInstance()->Allocate()
//
```
To allocate an object that doesn't require constructor or just require default-constructor only, use AllocObj:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
struct ST_OBJ {
int a_[100];
}
auto pObj = AllocObj(ST_OBJ);
// is equivalent to pObj = new ST_OBJ
```
If the class/struct declare the constructor as privete, include "..\WvsLib\Common\CommonDef.h" and put ALLOW_PRIVE_ALLOC in the private section:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
#include "..\WvsLib\Common\CommonDef.h"
class SIG_OBJ
{
ALLOW_PRIVATE_ALLOC
private:
SIG_OBJ() {}
public:
SIG_OBJ* GetInstance();
}
auto pObj = AllocObj(SIG_OBJ);
```
Similarly, it also provides de-allocation method called "FreeObj":
``` cpp=
FreeObj(pObj);
```
Note that the object allocated by AllocObj can only be destructed by FreeObj, if you use delete/free to release the resouce, then the undefined behavior is occurred and may cause the program crashed.
For the situation that allocation require non-default-constructor, we provide AllocObjCtor:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
struct ST_OBJ {
ST_OBJ(int nSize) {
}
int *a_;
}
auto pObj = AllocObjCtor(ST_OBJ)(2000);
// is equivalent to pObj = new ST_OBJ(2000)
```
The reason we provide such method is becuase that the default-constructor will not be automatically declared when you explictly define other constructors, AllocObj will failed to allocate an object and cause compiling error.
For array allocation, we also provide corresponding method AllocArray:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
int *aData = AllocArray(int, 1000);
// is equivalent to int* aData = new int[1000]
```
To destruct, FreeArray is allowable, but requires allocation size being given:
``` cpp=
#include "..\WvsLib\Memory\MemoryPoolMan.hpp"
FreeArray(aData, 1000)
// is equivalent to delete[] aData
```
When the number of bytes required to create the array larger than 512bytes, operator new is called and delete[] is used to release the memory.
Finally, the std::shared_ptr<T> and std::unique<T> are supported, but you should declare a deleter which uses FreeObj/FreeArray to destroy your object; otherwise, it would use operator delete and would cause your program crashed.
In the wrapper WvsSingleObjectAllocator, it uses some template techniques, for example, to perform two-way selection to decide whether or not call the default constructor. If you are interested in these stuffs, please refer to Modern C++ Design/More Effective C++, etc.