98f4b1dcd397addec19087c7c96e81b8e96f31f2
[swftools.git] / lib / mem.c
1 #include <memory.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 // memory allocation
6
7 void rfx_free(void*ptr)
8 {
9   if(!ptr)
10     return;
11   free(ptr);
12 }
13
14 void* rfx_alloc(int size)
15 {
16   void*ptr;
17   if(size == 0) {
18     //*(int*)0 = 0xdead;
19     //fprintf(stderr, "Warning: Zero alloc\n");
20     return 0;
21   }
22
23   ptr = malloc(size);
24   if(!ptr) {
25     fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
26     /* TODO: we should send a signal, so that the debugger kicks in? */
27     exit(1);
28   }
29   return ptr;
30 }
31 void* rfx_realloc(void*data, int size)
32 {
33   void*ptr;
34   if(size == 0) {
35     //*(int*)0 = 0xdead;
36     //fprintf(stderr, "Warning: Zero realloc\n");
37     rfx_free(data);
38     return 0;
39   }
40   if(!data) {
41     ptr = malloc(size);
42   } else {
43     ptr = realloc(data, size);
44   }
45
46   if(!ptr) {
47     fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
48     /* TODO: we should send a signal, so that the debugger kicks in? */
49     exit(1);
50   }
51   return ptr;
52 }
53 void* rfx_calloc(int size)
54 {
55   void*ptr;
56   if(size == 0) {
57     //*(int*)0 = 0xdead;
58     //fprintf(stderr, "Warning: Zero alloc\n");
59     return 0;
60   }
61 #ifdef HAVE_CALLOC
62   ptr = calloc(size);
63 #else
64   ptr = malloc(size);
65 #endif
66   if(!ptr) {
67     fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
68     /* TODO: we should send a signal, so that the debugger kicks in? */
69     exit(1);
70   }
71 #ifndef HAVE_CALLOC
72   memset(ptr, 0, size);
73 #endif
74   return ptr;
75 }
76
77 #ifdef MEMORY_INFO
78 long rfx_memory_used()
79 {
80 }
81
82 char* rfx_memory_used_str()
83 {
84 }
85 #endif
86