]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/platform.cpp
silence various compiler warnings (clang:rel)
[taylor/freespace2.git] / src / platform / platform.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15
16 #include "pstypes.h"
17
18 // use system versions of this stuff in here rather than the vm_* versions
19 #undef malloc
20 #undef free
21 #undef strdup
22
23
24 #define MAX_LINE_WIDTH 128
25
26
27 int TotalRam = 0;
28
29
30 int vm_init(int min_heap_size)
31 {
32         return 1;
33 }
34
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)
41 #else
42 #define MALLOC_SIZE(x)          0
43 #endif
44
45 int Watch_malloc = 0;
46
47 DCF_BOOL(watch_malloc, Watch_malloc)
48
49 #ifndef NDEBUG
50 static const char *clean_filename(const char *name)
51 {
52         const char *p = name+strlen(name)-1;
53         // Move p to point to first letter of EXE filename
54         while( (*p!='\\') && (*p!='/') && (*p!=':') )
55                 p--;
56         p++;
57
58         return p;
59 }
60 #endif
61
62 #ifndef NDEBUG
63 void vm_free(void* ptr, const char *file, int line)
64 #else
65 void vm_free(void* ptr)
66 #endif
67 {
68         if ( !ptr ) {
69 #ifndef NDEBUG
70                 mprintf(("Why are you trying to free a NULL pointer?  [%s(%d)]\n", clean_filename(file), line));
71 #endif
72                 return;
73         }
74
75 #ifndef NDEBUG
76         size_t actual_size = MALLOC_SIZE(ptr);
77
78         if (Watch_malloc) {
79                 mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
80         }
81
82         TotalRam -= actual_size;
83 #endif
84
85         free(ptr);
86 }
87
88 #ifndef NDEBUG
89 void *vm_malloc(int size, const char *file, int line)
90 #else
91 void *vm_malloc(int size)
92 #endif
93 {
94         void *ptr = malloc(size);
95
96         if ( !ptr )     {
97                 mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" ));
98
99                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
100                                 "virtual memory size, or installing more physical RAM.\n");
101
102                 return NULL;
103         }
104
105 #ifndef NDEBUG
106         size_t actual_size = MALLOC_SIZE(ptr);
107
108         if ( Watch_malloc )     {
109                 mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
110         }
111
112         TotalRam += actual_size;
113 #endif
114
115         return ptr;
116 }
117
118 #ifndef NDEBUG
119 char *vm_strdup(char const* str, const char *file, int line)
120 #else
121 char *vm_strdup(char const* str)
122 #endif
123 {
124         char *ptr = strdup(str);
125
126         if ( !ptr )     {
127                 mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" ));
128
129                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
130                                 "virtual memory size, or installing more physical RAM.\n");
131         }
132
133 #ifndef NDEBUG
134         size_t actual_size = MALLOC_SIZE(ptr);
135
136         if ( Watch_malloc )     {
137                 mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
138         }
139
140         TotalRam += actual_size;
141 #endif
142
143         return ptr;
144 }
145
146 void windebug_memwatch_init()
147 {
148         TotalRam = 0;
149 }
150
151
152 extern void gr_force_windowed();
153
154 void Warning( const char * filename, int line, const char * format, ... )
155 {
156 #ifndef NDEBUG
157         char tmp[MAX_LINE_WIDTH*4] = { 0 };
158         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
159         va_list args;
160
161         va_start(args, format);
162         SDL_vsnprintf(tmp, SDL_arraysize(tmp), format, args);
163         va_end(args);
164
165         SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
166
167         gr_force_windowed();
168
169         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL);
170 #endif
171 }
172
173 void Error( const char * filename, int line, const char * format, ... )
174 {
175         char tmp[MAX_LINE_WIDTH*4] = { 0 };
176         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
177         va_list args;
178
179         va_start (args, format);
180         SDL_vsnprintf (tmp, SDL_arraysize(tmp), format, args);
181         va_end(args);
182
183         SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
184
185         gr_force_windowed();
186
187         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL);
188
189         exit (1);
190 }
191
192 void base_filename(const char *path, char *filename, const int max_fname)
193 {
194         if ( (filename == NULL) || (max_fname <= 0) ) {
195                 return;
196         }
197
198         if (path == NULL) {
199                 filename[0] = '\0';
200                 return;
201         }
202
203         const char *sep = SDL_strrchr(path, DIR_SEPARATOR_CHAR);
204
205         if (sep) {
206                 sep++;  // move past separator
207         } else {
208                 sep = path;
209         }
210
211         const char *ext = SDL_strrchr(path, '.');
212
213         if (ext == NULL) {
214                 ext = sep + SDL_strlen(sep);    // to end
215         }
216
217         // NOTE: 'size' must include NULL terminator
218         int size = min((int)(ext - sep + 1), max_fname);
219
220         if (size <= 0) {
221                 filename[0] = '\0';
222         } else {
223                 SDL_strlcpy(filename, sep, size);
224         }
225 }