Dynamic Memory Allocator

This project is abandoned and its default file will likely not work with the most recent version of StarCraft II. Whether this project is out of date or its author has marked it as abandoned, this project is no longer maintained.

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  *************************************************************
//  *                                                           *
//  *               Dynamic Memory Allocator                    *
//  *                                                           *
//  *                                           v1.1.1.2        *
//  *                                           ~Nestharus      *
//  *                                                           *
//  *************************************************************
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Details
//
//      Heap memory allocation in order to declare arrays on the fly. Also includes memory display functions.
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Memory Types
//
//      const int MEMORY_TYPE_INVALID
//      const int MEMORY_TYPE_ABILCMD
//      const int MEMORY_TYPE_AIFILTER
//      const int MEMORY_TYPE_ACTOR
//      const int MEMORY_TYPE_BOOL
//      const int MEMORY_TYPE_BANK
//      const int MEMORY_TYPE_BYTE
//      const int MEMORY_TYPE_CAMERAINFO
//      const int MEMORY_TYPE_COLOR
//      const int MEMORY_TYPE_DOODAD
//      const int MEMORY_TYPE_FIXED
//      const int MEMORY_TYPE_INT
//      const int MEMORY_TYPE_MARKER
//      const int MEMORY_TYPE_ORDER
//      const int MEMORY_TYPE_PLAYERGROUP
//      const int MEMORY_TYPE_POINT
//      const int MEMORY_TYPE_REGION
//      const int MEMORY_TYPE_SOUND
//      const int MEMORY_TYPE_SOUNDLINK
//      const int MEMORY_TYPE_STRING
//      const int MEMORY_TYPE_TEXT
//      const int MEMORY_TYPE_TIMER
//      const int MEMORY_TYPE_TRANSMISSIONSOURCE
//      const int MEMORY_TYPE_TRIGGER
//      const int MEMORY_TYPE_UNIT
//      const int MEMORY_TYPE_UNITFILTER
//      const int MEMORY_TYPE_UNITGROUP
//      const int MEMORY_TYPE_UNITREF
//      const int MEMORY_TYPE_REVEALER
//      const int MEMORY_TYPE_WAVE
//      const int MEMORY_TYPE_WAVEINFO
//      const int MEMORY_TYPE_WAVETARGET
//      const int MEMORY_TYPE_DELEGATE              ->      int delegate(int data);
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Pointer Functions
//
//      int getMemoryType(int pointer)
//          -   O(1 + Log(MEMORY_TYPE_COUNT)/Log(2))
//
//      int sizeof  (int pointer)
//          -   retrieves array size. Size of 1 is scalar.
//
//      int getMemoryBlockAddress(int pointer, int memoryType)
//          -   used to manipulate memory arrays directly
//
//      int getPointerAddress(int memoryBlockAddress, int memoryType)
//          -   convert memory block address back to a pointer
//
//      abilcmd             readAbilcmd             (int pointer)
//      aifilter            readAifilter            (int pointer)
//      actor               readActor               (int pointer)
//      bool                readBool                (int pointer)
//      bank                readBank                (int pointer)
//      byte                readByte                (int pointer)
//      camerainfo          readCamerainfo          (int pointer)
//      color               readColor               (int pointer)
//      doodad              readDoodad              (int pointer)
//      fixed               readFixed               (int pointer)
//      int                 readInt                 (int pointer)
//      marker              readMarker              (int pointer)
//      order               readOrder               (int pointer)
//      playergroup         readPlayergroup         (int pointer)
//      point               readPoint               (int pointer)
//      region              readRegion              (int pointer)
//      sound               readSound               (int pointer)
//      soundlink           readSoundlink           (int pointer)
//      string              readString              (int pointer)
//      text                readText                (int pointer)
//      timer               readTimer               (int pointer)
//      transmissionsource  readTransmissionsource  (int pointer)
//      trigger             readTrigger             (int pointer)
//      unit                readUnit                (int pointer)
//      unitfilter          readUnitfilter          (int pointer)
//      unitgroup           readUnitgroup           (int pointer)
//      unitref             readUnitref             (int pointer)
//      revealer            readRevealer            (int pointer)
//      wave                readWave                (int pointer)
//      waveinfo            readWaveinfo            (int pointer)
//      wavetarget          readWavetarget          (int pointer)
//      funcref<delegate>   readDelegate            (int pointer)
//
//      void writeAbilcmd               (int pointer, abilcmd value)
//      void writeAifilter              (int pointer, aifilter value)
//      void writeActor                 (int pointer, actor value)
//      void writeBool                  (int pointer, bool value)
//      void writeBank                  (int pointer, bank value)
//      void writeByte                  (int pointer, byte value)
//      void writeCamerainfo            (int pointer, camerainfo value)
//      void writeColor                 (int pointer, color value)
//      void writeDoodad                (int pointer, doodad value)
//      void writeFixed                 (int pointer, fixed value)
//      void writeInt                   (int pointer, int value)
//      void writeMarker                (int pointer, marker value)
//      void writeOrder                 (int pointer, order value)
//      void writePlayergroup           (int pointer, playergroup value)
//      void writePoint                 (int pointer, point value)
//      void writeRegion                (int pointer, region value)
//      void writeSound                 (int pointer, sound value)
//      void writeSoundlink             (int pointer, soundlink value)
//      void writeString                (int pointer, string value)
//      void writeText                  (int pointer, text value)
//      void writeTimer                 (int pointer, timer value)
//      void writeTransmissionsource    (int pointer, transmissionsource value)
//      void writeTrigger               (int pointer, trigger value)
//      void writeUnit                  (int pointer, unit value)
//      void writeUnitfilter            (int pointer, unitfilter value)
//      void writeUnitgroup             (int pointer, unitgroup value)
//      void writeUnitref               (int pointer, unitref value)
//      void writeRevealer              (int pointer, revealer value)
//      void writeWave                  (int pointer, wave value)
//      void writeWaveinfo              (int pointer, waveinfo value)
//      void writeWavetarget            (int pointer, wavetarget value)
//      void writeDelegate              (int pointer, funcref<delegate> value)
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Memory arrays
//
//      Use getMemoryBlockAddress(pointer) to retrieve pointer to manipulate these. Keep in mind that no information can be retrieved
//      from a memory block address.
//
//      memoryAbilcmd               [memoryBlockPointer]
//      memoryAifilter              [memoryBlockPointer]
//      memoryActor                 [memoryBlockPointer]
//      memoryBool                  [memoryBlockPointer]
//      memoryBank                  [memoryBlockPointer]
//      memoryByte                  [memoryBlockPointer]
//      memoryCamerainfo            [memoryBlockPointer]
//      memoryColor                 [memoryBlockPointer]
//      memoryDoodad                [memoryBlockPointer]
//      memoryFixed                 [memoryBlockPointer]
//      memoryInt                   [memoryBlockPointer]
//      memoryMarker                [memoryBlockPointer]
//      memoryOrder                 [memoryBlockPointer]
//      memoryPlayergroup           [memoryBlockPointer]
//      memoryPoint                 [memoryBlockPointer]
//      memoryRegion                [memoryBlockPointer]
//      memorySound                 [memoryBlockPointer]
//      memorySoundlink             [memoryBlockPointer]
//      memoryString                [memoryBlockPointer]
//      memoryText                  [memoryBlockPointer]
//      memoryTimer                 [memoryBlockPointer]
//      memoryTransmissionsource    [memoryBlockPointer]
//      memoryTrigger               [memoryBlockPointer]
//      memoryUnit                  [memoryBlockPointer]
//      memoryUnitfilter            [memoryBlockPointer]
//      memoryUnitgroup             [memoryBlockPointer]
//      memoryUnitref               [memoryBlockPointer]
//      memoryRevealer              [memoryBlockPointer]
//      memoryWave                  [memoryBlockPointer]
//      memoryWaveinfo              [memoryBlockPointer]
//      memoryWavetarget            [memoryBlockPointer]
//      memoryDelegate              [memoryBlockPointer]
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Memory Functions
//
//      int allocate(int size, int memoryType)
//      void deallocate(int pointer)
//
//
//                            Bit Functions
//      ---------------------------------------------------------
//      byte intToByte(int i )
//
//      bool getBit(byte bits, int index)
//      byte setBit(byte bits, int index, bool value)
//
//
//                            DEBUG
//      ---------------------------------------------------------
//      void printMemoryRemaining       (int memoryType)
//      void printMemoryUsed            (int memoryType)
//      void printMemoryUsedPercent     (int memoryType)
//      void printHeapSize              (int memoryType)
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  System Functions
//
//      void initializeMemoryAllocation()
//          -   call To Initialize System
//
//------------------------------------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  memory sizes
//
//  -   min memory size: 8
//
//  -   must be in powers of 2
//
//          8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static const int MEMORY_SIZE_ABILCMD                = 128;      //abilcmd
    static const int MEMORY_SIZE_AIFILTER               = 128;      //aifilter
    static const int MEMORY_SIZE_ACTOR                  = 2048;     //actor
    static const int MEMORY_SIZE_BOOL                   = 1024;     //bool
    static const int MEMORY_SIZE_BANK                   = 32;       //bank
    static const int MEMORY_SIZE_BYTE                   = 1024;     //byte
    static const int MEMORY_SIZE_CAMERAINFO             = 128;      //camerainfo
    static const int MEMORY_SIZE_COLOR                  = 256;      //color
    static const int MEMORY_SIZE_DOODAD                 = 1024;     //doodad
    static const int MEMORY_SIZE_FIXED                  = 8192;     //fixed
    static const int MEMORY_SIZE_INT                    = 16384;    //int
    static const int MEMORY_SIZE_MARKER                 = 512;      //marker
    static const int MEMORY_SIZE_ORDER                  = 512;      //order
    static const int MEMORY_SIZE_PLAYERGROUP            = 32;       //playergroup
    static const int MEMORY_SIZE_POINT                  = 512;      //point
    static const int MEMORY_SIZE_REGION                 = 256;      //region
    static const int MEMORY_SIZE_SOUND                  = 128;      //sound
    static const int MEMORY_SIZE_SOUNDLINK              = 128;      //soundlink
    static const int MEMORY_SIZE_STRING                 = 512;      //string
    static const int MEMORY_SIZE_TEXT                   = 512;      //text
    static const int MEMORY_SIZE_TIMER                  = 256;      //timer
    static const int MEMORY_SIZE_TRANSMISSIONSOURCE     = 64;       //transmissionsource
    static const int MEMORY_SIZE_TRIGGER                = 512;      //trigger
    static const int MEMORY_SIZE_UNIT                   = 2048;     //unit
    static const int MEMORY_SIZE_UNITFILTER             = 128;      //unitfilter
    static const int MEMORY_SIZE_UNITGROUP              = 512;      //unitgroup
    static const int MEMORY_SIZE_UNITREF                = 1024;     //unitref
    static const int MEMORY_SIZE_REVEALER               = 128;      //revealer
    static const int MEMORY_SIZE_WAVE                   = 64;       //wave
    static const int MEMORY_SIZE_WAVEINFO               = 64;       //waveinfo
    static const int MEMORY_SIZE_WAVETARGET             = 64;       //wavetarget
    static const int MEMORY_SIZE_DELEGATE               = 1024;     //funcref<delegate>

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory types
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static const int MEMORY_TYPE_COUNT          = 32;

    const int MEMORY_TYPE_INVALID               = -1;
    const int MEMORY_TYPE_ABILCMD               = 0;        //abilcmd
    const int MEMORY_TYPE_AIFILTER              = 1;        //aifilter
    const int MEMORY_TYPE_ACTOR                 = 2;        //actor
    const int MEMORY_TYPE_BOOL                  = 3;        //bool
    const int MEMORY_TYPE_BANK                  = 4;        //bank
    const int MEMORY_TYPE_BYTE                  = 5;        //byte
    const int MEMORY_TYPE_CAMERAINFO            = 6;        //camerainfo
    const int MEMORY_TYPE_COLOR                 = 7;        //color
    const int MEMORY_TYPE_DOODAD                = 8;        //doodad
    const int MEMORY_TYPE_FIXED                 = 9;       //fixed
    const int MEMORY_TYPE_INT                   = 10;       //int
    const int MEMORY_TYPE_MARKER                = 11;       //marker
    const int MEMORY_TYPE_ORDER                 = 12;       //order
    const int MEMORY_TYPE_PLAYERGROUP           = 13;       //playergroup
    const int MEMORY_TYPE_POINT                 = 14;       //point
    const int MEMORY_TYPE_REGION                = 15;       //region
    const int MEMORY_TYPE_SOUND                 = 16;       //sound
    const int MEMORY_TYPE_SOUNDLINK             = 17;       //soundlink
    const int MEMORY_TYPE_STRING                = 18;       //string
    const int MEMORY_TYPE_TEXT                  = 19;       //text
    const int MEMORY_TYPE_TIMER                 = 20;       //timer
    const int MEMORY_TYPE_TRANSMISSIONSOURCE    = 21;       //transmissionsource
    const int MEMORY_TYPE_TRIGGER               = 22;       //trigger
    const int MEMORY_TYPE_UNIT                  = 23;       //unit
    const int MEMORY_TYPE_UNITFILTER            = 24;       //unitfilter
    const int MEMORY_TYPE_UNITGROUP             = 25;       //unitgroup
    const int MEMORY_TYPE_UNITREF               = 26;       //unitref
    const int MEMORY_TYPE_REVEALER              = 27;       //revealer
    const int MEMORY_TYPE_WAVE                  = 28;       //wave
    const int MEMORY_TYPE_WAVEINFO              = 29;       //waveinfo
    const int MEMORY_TYPE_WAVETARGET            = 30;       //wavetarget
    const int MEMORY_TYPE_DELEGATE              = 31;       //funcref<delegate>

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory address initialization
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static int [MEMORY_TYPE_COUNT] memoryTypeStartAddress;
    static int [MEMORY_TYPE_COUNT] memoryTypeSize;
    static text[MEMORY_TYPE_COUNT] memoryTypeName;

    static void initializeMemoryTypeAddresses() {
        memoryTypeStartAddress[MEMORY_TYPE_ABILCMD]             = 0;                                                                                            //abilcmd
        memoryTypeStartAddress[MEMORY_TYPE_AIFILTER]            = memoryTypeStartAddress[MEMORY_TYPE_ABILCMD]               + MEMORY_SIZE_ABILCMD;              //aifilter
        memoryTypeStartAddress[MEMORY_TYPE_ACTOR]               = memoryTypeStartAddress[MEMORY_TYPE_AIFILTER]              + MEMORY_SIZE_AIFILTER;             //actor
        memoryTypeStartAddress[MEMORY_TYPE_BOOL]                = memoryTypeStartAddress[MEMORY_TYPE_ACTOR]                 + MEMORY_SIZE_ACTOR;                //bool
        memoryTypeStartAddress[MEMORY_TYPE_BANK]                = memoryTypeStartAddress[MEMORY_TYPE_BOOL]                  + MEMORY_SIZE_BOOL;                 //bank
        memoryTypeStartAddress[MEMORY_TYPE_BYTE]                = memoryTypeStartAddress[MEMORY_TYPE_BANK]                  + MEMORY_SIZE_BANK;                 //byte
        memoryTypeStartAddress[MEMORY_TYPE_CAMERAINFO]          = memoryTypeStartAddress[MEMORY_TYPE_BYTE]                  + MEMORY_SIZE_BYTE;                 //camerainfo
        memoryTypeStartAddress[MEMORY_TYPE_COLOR]               = memoryTypeStartAddress[MEMORY_TYPE_CAMERAINFO]            + MEMORY_SIZE_CAMERAINFO;           //color
        memoryTypeStartAddress[MEMORY_TYPE_DOODAD]              = memoryTypeStartAddress[MEMORY_TYPE_COLOR]                 + MEMORY_SIZE_COLOR;                //doodad
        memoryTypeStartAddress[MEMORY_TYPE_FIXED]               = memoryTypeStartAddress[MEMORY_TYPE_DOODAD]                + MEMORY_SIZE_DOODAD;               //fixed
        memoryTypeStartAddress[MEMORY_TYPE_INT]                 = memoryTypeStartAddress[MEMORY_TYPE_FIXED]                 + MEMORY_SIZE_FIXED;                //int
        memoryTypeStartAddress[MEMORY_TYPE_MARKER]              = memoryTypeStartAddress[MEMORY_TYPE_INT]                   + MEMORY_SIZE_INT;                  //marker
        memoryTypeStartAddress[MEMORY_TYPE_ORDER]               = memoryTypeStartAddress[MEMORY_TYPE_MARKER]                + MEMORY_SIZE_MARKER;               //order
        memoryTypeStartAddress[MEMORY_TYPE_PLAYERGROUP]         = memoryTypeStartAddress[MEMORY_TYPE_ORDER]                 + MEMORY_SIZE_ORDER;                //playergroup
        memoryTypeStartAddress[MEMORY_TYPE_POINT]               = memoryTypeStartAddress[MEMORY_TYPE_PLAYERGROUP]           + MEMORY_SIZE_PLAYERGROUP;          //point
        memoryTypeStartAddress[MEMORY_TYPE_REGION]              = memoryTypeStartAddress[MEMORY_TYPE_POINT]                 + MEMORY_SIZE_POINT;                //region
        memoryTypeStartAddress[MEMORY_TYPE_SOUND]               = memoryTypeStartAddress[MEMORY_TYPE_REGION]                + MEMORY_SIZE_REGION;               //sound
        memoryTypeStartAddress[MEMORY_TYPE_SOUNDLINK]           = memoryTypeStartAddress[MEMORY_TYPE_SOUND]                 + MEMORY_SIZE_SOUND;                //soundlink
        memoryTypeStartAddress[MEMORY_TYPE_STRING]              = memoryTypeStartAddress[MEMORY_TYPE_SOUNDLINK]             + MEMORY_SIZE_SOUNDLINK;            //string
        memoryTypeStartAddress[MEMORY_TYPE_TEXT]                = memoryTypeStartAddress[MEMORY_TYPE_STRING]                + MEMORY_SIZE_STRING;               //text
        memoryTypeStartAddress[MEMORY_TYPE_TIMER]               = memoryTypeStartAddress[MEMORY_TYPE_TEXT]                  + MEMORY_SIZE_TEXT;                 //timer
        memoryTypeStartAddress[MEMORY_TYPE_TRANSMISSIONSOURCE]  = memoryTypeStartAddress[MEMORY_TYPE_TIMER]                 + MEMORY_SIZE_TIMER;                //transmissionsource
        memoryTypeStartAddress[MEMORY_TYPE_TRIGGER]             = memoryTypeStartAddress[MEMORY_TYPE_TRANSMISSIONSOURCE]    + MEMORY_SIZE_TRANSMISSIONSOURCE;   //trigger
        memoryTypeStartAddress[MEMORY_TYPE_UNIT]                = memoryTypeStartAddress[MEMORY_TYPE_TRIGGER]               + MEMORY_SIZE_TRIGGER;              //unit
        memoryTypeStartAddress[MEMORY_TYPE_UNITFILTER]          = memoryTypeStartAddress[MEMORY_TYPE_UNIT]                  + MEMORY_SIZE_UNIT;                 //unitfilter
        memoryTypeStartAddress[MEMORY_TYPE_UNITGROUP]           = memoryTypeStartAddress[MEMORY_TYPE_UNITFILTER]            + MEMORY_SIZE_UNITFILTER;           //unitgroup
        memoryTypeStartAddress[MEMORY_TYPE_UNITREF]             = memoryTypeStartAddress[MEMORY_TYPE_UNITGROUP]             + MEMORY_SIZE_UNITGROUP;            //unitref
        memoryTypeStartAddress[MEMORY_TYPE_REVEALER]            = memoryTypeStartAddress[MEMORY_TYPE_UNITREF]               + MEMORY_SIZE_UNITREF;              //revealer
        memoryTypeStartAddress[MEMORY_TYPE_WAVE]                = memoryTypeStartAddress[MEMORY_TYPE_REVEALER]              + MEMORY_SIZE_REVEALER;             //wave
        memoryTypeStartAddress[MEMORY_TYPE_WAVEINFO]            = memoryTypeStartAddress[MEMORY_TYPE_WAVE]                  + MEMORY_SIZE_WAVE;                 //waveinfo
        memoryTypeStartAddress[MEMORY_TYPE_WAVETARGET]          = memoryTypeStartAddress[MEMORY_TYPE_WAVEINFO]              + MEMORY_SIZE_WAVEINFO;             //wavetarget
        memoryTypeStartAddress[MEMORY_TYPE_DELEGATE]            = memoryTypeStartAddress[MEMORY_TYPE_WAVETARGET]            + MEMORY_SIZE_WAVETARGET;           //funcref<func>

        memoryTypeSize[MEMORY_TYPE_ABILCMD]             = MEMORY_SIZE_ABILCMD;              //abilcmd
        memoryTypeSize[MEMORY_TYPE_AIFILTER]            = MEMORY_SIZE_AIFILTER;             //aifilter
        memoryTypeSize[MEMORY_TYPE_ACTOR]               = MEMORY_SIZE_ACTOR;                //actor
        memoryTypeSize[MEMORY_TYPE_BOOL]                = MEMORY_SIZE_BOOL;                 //bool
        memoryTypeSize[MEMORY_TYPE_BANK]                = MEMORY_SIZE_BANK;                 //bank
        memoryTypeSize[MEMORY_TYPE_BYTE]                = MEMORY_SIZE_BYTE;                 //byte
        memoryTypeSize[MEMORY_TYPE_CAMERAINFO]          = MEMORY_SIZE_CAMERAINFO;           //camerainfo
        memoryTypeSize[MEMORY_TYPE_COLOR]               = MEMORY_SIZE_COLOR;                //color
        memoryTypeSize[MEMORY_TYPE_DOODAD]              = MEMORY_SIZE_DOODAD;               //doodad
        memoryTypeSize[MEMORY_TYPE_FIXED]               = MEMORY_SIZE_FIXED;                //fixed
        memoryTypeSize[MEMORY_TYPE_INT]                 = MEMORY_SIZE_INT;                  //int
        memoryTypeSize[MEMORY_TYPE_MARKER]              = MEMORY_SIZE_MARKER;               //marker
        memoryTypeSize[MEMORY_TYPE_ORDER]               = MEMORY_SIZE_ORDER;                //order
        memoryTypeSize[MEMORY_TYPE_PLAYERGROUP]         = MEMORY_SIZE_PLAYERGROUP;          //playergroup
        memoryTypeSize[MEMORY_TYPE_POINT]               = MEMORY_SIZE_POINT;                //point
        memoryTypeSize[MEMORY_TYPE_REGION]              = MEMORY_SIZE_REGION;               //region
        memoryTypeSize[MEMORY_TYPE_SOUND]               = MEMORY_SIZE_SOUND;                //sound
        memoryTypeSize[MEMORY_TYPE_SOUNDLINK]           = MEMORY_SIZE_SOUNDLINK;            //soundlink
        memoryTypeSize[MEMORY_TYPE_STRING]              = MEMORY_SIZE_STRING;               //string
        memoryTypeSize[MEMORY_TYPE_TEXT]                = MEMORY_SIZE_TEXT;                 //text
        memoryTypeSize[MEMORY_TYPE_TIMER]               = MEMORY_SIZE_TIMER;                //timer
        memoryTypeSize[MEMORY_TYPE_TRANSMISSIONSOURCE]  = MEMORY_SIZE_TRANSMISSIONSOURCE;   //transmissionsource
        memoryTypeSize[MEMORY_TYPE_TRIGGER]             = MEMORY_SIZE_TRIGGER;              //trigger
        memoryTypeSize[MEMORY_TYPE_UNIT]                = MEMORY_SIZE_UNIT;                 //unit
        memoryTypeSize[MEMORY_TYPE_UNITFILTER]          = MEMORY_SIZE_UNITFILTER;           //unitfilter
        memoryTypeSize[MEMORY_TYPE_UNITGROUP]           = MEMORY_SIZE_UNITGROUP;            //unitgroup
        memoryTypeSize[MEMORY_TYPE_UNITREF]             = MEMORY_SIZE_UNITREF;              //unitref
        memoryTypeSize[MEMORY_TYPE_REVEALER]            = MEMORY_SIZE_REVEALER;             //revealer
        memoryTypeSize[MEMORY_TYPE_WAVE]                = MEMORY_SIZE_WAVE;                 //wave
        memoryTypeSize[MEMORY_TYPE_WAVEINFO]            = MEMORY_SIZE_WAVEINFO;             //waveinfo
        memoryTypeSize[MEMORY_TYPE_WAVETARGET]          = MEMORY_SIZE_WAVETARGET;           //wavetarget
        memoryTypeSize[MEMORY_TYPE_DELEGATE]            = MEMORY_SIZE_DELEGATE;             //funcref<delegate>

        memoryTypeName[MEMORY_TYPE_ABILCMD]             = StringToText("abilcmd");              //abilcmd
        memoryTypeName[MEMORY_TYPE_AIFILTER]            = StringToText("aifilter");             //aifilter
        memoryTypeName[MEMORY_TYPE_ACTOR]               = StringToText("actor");                //actor
        memoryTypeName[MEMORY_TYPE_BOOL]                = StringToText("bool");                 //bool
        memoryTypeName[MEMORY_TYPE_BANK]                = StringToText("bank");                 //bank
        memoryTypeName[MEMORY_TYPE_BYTE]                = StringToText("byte");                 //byte
        memoryTypeName[MEMORY_TYPE_CAMERAINFO]          = StringToText("camerainfo");           //camerainfo
        memoryTypeName[MEMORY_TYPE_COLOR]               = StringToText("color");                //color
        memoryTypeName[MEMORY_TYPE_DOODAD]              = StringToText("doodad");               //doodad
        memoryTypeName[MEMORY_TYPE_FIXED]               = StringToText("fixed");                //fixed
        memoryTypeName[MEMORY_TYPE_INT]                 = StringToText("int");                  //int
        memoryTypeName[MEMORY_TYPE_MARKER]              = StringToText("marker");               //marker
        memoryTypeName[MEMORY_TYPE_ORDER]               = StringToText("order");                //order
        memoryTypeName[MEMORY_TYPE_PLAYERGROUP]         = StringToText("playergroup");          //playergroup
        memoryTypeName[MEMORY_TYPE_POINT]               = StringToText("point");                //point
        memoryTypeName[MEMORY_TYPE_REGION]              = StringToText("region");               //region
        memoryTypeName[MEMORY_TYPE_SOUND]               = StringToText("sound");                //sound
        memoryTypeName[MEMORY_TYPE_SOUNDLINK]           = StringToText("soundlink");            //soundlink
        memoryTypeName[MEMORY_TYPE_STRING]              = StringToText("string");               //string
        memoryTypeName[MEMORY_TYPE_TEXT]                = StringToText("text");                 //text
        memoryTypeName[MEMORY_TYPE_TIMER]               = StringToText("timer");                //timer
        memoryTypeName[MEMORY_TYPE_TRANSMISSIONSOURCE]  = StringToText("transmissionsource");   //transmissionsource
        memoryTypeName[MEMORY_TYPE_TRIGGER]             = StringToText("trigger");              //trigger
        memoryTypeName[MEMORY_TYPE_UNIT]                = StringToText("unit");                 //unit
        memoryTypeName[MEMORY_TYPE_UNITFILTER]          = StringToText("unitfilter");           //unitfilter
        memoryTypeName[MEMORY_TYPE_UNITGROUP]           = StringToText("unitgroup");            //unitgroup
        memoryTypeName[MEMORY_TYPE_UNITREF]             = StringToText("unitref");              //unitref
        memoryTypeName[MEMORY_TYPE_REVEALER]            = StringToText("revealer");             //revealer
        memoryTypeName[MEMORY_TYPE_WAVE]                = StringToText("wave");                 //wave
        memoryTypeName[MEMORY_TYPE_WAVEINFO]            = StringToText("waveinfo");             //waveinfo
        memoryTypeName[MEMORY_TYPE_WAVETARGET]          = StringToText("wavetarget");           //wavetarget
        memoryTypeName[MEMORY_TYPE_DELEGATE]            = StringToText("delegate");             //delegate
    } //initializeMemoryTypeAddresses

    //O(1 + Log(MEMORY_TYPE_COUNT)/Log(2))
    int getMemoryType(int pointer) {
        int memoryType  = MEMORY_TYPE_COUNT/2;
        int search      = MEMORY_TYPE_COUNT/4;

        if (pointer >= memoryTypeStartAddress[MEMORY_TYPE_COUNT - 1] + memoryTypeSize[MEMORY_TYPE_COUNT - 1] || pointer < 0) {
            return -1;
        }
        
        while (true) {
            //go up
            if (pointer >= memoryTypeStartAddress[memoryType] + memoryTypeSize[memoryType]) {
                if (search == 0) {
                    memoryType = memoryType + 1;
                } //if
                else {
                    memoryType = memoryType + search;
                    search = search/2;
                } //else
            } //if
            //go down
            else if (pointer < memoryTypeStartAddress[memoryType]) {
                if (search == 0) {
                    memoryType = memoryType - 1;
                }
                else {
                    memoryType = memoryType - search;
                    search = search/2;
                }
            } //else if
            //found
            else {
                break;
            } //else
        } //while

        return memoryType;
    } //getMemoryType

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory blocks
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static int delegate(int data);

    abilcmd[MEMORY_SIZE_ABILCMD]                        memoryAbilcmd;              //abilcmd
    aifilter[MEMORY_SIZE_AIFILTER]                      memoryAifilter;             //aifilter
    actor[MEMORY_SIZE_ACTOR]                            memoryActor;                //actor
    bool[MEMORY_SIZE_BOOL]                              memoryBool;                 //bool
    bank[MEMORY_SIZE_BANK]                              memoryBank;                 //bank
    byte[MEMORY_SIZE_BYTE]                              memoryByte;                 //byte
    camerainfo[MEMORY_SIZE_CAMERAINFO]                  memoryCamerainfo;           //camerainfo
    color[MEMORY_SIZE_COLOR]                            memoryColor;                //color
    doodad[MEMORY_SIZE_DOODAD]                          memoryDoodad;               //doodad
    fixed[MEMORY_SIZE_FIXED]                            memoryFixed;                //fixed
    int[MEMORY_SIZE_INT]                                memoryInt;                  //int
    marker[MEMORY_SIZE_MARKER]                          memoryMarker;               //marker
    order[MEMORY_SIZE_ORDER]                            memoryOrder;                //order
    playergroup[MEMORY_SIZE_PLAYERGROUP]                memoryPlayergroup;          //playergroup
    point[MEMORY_SIZE_POINT]                            memoryPoint;                //point
    region[MEMORY_SIZE_REGION]                          memoryRegion;               //region
    sound[MEMORY_SIZE_SOUND]                            memorySound;                //sound
    soundlink[MEMORY_SIZE_SOUNDLINK]                    memorySoundlink;            //soundlink
    string[MEMORY_SIZE_STRING]                          memoryString;               //string
    text[MEMORY_SIZE_TEXT]                              memoryText;                 //text
    timer[MEMORY_SIZE_TIMER]                            memoryTimer;                //timer
    transmissionsource[MEMORY_SIZE_TRANSMISSIONSOURCE]  memoryTransmissionsource;   //transmissionsource
    trigger[MEMORY_SIZE_TRIGGER]                        memoryTrigger;              //trigger
    unit[MEMORY_SIZE_UNIT]                              memoryUnit;                 //unit
    unitfilter[MEMORY_SIZE_UNITFILTER]                  memoryUnitfilter;           //unitfilter
    unitgroup[MEMORY_SIZE_UNITGROUP]                    memoryUnitgroup;            //unitgroup
    unitref[MEMORY_SIZE_UNITREF]                        memoryUnitref;              //unitref
    revealer[MEMORY_SIZE_REVEALER]                      memoryRevealer;             //revealer
    wave[MEMORY_SIZE_WAVE]                              memoryWave;                 //wave
    waveinfo[MEMORY_SIZE_WAVEINFO]                      memoryWaveinfo;             //waveinfo
    wavetarget[MEMORY_SIZE_WAVETARGET]                  memoryWavetarget;           //wavetarget
    funcref<delegate>[MEMORY_SIZE_DELEGATE]             memoryDelegate;             //delegate

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory fragments
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static byte[
        ( MEMORY_SIZE_ABILCMD       + MEMORY_SIZE_AIFILTER              + MEMORY_SIZE_ACTOR     + MEMORY_SIZE_BOOL          + MEMORY_SIZE_BANK 
        + MEMORY_SIZE_BYTE          + MEMORY_SIZE_CAMERAINFO            + MEMORY_SIZE_COLOR     + MEMORY_SIZE_DOODAD        + MEMORY_SIZE_FIXED
        + MEMORY_SIZE_INT           + MEMORY_SIZE_MARKER                + MEMORY_SIZE_ORDER     + MEMORY_SIZE_PLAYERGROUP   + MEMORY_SIZE_POINT
        + MEMORY_SIZE_REGION        + MEMORY_SIZE_SOUND                 + MEMORY_SIZE_SOUNDLINK + MEMORY_SIZE_STRING        + MEMORY_SIZE_TEXT
        + MEMORY_SIZE_TIMER         + MEMORY_SIZE_TRANSMISSIONSOURCE    + MEMORY_SIZE_TRIGGER   + MEMORY_SIZE_UNIT          + MEMORY_SIZE_UNITFILTER
        + MEMORY_SIZE_UNITGROUP     + MEMORY_SIZE_UNITREF               + MEMORY_SIZE_REVEALER  + MEMORY_SIZE_WAVE          + MEMORY_SIZE_WAVEINFO
        + MEMORY_SIZE_WAVETARGET    + MEMORY_SIZE_DELEGATE )/8
    ] fragmentFlags;

    byte intToByte(int i ) { return i; }
    bool getBit(byte bits, int index) { return 0 != (bits & intToByte(1 << index)); }
    byte setBit(byte bits, int index, bool value) {
        if (value)  {   return bits | intToByte(1 << index); }
                        return bits & ~intToByte(1 << index);
    } //setBit

    static bool isMemoryChunkLimit      (int pointer)           {   return getBit(fragmentFlags[pointer/8], pointer%8); }
    static void setMemoryChunkLimitFlag (int pointer, bool val) {          fragmentFlags[pointer/8] = setBit(fragmentFlags[pointer/8], pointer%8, val); }

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory heaps
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    static int[MEMORY_TYPE_COUNT] allocatedMemory;
    static int[MEMORY_TYPE_COUNT] heapSize;
    static int[MEMORY_TYPE_COUNT] releasedMemory;

    static void heapInitialization() {
        int memoryType = MEMORY_TYPE_COUNT;

        while (0 != memoryType) {
            memoryType = memoryType - 1;

            allocatedMemory[memoryType] = memoryTypeStartAddress[memoryType];
        } //while
    } //heapInitialization

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory address conversion
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    //used to manipulate memory blocks directly
    int getMemoryBlockAddress(int pointer, int memoryType)          { return pointer - 1 - memoryTypeStartAddress[memoryType]; }
    int getPointerAddress(int memoryBlockAddress, int memoryType)   { return memoryBlockAddress + 1 + memoryTypeStartAddress[memoryType]; }

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory read/write
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    abilcmd             readAbilcmd             (int pointer) { return memoryAbilcmd            [getMemoryBlockAddress(pointer, MEMORY_TYPE_ABILCMD)]; }
    aifilter            readAifilter            (int pointer) { return memoryAifilter           [getMemoryBlockAddress(pointer, MEMORY_TYPE_AIFILTER)]; }
    actor               readActor               (int pointer) { return memoryActor              [getMemoryBlockAddress(pointer, MEMORY_TYPE_ACTOR)]; }
    bool                readBool                (int pointer) { return memoryBool               [getMemoryBlockAddress(pointer, MEMORY_TYPE_BOOL)]; }
    bank                readBank                (int pointer) { return memoryBank               [getMemoryBlockAddress(pointer, MEMORY_TYPE_BANK)]; }
    byte                readByte                (int pointer) { return memoryByte               [getMemoryBlockAddress(pointer, MEMORY_TYPE_BYTE)]; }
    camerainfo          readCamerainfo          (int pointer) { return memoryCamerainfo         [getMemoryBlockAddress(pointer, MEMORY_TYPE_CAMERAINFO)]; }
    color               readColor               (int pointer) { return memoryColor              [getMemoryBlockAddress(pointer, MEMORY_TYPE_COLOR)]; }
    doodad              readDoodad              (int pointer) { return memoryDoodad             [getMemoryBlockAddress(pointer, MEMORY_TYPE_DOODAD)]; }
    fixed               readFixed               (int pointer) { return memoryFixed              [getMemoryBlockAddress(pointer, MEMORY_TYPE_FIXED)]; }
    int                 readInt                 (int pointer) { return memoryInt                [getMemoryBlockAddress(pointer, MEMORY_TYPE_INT)]; }
    marker              readMarker              (int pointer) { return memoryMarker             [getMemoryBlockAddress(pointer, MEMORY_TYPE_MARKER)]; }
    order               readOrder               (int pointer) { return memoryOrder              [getMemoryBlockAddress(pointer, MEMORY_TYPE_ORDER)]; }
    playergroup         readPlayergroup         (int pointer) { return memoryPlayergroup        [getMemoryBlockAddress(pointer, MEMORY_TYPE_PLAYERGROUP)]; }
    point               readPoint               (int pointer) { return memoryPoint              [getMemoryBlockAddress(pointer, MEMORY_TYPE_POINT)]; }
    region              readRegion              (int pointer) { return memoryRegion             [getMemoryBlockAddress(pointer, MEMORY_TYPE_REGION)]; }
    sound               readSound               (int pointer) { return memorySound              [getMemoryBlockAddress(pointer, MEMORY_TYPE_SOUND)]; }
    soundlink           readSoundlink           (int pointer) { return memorySoundlink          [getMemoryBlockAddress(pointer, MEMORY_TYPE_SOUNDLINK)]; }
    string              readString              (int pointer) { return memoryString             [getMemoryBlockAddress(pointer, MEMORY_TYPE_STRING)]; }
    text                readText                (int pointer) { return memoryText               [getMemoryBlockAddress(pointer, MEMORY_TYPE_TEXT)]; }
    timer               readTimer               (int pointer) { return memoryTimer              [getMemoryBlockAddress(pointer, MEMORY_TYPE_TIMER)]; }
    transmissionsource  readTransmissionsource  (int pointer) { return memoryTransmissionsource [getMemoryBlockAddress(pointer, MEMORY_TYPE_TRANSMISSIONSOURCE)]; }
    trigger             readTrigger             (int pointer) { return memoryTrigger            [getMemoryBlockAddress(pointer, MEMORY_TYPE_TRIGGER)]; }
    unit                readUnit                (int pointer) { return memoryUnit               [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNIT)]; }
    unitfilter          readUnitfilter          (int pointer) { return memoryUnitfilter         [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITFILTER)]; }
    unitgroup           readUnitgroup           (int pointer) { return memoryUnitgroup          [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITGROUP)]; }
    unitref             readUnitref             (int pointer) { return memoryUnitref            [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITREF)]; }
    revealer            readRevealer            (int pointer) { return memoryRevealer           [getMemoryBlockAddress(pointer, MEMORY_TYPE_REVEALER)]; }
    wave                readWave                (int pointer) { return memoryWave               [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVE)]; }
    waveinfo            readWaveinfo            (int pointer) { return memoryWaveinfo           [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVEINFO)]; }
    wavetarget          readWavetarget          (int pointer) { return memoryWavetarget         [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVETARGET)]; }
    funcref<delegate>   readDelegate            (int pointer) { return memoryDelegate           [getMemoryBlockAddress(pointer, MEMORY_TYPE_DELEGATE)]; }

    void writeAbilcmd               (int pointer, abilcmd value)            { memoryAbilcmd             [getMemoryBlockAddress(pointer, MEMORY_TYPE_ABILCMD)]            = value; }
    void writeAifilter              (int pointer, aifilter value)           { memoryAifilter            [getMemoryBlockAddress(pointer, MEMORY_TYPE_AIFILTER)]           = value; }
    void writeActor                 (int pointer, actor value)              { memoryActor               [getMemoryBlockAddress(pointer, MEMORY_TYPE_ACTOR)]              = value; }
    void writeBool                  (int pointer, bool value)               { memoryBool                [getMemoryBlockAddress(pointer, MEMORY_TYPE_BOOL)]               = value; }
    void writeBank                  (int pointer, bank value)               { memoryBank                [getMemoryBlockAddress(pointer, MEMORY_TYPE_BANK)]               = value; }
    void writeByte                  (int pointer, byte value)               { memoryByte                [getMemoryBlockAddress(pointer, MEMORY_TYPE_BYTE)]               = value; }
    void writeCamerainfo            (int pointer, camerainfo value)         { memoryCamerainfo          [getMemoryBlockAddress(pointer, MEMORY_TYPE_CAMERAINFO)]         = value; }
    void writeColor                 (int pointer, color value)              { memoryColor               [getMemoryBlockAddress(pointer, MEMORY_TYPE_COLOR)]              = value; }
    void writeDoodad                (int pointer, doodad value)             { memoryDoodad              [getMemoryBlockAddress(pointer, MEMORY_TYPE_DOODAD)]             = value; }
    void writeFixed                 (int pointer, fixed value)              { memoryFixed               [getMemoryBlockAddress(pointer, MEMORY_TYPE_FIXED)]              = value; }
    void writeInt                   (int pointer, int value)                { memoryInt                 [getMemoryBlockAddress(pointer, MEMORY_TYPE_INT)]                = value; }
    void writeMarker                (int pointer, marker value)             { memoryMarker              [getMemoryBlockAddress(pointer, MEMORY_TYPE_MARKER)]             = value; }
    void writeOrder                 (int pointer, order value)              { memoryOrder               [getMemoryBlockAddress(pointer, MEMORY_TYPE_ORDER)]              = value; }
    void writePlayergroup           (int pointer, playergroup value)        { memoryPlayergroup         [getMemoryBlockAddress(pointer, MEMORY_TYPE_PLAYERGROUP)]        = value; }
    void writePoint                 (int pointer, point value)              { memoryPoint               [getMemoryBlockAddress(pointer, MEMORY_TYPE_POINT)]              = value; }
    void writeRegion                (int pointer, region value)             { memoryRegion              [getMemoryBlockAddress(pointer, MEMORY_TYPE_REGION)]             = value; }
    void writeSound                 (int pointer, sound value)              { memorySound               [getMemoryBlockAddress(pointer, MEMORY_TYPE_SOUND)]              = value; }
    void writeSoundlink             (int pointer, soundlink value)          { memorySoundlink           [getMemoryBlockAddress(pointer, MEMORY_TYPE_SOUNDLINK)]          = value; }
    void writeString                (int pointer, string value)             { memoryString              [getMemoryBlockAddress(pointer, MEMORY_TYPE_STRING)]             = value; }
    void writeText                  (int pointer, text value)               { memoryText                [getMemoryBlockAddress(pointer, MEMORY_TYPE_TEXT)]               = value; }
    void writeTimer                 (int pointer, timer value)              { memoryTimer               [getMemoryBlockAddress(pointer, MEMORY_TYPE_TIMER)]              = value; }
    void writeTransmissionsource    (int pointer, transmissionsource value) { memoryTransmissionsource  [getMemoryBlockAddress(pointer, MEMORY_TYPE_TRANSMISSIONSOURCE)] = value; }
    void writeTrigger               (int pointer, trigger value)            { memoryTrigger             [getMemoryBlockAddress(pointer, MEMORY_TYPE_TRIGGER)]            = value; }
    void writeUnit                  (int pointer, unit value)               { memoryUnit                [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNIT)]               = value; }
    void writeUnitfilter            (int pointer, unitfilter value)         { memoryUnitfilter          [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITFILTER)]         = value; }
    void writeUnitgroup             (int pointer, unitgroup value)          { memoryUnitgroup           [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITGROUP)]          = value; }
    void writeUnitref               (int pointer, unitref value)            { memoryUnitref             [getMemoryBlockAddress(pointer, MEMORY_TYPE_UNITREF)]            = value; }
    void writeRevealer              (int pointer, revealer value)           { memoryRevealer            [getMemoryBlockAddress(pointer, MEMORY_TYPE_REVEALER)]           = value; }
    void writeWave                  (int pointer, wave value)               { memoryWave                [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVE)]               = value; }
    void writeWaveinfo              (int pointer, waveinfo value)           { memoryWaveinfo            [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVEINFO)]           = value; }
    void writeWavetarget            (int pointer, wavetarget value)         { memoryWavetarget          [getMemoryBlockAddress(pointer, MEMORY_TYPE_WAVETARGET)]         = value; }
    void writeDelegate              (int pointer, funcref<delegate> value)  { memoryDelegate            [getMemoryBlockAddress(pointer, MEMORY_TYPE_DELEGATE)]           = value; }

    //pointer manipulation
    int         sizeof  (int pointer)               { return    DataTableGetInt(true, "ARR_SIZE" + IntToString(pointer)); }
    static void setSize (int pointer, int size)     {           DataTableSetInt(true, "ARR_SIZE" + IntToString(pointer), size); }

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - heap manipulation
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    //pointer size manipulation
    static int  getMemorySize       (int pointer)                               { return    DataTableGetInt(true, "MEM_SIZE" + IntToString(pointer)); }
    static void setMemorySize       (int pointer, int size)                     {           DataTableSetInt(true, "MEM_SIZE" + IntToString(pointer), size); }

    //pointer.heapPosition manipulation
    static int  getPosition         (int pointer, int memoryType)               { return    DataTableGetInt(true, "HEAP_POSITION_" + IntToString(memoryType) + "->" + IntToString(pointer)); }
    static void setPosition         (int pointer, int position, int memoryType) {           DataTableSetInt(true, "HEAP_POSITION_" + IntToString(memoryType) + "->" + IntToString(pointer), position); }

    //heapPosition.pointer manipulation
    static int  getPointer          (int position, int memoryType)              { return    DataTableGetInt(true, "HEAP_POINTER_" + IntToString(memoryType) + "->" + IntToString(position)); }
    static void setPointer          (int position, int pointer, int memoryType) {           DataTableSetInt(true, "HEAP_POINTER_" + IntToString(memoryType) + "->" + IntToString(position), pointer); }

    //heap position neighbor getters
    static int  getParentPosition   (int position)                              { return position/2;        }
    static int  getLeftPosition     (int position)                              { return position*2;        }
    static int  getRightPosition    (int position)                              { return position*2 + 1;    }

    //link pointer position with pointer
    static void link(int pointer, int pointerPosition, int memoryType) {
        setPointer  (pointerPosition, pointer, memoryType);
        setPosition (pointer, pointerPosition, memoryType);
    } //link

    static void bubbleUp(int pointer, int memoryType) {
        int pointerSize     = getMemorySize     (pointer);
        int pointerPosition = getPosition       (pointer, memoryType);

        int parentPosition  = getParentPosition (pointerPosition);
        int parentPointer   = getPointer        (parentPosition, memoryType);

        while (0 != parentPosition && getMemorySize(parentPointer) < pointerSize) {
            link(parentPointer, pointerPosition, memoryType);

            pointerPosition     = parentPosition;
            parentPosition      = getParentPosition (pointerPosition);
            parentPointer       = getPointer        (parentPosition, memoryType);
        } //while

        link(pointer, pointerPosition, memoryType);
    } //bubbleUp

    static void bubbleDown(int pointer, int memoryType) {
        int pointerSize     = getMemorySize     (pointer);
        int pointerPosition = getPosition       (pointer, memoryType);

        int leftPosition    = getLeftPosition   (pointerPosition);
        int leftPointer     = getPointer        (leftPosition, memoryType);
        int leftSize        = getMemorySize     (leftPointer);

        int rightPosition   = getRightPosition  (pointerPosition);
        int rightPointer    = getPointer        (rightPosition, memoryType);
        int rightSize       = getMemorySize     (rightPointer);

        while ((0 != leftPointer && leftSize > pointerSize) || (0 != rightPointer && rightSize > pointerSize)) {
            if (0 == rightPointer || (0 != leftPointer && leftSize > rightSize)) {
                link(leftPointer, pointerPosition, memoryType);
                pointerPosition = leftPosition;
            } //if
            else {
                link(rightPointer, pointerPosition, memoryType);
                pointerPosition = rightPosition;
            } //else

            leftPosition    = getLeftPosition   (pointerPosition);
            leftPointer     = getPointer        (leftPosition, memoryType);
            leftSize        = getMemorySize     (leftPointer);

            rightPosition   = getRightPosition  (pointerPosition);
            rightPointer    = getPointer        (rightPosition, memoryType);
            rightSize       = getMemorySize     (rightPointer);
        } //while

        link(pointer, pointerPosition, memoryType);
    } //bubbleDown

    static void recycle(int pointerToRecycle, int memoryType) {
        int pointerPosition = getPosition   (pointerToRecycle, memoryType);
        int pointer         = getPointer    (heapSize[memoryType], memoryType);

        setPointer(heapSize[memoryType], 0, memoryType);
        link(pointer, pointerPosition, memoryType);

        if (pointerPosition != 1) {
            bubbleUp(pointer, memoryType);
        } //if
        bubbleDown(pointer, memoryType);

        heapSize[memoryType] = heapSize[memoryType] - 1;
    } //recycle

    //returns new size
    static int mergePointers(int pointer1, int pointerLastIndex1, int pointer2, int pointerLastIndex2, int memoryType) {
        int swap;

        if (pointer1 > pointer2) {
            swap = pointer2;

            pointer2 = pointer1;
            pointer1 = swap;

            swap = pointerLastIndex2;
            pointerLastIndex2 = pointerLastIndex1;
            pointerLastIndex1 = swap;
        } //if

        setMemoryChunkLimitFlag (pointerLastIndex1, false);
        setMemorySize           (pointerLastIndex1, 0);

        setMemoryChunkLimitFlag (pointer2, false);
        setMemorySize           (pointer2, 0);

        recycle(pointer2, memoryType);

        return (pointerLastIndex1 - pointer1 + 1) + (pointerLastIndex2 - pointer2 + 1);
    } //mergePointers

    static void defragment(int pointer, int memoryType) {
        int size                = getMemorySize(pointer);

        int pointerLastIndex    = pointer + size - 1;
        int pointerPosition     = getPosition(pointer, memoryType);

        int startAddress        = memoryTypeStartAddress[memoryType];
        int endAddress          = startAddress + memoryTypeSize[memoryType];

        int pointer2;
        int pointer2size;

        pointer2 = pointer - 1;
        if (startAddress != pointer && isMemoryChunkLimit(pointer2)) {
            pointer2size = getMemorySize(pointer2);
            pointer2 = pointer2 - pointer2size + 1;

            size = mergePointers(pointer, pointerLastIndex, pointer2, pointer2 + pointer2size - 1, memoryType);

            pointer = pointer2;
            pointerLastIndex = pointer + size - 1;
        } //if

        pointer2 = pointer + size;
        if (pointer2 < endAddress && isMemoryChunkLimit(pointer2)) {
            size = mergePointers(pointer, pointerLastIndex, pointer2, pointer2 + getMemorySize(pointer2) - 1, memoryType);

            pointerLastIndex = pointer + size - 1;
        } //if

        setMemorySize(pointer,          size);
        setMemorySize(pointerLastIndex, size);

        link(pointer, pointerPosition, memoryType);

        bubbleUp(pointer, memoryType);
    } //defragment

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - memory allocation
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    int allocate(int size, int memoryType) {
        int pointer     = getPointer    (1, memoryType);
        int memorySize  = getMemorySize (pointer);

        if (memoryType < 0 || memoryType >= MEMORY_TYPE_COUNT) {
            TriggerDebugOutput(1, StringToText("ALLOCATE FAILURE: ATTEMPT TO ALLOCATE INVALID MEMORY TYPE: (") + IntToText(memoryType) + StringToText(")"), true);
            return 0;
        } //if

        if (0 == heapSize[memoryType] || memorySize < size) {
            if (allocatedMemory[memoryType] + size - memoryTypeStartAddress[memoryType] >= memoryTypeSize[memoryType]) {
                TriggerDebugOutput(1, StringToText("ALLOCATE FAILURE: (") + memoryTypeName[memoryType] + StringToText(")"), true);
                return 0;
            } //if

            pointer                     = allocatedMemory[memoryType];
            allocatedMemory[memoryType] = allocatedMemory[memoryType] + size;
        } //if
        else {
            setMemorySize           (pointer, 0);
            setMemoryChunkLimitFlag (pointer, false);
            setMemorySize           (pointer + size, memorySize - size);
            setMemorySize           (pointer + memorySize - 1, memorySize - size);

            if (0 == memorySize - size) {
                recycle(pointer, memoryType);
                setMemoryChunkLimitFlag(pointer + memorySize - 1, false);
            } //if
            else {
                link(pointer + size, 1, memoryType);
                setMemoryChunkLimitFlag(pointer + size, true);
                bubbleDown(pointer + size, memoryType);
            } //else

            releasedMemory[memoryType] = releasedMemory[memoryType] - size;
        } //else

        pointer = pointer + 1;
        setSize(pointer, size);

        return pointer;
    } //allocate

    void deallocate(int pointer) {
        int size = sizeof(pointer);
        int memoryType;

        pointer = pointer - 1;
        memoryType = getMemoryType(pointer);

        if (memoryType == MEMORY_TYPE_INVALID) {
            TriggerDebugOutput(1, StringToText("ATTEMPT TO DEALLOCATE OUT OF BOUNDS POINTER: (") + IntToText(pointer + 1) + StringToText(")"), true);
            return;
        } //if
        
        if (size == 0) {
            TriggerDebugOutput(1, StringToText("ATTEMPT TO DEALLOCATE NULL POINTER: (") + memoryTypeName[memoryType] + StringToText(")"), true);
            return;
        } //if

        setSize(pointer + 1, 0);

        setMemorySize(pointer, size);
        setMemoryChunkLimitFlag(pointer, true);
        setMemoryChunkLimitFlag(pointer + size - 1, true);
        setMemorySize(pointer + size - 1, size);

        releasedMemory[memoryType] = releasedMemory[memoryType] + size;
        heapSize[memoryType] = heapSize[memoryType] + 1;
        link(pointer, heapSize[memoryType], memoryType);
        bubbleUp(pointer, memoryType);

        defragment(pointer, memoryType);
    } //deallocate

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - debug
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
void printMemoryRemaining       (int memoryType) { TriggerDebugOutput(1, StringToText("Memory Remaining (") + memoryTypeName[memoryType] + StringToText("): ") + IntToText(memoryTypeStartAddress[memoryType] + memoryTypeSize[memoryType] - allocatedMemory[memoryType] + releasedMemory[memoryType]), true); }
void printMemoryUsed            (int memoryType) { TriggerDebugOutput(1, StringToText("Memory Usage (") + memoryTypeName[memoryType] + StringToText("): ") + IntToText(allocatedMemory[memoryType] - memoryTypeStartAddress[memoryType] - releasedMemory[memoryType]), true); }
void printMemoryUsedPercent     (int memoryType) { TriggerDebugOutput(1, StringToText("Memory Usage (") + memoryTypeName[memoryType] + StringToText("): ") + FixedToText(IntToFixed(allocatedMemory[memoryType] - memoryTypeStartAddress[memoryType] - releasedMemory[memoryType])/IntToFixed(memoryTypeSize[memoryType])*100, 2) + StringToText("%"), true); }
void printHeapSize              (int memoryType) { TriggerDebugOutput(1, StringToText("Heap Size (") + memoryTypeName[memoryType] + StringToText("): ") + IntToText(heapSize[memoryType]), true); }

//------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  - initialization
//
//------------------------------------------------------------------------------------------------------------------------------------------------------
    void initializeMemoryAllocation() {
            initializeMemoryTypeAddresses();
            heapInitialization();
    } //initializeMemoryAllocation

Demonstration

void print(string msg) {
    TriggerDebugOutput(1, StringToText(msg), true);
}
void displayMemory(int memoryType) {
    printMemoryRemaining(memoryType);
    printMemoryUsed(memoryType);
    printMemoryUsedPercent(memoryType);
}
void test() {
    const int TEST_SIZE = 50;

    int[TEST_SIZE] arr;
    int i = 0;

    while (i != TEST_SIZE) {
        arr[i] = allocate(RandomInt(15, 35), MEMORY_TYPE_INT);
        i = i + 1;
    } //while

    displayMemory(MEMORY_TYPE_INT);
    printHeapSize(MEMORY_TYPE_INT);
    print("-----------------------------");

    deallocate(arr[22]);
    deallocate(arr[24]);
    deallocate(arr[23]);
    deallocate(arr[21]);

    deallocate(arr[21]);
    deallocate(294282);

    displayMemory(MEMORY_TYPE_INT);
    printHeapSize(MEMORY_TYPE_INT);

    print("Test Complete");
}
void tester() {
    initializeMemoryAllocation();
    test();
}

Comments

  • To post a comment, please or register a new account.
Posts Quoted:
Reply
Clear All Quotes

About This Project

  • Project ID
    42766
  • Created
    Aug 2, 2012
  • Last Released File
    Aug 3, 2012
  • Total Downloads
    148
  • License

Categories

Members

Recent Files