File: memory_debug.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (575 lines) | stat: -rw-r--r-- 15,338 bytes parent folder | download | duplicates (3)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010,2011,2012 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "memory_debug.h"

#include <stdlib.h>
#include <stdio.h>

#define DEBUG_MEMORY_MSG(x) { fprintf(stderr,"Memory Debug : %s \n",(x)); }

#include "memory_management.h"

int memory_debug_n;
SDL_EventFilter memory_debug_filter;
SDL_Event memory_debug_event;

Memory_debug_statistics memory_debug_statistics;

void init_memory_debug(){
	memory_debug_statistics.nb_create_surface = 0;
	memory_debug_statistics.nb_free_surface = 0;
	memory_debug_statistics.nb_init_sdl = 0;
	memory_debug_statistics.nb_close_sdl = 0;
	memory_debug_statistics.nb_create_semaphore = 0;
	memory_debug_statistics.nb_free_semaphore = 0;
	memory_debug_statistics.nb_malloc = 0;
	memory_debug_statistics.nb_free = 0;
	memory_debug_statistics.nb_free_wave = 0;
	memory_debug_statistics.nb_load_wave = 0;
	memory_debug_statistics.nb_open_font = 0;
	memory_debug_statistics.nb_close_font = 0;
	memory_debug_statistics.nb_init_ttf = 0;
	memory_debug_statistics.nb_quit_ttf = 0;
}

void print_memory_statistics(){
	fprintf(stderr,"--------------------------STATISTICS---------------------\n");
	fprintf(stderr,"Memory Debug : nb_create_surface : %i \n", memory_debug_statistics.nb_create_surface);
	fprintf(stderr,"Memory Debug : nb_free_surface : %i \n", memory_debug_statistics.nb_free_surface);
	fprintf(stderr,"Memory Debug : nb_init_sdl : %i \n", memory_debug_statistics.nb_init_sdl);
	fprintf(stderr,"Memory Debug : nb_close_sdl : %i \n", memory_debug_statistics.nb_close_sdl);
	fprintf(stderr,"Memory Debug : nb_create_semaphore : %i \n", memory_debug_statistics.nb_create_semaphore);
	fprintf(stderr,"Memory Debug : nb_free_semaphore : %i \n", memory_debug_statistics.nb_free_semaphore);
	fprintf(stderr,"Memory Debug : nb_malloc : %i \n", memory_debug_statistics.nb_malloc);
	fprintf(stderr,"Memory Debug : nb_calloc : %i \n", memory_debug_statistics.nb_calloc);
	fprintf(stderr,"Memory Debug : nb_free  : %i \n", memory_debug_statistics.nb_free);
	fprintf(stderr,"Memory Debug : nb_free_wave : %i \n", memory_debug_statistics.nb_free_wave);
	fprintf(stderr,"Memory Debug : nb_load_wave  : %i \n", memory_debug_statistics.nb_load_wave);
	fprintf(stderr,"Memory Debug : nb_open_font : %i \n", memory_debug_statistics.nb_open_font);
	fprintf(stderr,"Memory Debug : nb_close_font  : %i \n", memory_debug_statistics.nb_close_font);
	fprintf(stderr,"Memory Debug : nb_init_ttf : %i \n", memory_debug_statistics.nb_init_ttf);
	fprintf(stderr,"Memory Debug : nb_quit_ttf  : %i \n", memory_debug_statistics.nb_quit_ttf);
	fprintf(stderr,"----------------------END-STATISTICS---------------------\n");
}

typedef enum {
	MEMORY_DEBUG_CREATE_SURFACE,
	MEMORY_DEBUG_FREE_SURFACE,
	MEMORY_DEBUG_INIT_SDL,
	MEMORY_DEBUG_CLOSE_SDL,
	MEMORY_DEBUG_CREATE_SEMAPHORE,
	MEMORY_DEBUG_FREE_SEMAPHORE,
	MEMORY_DEBUG_MALLOC,
	MEMORY_DEBUG_CALLOC,
	MEMORY_DEBUG_FREE,
	MEMORY_DEBUG_FREE_WAVE,
	MEMORY_DEBUG_LOAD_WAVE,
	MEMORY_DEBUG_OPEN_FONT,
	MEMORY_DEBUG_CLOSE_FONT,
	MEMORY_DEBUG_QUIT_TTF,
	MEMORY_DEBUG_INIT_TTF
} Memory_debug_statistics_type;

void update_memory_statistics( Memory_debug_statistics_type type ){
	switch( type ){
		case MEMORY_DEBUG_CREATE_SURFACE :
			{
				memory_debug_statistics.nb_create_surface++;
			}
			break;
		case MEMORY_DEBUG_FREE_SURFACE :
			{
				memory_debug_statistics.nb_free_surface++;
			}
			break;
		case MEMORY_DEBUG_INIT_SDL :
			{
				memory_debug_statistics.nb_init_sdl++;
			}
			break;
		case MEMORY_DEBUG_CLOSE_SDL :
			{
				memory_debug_statistics.nb_close_sdl++;
			}
			break;
		case MEMORY_DEBUG_CREATE_SEMAPHORE :
			{
				memory_debug_statistics.nb_create_semaphore++;
			}
			break;
		case MEMORY_DEBUG_FREE_SEMAPHORE :
			{
				memory_debug_statistics.nb_free_semaphore ++;
			}
			break;
		case MEMORY_DEBUG_MALLOC :
			{
				memory_debug_statistics.nb_malloc ++;
			}
			break;
		case MEMORY_DEBUG_CALLOC :
			{
				memory_debug_statistics.nb_calloc ++;
			}
			break;
		case MEMORY_DEBUG_FREE :
			{
				memory_debug_statistics.nb_free ++;
			}
			break;
		case MEMORY_DEBUG_FREE_WAVE :
			{
				memory_debug_statistics.nb_free_wave ++;
			}
			break;
		case MEMORY_DEBUG_LOAD_WAVE :
			{
				memory_debug_statistics.nb_load_wave ++;
			}
			break;
		case MEMORY_DEBUG_OPEN_FONT :
			{
				memory_debug_statistics.nb_open_font ++;
			}
			break;
		case MEMORY_DEBUG_CLOSE_FONT :
			{
				memory_debug_statistics.nb_close_font ++;
			}
			break;
		case MEMORY_DEBUG_QUIT_TTF :
			{
				memory_debug_statistics.nb_quit_ttf ++;
			}
			break;
		case MEMORY_DEBUG_INIT_TTF :
			{
				memory_debug_statistics.nb_init_ttf ++;
			}
			break;
		default:;
	}
}

void close_memory_debug(){
}

void filledCircleColor( SDL_Surface* screen, int x, int y, int radius, Uint32 color){
}

void circleColor( SDL_Surface* screen, int x, int y, int radius, Uint32 color){
}

void ellipseColor( SDL_Surface* screen, int x, int y, int radius_x, int radius_y, Uint32 color ){
}

void filledEllipseColor( SDL_Surface* screen, int x, int y, int radius_x, int radius_y, Uint32 color){
}

void polygonColor( SDL_Surface* screen, Sint16* vx, Sint16* vy, int npoints, Uint32 color){
}

void filledPolygonColor( SDL_Surface* screen, Sint16* vx, Sint16* vy, int npoints, Uint32 color){
}

void rectangleColor( SDL_Surface* screen, int x, int y, int width, int height, Uint32 color){
}

void boxColor( SDL_Surface* screen, int x, int y, int width, int height, Uint32 color){
}

void lineColor( SDL_Surface* screen, int x1, int y1, int x2, int y2, Uint32 color){
}

void pixelColor( SDL_Surface* screen, int x, int y, Uint32 color){
}

void stringColor( SDL_Surface* screen, int x, int y, const char *text, Uint32 color){
}

int SDL_BlitSurface( SDL_Surface* src, SDL_Rect* rectangle_src, SDL_Surface* dst, SDL_Rect* rectangle_dest){
	return 0;
}


int memory_debug_modulo;

int SDL_PollEvent( SDL_Event* event ){
	memory_debug_n = (memory_debug_n+1)%memory_debug_modulo;
	switch( memory_debug_n ){
		case 0:
			{
				DEBUG_MEMORY_MSG("Pas d'evenement") 
				return 0;
			}
		case 1: ;
			{
				DEBUG_MEMORY_MSG("souris deplace 10 10") 
				event->type=SDL_MOUSEMOTION;
				event->motion.which = 0;
				event->motion.state = SDL_RELEASED;
				event->motion.x = 10;
				event->motion.y = 10;
				event->motion.xrel = -90;
				event->motion.yrel = -90;
			};
			break;
		case 2: ;
			{
				DEBUG_MEMORY_MSG("bouton gauche souris tape") 
				event->type=SDL_MOUSEBUTTONDOWN;
				event->button.which = 0;
				event->button.button = SDL_BUTTON_LEFT;
				event->button.state = SDL_PRESSED;
				event->button.x = 10;
				event->button.y = 10;
			};
			break;
		case 3: ;
			{
				DEBUG_MEMORY_MSG("bouton gauche souris relache") 
				event->type=SDL_MOUSEBUTTONUP;
				event->button.which = 0;
				event->button.button = SDL_BUTTON_LEFT;
				event->button.state = SDL_RELEASED;
				event->button.x = 10;
				event->button.y = 10;
			};
			break;
		case 4: ;
			{
				DEBUG_MEMORY_MSG("souris deplace 100") 
				event->type=SDL_MOUSEMOTION;
				event->motion.which = 0;
				event->motion.state = SDL_RELEASED;
				event->motion.x = 100;
				event->motion.y = 100;
				event->motion.xrel = 90;
				event->motion.yrel = 90;
			};
			break;
		case 5: ;
			{
				DEBUG_MEMORY_MSG("bouton gauche souris tape") 
				event->type=SDL_MOUSEBUTTONDOWN;
				event->button.which = 0;
				event->button.button = SDL_BUTTON_LEFT;
				event->button.state = SDL_PRESSED;
				event->button.x = 100;
				event->button.y = 100;
			};
			break;
		case 6: ;
			{
				DEBUG_MEMORY_MSG("bouton gausche souris relache") 
				event->type=SDL_MOUSEBUTTONUP;
				event->button.which = 0;
				event->button.button = SDL_BUTTON_LEFT;
				event->button.state = SDL_RELEASED;
				event->button.x = 100;
				event->button.y = 100;
			};
			break;
		case 7:
			{
				DEBUG_MEMORY_MSG("a tape") 
				event->type=SDL_KEYDOWN;
				event->key.type=SDL_KEYDOWN;
				event->key.which=0;
				event->key.state=SDL_PRESSED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_a;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=97;
			};
			break;
		case 8: ;
			{
				DEBUG_MEMORY_MSG("a relache") 
				event->type=SDL_KEYUP;
				event->key.type=SDL_KEYUP;
				event->key.which=0;
				event->key.state=SDL_RELEASED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_a;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=97;
			};
			break;
		case 9: ;
			{
				DEBUG_MEMORY_MSG("b tape") 
				event->type=SDL_KEYDOWN;
				event->key.type=SDL_KEYDOWN;
				event->key.which=0;
				event->key.state=SDL_PRESSED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_b;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=98;
			};
			break;
		case 10: ;
			{
				DEBUG_MEMORY_MSG("b relache") 
				event->type=SDL_KEYUP;
				event->key.type=SDL_KEYUP;
				event->key.which=0;
				event->key.state=SDL_RELEASED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_b;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=98;
			};
			break;
		case 11: ;
			{
				DEBUG_MEMORY_MSG("return tape") 
				event->type=SDL_KEYDOWN;
				event->key.type=SDL_KEYDOWN;
				event->key.which=0;
				event->key.state=SDL_PRESSED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_RETURN;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=13;
			};
			break;
		case 12: ;
			{
				DEBUG_MEMORY_MSG("Return relache") 
				event->type=SDL_KEYUP;
				event->key.type=SDL_KEYUP;
				event->key.which=0;
				event->key.state=SDL_RELEASED;
				event->key.keysym.scancode = 0;
				event->key.keysym.sym=SDLK_RETURN;
				event->key.keysym.mod=KMOD_NONE;
				event->key.keysym.unicode=13;
			};
			break;
		case 13:
			{
				DEBUG_MEMORY_MSG("Evenement User") 
				(*event)=memory_debug_event;
			};
			break;
		default: ;
	}
	return memory_debug_filter( event );
}

void SDL_SetAlpha(SDL_Surface* surface, int prop1, int prop2){
}


SDL_Surface* SDL_LoadBMP( const char* path ){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof(SDL_Surface) );
}

SDL_Surface* SDL_CreateRGBSurface( int prop1, int w, int h, int resol, int r, int g, int b, int prop2 ){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof(SDL_Surface) );
}

void SDL_FreeSurface( SDL_Surface* surface ){
	update_memory_statistics( MEMORY_DEBUG_FREE_SURFACE );
	free( surface );
}

SDL_Surface* rotozoomSurface( SDL_Surface* surface, double rotation, double zoom, int antialias){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof(SDL_Surface) );
}


SDL_Surface* rotozoomSurfaceXY( SDL_Surface* surface, double rotation, double zoomx, double zoomy, int antialias){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof(SDL_Surface) );
}

char* memory_debug_sdl_init;

int SDL_Init( int flags ){
	memory_debug_n = 0;
	memory_debug_modulo = 14;
	update_memory_statistics( MEMORY_DEBUG_INIT_SDL );
	memory_debug_sdl_init = (char*) malloc( sizeof(char) );
	return 0;
}

void SDL_Quit(){
	update_memory_statistics( MEMORY_DEBUG_CLOSE_SDL );
	free(memory_debug_sdl_init);
}

char* SDL_GetError(){
	return "TOTO";
}

SDL_Surface* SDL_SetVideoMode( int w, int h, int bits, int flags ){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof( SDL_Surface ) );
}

void SDL_WM_SetCaption( const char* wind, const char* icon ){
}

void SDL_SetEventFilter( SDL_EventFilter filter ){
	memory_debug_filter = filter;
}

void SDL_Flip( SDL_Surface* surface ){
}

int SDL_SemWait( SDL_sem* semaphore ){
	return 0;
}

int SDL_SemPost( SDL_sem* semaphore ){
	return 0;
}

SDL_sem* SDL_CreateSemaphore( int valeur ){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SEMAPHORE );
	return (SDL_sem*) malloc( sizeof( SDL_sem ) );
}

void SDL_DestroySemaphore( SDL_sem* semaphore ){
	update_memory_statistics( MEMORY_DEBUG_FREE_SEMAPHORE );
	free(semaphore);
}

void SDL_EnableUNICODE( int flags ){
}

int SDL_PushEvent( SDL_Event* event ){
	memory_debug_event = (*event);
	memory_debug_n = memory_debug_modulo - 2 ;
	return 0;
}

int SDL_GetTicks(){
	return 1;
}

void SDL_Delay( int milliseconds ){
}

void* memory_debug_malloc(size_t size, int line, char* file ){
	update_memory_statistics( MEMORY_DEBUG_MALLOC );
	fprintf(stderr, "Memory Debug : malloc ligne : %i, fichier : %s \n", line, file );
	return malloc( size );
}

void* memory_debug_calloc( size_t nmemb, size_t size, int line, char* file ){
	update_memory_statistics( MEMORY_DEBUG_CALLOC );
	fprintf(stderr, "Memory Debug : calloc ligne : %i, fichier : %s \n", line, file );
	return calloc( nmemb, size );
}

void memory_debug_free( void* ptr, int line, char* file ){
	update_memory_statistics( MEMORY_DEBUG_FREE );
	fprintf(stderr, "Memory Debug : free ligne : %i, fichier : %s \n", line, file );
	free( ptr );
}


void SDL_LockAudio(){
}

void SDL_UnlockAudio(){
}

void SDL_MixAudio(Uint8 *dst, Uint8 *src, Uint32 len, int volume){
}

SDL_AudioSpec* SDL_LoadWAV(const char *file, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len){
	update_memory_statistics( MEMORY_DEBUG_LOAD_WAVE );
	(*audio_buf) = (Uint8*)  malloc( 10000 * sizeof( Uint8 ) );
	(*audio_len) = 10000 * sizeof(Uint8);
	return spec;
}

void SDL_FreeWAV(Uint8 *audio_buf){
	update_memory_statistics( MEMORY_DEBUG_FREE_WAVE );
	free( audio_buf );
}

void SDL_PauseAudio(int pause_on){
}

int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained){
	return 0;
}

void SDL_CloseAudio(){
}





int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key){
	return 0;
}




int TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h){
	return 0;
}


char* memory_debug_ttf_init;

int TTF_Init(void){
	update_memory_statistics( MEMORY_DEBUG_INIT_TTF );
	memory_debug_ttf_init = (char*) malloc( sizeof(char) );
	return 0;
}

void TTF_Quit(void){
	update_memory_statistics( MEMORY_DEBUG_QUIT_TTF );
	free( memory_debug_ttf_init );
}

TTF_Font * TTF_OpenFont(const char *file, int ptsize){
	update_memory_statistics( MEMORY_DEBUG_OPEN_FONT );
	return (TTF_Font*) malloc( sizeof(TTF_Font) );
}

void TTF_SetFontStyle(TTF_Font *font, int style){
}

void TTF_CloseFont(TTF_Font *font){
	update_memory_statistics( MEMORY_DEBUG_CLOSE_FONT );
	free( font );
}

SDL_Surface * TTF_RenderUTF8_Solid(TTF_Font *font, const char *text, SDL_Color fg){
	update_memory_statistics( MEMORY_DEBUG_CREATE_SURFACE );
	return (SDL_Surface*) malloc( sizeof(SDL_Surface) );
}