From 7e6d1bfeef5c382eb834ad5d16d9d9e7da81b481 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Wed, 1 Oct 2014 21:56:48 -0400 Subject: [PATCH] split cross-platform code out of unix.cpp --- src/CMakeLists.txt | 9 +- src/platform/platform.cpp | 186 ++++++++++++++++++++++++++++++++ src/platform/unix.cpp | 216 ++------------------------------------ 3 files changed, 203 insertions(+), 208 deletions(-) create mode 100644 src/platform/platform.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index be57ee1..5086c85 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -233,8 +233,15 @@ set(code_SOURCE PARENT_SCOPE ) -if(NOT WIN32) + +if(WIN32) + set(platform_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/platform/platform.cpp + PARENT_SCOPE + ) +else() set(platform_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/platform/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform/unix.cpp PARENT_SCOPE ) diff --git a/src/platform/platform.cpp b/src/platform/platform.cpp new file mode 100644 index 0000000..dc04e6d --- /dev/null +++ b/src/platform/platform.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (C) Volition, Inc. 1999. All rights reserved. + * + * All source code herein is the property of Volition, Inc. You may not sell + * or otherwise commercially exploit the source or things you created based on + * the source. + */ + + +#include +#include +#include +#include +#include + +#include "pstypes.h" + +// use system versions of this stuff in here rather than the vm_* versions +#undef malloc +#undef free +#undef strdup + + +#define MAX_LINE_WIDTH 128 + + +int TotalRam = 0; + + +int vm_init(int min_heap_size) +{ + return 1; +} + +#if defined(__MACOSX__) +#define MALLOC_SIZE(x) malloc_size(x) +#elif defined(__GNUC__) +#define MALLOC_SIZE(x) malloc_usable_size(x) +#elif defined(_WIN32) +#define MALLOC_SIZE(x) _msize(x) +#else +#define MALLOC_SIZE(x) 0 +#endif + +int Watch_malloc = 0; + +DCF_BOOL(watch_malloc, Watch_malloc) + +static const char *clean_filename(const char *name) +{ + const char *p = name+strlen(name)-1; + // Move p to point to first letter of EXE filename + while( (*p!='\\') && (*p!='/') && (*p!=':') ) + p--; + p++; + + return p; +} + +#ifndef NDEBUG +void vm_free(void* ptr, const char *file, int line) +#else +void vm_free(void* ptr) +#endif +{ + if ( !ptr ) { +#ifndef NDEBUG + mprintf(("Why are you trying to free a NULL pointer? [%s(%d)]\n", clean_filename(file), line)); +#endif + return; + } + +#ifndef NDEBUG + size_t actual_size = MALLOC_SIZE(ptr); + + if (Watch_malloc) { + mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); + } + + TotalRam -= actual_size; +#endif + + SDL_free(ptr); +} + +#ifndef NDEBUG +void *vm_malloc(int size, const char *file, int line) +#else +void *vm_malloc(int size) +#endif +{ + void *ptr = SDL_malloc(size); + + if ( !ptr ) { + mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" )); + + Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n" + "virtual memory size, or installing more physical RAM.\n"); + + return NULL; + } + +#ifndef NDEBUG + size_t actual_size = MALLOC_SIZE(ptr); + + if ( Watch_malloc ) { + mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); + } + + TotalRam += actual_size; +#endif + + return ptr; +} + +#ifndef NDEBUG +char *vm_strdup(char const* str, const char *file, int line) +#else +char *vm_strdup(char const* str) +#endif +{ + char *ptr = SDL_strdup(str); + + if ( !ptr ) { + mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" )); + + Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n" + "virtual memory size, or installing more physical RAM.\n"); + } + +#ifndef NDEBUG + size_t actual_size = MALLOC_SIZE(ptr); + + if ( Watch_malloc ) { + mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); + } + + TotalRam += actual_size; +#endif + + return ptr; +} + +void windebug_memwatch_init() +{ + TotalRam = 0; +} + + +extern void gr_force_windowed(); + +void Warning( const char * filename, int line, const char * format, ... ) +{ + char tmp[MAX_LINE_WIDTH*4] = { 0 }; + char tmp2[MAX_LINE_WIDTH*4] = { 0 }; + va_list args; + + va_start(args, format); + SDL_vsnprintf(tmp, sizeof(tmp), format, args); + va_end(args); + + SDL_snprintf(tmp2, sizeof(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line); + + gr_force_windowed(); + + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL); +} + +void Error( const char * filename, int line, const char * format, ... ) +{ + char tmp[MAX_LINE_WIDTH*4] = { 0 }; + char tmp2[MAX_LINE_WIDTH*4] = { 0 }; + va_list args; + + va_start (args, format); + SDL_vsnprintf (tmp, sizeof(tmp), format, args); + va_end(args); + + SDL_snprintf(tmp2, sizeof(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line); + + gr_force_windowed(); + + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL); + + exit (1); +} diff --git a/src/platform/unix.cpp b/src/platform/unix.cpp index cbbc1ff..8bbf4e5 100644 --- a/src/platform/unix.cpp +++ b/src/platform/unix.cpp @@ -1,23 +1,20 @@ -#include -#include +/* + * Copyright (C) Volition, Inc. 1999. All rights reserved. + * + * All source code herein is the property of Volition, Inc. You may not sell + * or otherwise commercially exploit the source or things you created based on + * the source. + */ + + #include #include #include -#include #include -#include #include #include "pstypes.h" -// use system versions of this stuff in here rather than the vm_* versions -#undef malloc -#undef free -#undef strdup - - -#define MAX_LINE_WIDTH 128 - int filelength (int fd) { @@ -60,198 +57,3 @@ void _splitpath (const char *path, char *drive, char *dir, char *fname, char *ex fname[dist] = 0; // add null, just in case } } - -int TotalRam = 0; - - -int vm_init(int min_heap_size) -{ - return 1; -} - -#if defined(__MACOSX__) -#define MALLOC_SIZE(x) malloc_size(x) -#elif defined(__GNUC__) -#define MALLOC_SIZE(x) malloc_usable_size(x) -#else -#define MALLOC_SIZE(x) 0 -#endif - -int Watch_malloc = 0; - -DCF_BOOL(watch_malloc, Watch_malloc) - -static const char *clean_filename(const char *name) -{ - const char *p = name+strlen(name)-1; - // Move p to point to first letter of EXE filename - while( (*p!='\\') && (*p!='/') && (*p!=':') ) - p--; - p++; - - return p; -} - -#ifndef NDEBUG -void vm_free(void* ptr, const char *file, int line) -#else -void vm_free(void* ptr) -#endif -{ - if ( !ptr ) { -#ifndef NDEBUG - mprintf(("Why are you trying to free a NULL pointer? [%s(%d)]\n", clean_filename(file), line)); -#endif - return; - } - -#ifndef NDEBUG - size_t actual_size = MALLOC_SIZE(ptr); - - if (Watch_malloc) { - mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); - } - - TotalRam -= actual_size; -#endif - - free(ptr); -} - -#ifndef NDEBUG -void *vm_malloc(int size, const char *file, int line) -#else -void *vm_malloc(int size) -#endif -{ - void *ptr = malloc(size); - - if ( !ptr ) { - mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" )); - - Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n" - "virtual memory size, or installing more physical RAM.\n"); - - return NULL; - } - -#ifndef NDEBUG - size_t actual_size = MALLOC_SIZE(ptr); - - if ( Watch_malloc ) { - mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); - } - - TotalRam += actual_size; -#endif - - return ptr; -} - -#ifndef NDEBUG -char *vm_strdup(char const* str, const char *file, int line) -#else -char *vm_strdup(char const* str) -#endif -{ - char *ptr = strdup(str); - - if ( !ptr ) { - mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" )); - - Error(LOCATION, "Out of memory. Try closing down other applications, increasing your\n" - "virtual memory size, or installing more physical RAM.\n"); - } - -#ifndef NDEBUG - size_t actual_size = MALLOC_SIZE(ptr); - - if ( Watch_malloc ) { - mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line )); - } - - TotalRam += actual_size; -#endif - - return ptr; -} - -void windebug_memwatch_init() -{ - TotalRam = 0; -} - -/* error message debugging junk */ -/* -int Log_debug_output_to_file = 0; - -void load_filter_info(void) -{ -// STUB_FUNCTION; -} - -void outwnd_printf(char* id, char* format, ...) -{ - char tmp[MAX_LINE_WIDTH*4]; - va_list args; - - va_start (args, format); - vsprintf (tmp, format, args); - va_end(args); - fprintf (stderr, "%s: %s\n", id, tmp); -} - -void outwnd_printf2(char* format, ...) -{ - char tmp[MAX_LINE_WIDTH*4]; - va_list args; - - va_start (args, format); - vsprintf (tmp, format, args); - va_end(args); - fprintf (stderr, "General: %s", tmp); -} - -void outwnd_close() -{ -// STUB_FUNCTION; -} -*/ - -extern void gr_force_windowed(); - -void Warning( const char * filename, int line, const char * format, ... ) -{ - char tmp[MAX_LINE_WIDTH*4] = { 0 }; - char tmp2[MAX_LINE_WIDTH*4] = { 0 }; - va_list args; - - va_start(args, format); - vsnprintf(tmp, sizeof(tmp), format, args); - va_end(args); -// fprintf (stderr, "Warning: (%s:%d): %s\n", filename, line, tmp); - snprintf(tmp2, sizeof(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line); - - gr_force_windowed(); - - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL); -} - -void Error( const char * filename, int line, const char * format, ... ) -{ - char tmp[MAX_LINE_WIDTH*4] = { 0 }; - char tmp2[MAX_LINE_WIDTH*4] = { 0 }; - va_list args; - - va_start (args, format); - vsnprintf (tmp, sizeof(tmp), format, args); - va_end(args); -// fprintf (stderr, "Error: (%s:%d): %s\n", filename, line, tmp); - snprintf(tmp2, sizeof(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line); - - gr_force_windowed(); - - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL); - - exit (1); -} -- 2.39.2