Dynamic allocation

Milkyway precompiler enables a form of dynamic allocation. The user can use "new <Type>()", to get a pointer to a fresh object of type <Type>. There is no garbage collection yet, so all allocated objects must be manually deallocated. This is done using "destroy <Type>(<pointerToObjectToDestroy>)". However, dynamic allocation works by giving each object its own limited memory pool. When a maximum number of objects has been allocated, asking for a new one results in a runtime error. The user must specify the size of a struct's the memory pool when declaring the struct. Not specifing a size means the struct cannot be dynamically allocated. There is a maximum pool size, but I don't know the details. Also, trying to deallocate a pointer that did not originate from an allocation, or one that has already been deallocated, results in a runtime error.

Example

Input File

struct myPoint 200 {
  int x;
  int y;
};

void foo() {
  myPoint* p = new myPoint();
  int y = p->y;
  p->x = 2;
  delete myPoint(p);
}

Output File

// This file was generated by the MilyWay galaxy precompiler.
// The input .galaxy file has been transformed.
// Block comments have been removed.
// Original formatting has changed.
// 
// Author: Remy Willems, email: [email protected]
 
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
 
// ---------------------------------------------------------------------------
// Structs
// ---------------------------------------------------------------------------
 
struct myPoint
{
  int x;
  int y;
};
 
// ---------------------------------------------------------------------------
// Globals
// ---------------------------------------------------------------------------
 
const int r__myPointMaxAlloc = 200;
myPoint[r__myPointMaxAlloc] r__myPointMemory;
int[r__myPointMaxAlloc] r__myPointFreePtrs;
int r__myPointPtrTotal = 0;
int r__myPointFreePtrCount = 0;
 
// ---------------------------------------------------------------------------
// Functions
// ---------------------------------------------------------------------------
 
myPoint* r__myPointNew ()
{
  int pointer;
  if (r__myPointFreePtrCount == 0)
  {
    if (r__myPointPtrTotal <= r__myPointMaxAlloc)
    {
      pointer = r__myPointPtrTotal;
      r__myPointPtrTotal += 1;
    }
  }
  else
  {
    r__myPointFreePtrCount -= 1;
    pointer = r__myPointFreePtrs[r__myPointFreePtrCount];
  }
  return &r__myPointMemory[pointer];
}
 
void r__myPointDelete (myPoint* p)
{
  int freedPointer = 0;
  while (freedPointer < r__myPointPtrTotal)
  {
    if (&r__myPointMemory[freedPointer] == p)
    {
      r__myPointFreePtrCount += 1;
      r__myPointFreePtrs[r__myPointFreePtrCount] = freedPointer;
      break;
    }
    freedPointer += 1;
  }
}
 
void foo ()
{
  myPoint* p = r__myPointNew();
  int y = p->y;
  p->x = 2;
  r__myPointDelete(p);
}

Future

Add garbage collection.

Caveats

Hardly tested at the moment. Someone who could do some testing would really be of great help. I don't know the max pool size.


Comments

Posts Quoted:
Reply
Clear All Quotes