Browse Source

FormatBytes 추가.

Hongtae Kim 6 years ago
parent
commit
aa36e25076
2 changed files with 41 additions and 4 deletions
  1. 40
    3
      TestApp1/TestApp1.cpp
  2. 1
    1
      TestApp1/TestApp1.vcxproj

+ 40
- 3
TestApp1/TestApp1.cpp View File

7
 
7
 
8
 #include <DK.h>
8
 #include <DK.h>
9
 
9
 
10
+auto FormatBytes(size_t size, int maxDigits=3)->DKString
11
+{
12
+	if (size > 1)
13
+	{
14
+		maxDigits = Clamp(maxDigits, 1, 4);
15
+
16
+		const char* suffixList[7] = { "Bytes","KB","MB","GB","TB","PB","EB" };
17
+		int suffixIndex = 0;
18
+		while (size >= 1024000 && suffixIndex < 6)
19
+		{
20
+			suffixIndex++;
21
+			size = size >> 10;
22
+		}
23
+		double s = static_cast<double>(size);
24
+		if (size >= 1000 && suffixIndex < 6)
25
+		{
26
+			s = s / 1024.0;
27
+			suffixIndex++;
28
+		}
29
+		if (suffixIndex > 0)
30
+		{
31
+			int numDigits = [](int s)->int
32
+			{
33
+				int n = 0;
34
+				while (s > 0)
35
+				{
36
+					n++;
37
+					s /= 10;
38
+				}
39
+				return Max(n, 1);
40
+			}(s);
41
+			return DKString::Format("%.*f %s", Clamp<int>(maxDigits - numDigits, 0, 3), s, suffixList[suffixIndex]);
42
+		}
43
+		return DKString::Format("%d %s", int(size), suffixList[0]);
44
+	}
45
+	return DKString::Format("%d Byte", int(size));
46
+};
10
 
47
 
11
 class TestApp1 : public DKApplication
48
 class TestApp1 : public DKApplication
12
 {
49
 {
38
 		{
75
 		{
39
 			if (buckets[i].totalChunks > 0)
76
 			if (buckets[i].totalChunks > 0)
40
 			{
77
 			{
41
-				DKLogI("--> %lu: allocated:%lu, reserved:%.1fKB. (usage:%.1f%%)",
78
+				DKLogI("--> %lu: allocated:%lu, reserved:%ls. (usage:%.1f%%)",
42
 					buckets[i].chunkSize,
79
 					buckets[i].chunkSize,
43
 					buckets[i].chunkSize * buckets[i].usedChunks,
80
 					buckets[i].chunkSize * buckets[i].usedChunks,
44
-					double(buckets[i].chunkSize * (buckets[i].totalChunks - buckets[i].usedChunks)) / 1024.0,
81
+					(const wchar_t*)FormatBytes(buckets[i].chunkSize * (buckets[i].totalChunks - buckets[i].usedChunks)),
45
 					double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0);
82
 					double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0);
46
 				usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
83
 				usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
47
 			}
84
 			}
48
 		}
85
 		}
49
-		DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
86
+		DKLogI("MemoryPool Usage: %ls / %ls", (const wchar_t*)FormatBytes(usedBytes), (const wchar_t*)FormatBytes(DKMemoryPoolSize()));
50
 		delete[] buckets;
87
 		delete[] buckets;
51
 	}
88
 	}
52
 };
89
 };

+ 1
- 1
TestApp1/TestApp1.vcxproj View File

22
     <ProjectGuid>{AE0AF61D-F069-4AFA-AE24-A75C6A233B37}</ProjectGuid>
22
     <ProjectGuid>{AE0AF61D-F069-4AFA-AE24-A75C6A233B37}</ProjectGuid>
23
     <Keyword>Win32Proj</Keyword>
23
     <Keyword>Win32Proj</Keyword>
24
     <RootNamespace>TestApp1</RootNamespace>
24
     <RootNamespace>TestApp1</RootNamespace>
25
-    <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion>
25
+    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
26
   </PropertyGroup>
26
   </PropertyGroup>
27
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
27
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
28
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">