]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/platform.cpp
split cross-platform code out of unix.cpp
[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 static const char *clean_filename(const char *name)
50 {
51         const char *p = name+strlen(name)-1;
52         // Move p to point to first letter of EXE filename
53         while( (*p!='\\') && (*p!='/') && (*p!=':') )
54                 p--;
55         p++;
56
57         return p;
58 }
59
60 #ifndef NDEBUG
61 void vm_free(void* ptr, const char *file, int line)
62 #else
63 void vm_free(void* ptr)
64 #endif
65 {
66         if ( !ptr ) {
67 #ifndef NDEBUG
68                 mprintf(("Why are you trying to free a NULL pointer?  [%s(%d)]\n", clean_filename(file), line));
69 #endif
70                 return;
71         }
72
73 #ifndef NDEBUG
74         size_t actual_size = MALLOC_SIZE(ptr);
75
76         if (Watch_malloc) {
77                 mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
78         }
79
80         TotalRam -= actual_size;
81 #endif
82
83         SDL_free(ptr);
84 }
85
86 #ifndef NDEBUG
87 void *vm_malloc(int size, const char *file, int line)
88 #else
89 void *vm_malloc(int size)
90 #endif
91 {
92         void *ptr = SDL_malloc(size);
93
94         if ( !ptr )     {
95                 mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" ));
96
97                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
98                                 "virtual memory size, or installing more physical RAM.\n");
99
100                 return NULL;
101         }
102
103 #ifndef NDEBUG
104         size_t actual_size = MALLOC_SIZE(ptr);
105
106         if ( Watch_malloc )     {
107                 mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
108         }
109
110         TotalRam += actual_size;
111 #endif
112
113         return ptr;
114 }
115
116 #ifndef NDEBUG
117 char *vm_strdup(char const* str, const char *file, int line)
118 #else
119 char *vm_strdup(char const* str)
120 #endif
121 {
122         char *ptr = SDL_strdup(str);
123
124         if ( !ptr )     {
125                 mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" ));
126
127                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
128                                 "virtual memory size, or installing more physical RAM.\n");
129         }
130
131 #ifndef NDEBUG
132         size_t actual_size = MALLOC_SIZE(ptr);
133
134         if ( Watch_malloc )     {
135                 mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
136         }
137
138         TotalRam += actual_size;
139 #endif
140
141         return ptr;
142 }
143
144 void windebug_memwatch_init()
145 {
146         TotalRam = 0;
147 }
148
149
150 extern void gr_force_windowed();
151
152 void Warning( const char * filename, int line, const char * format, ... )
153 {
154         char tmp[MAX_LINE_WIDTH*4] = { 0 };
155         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
156         va_list args;
157
158         va_start(args, format);
159         SDL_vsnprintf(tmp, sizeof(tmp), format, args);
160         va_end(args);
161
162         SDL_snprintf(tmp2, sizeof(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
163
164         gr_force_windowed();
165
166         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL);
167 }
168
169 void Error( const char * filename, int line, const char * format, ... )
170 {
171         char tmp[MAX_LINE_WIDTH*4] = { 0 };
172         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
173         va_list args;
174
175         va_start (args, format);
176         SDL_vsnprintf (tmp, sizeof(tmp), format, args);
177         va_end(args);
178
179         SDL_snprintf(tmp2, sizeof(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
180
181         gr_force_windowed();
182
183         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL);
184
185         exit (1);
186 }