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