2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
14 #include <sys/types.h>
18 // use system versions of this stuff in here rather than the vm_* versions
24 #define MAX_LINE_WIDTH 128
30 int vm_init(int min_heap_size)
35 #if defined(__MACOSX__)
36 #define MALLOC_SIZE(x) malloc_size(x)
37 #elif defined(__GNUC__)
38 #define MALLOC_SIZE(x) malloc_usable_size(x)
39 #elif defined(__WIN32__)
40 #define MALLOC_SIZE(x) _msize(x)
42 #define MALLOC_SIZE(x) 0
47 DCF_BOOL(watch_malloc, Watch_malloc)
49 static const char *clean_filename(const char *name)
51 const char *p = name+strlen(name)-1;
52 // Move p to point to first letter of EXE filename
53 while( (*p!='\\') && (*p!='/') && (*p!=':') )
61 void vm_free(void* ptr, const char *file, int line)
63 void vm_free(void* ptr)
68 mprintf(("Why are you trying to free a NULL pointer? [%s(%d)]\n", clean_filename(file), line));
74 size_t actual_size = MALLOC_SIZE(ptr);
77 mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
80 TotalRam -= actual_size;
87 void *vm_malloc(int size, const char *file, int line)
89 void *vm_malloc(int size)
92 void *ptr = malloc(size);
95 mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" ));
97 Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n"
98 "virtual memory size, or installing more physical RAM.\n");
104 size_t actual_size = MALLOC_SIZE(ptr);
106 if ( Watch_malloc ) {
107 mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
110 TotalRam += actual_size;
117 char *vm_strdup(char const* str, const char *file, int line)
119 char *vm_strdup(char const* str)
122 char *ptr = strdup(str);
125 mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" ));
127 Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n"
128 "virtual memory size, or installing more physical RAM.\n");
132 size_t actual_size = MALLOC_SIZE(ptr);
134 if ( Watch_malloc ) {
135 mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
138 TotalRam += actual_size;
144 void windebug_memwatch_init()
150 extern void gr_force_windowed();
152 void Warning( const char * filename, int line, const char * format, ... )
154 char tmp[MAX_LINE_WIDTH*4] = { 0 };
155 char tmp2[MAX_LINE_WIDTH*4] = { 0 };
158 va_start(args, format);
159 SDL_vsnprintf(tmp, sizeof(tmp), format, args);
162 SDL_snprintf(tmp2, sizeof(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
166 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL);
169 void Error( const char * filename, int line, const char * format, ... )
171 char tmp[MAX_LINE_WIDTH*4] = { 0 };
172 char tmp2[MAX_LINE_WIDTH*4] = { 0 };
175 va_start (args, format);
176 SDL_vsnprintf (tmp, sizeof(tmp), format, args);
179 SDL_snprintf(tmp2, sizeof(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
183 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL);