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