summaryrefslogtreecommitdiff
path: root/memory.cpp
blob: 2a9b29b4381e69c0b034b74b31ee880fe0501018 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

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 = (ptrsize *)((uint8 *)GlobalMemory->Address + GlobalMemory->CurrentPosition);
    Table->Size = Size;
    GlobalMemory->CurrentPosition += Size;
}

// NOTE(fox): Currently memory acts like simple stack that can only grow forwards.
// Undos/deletes create "holes." Once someone undos/deletes enough to trigger
// the limit or we start caring more about space, I'll revamp the system to
// keep track of free holes and allow those to be returned.

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 = (ptrsize *)((uint8 *)Table->Address + Table->CurrentPosition);
    Table->CurrentPosition += Size;
    return Address;
}

// Returns the address and THEN advances
static void*
Memory_Advance(memory *Memory, uint64 Size, memory_table_list TableName) {
    return AllocateMemory(Memory, Size, TableName);
}

// Rewinds and THEN returns the address
static void*
Memory_Rewind(memory *Memory, uint64 Size, memory_table_list TableName) {
    void *Address;
    memory_table *Table = &Memory->Slot[TableName];
    if (Table->CurrentPosition - Size < 0) {
        return NULL;
    }
    Table->CurrentPosition -= Size;
    Address = (ptrsize *)((uint8 *)Table->Address + Table->CurrentPosition);
    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
}

static cached_bitmap *
Memory_RollingBitmap(memory *Memory, source *Source, uint32 FrameToSeek)
{
    uint16 Width = Source->Info.Width;
    uint16 Height = Source->Info.Height;
    uint16 BytesPerPixel = Source->Info.BytesPerPixel;

    memory_table *Table = &Memory->Slot[B_LoadedBitmaps];

    // First check whether we'd run over the buffer at the current
    // position, and reset the position if so.
    uint64 Size = Bitmap_CalcTotalBytes(Width, Height, BytesPerPixel);
    if (Table->CurrentPosition + Size > Table->Size) {
        Table->CurrentPosition = 0;
        Table->PointerIndex = 0;
    }

    cached_bitmap *Bitmap = &Memory->Bitmap[Table->PointerIndex];

    // If there are no pointers in front of us, we don't have to free any space
    // and just need to increment the number of pointers.
    if (Table->PointerIndex != Table->NumberOfPointers) {
        // Next, if there's a pointer in front of the current position,
        // check whether it's far away enough so that the size fits.
        bool32 BS = true;
        if (Bitmap->Data) {
            uint64 BytesBetween = (uint8 *)Bitmap->Data - ((uint8 *)Table->Address + Table->CurrentPosition);
            if (BytesBetween > Size) {
                uint32 StopAt = Table->NumberOfPointers - 1;
                while (StopAt > Table->PointerIndex - 1) {
                    Memory->Bitmap[StopAt + 1] = Memory->Bitmap[StopAt];
                    StopAt--;
                }
                Table->NumberOfPointers++;
                BS = false;
            }
        }
        // If it doesn't fit, then we need to dereference the pointers
        // until we have enough space.
        if ((Table->PointerIndex < Table->NumberOfPointers) && BS)
        {
            bool32 Avail = false;
            void *AddressStart = (void *)((uint8 *)Table->Address + Table->CurrentPosition);
            uint32 Amount = 0;
            while(!Avail) {
                // Bail out if we're on the last index, as we don't need to do anything else.
                // TODO(fox): This could be simplified if we compared
                // pointer start plus data instead of just the start.
                if (Table->PointerIndex != Table->NumberOfPointers - 1) {
                    void *Data2 = Memory->Bitmap[Table->PointerIndex+1].Data;
                    uint64 BytesBetween = (uint8 *)Data2 - (uint8 *)AddressStart;
                    if (BytesBetween < Size) {
                        uint32 StopAt = Table->PointerIndex;
                        while (StopAt < Table->NumberOfPointers - 1) {
                            Memory->Bitmap[StopAt] = Memory->Bitmap[StopAt + 1];
                            StopAt++;
                        }
                        Amount++;
                        Table->NumberOfPointers--;
                        if (Amount > 2) {
                            Amount += 0;
                        }
                    } else {
                        Avail = true;
                    }
                } else {
                    Avail = true;
                }
            }
        }
    } else {
        Table->NumberOfPointers++;
    }
    Bitmap->Data = AllocateMemory(Memory, Size, B_LoadedBitmaps);
    if (!Bitmap->Data) {
        Assert(0);
    }
    Bitmap->SourceOwner = Source;
    Bitmap->Frame = FrameToSeek;
    Table->PointerIndex++;

    // No two pointers on the table should hold the same data
    // address or be empty.
    Debug_Memory_Assert_Cohesion(Memory, Table);
    return Bitmap;
}