Go to the documentation of this file.00001 #ifndef _DJS_MEMORYMANAGER_H_
00002 #define _DJS_MEMORYMANAGER_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include<stdlib.h>
00020
00038 #ifdef MEMORY_DEBUG
00039 #include<stdio.h>
00040
00049 static void* MallocWithCheck(size_t x)
00050 {
00051 #ifdef ALLOW_ALLOC_ZERO_BYTES
00052 void* retvalue = malloc(x);
00053 #else
00054 void* retvalue = malloc(max(x,1));
00055 #endif
00056
00057 if(retvalue==NULL)
00058 {
00059 fprintf(stderr, "ERROR, malloc returned null pointer, that means we probably ran out of memory...\n");
00060 exit(1);
00061 }
00062
00063 return retvalue;
00064 };
00065
00076 static void* CallocWithCheck(size_t x, size_t y)
00077 {
00078 #ifdef ALLOW_ALLOC_ZERO_BYTES
00079 void* retvalue = calloc(x,y);
00080 #else
00081 void* retvalue = calloc(max(x,1),max(y,1));
00082 #endif
00083
00084 if(retvalue==NULL)
00085 {
00086 fprintf(stderr, "ERROR, calloc returned null pointer, that means we probably ran out of memory...\n");
00087 exit(1);
00088 }
00089
00090 return retvalue;
00091 };
00092
00093 #define Malloc(x) MallocWithCheck(x)
00094 #define Calloc(x,y) CallocWithCheck(x,y)
00095 #define Free(x) free(x)
00096
00097 #else
00098
00099 #ifdef ALLOW_ALLOC_ZERO_BYTES
00100 #define Malloc(x) malloc(x)
00101 #define Calloc(x,y) calloc(x,y)
00102 #define Free(x) free(x)
00103
00104 #else
00105
00106 #define Malloc(x) malloc(max(x,1))
00107 #define Calloc(x,y) calloc(max(x,1),max(y,1))
00108 #define Free(x) free(x)
00109
00110 #endif // ALLOW_ALLOC_ZERO_BYTES
00111 #endif // MEMORY_DEBUG
00112
00113 #endif
00114