-
Notifications
You must be signed in to change notification settings - Fork 44
/
MALLOC.C
100 lines (85 loc) · 2.66 KB
/
MALLOC.C
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
#include "MALLOC.H"
#include "GAMEFLOW.H"
#include "SPECIFIC.H"
#include <STDLIB.H>
#include <STDIO.H>
#include <STRING.H>
#define GAME_MALLOC_BUFFER_SIZE (1024*1060)
char* malloc_ptr = NULL;
int malloc_used = 0;
int malloc_free = GAME_MALLOC_BUFFER_SIZE;
int script_malloc_size = 0;
char malloc_buffer[GAME_MALLOC_BUFFER_SIZE];
/*
* [FUNCTIONALITY] - init_game_malloc.
* Resets malloc_buffer and all allocation stats back to their default values.
* Note: The entire malloc_buffer is zero initialised.
* Note: Once the gameflow script is loaded it's always in malloc_buffer regardless.
*/
void init_game_malloc()//5E79C(<), 5F4F8(<) (F) (*) (D) (ND)
{
malloc_used = gfScriptLen;
malloc_free = GAME_MALLOC_BUFFER_SIZE - gfScriptLen;
malloc_ptr = &malloc_buffer[gfScriptLen];
memset(&malloc_ptr[0], 0, GAME_MALLOC_BUFFER_SIZE - gfScriptLen);
}
/*
* [FUNCTIONALITY] - game_malloc.
* "Allocates" memory from our malloc_buffer declared on the stack.
* Note: Memory that is "allocated" is not zero initialised.
* Note: If you run out of memory the application will exit.
* Note: Usage should be seen as stack functionality, always free the last block of memory you allocated (LIFO).
* [USAGE]
* @PARAM - [size] The amount of memory you wish to "allocate".
* @RETURN - [ptr] Pointer to the memory block you just "allocated".
*/
char* game_malloc(int size)//5E7E8(<), 5F544(<) (F) (*) (?) (D) (ND)
{
#if DEBUG_VERSION
char buf[80];
#endif
char* ptr;
size = (size + 3) & -4;
if (size <= malloc_free)
{
ptr = malloc_ptr;
malloc_free -= size;
malloc_used += size;
malloc_ptr += size;
}
#if DEBUG_VERSION
else
{
sprintf(buf, "game_malloc() out of space (needs %d only got %d)\n", size, malloc_free);
S_ExitSystem(buf);
return NULL;
}
#endif
return ptr;
}
/*
* [FUNCTIONALITY] - game_free.
* "Frees" memory from malloc_buffer.
* Note: Memory that is freed is not zero initialised.
* Note: There is no error checking so ensure you do not free more memory than the buffer can hold.
* Note: Usage should be seen as stack functionality, always free the last block of memory you allocated (LIFO).
* [USAGE]
* @PARAM - [size] The amount of memory you wish to "free".
*/
void game_free(int size)//5E85C(<), 5F590(<) (F) (*) (D) (ND)
{
size = (size + 3) & -4;
malloc_ptr -= size;
malloc_free += size;
malloc_used -= size;
}
/*
* [FUNCTIONALITY] - show_game_malloc_totals.
* Prints the amount of free/used malloc_buffer memory to stdio in Kilobytes.
*/
#if DEBUG_VERSION
void show_game_malloc_totals()//5E894(<), * (F) (*) (D) (ND)
{
printf("---->Total Memory Used %dK of %dK<----\n", ((malloc_used + 1023) / 1024) - 10, ((malloc_used + malloc_free) / 1024) - 10);
}
#endif