No Description

TestApp1.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // TestApp1.cpp : Defines the entry point for the application.
  2. //
  3. #ifdef _WIN32
  4. #include "Win32/stdafx.h"
  5. #endif
  6. #include <DK.h>
  7. auto FormatBytes(size_t size, int maxDigits=3)->DKString
  8. {
  9. if (size > 1)
  10. {
  11. maxDigits = Clamp(maxDigits, 1, 4);
  12. const char* suffixList[7] = { "Bytes","KB","MB","GB","TB","PB","EB" };
  13. int suffixIndex = 0;
  14. while (size >= 1024000 && suffixIndex < 6)
  15. {
  16. suffixIndex++;
  17. size = size >> 10;
  18. }
  19. double s = static_cast<double>(size);
  20. if (size >= 1000 && suffixIndex < 6)
  21. {
  22. s = s / 1024.0;
  23. suffixIndex++;
  24. }
  25. if (suffixIndex > 0)
  26. {
  27. int numDigits = [](int s)->int
  28. {
  29. int n = 0;
  30. while (s > 0)
  31. {
  32. n++;
  33. s /= 10;
  34. }
  35. return Max(n, 1);
  36. }(s);
  37. return DKString::Format("%.*f %s", Clamp<int>(maxDigits - numDigits, 0, 3), s, suffixList[suffixIndex]);
  38. }
  39. return DKString::Format("%d %s", int(size), suffixList[0]);
  40. }
  41. return DKString::Format("%d Byte", int(size));
  42. };
  43. class TestApp1 : public DKApplication
  44. {
  45. DKObject<DKWindow> window;
  46. public:
  47. void OnInitialize(void) override
  48. {
  49. DKLogD("%s", DKGL_FUNCTION_NAME);
  50. window = DKWindow::Create("DefaultWindow");
  51. window->Activate();
  52. window->AddEventHandler(this,
  53. DKFunction([this](const DKWindow::WindowEvent& e) {
  54. if (e.type == DKWindow::WindowEvent::WindowClosed)
  55. DKApplication::Instance()->Terminate(0);
  56. }),
  57. NULL, NULL);
  58. }
  59. void OnTerminate(void) override
  60. {
  61. DKLogD("%s", DKGL_FUNCTION_NAME);
  62. DKLogI("Memory Pool Statistics");
  63. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  64. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  65. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  66. size_t usedBytes = 0;
  67. for (int i = 0; i < numBuckets; ++i)
  68. {
  69. if (buckets[i].totalChunks > 0)
  70. {
  71. DKLogI("--> %lu: allocated:%lu, reserved:%ls. (usage:%.1f%%)",
  72. buckets[i].chunkSize,
  73. buckets[i].chunkSize * buckets[i].usedChunks,
  74. (const wchar_t*)FormatBytes(buckets[i].chunkSize * (buckets[i].totalChunks - buckets[i].usedChunks)),
  75. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0);
  76. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  77. }
  78. }
  79. DKLogI("MemoryPool Usage: %ls / %ls", (const wchar_t*)FormatBytes(usedBytes), (const wchar_t*)FormatBytes(DKMemoryPoolSize()));
  80. delete[] buckets;
  81. }
  82. };
  83. #ifdef _WIN32
  84. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  85. _In_opt_ HINSTANCE hPrevInstance,
  86. _In_ LPWSTR lpCmdLine,
  87. _In_ int nCmdShow)
  88. #else
  89. int main(int argc, const char * argv[])
  90. #endif
  91. {
  92. TestApp1 app;
  93. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  94. return app.Run();
  95. }