File: sarmemory.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (267 lines) | stat: -rw-r--r-- 7,069 bytes parent folder | download | duplicates (6)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __MSW__
# include <windows.h>
#endif

#include "gw.h"
#include "v3dtex.h"
#include "sfm.h"
#include "obj.h"
#include "sar.h"
#include "sarmemory.h"
#include "config.h"


static unsigned long SARMemoryStatTexture(
	const v3d_texture_ref_struct *t
);
static unsigned long SARMemoryStatSoundSource(
	const sar_sound_source_struct *sndsrc
);

void SARMemoryStat(
	sar_core_struct *core_ptr,
	sar_scene_struct *scene,
	sar_object_struct **object, int total_objects,
	sar_memory_stat_struct *stat_buf
);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? (float)atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))

#define RADTODEG(r)     ((r) * 180.0 / PI)
#define DEGTORAD(d)     ((d) * PI / 180.0)

#define ISSTREMPTY(s)   (((s) != NULL) ? (*(s) == '\0') : True)
#define STRLEN(s)       (((s) != NULL) ? ((int)strlen(s)) : 0)


/*
 *	Returns memory used by the given texture
 */
static unsigned long SARMemoryStatTexture(
	const v3d_texture_ref_struct *t
)
{
	unsigned long m = 0;

	if(t == NULL)
	    return(m);

	m += STRLEN(t->name) + STRLEN(t->filename);
	switch(t->dimensions)
	{
	  case 1:
	    m += t->width * t->total_frames * 4;
	    break;
	  case 2:
	    m += t->width * t->height + t->total_frames * 4;
	    break;
	}
	m += t->total_frames * sizeof(void *);
	m += sizeof(v3d_texture_ref_struct);

	return(m);
}

/*
 *	Returns memory used by the given sound source
 */
static unsigned long SARMemoryStatSoundSource(
	const sar_sound_source_struct *sndsrc
)
{
	unsigned long m = 0;

	if(sndsrc == NULL)
	    return(m);

	m += STRLEN(sndsrc->name);
	m += STRLEN(sndsrc->filename);
	m += STRLEN(sndsrc->filename_far);
	m += sizeof(sar_sound_source_struct);

	return(m);
}

/*
 *	Fetches memory stats and stores them on the given stat_buf
 */
void SARMemoryStat(
	sar_core_struct *core_ptr,
	sar_scene_struct *scene,
	sar_object_struct **object, int total_objects,
	sar_memory_stat_struct *stat_buf
)
{
	if((core_ptr == NULL) || (stat_buf == NULL))
	    return;

	memset(stat_buf, 0x00, sizeof(sar_memory_stat_struct));

	/* Add up memory used on scene */
	if(scene != NULL)
	{
	    int i;
	    unsigned long m = 0;
	    const sar_visual_model_struct *vmodel;
/*	    sar_scene_base_struct *base = &scene->base; */
	    sar_scene_horizon_struct *horizon = &scene->horizon;

	    m += sizeof(sar_scene_struct);
	    m += STRLEN(scene->title);

	    for(i = 0; i < scene->total_visual_models; i++)
	    {
		vmodel = scene->visual_model[i];
		if(vmodel == NULL)
		    continue;
		stat_buf->vmodel += STRLEN(vmodel->name);
		stat_buf->vmodel += STRLEN(vmodel->filename);
		stat_buf->vmodel += vmodel->mem_size;
		stat_buf->vmodel += sizeof(sar_visual_model_struct);
	    }
	    stat_buf->nvmodels += scene->total_visual_models;
	    m += scene->total_visual_models * sizeof(sar_visual_model_struct *);

	    m += scene->total_ground_objects * sizeof(sar_object_struct *);
	    m += scene->total_human_need_rescue_objects * sizeof(sar_object_struct *);

	    for(i = 0; i < horizon->total_textures; i++)
		stat_buf->texture += SARMemoryStatTexture(
		    horizon->texture[i]
		);
	    stat_buf->ntextures += horizon->total_textures;
	    m += horizon->total_textures * sizeof(v3d_texture_ref_struct *);

	    for(i = 0; i < scene->total_cloud_layers; i++)
	    {
		const sar_cloud_layer_struct *cloud = scene->cloud_layer[i];
		if(cloud == NULL)
		    continue;
		m += STRLEN(cloud->tex_name);
		m += sizeof(sar_cloud_layer_struct *);
	    }
	    m += scene->total_cloud_layers * sizeof(sar_cloud_layer_struct *);

	    for(i = 0; i < scene->total_cloud_bbs; i++)
	    {
		const sar_cloud_bb_struct *cloud = scene->cloud_bb[i];
		if(cloud == NULL)
		    continue;
		m += STRLEN(cloud->tex_name);
		m += sizeof(sar_cloud_bb_struct *);
	    }
	    m += scene->total_cloud_bbs * sizeof(sar_cloud_bb_struct *);

	    for(i = 0; i < scene->total_texture_refs; i++)
		stat_buf->texture += SARMemoryStatTexture(
		    scene->texture_ref[i]
		);
	    stat_buf->ntextures += scene->total_texture_refs;
	    m += scene->total_texture_refs * sizeof(v3d_texture_ref_struct *);

	    for(i = 0; i < scene->total_sndsrcs; i++)
		m += SARMemoryStatSoundSource(scene->sndsrc[i]);
	    m += scene->total_sndsrcs * sizeof(sar_sound_source_struct *);

/* TODO */
/*	    scene->player_control_panel */

	    for(i = 0; i < scene->total_messages; i++)
		m += STRLEN(scene->message[i]);
	    m += scene->total_messages * sizeof(char *);

	    for(i = 0; i < scene->total_sticky_banner_messages; i++)
		m += STRLEN(scene->sticky_banner_message[i]);
	    m += scene->total_sticky_banner_messages * sizeof(char *);

	    m += STRLEN(scene->camera_ref_title);

	    m += sizeof(SFMRealmStruct *);

	    stat_buf->scene += m;
	}

	/* Add up memory used by objects */
	if(object != NULL)
	{
	    int i, n;
	    unsigned long m = 0;

	    for(i = 0; i < total_objects; i++)
	    {
		const sar_object_struct *obj_ptr = object[i];
		const sar_object_aircraft_struct *obj_aircraft;

		if(obj_ptr == NULL)
		    continue;

		obj_aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
		if(obj_aircraft != NULL)
		{
		    m += (obj_aircraft->fdm != NULL) ?
			sizeof(SFMModelStruct) : 0;

		    m += obj_aircraft->total_external_fueltanks *
			sizeof(sar_external_fueltank_struct);
		    m += obj_aircraft->total_external_fueltanks *
			sizeof(sar_external_fueltank_struct *);

		    m += obj_aircraft->total_parts *
			sizeof(sar_obj_part_struct);
		    m += obj_aircraft->total_parts *
			sizeof(sar_obj_part_struct *);

		    m += obj_aircraft->total_rotors *
			sizeof(sar_obj_rotor_struct);
		    m += obj_aircraft->total_rotors *
			sizeof(sar_obj_rotor_struct *);

		    if(obj_aircraft->hoist != NULL)
		    {
			const sar_obj_hoist_struct *hoist = obj_aircraft->hoist;
			m += hoist->total_occupants * sizeof(int);
			m += sizeof(sar_obj_hoist_struct);
		    }

		    m += obj_aircraft->total_intercepts *
			sizeof(sar_intercept_struct);
		    m += obj_aircraft->total_intercepts *
			sizeof(sar_intercept_struct *);

		    m += sizeof(sar_object_aircraft_struct);
		}

		m += STRLEN(obj_ptr->name);
		m += (obj_ptr->contact_bounds != NULL) ?
		    sizeof(sar_contact_bounds_struct) : 0;
		m += obj_ptr->total_lights * sizeof(sar_light_struct);
		m += obj_ptr->total_lights * sizeof(sar_light_struct *);
		for(n = 0; n < obj_ptr->total_sndsrcs; n++)
		    m += SARMemoryStatSoundSource(obj_ptr->sndsrc[n]);
		m += obj_ptr->total_sndsrcs * sizeof(sar_sound_source_struct *);

		m += sizeof(sar_object_struct);
	    }
	    m += total_objects * sizeof(sar_object_struct *);
	    stat_buf->object += m;

	    stat_buf->nobjects += total_objects;
	}


	/* Add up total */
	stat_buf->total += stat_buf->texture + stat_buf->vmodel +
	    stat_buf->scene + stat_buf->object;
}