1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// File: platform.cpp
// See Copyright Notice and license at the end of include/lzham.h
#include "lzham_core.h"
#include "lzham_timer.h"
#include <assert.h>
#if LZHAM_PLATFORM_X360
#include <xbdm.h>
#endif
#define LZHAM_FORCE_DEBUGGER_PRESENT 0
#ifndef _MSC_VER
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...)
{
if (!sizeOfBuffer)
return 0;
va_list args;
va_start(args, format);
int c = vsnprintf(buffer, sizeOfBuffer, format, args);
va_end(args);
buffer[sizeOfBuffer - 1] = '\0';
if (c < 0)
return static_cast<int>(sizeOfBuffer - 1);
return LZHAM_MIN(c, (int)sizeOfBuffer - 1);
}
int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args)
{
if (!sizeOfBuffer)
return 0;
int c = vsnprintf(buffer, sizeOfBuffer, format, args);
buffer[sizeOfBuffer - 1] = '\0';
if (c < 0)
return static_cast<int>(sizeOfBuffer - 1);
return LZHAM_MIN(c, (int)sizeOfBuffer - 1);
}
#endif // __GNUC__
bool lzham_is_debugger_present(void)
{
#if LZHAM_PLATFORM_X360
return DmIsDebuggerPresent() != 0;
#elif LZHAM_USE_WIN32_API
return IsDebuggerPresent() != 0;
#elif LZHAM_FORCE_DEBUGGER_PRESENT
return true;
#else
return false;
#endif
}
void lzham_debug_break(void)
{
#if LZHAM_USE_WIN32_API
DebugBreak();
#elif (TARGET_OS_MAC == 1) && (TARGET_IPHONE_SIMULATOR == 0) && (TARGET_OS_IPHONE == 0)
// __asm {int 3}
// __asm("int $3")
assert(0);
#else
assert(0);
#endif
}
void lzham_output_debug_string(const char* p)
{
LZHAM_NOTE_UNUSED(p);
#if LZHAM_USE_WIN32_API
OutputDebugStringA(p);
#else
fputs(p, stderr);
#endif
}
#if LZHAM_BUFFERED_PRINTF
#include <vector>
// This stuff was a quick hack only intended for debugging/development.
namespace lzham
{
struct buffered_str
{
enum { cBufSize = 256 };
char m_buf[cBufSize];
};
static std::vector<buffered_str> g_buffered_strings;
static volatile long g_buffered_string_locked;
static void lock_buffered_strings()
{
while (atomic_exchange32(&g_buffered_string_locked, 1) == 1)
{
lzham_yield_processor();
lzham_yield_processor();
lzham_yield_processor();
lzham_yield_processor();
}
LZHAM_MEMORY_IMPORT_BARRIER
}
static void unlock_buffered_strings()
{
LZHAM_MEMORY_EXPORT_BARRIER
atomic_exchange32(&g_buffered_string_locked, 0);
}
void lzham_buffered_printf(const char *format, ...)
{
format;
char buf[lzham::buffered_str::cBufSize];
va_list args;
va_start(args, format);
vsnprintf_s(buf, sizeof(buf), sizeof(buf), format, args);
va_end(args);
buf[sizeof(buf) - 1] = '\0';
lzham::lock_buffered_strings();
if (!lzham::g_buffered_strings.capacity())
{
lzham::g_buffered_strings.reserve(2048);
}
lzham::g_buffered_strings.resize(lzham::g_buffered_strings.size() + 1);
memcpy(lzham::g_buffered_strings.back().m_buf, buf, sizeof(buf));
lzham::unlock_buffered_strings();
}
void lzham_flush_buffered_printf()
{
lzham::lock_buffered_strings();
for (lzham::uint i = 0; i < lzham::g_buffered_strings.size(); i++)
{
printf("%s", lzham::g_buffered_strings[i].m_buf);
}
lzham::g_buffered_strings.resize(0);
lzham::unlock_buffered_strings();
}
} // namespace lzham
#endif
|