lzham_platform.cpp 3.3 KB

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