-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarray.cpp
More file actions
135 lines (113 loc) · 2.43 KB
/
marray.cpp
File metadata and controls
135 lines (113 loc) · 2.43 KB
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
#include "marray.h"
struct marray_list marrayFreeList = {};
void AddToMFreeList(int index)
{
if (marrayFreeList.head == NULL)
{
marrayFreeList.head = Memory(struct marray_link);
marrayFreeList.head->id = index;
marrayFreeList.head->next = NULL;
marrayFreeList.tail = marrayFreeList.head;
}
else {
marrayFreeList.tail->next = Memory(struct marray_link);
marrayFreeList.tail->next->id = index;
marrayFreeList.tail->next->next = NULL;
marrayFreeList.tail = marrayFreeList.tail->next;
}
}
void CreateMArray_(struct marray* mArray, char* filename, int line)
{
mArray->realCount = 0;
mArray->count = 0;
mArray->size = 2;
mArray->ptr = MemoryAL(i32*, mArray->size, line, filename);
}
void AddToMArray_(struct marray* mArray, i32* addr, char* filename, int line)
{
if (mArray->ptr == NULL)
{
CreateMArray(mArray);
}
else {
if (marrayFreeList.head)
{
int newIndex = marrayFreeList.head->id;
struct marray_link* temp = marrayFreeList.head;
marrayFreeList.head = marrayFreeList.head->next;
if (temp)
{
FreeMemory((i8*)temp);
temp = NULL;
}
mArray->ptr[newIndex] = (i32*)addr;
return;
}
}
if (mArray->realCount < mArray->size)
{
mArray->ptr[mArray->realCount] = (i32*)addr;
//newArray= addr;
mArray->count++;
mArray->realCount++;
}
else {
i32 oldSize = 0;
i32** tempChunk = mArray->ptr;
oldSize = mArray->size;
mArray->size *= 2;
mArray->ptr = MemoryA(i32*, mArray->size);
for (int i = 0; i < oldSize; i++)
{
mArray->ptr[i] = tempChunk[i];
}
mArray->ptr[mArray->realCount] = addr;
mArray->count++;
mArray->realCount++;
if (tempChunk)
{
FreeMemory((i8*)tempChunk);
tempChunk = NULL;
}
}
}
i32* GetFromMArray(struct marray* Array, i32 index)
{
return (i32*)Array->ptr[index];
}
void RemoveFromArray(struct marray* Array, i32 index)
{
void* ptr = Array->ptr[index];
if (ptr)
{
FreeMemory((i8*)ptr);
ptr = NULL;
}
// AddToMFreeList(index);
Array->count--;
}
void FreeMArray(struct marray* Array)
{
struct marray_link* arrayListHead = NULL;
if (Array->realCount == 0) return;
for (int i = 0; i < Array->realCount; i++)
{
RemoveFromArray(Array, i);
}
if (Array->ptr)
{
FreeMemory((i8*)Array->ptr);
Array->ptr = NULL;
}
arrayListHead = marrayFreeList.head;
while (arrayListHead)
{
struct marray_link* temp = arrayListHead;
arrayListHead = arrayListHead->next;
if (temp)
{
FreeMemory((i8*)temp);
temp = NULL;
}
}
}