<commit_before>#include <MemoryManager.hh>

#include <Memory.hh>
#include <Debug.hh>

#include <cstdio>

MemoryManager* MemoryManager::self = 0;

MemoryManager::MemoryManager()
   : freeList()
{
   // empty!
}

void*
MemoryManager::operator new(std::size_t /*size*/, void *placement)
{
   return placement;
}

MemoryManager::Segment*
MemoryManager::headerOf(void* data)
{
   return reinterpret_cast<Segment*>(uintptr_t(data) - sizeof(Segment));
}

void
MemoryManager::init()
{
   printf("Intializing Memory Manager\n");
   void* object = allocateBackend(sizeof(MemoryManager));

   MemoryManager::self = new (object) MemoryManager();

   printf("Memory Manager at %p\n", (void* )MemoryManager::self);
}

MemoryManager&
MemoryManager::get()
{
   return *self;
}

void*
MemoryManager::allocateBackend(std::size_t size)
{
   std::size_t rsize = roundTo(size, PageSize);
   void* data = reinterpret_cast<void*>(Memory::sbrk(rsize));

   KASSERT(data != 0);

   return data;
}

void*
MemoryManager::allocate(std::size_t size)
{
   for (auto& s : freeList)
   {
      if (s.getSize() >= size)
      {
	 freeList.remove(&s);
	 s.markAllocated();
#ifdef DEBUG
	 printf("Debug: allocating from freelist\n");<commit_msg>use memory mapped backend<commit_after>#include <MemoryManager.hh>

#include <Memory.hh>
#include <Debug.hh>

#include <cstdio>

MemoryManager* MemoryManager::self = 0;

MemoryManager::MemoryManager()
   : freeList()
{
   // empty!
}

void*
MemoryManager::operator new(std::size_t /*size*/, void *placement)
{
   return placement;
}

MemoryManager::Segment*
MemoryManager::headerOf(void* data)
{
   return reinterpret_cast<Segment*>(uintptr_t(data) - sizeof(Segment));
}

void
MemoryManager::init()
{
   printf("Intializing Memory Manager\n");
   void* object = allocateBackend(sizeof(MemoryManager));

   MemoryManager::self = new (object) MemoryManager();

   printf("Memory Manager at %p\n", (void* )MemoryManager::self);
}

MemoryManager&
MemoryManager::get()
{
   return *self;
}

void*
MemoryManager::allocateBackend(std::size_t size)
{
   std::size_t rsize = roundTo(size, PageSize);

   void* data = reinterpret_cast<void*>(Memory::mapAnonymousRegion(rsize));

   KASSERT(data != 0);

   return data;
}

void*
MemoryManager::allocate(std::size_t size)
{
   for (auto& s : freeList)
   {
      if (s.getSize() >= size)
      {
	 freeList.remove(&s);
	 s.markAllocated();
#ifdef DEBUG
	 printf("Debug: allocating from freelist\n");
{'fraction_non_alphanumeric': 0.15774417654270537, 'fraction_numerical': 0.0016346546791990192, 'mean_word_length': 5.104738154613466, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6583006', 'n_tokens_mistral': 840, 'n_tokens_neox': 815, 'n_words': 166}