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)
50 static const char *clean_filename(const char *name)
52 const char *p = name+strlen(name)-1;
53 // Move p to point to first letter of EXE filename
54 while( (*p!='\\') && (*p!='/') && (*p!=':') )
63 void vm_free(void* ptr, const char *file, int line)
65 void vm_free(void* ptr)
70 mprintf(("Why are you trying to free a NULL pointer? [%s(%d)]\n", clean_filename(file), line));
76 size_t actual_size = MALLOC_SIZE(ptr);
79 mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
82 TotalRam -= actual_size;
89 void *vm_malloc(int size, const char *file, int line)
91 void *vm_malloc(int size)
94 void *ptr = malloc(size);
97 mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" ));
99 Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n"
100 "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, ... )
155 char tmp[MAX_LINE_WIDTH*4] = { 0 };
156 char tmp2[MAX_LINE_WIDTH*4] = { 0 };
159 va_start(args, format);
160 SDL_vsnprintf(tmp, SDL_arraysize(tmp), format, args);
163 SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
167 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL);
171 void Error( const char * filename, int line, const char * format, ... )
173 char tmp[MAX_LINE_WIDTH*4] = { 0 };
174 char tmp2[MAX_LINE_WIDTH*4] = { 0 };
177 va_start (args, format);
178 SDL_vsnprintf (tmp, SDL_arraysize(tmp), format, args);
181 SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
185 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL);
190 void base_filename(const char *path, char *filename, const int max_fname)
192 if ( (filename == NULL) || (max_fname <= 0) ) {
201 const char *sep = SDL_strrchr(path, DIR_SEPARATOR_CHAR);
204 sep++; // move past separator
209 const char *ext = SDL_strrchr(path, '.');
212 ext = sep + SDL_strlen(sep); // to end
215 // NOTE: 'size' must include NULL terminator
216 int size = min((int)(ext - sep + 1), max_fname);
221 SDL_strlcpy(filename, sep, size);