summaryrefslogtreecommitdiff
path: root/memory.cpp
blob: 4177448f83606ae0580d958c25a79b9515898a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static void
InitMemoryTable(global_memory *GlobalMemory, memory *Memory, uint64 Size, memory_table_list TableName, char *Name) {
    memory_table *Table = &Memory->Slot[TableName];
    Table->Name = Name;
    Table->Address = (uint64 *)((uint8 *)GlobalMemory->Address + GlobalMemory->CurrentPosition);
    Table->Size = Size;
    GlobalMemory->CurrentPosition += Size;
}

static void*
AllocateMemory(memory *Memory, uint64 Size, memory_table_list TableName) {
    void *Address;
    memory_table *Table = &Memory->Slot[TableName];
    if (Table->CurrentPosition + Size > Table->Size) {
        return NULL;
    }
    Address = (uint64 *)((uint8 *)Table->Address + Table->CurrentPosition);
    Table->CurrentPosition += Size;
    return Address;
}

// Returns 0-1 range wherever Pointer is in relation to StartingPointer to Size*Amount.
static real32
Memory_NormalizedPosition(void *StartingPointer, uint32 Amount, uint32 Size, void *Pointer)
{
    real32 Result = 0;
    uint64 TotalSize = Amount*Size;
    uint64 PointerLocationSize = (uint8 *)Pointer - (uint8 *)StartingPointer;
    Result = (real32)PointerLocationSize / (real32)TotalSize;
    return Result;
}

static void
Debug_Memory_Assert_Cohesion(memory *Memory, memory_table *Table)
{
#if DEBUG
    for (uint32 i = 0; i < Table->NumberOfPointers; i++) {
        cached_bitmap *CurrentBitmap = &Memory->Bitmap[i];
        Assert(CurrentBitmap->Data);
        for (uint32 a = 0; a < Table->NumberOfPointers; a++) {
            if (a == i) {
                continue;
            }
            cached_bitmap *OtherBitmap = &Memory->Bitmap[a];
            if (OtherBitmap->Data == CurrentBitmap->Data) {
                Assert(0);
            }
        }
    }
#else
#endif
}