first commit
This commit is contained in:
131
malloc/Cpp/malloc.cpp
Normal file
131
malloc/Cpp/malloc.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
|
||||
/*
|
||||
* Contains functions provided by the operating system
|
||||
*/
|
||||
namespace OurImaginaryOS
|
||||
{
|
||||
/*
|
||||
* Return 1024byte aligned page of size PAGE_SIZE * pageCount
|
||||
* e.g. GetNewPage(1) returns a 32MiB page (PAGE_SIZE)
|
||||
* GetNewPage(2) returns two contiguous pages, totaling 64MiB
|
||||
* GetNewPage(0) is undefined.
|
||||
*/
|
||||
void *GetNewPage(size_t pageCount = 1);
|
||||
void FreePage(void *p);
|
||||
|
||||
const size_t PAGE_SIZE = 32 * 1024 * 1024;
|
||||
}
|
||||
|
||||
/* IAllocator interface for memory allocations.
|
||||
Example Usage:
|
||||
|
||||
struct Foo() {
|
||||
IAllocator &mAllocator;
|
||||
MyDynamicObject *mDynamicObject;
|
||||
|
||||
void Foo(IAllocator &allocator)
|
||||
: mAllocator(allocator)
|
||||
{
|
||||
mDynamicObject = new(mAllocator.Alloc(sizeof(*mDynamicObject)) MyDynamicObject();
|
||||
}
|
||||
|
||||
void ~Foo()
|
||||
{
|
||||
mDynamicObject->~MyDynamicObject();
|
||||
mAllocator.Free(mDynamicObject);
|
||||
}
|
||||
}
|
||||
*/
|
||||
class IAllocator
|
||||
{
|
||||
public:
|
||||
// size in bytes
|
||||
// alignment in bytes, 0 means don't care
|
||||
// alignOffset: offset to align returned pointer. E.g. alignoffset of 4:
|
||||
// ptr aligned start point that matches alignment parameter.
|
||||
// ^ ^
|
||||
// | 4 bytes | Rest of block of size "size - alignOffset" |
|
||||
// flags - currently unused
|
||||
virtual void *Alloc(size_t size, size_t alignment = 0, size_t alignOffset = 0, int64_t flags = 0) = 0;
|
||||
virtual void Free(void *ptr) = 0;
|
||||
|
||||
virtual ~IAllocator() = default;
|
||||
};
|
||||
|
||||
using namespace OurImaginaryOS;
|
||||
|
||||
class MyAllocator : public IAllocator
|
||||
{
|
||||
public:
|
||||
MyAllocator()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual void *Alloc(size_t size, size_t alignment = 0, size_t alignOffset = 0, int64_t flags = 0)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual void Free(void *ptr)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~MyAllocator()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************************/
|
||||
// Stuff to make coding game syntax highlighting work
|
||||
// Don't worry about what's below here.
|
||||
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
namespace OurImaginaryOS
|
||||
{
|
||||
static std::mutex s_internalPagesLock;
|
||||
static std::vector<void *> s_internalPages;
|
||||
|
||||
void *GetNewPage(size_t pageCount)
|
||||
{
|
||||
void *newPage = malloc(PAGE_SIZE * pageCount);
|
||||
|
||||
s_internalPagesLock.lock();
|
||||
s_internalPages.push_back(newPage);
|
||||
s_internalPagesLock.unlock();
|
||||
|
||||
return newPage;
|
||||
}
|
||||
|
||||
void FreePage(void *p)
|
||||
{
|
||||
void *toFree = nullptr;
|
||||
|
||||
s_internalPagesLock.lock();
|
||||
for (auto iter = s_internalPages.begin(); iter != s_internalPages.end(); iter++)
|
||||
{
|
||||
if (*iter == p)
|
||||
{
|
||||
toFree = p;
|
||||
s_internalPages.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
s_internalPagesLock.unlock();
|
||||
|
||||
if (toFree)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user