Memory Classes

// Destruct callback for cleaning up item.
typedef void (*FDestruct)(void *ptr);

// Compare keys callback indicating key order. Returns -1 when [a] is less than [b],
// 0 when [a] equals [b] and 1 when [a] is greater than [b].
typedef int (*FCompareKeys)(cptr a, cptr b);

// Construct [key] using [mem]. Usage: new (mem) T(key).
typedef void (*FConstructKey)(void *mem, cptr key);

// Power of 2 bucketed (po2b) memory heap.
class RVAPI HeapMem
{
public:
    // Allocate [size] memory.
    static void *Alloc(uint size);

    // Free memory. [ptr] is set to zero.
    static void Free(void **ptr);

    // Start printing memory allocations.
    static void Mark();

    // Stop printing memory allocations.
    static void Unmark();
};

// Array po2b memory.
class RVAPI ArrayMem
{
public:
    // Get item count.
    uint GetCount() const;

    // Get item at [index]. Negative [index] offsets from end.
    void *GetItem(int index) const;

    // Check if array is empty.
    bool operator ! () const { return GetCount() == 0; }

    // Add item. [at] is index of insertion. Negative [at] offsets from end. 
    // [index] pointer receives index of added item if non-zero.
    // Returns memory to use with new. Usage: new (array.Add()) Type().
    void *Add(int at, int *index = 0);

    // Delete item at [index]. Negative [index] offsets from end.
    void Delete(int index);

    // Remove all items.
    void Clear();

    // Internal.
    void *operator () () const { return (void *)wobj; }

    // Construct array with items of size [itemSize]. Set [incr] to estimated max number of items.
    // [Destruct] callback is called on items being deleted.
    ArrayMem(uint itemSize, uint incr, FDestruct Destruct);

    // Destructor. Destruct callback is called on remaining items.
    ~ArrayMem();

private:
    byte wobj[64];
};

// List po2b memory.
class RVAPI ListMem
{
public:
    // Check if original item exists.
    bool IsItem(int index, int ver) const;

    // Get item count.
    uint GetCount() const;

    // Get item version.
    uint GetVer(void *item) const;

    // Get first item. Sets [index] iteration variable.
    void *GetFirst(int &index) const;

    // Get last item. Sets [index] iteration variable.
    void *GetLast(int &index) const;

    // Get previous item using [index] iteration variable.
    void *GetPrev(int &index) const;

    // Get next item using [index] iteration variable.
    void *GetNext(int &index) const;

    // Get item at [index]. Negative [index] offsets from end.
    void *GetItem(int index) const;

    // Check if list is empty.
    bool operator ! () const { return GetCount() == 0; }

    // Add item. [index] pointer receives index of added item if non-zero.
    // Returns memory to use with new. Usage: new (list.Add()) Type().
    void *Add(int *index = 0);

    // Delete item at [index]. Negative [index] offsets from end.
    void Delete(int index);

    // Delete all items.
    void Clear();

    // Internal.
    void *operator () () const { return (void *)wobj; }

    // Initialize list with items of size [itemSize]. Set [incr] to estimated max number of items.
    // [Destruct] callback is called on items being deleted.
    ListMem(uint itemSize, uint incr, FDestruct Destruct);

    // Destruct queue. Destruct callback is called on remaining items.
    ~ListMem();

private:
    byte wobj[192];
};

// Queue po2b memory.
class RVAPI QueueMem
{
public:
    // Get item count.
    uint GetCount() const;

    // Get item at front of queue.
    void *GetItem() const;

    // Check if queue is empty.
    bool operator ! () const { return GetCount() == 0; }

    // Add item to end of queue. Returns memory to use with new. Usage: new (queue.Add()) Type().
    void *Add();

    // Delete item from front of queue.
    void Delete();

    // Delete all items.
    void Clear();

    // Internal.
    void *operator () () const { return (void *)wobj; }

    // Construct queue with items of size [itemSize]. Set [incr] to estimated max number of items.
    // [Destruct] callback is called on items being deleted.
    QueueMem(uint itemSize, uint incr, FDestruct Destruct);

    // Deconstuct queue. Destruct is called on remaining items.
    ~QueueMem();

private:
    byte wobj[96];
};

// Map po2b memory
class RVAPI MapMem
{
public:
    // Get item count.
    uint GetCount() const;

    // Get key by [index]. Walking through the [map] by [index] returns items in key order.
    void *GetKey(uint index) const;

    // Get item by [index]. Walking through the [map] by [index] returns items in key order.
    void *GetItem(uint index) const;

    // Check if map is empty.
    bool operator ! () const { return GetCount() == 0; }

    // Find item by [key].
    void *Find(void *key) const;

    // Add item using [key]. Returns memory to use with new. Usage: new (map.Add()) Type().
    void *Add(void *key);

    // Delete item using [key].
    void Delete(void *key);

    // Delete all items.
    void Clear();

    // Internal.
    void *operator () () const { return (void *)wobj; }

    // Construct map with items of size [itemSize] and keys of [keySize]. Set [incr] to estimated max number 
    // of items. [CompareKeys] is called when keys need to be compared. [Constructkey] is called when a key
    // needs to be constructed. [Destruct] is called on items being deleted.
    MapMem(uint itemSize, uint keySize, uint incr, FCompareKeys CompareKeys,
        FConstructKey ConstructKey, FDestruct IDestruct, FDestruct KDestruct);

    // Destruct map. Destruct callback is called on remaining items.
    ~MapMem();

private:
    byte wobj[256];
};