1 /*
2
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 */
19 /*
20 * $Source: r:/prj/cit/src/RCS/cybmem.c $
21 * $Revision: 1.40 $
22 * $Author: xemu $
23 * $Date: 1994/11/28 06:40:01 $
24 *
25 */
26
27 // Memory management and manipulation functions
28 // for Cyberia
29 #define __CYBMEM_SRC
30 //#include "ShockDialogs.h"
31
32 #include "cybmem.h"
33 #include "tools.h"
34 #include "objsim.h"
35 #include "objclass.h"
36 #include "objprop.h"
37 #include "textmaps.h"
38 #include "objcrit.h"
39 #include "dynmem.h"
40 #include "Shock.h"
41 #include "sideicon.h"
42 #include "criterr.h"
43 #include "OpenGL.h"
44
45 /*
46 #include <memstat.h>
47 #include <ckpoint.h>
48 #include <keydefs.h>
49 #include <musicai.h> // for stop_digi_fx()
50 */
51 uint32_t loadcount = 0;
52
53 extern Id critter_id_table[NUM_CRITTER][NUM_CRITTER_POSTURES];
54 extern Id posture_bases[];
55
56 extern void free_textures(void);
57 int flush_resource_cache(void);
58
59 int hand_fnum, digi_fnum, critter_fnum, critter_fnum2, texture_fnum;
60
flush_resource_cache(void)61 int flush_resource_cache(void) {
62 Id curr_id = ID_MIN;
63 int count = 0;
64 while (curr_id < resDescMax) {
65 if (ResInUse(curr_id) && ResPtr(curr_id) && !ResLocked(curr_id)) {
66 ResDrop(curr_id);
67 count++;
68 }
69 curr_id++;
70 }
71 return (count);
72 }
73
free_dynamic_memory(int mask)74 errtype free_dynamic_memory(int mask) {
75 // Release textures
76 if (loadcount & DYNMEM_TEXTURES & mask) {
77 free_textures();
78 ResCloseFile(texture_fnum);
79 }
80
81 if (loadcount & mask & DYNMEM_SIDEICONS) {
82 side_icon_free_bitmaps();
83 }
84
85 if (loadcount & mask & DYNMEM_FHANDLE_1) {
86 ResCloseFile(hand_fnum);
87 }
88
89 // digifx used to be fhandle 2
90 if (loadcount & mask & DYNMEM_FHANDLE_3) {
91 ResCloseFile(critter_fnum);
92 }
93
94 if (loadcount & mask & DYNMEM_FHANDLE_4) {
95 ResCloseFile(critter_fnum2);
96 }
97
98 loadcount &= ~mask;
99 return (OK);
100 }
101
load_dynamic_memory(int mask)102 errtype load_dynamic_memory(int mask) {
103 extern short _new_mode;
104
105 if (_new_mode != -1) {
106 if ((~loadcount) & mask & DYNMEM_TEXTURES) {
107 texture_fnum = ResOpenFile("res/data/texture.res");
108 load_textures();
109
110 if (texture_fnum < 0)
111 critical_error(CRITERR_RES | 7);
112 }
113 if ((~loadcount) & mask & DYNMEM_SIDEICONS) {
114 side_icon_load_bitmaps();
115 }
116
117 if ((~loadcount) & mask & DYNMEM_FHANDLE_1) {
118 hand_fnum = ResOpenFile("res/data/handart.res");
119 if (hand_fnum < 0)
120 critical_error(CRITERR_RES | 3);
121 }
122
123 // digifx used to be FHANDLE_2
124 if ((~loadcount) & mask & DYNMEM_FHANDLE_3) {
125 critter_fnum = ResOpenFile("res/data/objart2.res");
126 if (critter_fnum < 0)
127 critical_error(CRITERR_RES | 8);
128 }
129
130 if ((~loadcount) & mask & DYNMEM_FHANDLE_4) {
131 critter_fnum2 = ResOpenFile("res/data/objart3.res");
132 if (critter_fnum2 < 0)
133 critical_error(CRITERR_RES | 8);
134 }
135
136 loadcount |= mask;
137 }
138
139 opengl_clear_texture_cache();
140
141 return (OK);
142 }
143
144 #define LARGEST_GUESS 8000000
145 #define DECREMENT_INTERVAL 10000
146 #define MAX_PTRS 25
147 #define MINIMUM_SLORK_SIZE 100000
148
slorkatron_memory_check()149 int slorkatron_memory_check() {
150 int retval, size;
151 int ptr_count, i;
152 uchar *mem_ptrs[MAX_PTRS];
153
154 for (ptr_count = 0; ptr_count < MAX_PTRS; ptr_count++)
155 mem_ptrs[ptr_count] = NULL;
156
157 ptr_count = 0;
158
159 size = LARGEST_GUESS + DECREMENT_INTERVAL;
160 retval = 0;
161
162 while ((size > MINIMUM_SLORK_SIZE) && (ptr_count < MAX_PTRS)) {
163 mem_ptrs[ptr_count] = (uchar *)malloc(size); // mem_ptrs[ptr_count] = Malloc(size);
164 if (mem_ptrs[ptr_count] == NULL)
165 size -= DECREMENT_INTERVAL;
166 else {
167 retval += size;
168 ptr_count++;
169 }
170 }
171 for (i = ptr_count - 1; i >= 0; i--)
172 if (mem_ptrs[i] != NULL)
173 free(mem_ptrs[i]); // Free(mem_ptrs[i]);
174
175 return (retval);
176 }
177