102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
handles.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/ui/functions.h"
11#include "pcdogs/ui/globals.h"
12
13// Original pcdogs.exe: 0x444bd0
14// Retries allocation after allocation-failure dialogs until memory is obtained or the user terminates the process.
15void *Mem_MallocWithRetry(uint32_t size, char const *context)
16{
17 void *result = CRT_malloc(size);
18
19 if (!result)
20 {
21 CRT_printf(g_fmt_Mem_MallocFailed, context, size);
22
24
25 while (true)
26 {
28
29 result = CRT_malloc(size);
30
31 if (result)
32 break;
33
35 {
37 CRT_exit(status);
38 /* no return */
39 }
40 }
41 }
42
43 return result;
44}
45
46// Original pcdogs.exe: 0x444ce0
47// Pops a free handle entry, allocates its extent, and stores a one-based handle, pointer, and size. Missing entries or allocation failure return zero.
48uint32_t Mem_AllocateHandle(uint32_t size)
49{
51
52 if (!free_entry)
53 {
55
56 return 0;
57 }
58
60
61 free_entry->ptr = allocation;
62
63 if (!allocation && size)
64 {
66
67 return 0;
68 }
69
71 free_entry->size = size;
72 free_entry->handle_id = (uint32_t)(free_entry - g_memoryHandlePool) + 1;
73
76 free_entry->handle_id,
77 free_entry->ptr, size);
78
79 return free_entry->handle_id;
80}
81
82// Original pcdogs.exe: 0x444e00
83// Initializes 100000 extent entries and their free chain, marking each handle unused and clearing pointer and size fields. It returns one.
85{
87
88 for (struct Mem_HandlePoolEntry *entry = g_memoryHandlePool;
89 entry < g_memoryHandlePool + MEM_HANDLE_CAPACITY; ++entry)
90 {
91 entry->ptr = NULL;
92 entry->handle_id = MEM_HANDLE_UNUSED;
93 entry->next_free = entry + 1;
94 entry->size = 0;
95 }
96
97 g_memoryHandlePool[MEM_HANDLE_CAPACITY - 1].next_free = NULL;
98
99 return 1;
100}
101
102// Original pcdogs.exe: 0x444e40
103// Reports and frees outstanding extents, then clears the free-list head. This is allocator teardown rather than a reusable allocator reset.
105{
106 for (struct Mem_HandlePoolEntry *p_extent_entry = g_memoryHandlePool;
107 p_extent_entry < g_memoryHandlePool + MEM_HANDLE_CAPACITY; ++p_extent_entry)
108 {
109 uint32_t handle_id = p_extent_entry->handle_id;
110
112 {
113 CRT_printf(STR_LEAK_UNRELEASED_EXTENT, handle_id, p_extent_entry->ptr,
114 p_extent_entry->size);
115 p_extent_entry->handle_id = MEM_HANDLE_UNUSED;
116
117 void *ptr = p_extent_entry->ptr;
118
119 if (ptr)
120 {
121 CRT_free(ptr);
122 p_extent_entry->ptr = 0;
123 }
124 }
125
126 p_extent_entry->size = 0;
127 }
128
130
131 return 1;
132}
133
134// Original pcdogs.exe: 0x444eb0
135// Frees a valid-range allocated handle entry and pushes it onto the free list without clearing its size. Handles are reused without generation tracking, and this path checks allocation state rather than stored-handle equality.
136BOOL Mem_ReleaseHandle(uint32_t handle)
137{
138 if (handle - 1 >= MEM_HANDLE_CAPACITY)
139 {
141
142 return 0;
143 }
144
145 uint32_t handle_id = g_memoryHandlePool[handle - 1].handle_id;
146
148 {
150
151 return 0;
152 }
153
156
157 void *ptr = g_memoryHandlePool[handle - 1].ptr;
158
159 if (ptr)
160 {
161 CRT_free(ptr);
162 g_memoryHandlePool[handle - 1].ptr = 0;
163 }
164
165 struct Mem_HandlePoolEntry *previous_free_head = g_memoryHandlePoolHead;
166
167 g_memoryHandlePool[handle - 1].handle_id = MEM_HANDLE_UNUSED;
169 g_memoryHandlePool[handle - 1].next_free = previous_free_head;
170
171 return 1;
172}
173
174// Original pcdogs.exe: 0x444f60
175// Returns an extent pointer only when the handle is in range and matches the entry's stored identifier; otherwise it returns null.
176void *Mem_GetPointerByHandle(uint32_t handle)
177{
178 if (handle == 0 || handle > MEM_HANDLE_CAPACITY)
179 return NULL;
180
181 struct Mem_HandlePoolEntry *entry = &g_memoryHandlePool[handle - 1];
182
183 return entry->handle_id == handle ? entry->ptr : NULL;
184}
185
186// Original pcdogs.exe: 0x444f90
187// Tests whether a handle is in range and matches the entry's stored identifier.
188BOOL Mem_IsValidHandle(uint32_t handle)
189{
190 return (handle > 0 && handle <= MEM_HANDLE_CAPACITY && g_memoryHandlePool[handle - 1].handle_id == handle);
191}
BOOL Mem_ReleaseHandle(uint32_t handle)
Definition handles.c:136
BOOL Mem_FreeAllExtents()
Definition handles.c:104
BOOL Mem_IsValidHandle(uint32_t handle)
Definition handles.c:188
BOOL Mem_InitializeAllocator()
Definition handles.c:84
void * Mem_GetPointerByHandle(uint32_t handle)
Definition handles.c:176
uint32_t Mem_AllocateHandle(uint32_t size)
Definition handles.c:48
void * Mem_MallocWithRetry(uint32_t size, char const *context)
Definition handles.c:15
#define MEM_HANDLE_UNUSED
Definition constants.h:7
#define MEM_HANDLE_CAPACITY
Definition constants.h:6
#define g_sz_Mem_AllocPrefix
Definition globals.h:18
#define g_sz_Mem_AllocFailed
Definition globals.h:15
#define g_sz_Mem_OutOfExtents
Definition globals.h:21
#define g_memoryHandlePoolHead
Definition globals.h:36
#define g_fmt_Mem_FreeDebug
Definition globals.h:27
#define g_memDebugEnabled
Definition globals.h:39
#define g_fmt_Mem_MallocFailed
Definition globals.h:9
#define g_memoryHandlePool
Definition globals.h:33
#define g_fmt_Mem_LeakUnallocated
Definition globals.h:30
#define g_fmt_Mem_LeakInvalidExtent
Definition globals.h:24
#define g_fmt_Mem_AllocDebug
Definition globals.h:12
#define STR_LEAK_UNRELEASED_EXTENT
Definition globals.h:156
#define STR_OUT_OF_MEMORY
Definition globals.h:153
#define STR_DEBUG_KILLED_BY_PLAYER
Definition globals.h:150
int32_t CRT_printf(char const *format,...)
Definition formatting.c:63
void * CRT_malloc(uint32_t size)
Definition heap.c:16
void CRT_free(void *ptr)
Definition heap.c:164
int32_t CRT_fflush(struct File_Handle *stream)
Definition stdio.c:17
void CRT_exit(int32_t status)
Definition startup.c:135
#define g_crtStaticStreams
Definition globals.h:336
uint32_t handle_id
Definition types.h:48
struct Mem_HandlePoolEntry * next_free
Definition types.h:47
uint32_t size
Definition types.h:49
BOOL UI_ShowConfirmDialog(char const *format,...)
Definition hud.c:935
void UI_ShowErrorMessage(char const *format,...)
Definition hud.c:962
#define g_sz_UI_ConfirmStopGame
Definition globals.h:17