TestMemory.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EAStdC/EAMemory.h>
  5. #include <EAStdC/EAString.h>
  6. #include <EAStdC/EAStopwatch.h>
  7. #include <EAStdC/EASprintf.h>
  8. #include <EAStdC/EARandom.h>
  9. #include <EAStdC/EARandomDistribution.h>
  10. #include <EAStdC/EABitTricks.h>
  11. #include <EAStdCTest/EAStdCTest.h>
  12. #include <EATest/EATest.h>
  13. #include <string.h>
  14. #include <EAStdC/EAAlignment.h>
  15. #include <EASTL/vector.h>
  16. #if defined(_MSC_VER)
  17. #pragma warning(push)
  18. #pragma warning(disable: 4996) // Function is deprecated.
  19. #pragma warning(disable: 6255) // _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead.
  20. #pragma warning(disable: 6211) // Leaking memory due to an exception. Consider using a local catch block to clean up memory.
  21. #pragma warning(disable: 6200) // Index '15' is out of valid index range '0' to '9' for non-stack buffer 'kPredefinedMemSizes'
  22. #endif
  23. // The memory we will use for testing.
  24. static uint8_t* gMem1 = NULL;
  25. static uint8_t* gMem2 = NULL;
  26. // Define expected fill values for gMem1 / gMem2.
  27. const uint8_t kByte1 = 0xaa;
  28. const uint8_t kByte2 = 0xbb;
  29. // For memcpy tests we allocate two large blocks of memory that are
  30. // of this alignment.
  31. static const size_t kBaseMemAlignment = 65536;
  32. // For memcpy tests we allocate two large blocks of memory that are
  33. // of this alignment. We will copy memory around to and from memory
  34. // segments within this block.
  35. static const size_t kBaseMemSize = 16777216; // 16 MiB
  36. // These are some predefined sizes that we test.
  37. // We also have random size testing.
  38. static const size_t kPredefinedMemSizes[] =
  39. {
  40. 0,
  41. 1,
  42. 24,
  43. 96,
  44. 200,
  45. 1024,
  46. 4096,
  47. 65536, // 64 KiB
  48. 1048576, // 1 MiB
  49. 8388608 // 8 MiB
  50. };
  51. static void TestEAAllocaHelper()
  52. {
  53. void* p = EAAlloca(32768);
  54. EA_ANALYSIS_ASSUME(p != NULL);
  55. // Try to force a reference to the memory
  56. memset(p, 0, 1);
  57. }
  58. static int TestEAAlloca()
  59. {
  60. int nErrorCount = 0;
  61. {
  62. void* p = EAAlloca(37); // It's actually possible that this could throw an exception (Microsoft) or a signal (Unix).
  63. EA_ANALYSIS_ASSUME(p != NULL);
  64. memset(p, 0, 37);
  65. }
  66. {
  67. // Call a function using alloca repeatedly to ensure the memory is returned. If memory is not released when
  68. // returning from TestEAAllocaHelper, then the test will run out of memory (or use huge amounts of virtual memory
  69. // on PC and likely crash or timeout).
  70. for (int i=0; i < 1000000; i++)
  71. {
  72. TestEAAllocaHelper();
  73. }
  74. }
  75. return nErrorCount;
  76. }
  77. static int TestEAMalloca()
  78. {
  79. int nErrorCount = 0;
  80. {
  81. void* p = EAMalloca(37);
  82. if(p)
  83. {
  84. memset(p, 0, 37);
  85. EAFreea(p);
  86. }
  87. p = EAMalloca(EAMALLOCA_THRESHOLD * 2); // Allocate something that's too large for alloca.
  88. if(p)
  89. {
  90. memset(p, 0, EAMALLOCA_THRESHOLD * 2);
  91. EAFreea(p);
  92. }
  93. }
  94. return nErrorCount;
  95. }
  96. static int TestMemset()
  97. {
  98. using namespace EA::StdC;
  99. int nErrorCount = 0;
  100. // uint8_t* Memset8C(void* pDestination, uint8_t c, size_t count);
  101. {
  102. EA::StdC::Random r;
  103. const void* pCheck;
  104. for(size_t i = 0; i < 16; ++i)
  105. {
  106. // Randomly choose a copy size, but make sure the predifined ones are always tested.
  107. const size_t copyCount = (i < EAArrayCount(kPredefinedMemSizes) && (kPredefinedMemSizes[i] < 4096)) ? kPredefinedMemSizes[i] : r.RandomUint32Uniform(4096);
  108. const size_t copySize = copyCount * sizeof(uint8_t);
  109. for(size_t j = 0; j < 7; ++j)
  110. {
  111. const size_t offset1 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment);
  112. uint8_t* pMem1 = gMem1 + offset1;
  113. EA::StdC::Memset8C(pMem1, kByte2, copyCount); // Copy pMem2's kByte2 values over pMem1's kByte1 values.
  114. // Verify memory prior to pMem1 is unmodified.
  115. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  116. EATEST_VERIFY(pCheck == NULL);
  117. // Verify copied memory.
  118. pCheck = Memcheck8(pMem1, kByte2, copySize);
  119. EATEST_VERIFY(pCheck == NULL);
  120. // Verify memory after pMem1+copySize.
  121. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  122. EATEST_VERIFY(pCheck == NULL);
  123. // Set the memory back to its original value.
  124. memset(pMem1, kByte1, copySize);
  125. }
  126. }
  127. }
  128. // uint8_t* Memset8_128C(void* pDestination, uint8_t c, size_t uint8Count);
  129. {
  130. for(size_t i = 0; i < 50; i++)
  131. {
  132. const size_t copySize = i * 128;
  133. const size_t copyCount = copySize * sizeof(uint8_t);
  134. uint8_t* pMem1 = gMem1;
  135. const void* pCheck;
  136. EA::StdC::Memset8_128C(pMem1, kByte2, copyCount);
  137. // Verify memory prior to pMem1 is unmodified.
  138. pCheck = Memcheck8(pMem1 - 256, kByte1, 256);
  139. EATEST_VERIFY(pCheck == NULL);
  140. // Verify copied memory.
  141. pCheck = Memcheck8(pMem1, kByte2, copySize);
  142. EATEST_VERIFY(pCheck == NULL);
  143. // Verify memory after pMem1+copySize.
  144. pCheck = Memcheck8(pMem1 + copySize, kByte1, 256);
  145. EATEST_VERIFY(pCheck == NULL);
  146. // Set the memory back to its original value.
  147. memset(pMem1, kByte1, copySize);
  148. }
  149. }
  150. // uint16_t* Memset16(void* pDestination, uint16_t c, size_t count);
  151. {
  152. EA::StdC::Random r;
  153. const void* pCheck;
  154. const uint16_t kByte2_16 = ((kByte2 << 8) | (kByte2 + 1));
  155. for(size_t i = 0; i < 16; ++i)
  156. {
  157. // Randomly choose a copy count, but make sure the predifined ones are always tested.
  158. const size_t copyCount = (i < EAArrayCount(kPredefinedMemSizes) && (kPredefinedMemSizes[i] < 2048)) ? kPredefinedMemSizes[i] : r.RandomUint32Uniform(2048);
  159. const size_t copySize = copyCount * sizeof(uint16_t);
  160. for(size_t j = 0; j < 7; ++j)
  161. {
  162. const size_t offset1 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment) / sizeof(uint16_t) * sizeof(uint16_t); // Divide, then multiply.
  163. uint8_t* pMem1 = gMem1 + offset1;
  164. EA::StdC::Memset16(pMem1, kByte2_16, copyCount);
  165. // Verify memory prior to pMem1 is unmodified.
  166. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  167. EATEST_VERIFY(pCheck == NULL);
  168. // Verify copied memory.
  169. pCheck = Memcheck16(pMem1, kByte2_16, copySize);
  170. EATEST_VERIFY(pCheck == NULL);
  171. // Verify memory after pMem1+copySize.
  172. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  173. EATEST_VERIFY(pCheck == NULL);
  174. // Set the memory back to its original value.
  175. memset(pMem1, kByte1, copySize);
  176. }
  177. }
  178. }
  179. // uint32_t* Memset32(void* pDestination, uint32_t c, size_t count);
  180. {
  181. EA::StdC::Random r;
  182. const void* pCheck;
  183. const uint32_t kByte2_32 = ((kByte2 << 8) | (kByte2 + 1));
  184. for(size_t i = 0; i < 16; ++i)
  185. {
  186. // Randomly choose a copy count, but make sure the predifined ones are always tested.
  187. const size_t copyCount = (i < EAArrayCount(kPredefinedMemSizes) && (kPredefinedMemSizes[i] < 1024)) ? kPredefinedMemSizes[i] : r.RandomUint32Uniform(1024);
  188. const size_t copySize = copyCount * sizeof(uint32_t);
  189. for(size_t j = 0; j < 7; ++j)
  190. {
  191. const size_t offset1 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment) / sizeof(uint32_t) * sizeof(uint32_t); // Divide, then multiply.
  192. uint8_t* pMem1 = gMem1 + offset1;
  193. EA::StdC::Memset32(pMem1, kByte2_32, copyCount);
  194. // Verify memory prior to pMem1 is unmodified.
  195. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  196. EATEST_VERIFY(pCheck == NULL);
  197. // Verify copied memory.
  198. pCheck = Memcheck32(pMem1, kByte2_32, copySize);
  199. EATEST_VERIFY(pCheck == NULL);
  200. // Verify memory after pMem1+copySize.
  201. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  202. EATEST_VERIFY(pCheck == NULL);
  203. // Set the memory back to its original value.
  204. memset(pMem1, kByte1, copySize);
  205. }
  206. }
  207. }
  208. // uint64_t* Memset64(void* pDestination, uint64_t c, size_t count);
  209. {
  210. EA::StdC::Random r;
  211. const void* pCheck;
  212. const uint64_t kByte2_64 = ((kByte2 << 8) | (kByte2 + 1));
  213. for(size_t i = 0; i < 16; ++i)
  214. {
  215. // Randomly choose a copy count, but make sure the predifined ones are always tested.
  216. const size_t copyCount = (i < EAArrayCount(kPredefinedMemSizes) && (kPredefinedMemSizes[i] < 512)) ? kPredefinedMemSizes[i] : r.RandomUint32Uniform(512);
  217. const size_t copySize = copyCount * sizeof(uint64_t);
  218. for(size_t j = 0; j < 7; ++j)
  219. {
  220. const size_t offset1 = r.RandomUint32Uniform((uint64_t)kBaseMemAlignment) / sizeof(uint64_t) * sizeof(uint64_t); // Divide, then multiply.
  221. uint8_t* pMem1 = gMem1 + offset1;
  222. EA::StdC::Memset64(pMem1, kByte2_64, copyCount);
  223. // Verify memory prior to pMem1 is unmodified.
  224. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  225. EATEST_VERIFY(pCheck == NULL);
  226. // Verify copied memory.
  227. pCheck = Memcheck64(pMem1, kByte2_64, copySize);
  228. EATEST_VERIFY(pCheck == NULL);
  229. // Verify memory after pMem1+copySize.
  230. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  231. EATEST_VERIFY(pCheck == NULL);
  232. // Set the memory back to its original value.
  233. memset(pMem1, kByte1, copySize);
  234. }
  235. }
  236. }
  237. // void* MemsetPointer(void* pDestination, const void* const pValue, size_t count)
  238. {
  239. const size_t kBufferSize = 2000;
  240. void** const pBuffer = new void*[kBufferSize];
  241. for(size_t i = 1; i < 2000; i *= 3)
  242. {
  243. memset(pBuffer, 0, kBufferSize * sizeof(void*));
  244. void* p = MemsetPointer(pBuffer, (void*)(uintptr_t)i, i);
  245. EATEST_VERIFY(p == pBuffer);
  246. for(size_t k = 0; k < 2000; ++k)
  247. {
  248. if(k < i)
  249. EATEST_VERIFY(pBuffer[k] == (void*)(uintptr_t)i);
  250. else
  251. EATEST_VERIFY(pBuffer[k] == (void*)(uintptr_t)0);
  252. }
  253. }
  254. delete[] pBuffer;
  255. }
  256. // void* MemsetN(void* pDestination, const void* pSource, size_t sourceBytes, size_t count);
  257. {
  258. // To do: We need a more extensive test.
  259. char buffer[2000];
  260. const char* pattern = "012345678";
  261. size_t sl = Strlen(pattern);
  262. EATEST_VERIFY(buffer == MemsetN(buffer, pattern, sl, 2000));
  263. EATEST_VERIFY(buffer[sl] == '0' && buffer[77*sl+1] == '1');
  264. EATEST_VERIFY(buffer[sl*33+4] == '4' && buffer[123*sl+8] == '8');
  265. EATEST_VERIFY(buffer[sl*98+3] == '3' && buffer[181*sl+6] == '6');
  266. }
  267. return nErrorCount;
  268. }
  269. static int TestMemfill()
  270. {
  271. using namespace EA::StdC;
  272. int nErrorCount = 0;
  273. // void Memfill8(void* pDestination, uint8_t c, size_t byteCount);
  274. {
  275. const void* result = nullptr;
  276. const int ARR_SIZE = 4096;
  277. uint8_t buf[ARR_SIZE];
  278. void* pMem = static_cast<void*>(&buf);
  279. Memfill8(pMem, kByte1, ARR_SIZE);
  280. result = Memcheck8(pMem, kByte1, ARR_SIZE);
  281. EATEST_VERIFY(result == NULL);
  282. Memfill8(pMem, kByte2, ARR_SIZE);
  283. result = Memcheck8(pMem, kByte2, ARR_SIZE);
  284. EATEST_VERIFY(result == NULL);
  285. }
  286. // void Memfill16(void* pDestination, uint16_t c, size_t byteCount);
  287. {
  288. // Test different alignments, sizes 0 to 257, 1023 to 1026
  289. uint16_t val16 = 0x1234;
  290. uint16_t* val16Array = new uint16_t[2048 + 32]; // To consider: Would it make the code better if we created constants for these
  291. uint8_t* buf8Array = new uint8_t[4096 + 64]; // sizes, or would it instead just make this harder to follow?
  292. uint8_t* buf8Array2 = new uint8_t[4096 + 64];
  293. size_t j;
  294. EATEST_VERIFY(val16Array && buf8Array && buf8Array2);
  295. Memset16(val16Array, val16, 2048 + 32);
  296. for(int32_t i = 0; i < 4; i++)
  297. {
  298. for(j = 0; j <= 257; j++)
  299. {
  300. memset(buf8Array, 0, 4096);
  301. memset(buf8Array2, 0, 4096);
  302. Memfill16(buf8Array + i, val16, j);
  303. for(size_t k = 0; k < (j / sizeof(uint16_t) + 1); ++k)
  304. memcpy(buf8Array2 + i + (k * sizeof(uint16_t)), val16Array, j - (k * sizeof(uint16_t)));
  305. EATEST_VERIFY(memcmp(buf8Array, buf8Array2, 4096) == 0);
  306. }
  307. for(j = 1023; j <= 1026; j++)
  308. {
  309. memset(buf8Array, 0, 4096);
  310. memset(buf8Array2, 0, 4096);
  311. Memfill16(buf8Array + i, val16, j);
  312. for(size_t k = 0; k < (j / sizeof(uint16_t) + 1); ++k)
  313. memcpy(buf8Array2 + i + (k * sizeof(uint16_t)), val16Array, j - (k * sizeof(uint16_t)));
  314. EATEST_VERIFY(memcmp(buf8Array, buf8Array2, 4096) == 0);
  315. }
  316. }
  317. delete[] val16Array;
  318. delete[] buf8Array;
  319. delete[] buf8Array2;
  320. }
  321. // void Memfill24(void* pDestination, uint32_t c, size_t byteCount);
  322. {
  323. // To do.
  324. }
  325. // void Memfill32(void* pDestination, uint32_t c, size_t byteCount);
  326. {
  327. // To do.
  328. }
  329. // void Memfill64(void* pDestination, uint64_t c, size_t byteCount);
  330. {
  331. // To do.
  332. }
  333. // void MemfillSpecific(void* pDestination, const void* pSource, size_t destByteCount, size_t sourceByteCount);
  334. {
  335. // To do.
  336. }
  337. return nErrorCount;
  338. }
  339. static int TestMemclear()
  340. {
  341. using namespace EA::StdC;
  342. int nErrorCount = 0;
  343. // void MemclearC(void* pDestination, size_t n);
  344. {
  345. EA::StdC::Random r;
  346. const void* pCheck;
  347. for(size_t i = 0; i < 16; ++i)
  348. {
  349. // Randomly choose a copy size, but make sure the predefined ones are always tested.
  350. const size_t copyCount = (i < EAArrayCount(kPredefinedMemSizes) && (kPredefinedMemSizes[i] < 4096)) ? kPredefinedMemSizes[i] : r.RandomUint32Uniform(4096);
  351. const size_t copySize = copyCount * sizeof(uint8_t);
  352. for(size_t j = 0; j < 7; ++j)
  353. {
  354. const size_t offset1 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment);
  355. uint8_t* pMem1 = gMem1 + offset1;
  356. EA::StdC::MemclearC(pMem1, copyCount); // Set zero values over pMem1's kByte1 values.
  357. // Verify memory prior to pMem1 is unmodified.
  358. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  359. EATEST_VERIFY(pCheck == NULL);
  360. // Verify copied memory.
  361. pCheck = Memcheck8(pMem1, 0, copySize);
  362. EATEST_VERIFY(pCheck == NULL);
  363. // Verify memory after pMem1+copySize.
  364. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  365. EATEST_VERIFY(pCheck == NULL);
  366. // Set the memory back to its original value.
  367. memset(pMem1, kByte1, copySize);
  368. }
  369. }
  370. }
  371. return nErrorCount;
  372. }
  373. static int TestMemcheck()
  374. {
  375. using namespace EA::StdC;
  376. int nErrorCount = 0;
  377. const void* pCheck;
  378. // const void* Memcheck8(const void* p, uint8_t c, size_t byteCount);
  379. {
  380. const uint8_t bytes[5] = { 0x00, 0x01, 0x01, 0x01, 0x00 };
  381. pCheck = Memcheck8(bytes + 0, 0x00, 1);
  382. EATEST_VERIFY(pCheck == NULL);
  383. pCheck = Memcheck8(bytes + 0, 0x01, 1);
  384. EATEST_VERIFY(pCheck == bytes);
  385. pCheck = Memcheck8(bytes + 0, 0x00, 2);
  386. EATEST_VERIFY(pCheck == bytes + 1);
  387. pCheck = Memcheck8(bytes + 1, 0x01, 3);
  388. EATEST_VERIFY(pCheck == NULL);
  389. }
  390. // const void* Memcheck16(const void* p, uint16_t c, size_t byteCount);
  391. {
  392. union U16 {
  393. uint16_t c16;
  394. uint8_t c8[2];
  395. };
  396. const U16 bytes[5] = { { 0x0000 }, { 0x0001 }, { 0x0001 }, { 0x0001 }, { 0x0101 } };
  397. pCheck = Memcheck16(bytes + 0, 0x0000, 2);
  398. EATEST_VERIFY(pCheck == NULL);
  399. pCheck = Memcheck16(bytes + 0, 0x0001, 2);
  400. #ifdef EA_SYSTEM_BIG_ENDIAN
  401. EATEST_VERIFY(pCheck == bytes[0].c8 + 1);
  402. #else
  403. EATEST_VERIFY(pCheck == bytes[0].c8 + 0);
  404. #endif
  405. pCheck = Memcheck16(bytes + 1, 0x0001, 6);
  406. EATEST_VERIFY(pCheck == NULL);
  407. pCheck = Memcheck16(bytes[0].c8 + 1, 0x0001, 2);
  408. #ifdef EA_SYSTEM_BIG_ENDIAN
  409. EATEST_VERIFY(pCheck == bytes[0].c8 + 1);
  410. #else
  411. EATEST_VERIFY(pCheck == NULL); // Due to byte ordering, little-endian sees this as matching.
  412. #endif
  413. pCheck = Memcheck16(bytes[0].c8 + 1, 0x0000, 2);
  414. #ifdef EA_SYSTEM_BIG_ENDIAN
  415. EATEST_VERIFY(pCheck == NULL); // Due to byte ordering, big-endian sees this as matching.
  416. #else
  417. EATEST_VERIFY(pCheck == bytes[1].c8 + 0);
  418. #endif
  419. }
  420. // const void* Memcheck32(const void* p, uint32_t c, size_t byteCount);
  421. {
  422. union U32 {
  423. uint32_t c32;
  424. uint8_t c8[4];
  425. };
  426. const U32 bytes[5] = { { 0x00010203 }, { 0x00010203 }, { 0x00010203 }, { 0x00010203 }, { 0x00010203 } };
  427. for(int i = 0; i <= 4; ++i)
  428. {
  429. pCheck = Memcheck32(bytes[0].c8 + i, 0x00010203, 9);
  430. EATEST_VERIFY(pCheck == NULL);
  431. }
  432. for(int i = 0; i <= 4; ++i)
  433. {
  434. pCheck = Memcheck32(bytes[0].c8 + i, 0x01020300, 9);
  435. EATEST_VERIFY(pCheck != NULL);
  436. }
  437. }
  438. // const void* Memcheck64(const void* p, uint64_t c, size_t byteCount);
  439. {
  440. union U64 {
  441. uint64_t c64;
  442. uint8_t c8[8];
  443. };
  444. // Some platforms' (e.g. x86) compilers don't align 64 bit values on 64 bit boundaries. So we guarantee it here, as Memcheck64 expects it.
  445. // Additionally, some of the platforms we test for require 16 bit alignment of types, so we use that instead of 8.
  446. static EA_ALIGNED(const U64, bytes[5], 16) = { { UINT64_C(0x0001020304050607) }, { UINT64_C(0x0001020304050607) }, { UINT64_C(0x0001020304050607) }, { UINT64_C(0x0001020304050607) }, { UINT64_C(0x0001020304050607) } };
  447. for(int i = 0; i <= 8; ++i)
  448. {
  449. pCheck = Memcheck64(bytes[0].c8 + i, UINT64_C(0x0001020304050607), 18);
  450. EATEST_VERIFY(pCheck == NULL);
  451. }
  452. for(int i = 0; i <= 8; ++i)
  453. {
  454. pCheck = Memcheck64(bytes[0].c8 + i, UINT64_C(0x0102030405060700), 18);
  455. EATEST_VERIFY(pCheck != NULL);
  456. }
  457. }
  458. return nErrorCount;
  459. }
  460. static int TestMemchr()
  461. {
  462. using namespace EA::StdC;
  463. int nErrorCount = 0;
  464. { // Memchr8
  465. const char* const s = "qwertyuiopASDFGHJKL:!@#$%^&*,=/";
  466. EATEST_VERIFY((char*)Memchr(s, (char)'q', Strlen(s)) - s == 0);
  467. EATEST_VERIFY((char*)Memchr(s, (char)'F', Strlen(s)) - s == 13);
  468. EATEST_VERIFY((char*)Memchr(s, (char)':', Strlen(s)) - s == 19);
  469. EATEST_VERIFY((char*)Memchr(s, (char)'&', Strlen(s)) - s == 26);
  470. }
  471. return nErrorCount;
  472. }
  473. static int TestMemcmp()
  474. {
  475. using namespace EA::StdC;
  476. int nErrorCount = 0;
  477. { // Memcmp8
  478. char buffer1[] = "01234567a";
  479. char buffer2[] = "01234567b";
  480. char buffer3[] = "01234567c";
  481. EATEST_VERIFY(Memcmp(buffer1, buffer1, 9) == 0);
  482. EATEST_VERIFY(Memcmp(buffer2, buffer1, 9) > 0);
  483. EATEST_VERIFY(Memcmp(buffer3, buffer2, 9) > 0);
  484. EATEST_VERIFY(Memcmp(buffer2, buffer3, 9) < 0);
  485. EATEST_VERIFY(Memcmp(buffer1, buffer2, 9) < 0);
  486. }
  487. #if EASTDC_MEMCPY16_ENABLED
  488. { // Memcmp16
  489. char16_t buffer1[] = EA_CHAR16("01234567a");
  490. char16_t buffer2[] = EA_CHAR16("01234567b");
  491. char16_t buffer3[] = EA_CHAR16("01234567c");
  492. EATEST_VERIFY(Memcmp(buffer1, buffer1, 9) == 0);
  493. EATEST_VERIFY(Memcmp(buffer2, buffer1, 9) > 0);
  494. EATEST_VERIFY(Memcmp(buffer3, buffer2, 9) > 0);
  495. EATEST_VERIFY(Memcmp(buffer2, buffer3, 9) < 0);
  496. EATEST_VERIFY(Memcmp(buffer1, buffer2, 9) < 0);
  497. }
  498. #endif
  499. return nErrorCount;
  500. }
  501. static int TestMemmem()
  502. {
  503. using namespace EA::StdC;
  504. int nErrorCount = 0;
  505. const size_t kSize = 37;
  506. const char buffer1[kSize] = "abcdefghijklmnopqrstuvwxyz0123456789";
  507. EATEST_VERIFY(Memmem(buffer1, 0, "", 0) == NULL); // An empty haystack always results in NULL, regardless of the needle.
  508. EATEST_VERIFY(Memmem(buffer1, kSize, "", 0) == buffer1); // Otherwise, an empty needle results in success.
  509. EATEST_VERIFY(Memmem(buffer1, 0, "_", 1) == NULL); //
  510. EATEST_VERIFY(Memmem("_", 1, buffer1, kSize) == NULL); // Search of a needle that is bigger than the haystack. Always failure.
  511. EATEST_VERIFY(Memmem(buffer1, kSize, "_", 1) == NULL);
  512. EATEST_VERIFY(Memmem(buffer1, kSize, buffer1, kSize) == buffer1);
  513. EATEST_VERIFY(Memmem(buffer1, kSize, "a", 1) == buffer1);
  514. EATEST_VERIFY(Memmem(buffer1, kSize, "abc", 3) == buffer1);
  515. EATEST_VERIFY(Memmem(buffer1, kSize, "bcd", 3) == buffer1 + 1);
  516. EATEST_VERIFY(Memmem(buffer1, kSize, "tuv", 3) == buffer1 + 19);
  517. EATEST_VERIFY(Memmem(buffer1, kSize, "9", 1) == buffer1 + 35);
  518. EATEST_VERIFY(Memmem(buffer1, kSize, "789", 3) == buffer1 + 33);
  519. EATEST_VERIFY(Memmem(buffer1, kSize, "9__", 3) == NULL);
  520. EATEST_VERIFY(Memmem("\1\0", 2, "\1\0", 2) != NULL);
  521. EATEST_VERIFY(Memmem("\1\1", 2, "\1\0", 2) == NULL);
  522. return nErrorCount;
  523. }
  524. static int TestMemcpy()
  525. {
  526. using namespace EA::StdC;
  527. int nErrorCount = 0;
  528. { // MemcpyC
  529. char buffer1[] = " ";
  530. char buffer2[] = "01234567b";
  531. EATEST_VERIFY(buffer1 == MemcpyC(buffer1, buffer2, 9));
  532. EATEST_VERIFY(Memcmp(buffer2, buffer1, 9) == 0);
  533. }
  534. #if EASTDC_MEMCPY16_ENABLED
  535. { // Memcpy16
  536. char16_t buffer1[] = EA_CHAR16(" ");
  537. char16_t buffer2[] = EA_CHAR16("01234567b");
  538. EATEST_VERIFY(buffer1 == (char16_t*)Memcpy((void*)buffer1, (void*)buffer2, 9 * sizeof(char16_t)));
  539. EATEST_VERIFY(memcmp(buffer2, buffer1, 9 * sizeof(char16_t)) == 0);
  540. }
  541. #endif
  542. { // char* MemcpyC(void* pDestination, const void* pSource, size_t nByteCount);
  543. EA::StdC::Random r;
  544. const void* pCheck;
  545. for(size_t i = 0; i < EAArrayCount(kPredefinedMemSizes); ++i)
  546. {
  547. const size_t copySize = kPredefinedMemSizes[i];
  548. for(size_t j = 0; j < 7; ++j)
  549. {
  550. const size_t offset1 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment);
  551. const size_t offset2 = r.RandomUint32Uniform((uint32_t)kBaseMemAlignment);
  552. uint8_t* pMem1 = gMem1 + offset1;
  553. uint8_t* pMem2 = gMem2 + offset2;
  554. EA::StdC::MemcpyC(pMem1, pMem2, copySize); // Copy pMem2's kByte2 values over pMem1's kByte1 values.
  555. // Verify memory prior to pMem1 is unmodified.
  556. pCheck = Memcheck8(pMem1 - kBaseMemAlignment, kByte1, kBaseMemAlignment);
  557. EATEST_VERIFY(pCheck == NULL);
  558. // Verify copied memory.
  559. pCheck = Memcheck8(pMem1, kByte2, copySize);
  560. EATEST_VERIFY(pCheck == NULL);
  561. // Verify memory after pMem1+copySize.
  562. pCheck = Memcheck8(pMem1 + copySize, kByte1, kBaseMemAlignment);
  563. EATEST_VERIFY(pCheck == NULL);
  564. // Set the memory back to its original value.
  565. memset(pMem1, kByte1, copySize);
  566. }
  567. }
  568. }
  569. { // char* Memcpy128(void* pDestination, const void* pSource, size_t nByteCount);
  570. EA::StdC::Stopwatch stopwatch1(EA::StdC::Stopwatch::kUnitsCPUCycles);
  571. EA::StdC::Stopwatch stopwatch2(EA::StdC::Stopwatch::kUnitsCPUCycles);
  572. EA::StdC::Stopwatch stopwatch3(EA::StdC::Stopwatch::kUnitsCPUCycles);
  573. for(int t = 0; t < 2; t++)
  574. {
  575. stopwatch1.Reset();
  576. stopwatch2.Reset();
  577. stopwatch3.Reset();
  578. for(size_t i = 0; i < 50; i++)
  579. {
  580. const size_t copySize = i * 128;
  581. const void* pCheck;
  582. uint8_t* pMem1 = gMem1;
  583. uint8_t* pMem2 = gMem2;
  584. stopwatch1.Start();
  585. EA::StdC::Memcpy128(pMem1, pMem2, copySize); // Copy pMem2's kByte2 values over pMem1's kByte1 values.
  586. stopwatch1.Stop();
  587. // Verify memory prior to pMem1 is unmodified.
  588. pCheck = Memcheck8(pMem1 - 256, kByte1, 256);
  589. EATEST_VERIFY(pCheck == NULL);
  590. // Verify copied memory.
  591. pCheck = Memcheck8(pMem1, kByte2, copySize);
  592. EATEST_VERIFY(pCheck == NULL);
  593. // Verify memory after pMem1+copySize.
  594. pCheck = Memcheck8(pMem1 + copySize, kByte1, 256);
  595. EATEST_VERIFY(pCheck == NULL);
  596. // Set the memory back to its original value.
  597. memset(pMem1, kByte1, copySize);
  598. // Compare to regular memcpy.
  599. stopwatch2.Start();
  600. memcpy(pMem1, pMem2, copySize);
  601. stopwatch2.Stop();
  602. memset(pMem1, kByte1, copySize);
  603. // Compare to regular __builtin_memcpy.
  604. stopwatch3.Start();
  605. #if defined(__GNUC__)
  606. __builtin_memcpy(pMem1, pMem2, copySize);
  607. #else
  608. memcpy(pMem1, pMem2, copySize);
  609. #endif
  610. stopwatch3.Stop();
  611. memset(pMem1, kByte1, copySize);
  612. }
  613. if(t == 1)
  614. EA::UnitTest::ReportVerbosity(1, "Memcpy128: %I64u cycles; memcpy: %I64u cycles, __builtin_memcpy: %I64u\n",
  615. stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime(), stopwatch3.GetElapsedTime());
  616. }
  617. }
  618. { // char* Memcpy128C(void* pDestination, const void* pSource, size_t nByteCount);
  619. for(size_t i = 0; i < 50; i++)
  620. {
  621. const size_t copySize = i * 128;
  622. const void* pCheck;
  623. uint8_t* pMem1 = gMem1;
  624. uint8_t* pMem2 = gMem2;
  625. EA::StdC::Memcpy128C(pMem1, pMem2, copySize); // Copy pMem2's kByte2 values over pMem1's kByte1 values.
  626. // Verify memory prior to pMem1 is unmodified.
  627. pCheck = Memcheck8(pMem1 - 256, kByte1, 256);
  628. EATEST_VERIFY(pCheck == NULL);
  629. // Verify copied memory.
  630. pCheck = Memcheck8(pMem1, kByte2, copySize);
  631. EATEST_VERIFY(pCheck == NULL);
  632. // Verify memory after pMem1+copySize.
  633. pCheck = Memcheck8(pMem1 + copySize, kByte1, 256);
  634. EATEST_VERIFY(pCheck == NULL);
  635. // Set the memory back to its original value.
  636. memset(pMem1, kByte1, copySize);
  637. }
  638. }
  639. return nErrorCount;
  640. }
  641. static int TestMemmove()
  642. {
  643. using namespace EA::StdC;
  644. int nErrorCount = 0;
  645. { // Memmove8
  646. char buffer1[] = "...........";
  647. char buffer2[] = ".......0123";
  648. char buffer3[] = "0123.......";
  649. EATEST_VERIFY(buffer1 == MemmoveC(buffer1, buffer2, Strlen(buffer2)));
  650. EATEST_VERIFY(memcmp(buffer1, buffer2, Strlen(buffer2)) == 0);
  651. EATEST_VERIFY(memset(buffer1, (char )0, Strlen(buffer1)) != NULL);
  652. EATEST_VERIFY(buffer1 == MemmoveC(buffer1, buffer2+7, Strlen(buffer2) - 7));
  653. EATEST_VERIFY(memcmp(buffer1, buffer2+7, Strlen(buffer2) - 7) == 0);
  654. EATEST_VERIFY(buffer2+5 == MemmoveC(buffer2+5, buffer2+7, Strlen(buffer2) - 7));
  655. EATEST_VERIFY(memcmp(buffer2+5, buffer1, Strlen(buffer2) - 7) == 0);
  656. EATEST_VERIFY(buffer1 == MemmoveC(buffer1, buffer3, Strlen(buffer3)));
  657. EATEST_VERIFY(buffer3+2 == MemmoveC(buffer3+2, buffer3, Strlen(buffer3) - 2));
  658. EATEST_VERIFY(memcmp(buffer3+2, buffer1, Strlen(buffer3) - 2) == 0);
  659. // To do: We need a much better test than this.
  660. }
  661. #if EASTDC_MEMCPY16_ENABLED
  662. { // Memmove16
  663. char16_t buffer1[] = EA_CHAR16("...........");
  664. char16_t buffer2[] = EA_CHAR16(".......0123");
  665. char16_t buffer3[] = EA_CHAR16("0123.......");
  666. EATEST_VERIFY(buffer1 == (char16_t*)Memmove((void*)buffer1, (void*)buffer2, Strlen(buffer2) * sizeof(char16_t)));
  667. EATEST_VERIFY(memcmp(buffer1, buffer2, Strlen(buffer2) * sizeof(char16_t)) == 0);
  668. EATEST_VERIFY(memset(buffer1, (char16_t )0, Strlen(buffer1) * sizeof(char16_t)) != NULL);
  669. EATEST_VERIFY(buffer1 == (char16_t*)Memmove((void*)buffer1, (void*)(buffer2+7), (Strlen(buffer2) - 7) * sizeof(char16_t)));
  670. EATEST_VERIFY(memcmp(buffer1, buffer2+7, (Strlen(buffer2) - 7) * sizeof(char16_t)) == 0);
  671. EATEST_VERIFY(buffer2+5 == (char16_t*)Memmove((void*)(buffer2+5), (void*)(buffer2+7), (Strlen(buffer2) - 7) * sizeof(char16_t)));
  672. EATEST_VERIFY(memcmp(buffer2+5, buffer1, (Strlen(buffer2) - 7) * sizeof(char16_t)) == 0);
  673. EATEST_VERIFY(buffer1 == (char16_t*)Memmove((void*)buffer1, (void*)buffer3, Strlen(buffer3) * sizeof(char16_t)));
  674. EATEST_VERIFY(buffer3+2 == (char16_t*)Memmove((void*)(buffer3+2), (void*)buffer3, (Strlen(buffer3) - 2) * sizeof(char16_t)));
  675. EATEST_VERIFY(memcmp(buffer3+2, buffer1, (Strlen(buffer3) - 2) * sizeof(char16_t)) == 0);
  676. }
  677. #endif
  678. return nErrorCount;
  679. }
  680. static int TestTimingSafe()
  681. {
  682. using namespace EA::StdC;
  683. int nErrorCount = 0;
  684. {
  685. // bool TimingSafeMemEqual(const void* p1, const void* p2, size_t n);
  686. // int TimingSafeMemcmp(const void* p1, const void* p2, size_t n);
  687. // bool TimingSafeMemIsClear(const void* p, size_t n);
  688. { // Basic accuracy tests.
  689. char buffer1[] = "01234567a";
  690. char buffer2[] = "01234567b";
  691. char buffer3[] = "01234567c";
  692. char buffer4[] = "\0\0\0\0\0\0\0\0\0";
  693. EATEST_VERIFY(TimingSafeMemcmp(buffer1, buffer1, 0) == Memcmp(buffer1, buffer1, 0));
  694. EATEST_VERIFY(TimingSafeMemcmp(buffer2, buffer1, 9) == Memcmp(buffer2, buffer1, 9));
  695. EATEST_VERIFY(TimingSafeMemcmp(buffer3, buffer2, 9) == Memcmp(buffer3, buffer2, 9));
  696. EATEST_VERIFY(TimingSafeMemcmp(buffer2, buffer3, 9) == Memcmp(buffer2, buffer3, 9));
  697. EATEST_VERIFY(TimingSafeMemcmp(buffer1, buffer2, 9) == Memcmp(buffer1, buffer2, 9));
  698. EATEST_VERIFY(TimingSafeMemEqual(buffer1, buffer1, 0) == (Memcmp(buffer1, buffer1, 0) == 0));
  699. EATEST_VERIFY(TimingSafeMemEqual(buffer1, buffer1, 9) == (Memcmp(buffer1, buffer1, 9) == 0));
  700. EATEST_VERIFY(TimingSafeMemEqual(buffer2, buffer1, 9) == (Memcmp(buffer2, buffer1, 9) == 0));
  701. EATEST_VERIFY(TimingSafeMemEqual(buffer3, buffer2, 9) == (Memcmp(buffer3, buffer2, 9) == 0));
  702. EATEST_VERIFY(TimingSafeMemEqual(buffer2, buffer3, 9) == (Memcmp(buffer2, buffer3, 9) == 0));
  703. EATEST_VERIFY(TimingSafeMemEqual(buffer1, buffer2, 9) == (Memcmp(buffer1, buffer2, 9) == 0));
  704. EATEST_VERIFY(TimingSafeMemIsClear(buffer1, 0) == true);
  705. EATEST_VERIFY(TimingSafeMemIsClear(buffer1, 1) == false);
  706. EATEST_VERIFY(TimingSafeMemIsClear(buffer1, 9) == false);
  707. EATEST_VERIFY(TimingSafeMemIsClear(buffer4, 1) == true);
  708. EATEST_VERIFY(TimingSafeMemIsClear(buffer4, 9) == true);
  709. }
  710. { // Timing tests.
  711. // It's not easy to fully validate the constant timing of these functions, due to the
  712. // tiny cycle count differences potentially involved. However, we can pretty easily
  713. // test extreme cases and verify that at least the basic logic of the functions are
  714. // timing constant and not optimized away by the compiler.
  715. Stopwatch stopwatch1(Stopwatch::kUnitsCPUCycles, false);
  716. Stopwatch stopwatch2(Stopwatch::kUnitsCPUCycles, false);
  717. bool success = false;
  718. eastl::vector<uint8_t> vLarge1((eastl_size_t)100000, (uint8_t)0); // Some large sized memory.
  719. eastl::vector<uint8_t> vLarge2((eastl_size_t)100000, (uint8_t)0);
  720. // We run this test multiple times because it may fail due to some execution hiccup and we want to give it
  721. // another chance. In a sense this is a bad idea because it seems to be going against what the function is
  722. // intended to be tested for, but it's impossible to truly know why something didn't execute in constant
  723. // time without looking at the executed machine code by hand.
  724. // TimingSafeMemEqual
  725. for(int i = 0; (i < 3) && !success; i++)
  726. {
  727. stopwatch1.Restart();
  728. bool bResult = TimingSafeMemEqual(vLarge1.data(), vLarge2.data(), vLarge1.size());
  729. stopwatch1.Stop();
  730. EATEST_VERIFY(bResult == true);
  731. vLarge1[0] = 1; // Change the first and last bytes. With regular memcmp this changed byte would result in memcmp returning very quickly, but we don't want that.
  732. vLarge1[vLarge1.size()-1] = 1;
  733. stopwatch2.Restart();
  734. bResult = TimingSafeMemEqual(vLarge1.data(), vLarge2.data(), vLarge1.size());
  735. stopwatch2.Stop();
  736. EATEST_VERIFY(bResult == false);
  737. success = (((stopwatch1.GetElapsedTimeFloat() - stopwatch2.GetElapsedTimeFloat()) / stopwatch1.GetElapsedTimeFloat()) < 0.25); // We give it a lot of leeway so our unit tests don't frequently fail.
  738. vLarge1[0] = 0; // Restore the changed bytes.
  739. vLarge1[vLarge1.size()-1] = 0;
  740. }
  741. EATEST_VERIFY_MSG(success, "TimingSafeMemEqual didn't seem to be able to execute in constant time.");
  742. // TimingSafeMemcmp
  743. for(int i = 0; (i < 3) && !success; i++)
  744. {
  745. stopwatch1.Restart();
  746. int iResult = TimingSafeMemcmp(vLarge1.data(), vLarge2.data(), vLarge1.size());
  747. stopwatch1.Stop();
  748. EATEST_VERIFY(iResult == 0);
  749. vLarge1[0] = 1; // Change the first and last bytes. With regular memcmp this changed byte would result in memcmp returning very quickly, but we don't want that.
  750. vLarge1[vLarge1.size()-1] = 1;
  751. stopwatch2.Restart();
  752. iResult = TimingSafeMemcmp(vLarge1.data(), vLarge2.data(), vLarge1.size());
  753. stopwatch2.Stop();
  754. EATEST_VERIFY(iResult == 1);
  755. success = ((fabsf(stopwatch1.GetElapsedTimeFloat() - stopwatch2.GetElapsedTimeFloat()) / stopwatch1.GetElapsedTimeFloat()) < 0.25); // We give it a lot of leeway so our unit tests don't frequently fail.
  756. vLarge1[0] = 0;
  757. vLarge1[vLarge1.size()-1] = 0;
  758. }
  759. EATEST_VERIFY_MSG(success, "TimingSafeMemcmp didn't seem to be able to execute in constant time.");
  760. // TimingSafeMemIsClear
  761. for(int i = 0; (i < 3) && !success; i++)
  762. {
  763. stopwatch1.Restart();
  764. bool bResult = TimingSafeMemIsClear(vLarge1.data(), vLarge1.size());
  765. stopwatch1.Stop();
  766. EATEST_VERIFY(bResult == true);
  767. vLarge1[0] = 1;
  768. vLarge1[vLarge1.size()-1] = 1;
  769. stopwatch2.Restart();
  770. bResult = TimingSafeMemIsClear(vLarge1.data(), vLarge1.size());
  771. stopwatch2.Stop();
  772. EATEST_VERIFY(bResult == false);
  773. success = ((fabsf(stopwatch1.GetElapsedTimeFloat() - stopwatch2.GetElapsedTimeFloat()) / stopwatch1.GetElapsedTimeFloat()) < 0.25); // We give it a lot of leeway so our unit tests don't frequently fail.
  774. vLarge1[0] = 0;
  775. vLarge1[vLarge1.size()-1] = 0;
  776. }
  777. EATEST_VERIFY_MSG(success, "TimingSafeMemIsClear didn't seem to be able to execute in constant time.");
  778. }
  779. }
  780. return nErrorCount;
  781. }
  782. static void TestMemcpySpeed()
  783. {
  784. using namespace EA::StdC;
  785. struct SizeOffset
  786. {
  787. size_t mSize;
  788. size_t mOffset1;
  789. size_t mOffset2;
  790. };
  791. Stopwatch s(Stopwatch::kUnitsCPUCycles);
  792. uint32_t kSeed = 0x12345678;
  793. Random r(kSeed);
  794. size_t kSizeArraySize = 512; // We don't want this too large, else we start getting a lot of cache effects.
  795. SizeOffset* sizeArray = new SizeOffset[kSizeArraySize];
  796. for(size_t i = 0; i < kSizeArraySize; ++i)
  797. {
  798. sizeArray[i].mOffset1 = (size_t)(uint32_t)RandomInt32UniformRange(r, 0, 32);
  799. sizeArray[i].mOffset2 = (size_t)(uint32_t)RandomInt32UniformRange(r, 0, 32);
  800. }
  801. ////////////////////////
  802. // Small copies
  803. for(size_t i = 0; i < kSizeArraySize; ++i)
  804. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 0, 256);
  805. s.Restart();
  806. for(size_t j = 0; j < 128; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  807. {
  808. for(size_t i = 0; i < kSizeArraySize; ++i)
  809. {
  810. const SizeOffset& so = sizeArray[i];
  811. MemcpyC(gMem1 + so.mOffset1, gMem2 + so.mOffset2, so.mSize);
  812. }
  813. }
  814. s.Stop();
  815. //Printf("%I64u\n", s.GetElapsedTime());
  816. ////////////////////////
  817. // Medium copies
  818. for(size_t i = 0; i < kSizeArraySize; ++i)
  819. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 256, 4096);
  820. s.Restart();
  821. for(size_t j = 0; j < 64; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  822. {
  823. for(size_t i = 0; i < kSizeArraySize; ++i)
  824. {
  825. const SizeOffset& so = sizeArray[i];
  826. MemcpyC(gMem1 + so.mOffset1, gMem2 + so.mOffset2, so.mSize);
  827. }
  828. }
  829. s.Stop();
  830. //Printf("%I64u\n", s.GetElapsedTime());
  831. ////////////////////////
  832. // Large copies
  833. for(size_t i = 0; i < kSizeArraySize; ++i)
  834. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 4096, 262144);
  835. s.Restart();
  836. for(size_t j = 0; j < 32; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  837. {
  838. for(size_t i = 0; i < kSizeArraySize; ++i)
  839. {
  840. const SizeOffset& so = sizeArray[i];
  841. MemcpyC(gMem1 + (so.mOffset1 * 8), gMem2 + (so.mOffset2 * 8), so.mSize); // Test with 8 byte alignments.
  842. }
  843. }
  844. s.Stop();
  845. //Printf("%I64u\n", s.GetElapsedTime());
  846. ////////////////////////
  847. // Giant copies
  848. for(size_t i = 0; i < kSizeArraySize; ++i)
  849. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 262144, 4194304);
  850. s.Restart();
  851. for(size_t j = 0; j < 16; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  852. {
  853. for(size_t i = 0; i < kSizeArraySize; ++i)
  854. {
  855. const SizeOffset& so = sizeArray[i];
  856. MemcpyC(gMem1 + (so.mOffset1 * 128), gMem2 + (so.mOffset2 * 128), so.mSize); // Test with 128 byte alignments.
  857. }
  858. }
  859. s.Stop();
  860. //Printf("%I64u\n", s.GetElapsedTime());
  861. delete[] sizeArray;
  862. }
  863. static void TestMemmoveSpeed()
  864. {
  865. using namespace EA::StdC;
  866. // To do.
  867. }
  868. static void TestMemsetSpeed()
  869. {
  870. using namespace EA::StdC;
  871. // To do.
  872. }
  873. static void TestMemclearSpeed()
  874. {
  875. using namespace EA::StdC;
  876. struct SizeOffset
  877. {
  878. size_t mSize;
  879. size_t mOffset1;
  880. };
  881. Stopwatch s(Stopwatch::kUnitsCPUCycles);
  882. uint32_t kSeed = 0x12345678;
  883. Random r(kSeed);
  884. size_t kSizeArraySize = 512; // We don't want this too large, else we start getting a lot of cache effects.
  885. SizeOffset* sizeArray = new SizeOffset[kSizeArraySize];
  886. for(size_t i = 0; i < kSizeArraySize; ++i)
  887. sizeArray[i].mOffset1 = (size_t)(uint32_t)RandomInt32UniformRange(r, 0, 32);
  888. ////////////////////////
  889. // Small clears
  890. for(size_t i = 0; i < kSizeArraySize; ++i)
  891. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 0, 256);
  892. s.Restart();
  893. for(size_t j = 0; j < 128; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  894. {
  895. for(size_t i = 0; i < kSizeArraySize; ++i)
  896. {
  897. const SizeOffset& so = sizeArray[i];
  898. MemclearC(gMem1 + so.mOffset1, so.mSize);
  899. // memset(gMem1 + so.mOffset1, 0, so.mSize);
  900. }
  901. }
  902. s.Stop();
  903. //Printf("%I64u\n", s.GetElapsedTime());
  904. ////////////////////////
  905. // Medium clears
  906. for(size_t i = 0; i < kSizeArraySize; ++i)
  907. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 256, 4096);
  908. s.Restart();
  909. for(size_t j = 0; j < 64; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  910. {
  911. for(size_t i = 0; i < kSizeArraySize; ++i)
  912. {
  913. const SizeOffset& so = sizeArray[i];
  914. MemclearC(gMem1 + so.mOffset1, so.mSize);
  915. // memset(gMem1 + so.mOffset1, 0, so.mSize);
  916. }
  917. }
  918. s.Stop();
  919. //Printf("%I64u\n", s.GetElapsedTime());
  920. ////////////////////////
  921. // Large clears
  922. for(size_t i = 0; i < kSizeArraySize; ++i)
  923. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 4096, 262144);
  924. s.Restart();
  925. for(size_t j = 0; j < 32; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  926. {
  927. for(size_t i = 0; i < kSizeArraySize; ++i)
  928. {
  929. const SizeOffset& so = sizeArray[i];
  930. MemclearC(gMem1 + (so.mOffset1 * 8), so.mSize); // Test with 8 byte alignments.
  931. // memset(gMem1 + (so.mOffset1 * 8), 0, so.mSize); // Test with 8 byte alignments.
  932. }
  933. }
  934. s.Stop();
  935. //Printf("%I64u\n", s.GetElapsedTime());
  936. ////////////////////////
  937. // Giant clears
  938. for(size_t i = 0; i < kSizeArraySize; ++i)
  939. sizeArray[i].mSize = (size_t)(uint32_t)RandomInt32UniformRange(r, 262144, 4194304);
  940. s.Restart();
  941. for(size_t j = 0; j < 16; ++j) // Do a double loop so that we can get a lot of copies done without sizeArray being so large that it starts having cache effects.
  942. {
  943. for(size_t i = 0; i < kSizeArraySize; ++i)
  944. {
  945. const SizeOffset& so = sizeArray[i];
  946. MemclearC(gMem1 + (so.mOffset1 * 128), so.mSize); // Test with 128 byte alignments.
  947. // memset(gMem1 + (so.mOffset1 * 128), 0, so.mSize); // Test with 128 byte alignments.
  948. }
  949. }
  950. s.Stop();
  951. //Printf("%I64u\n", s.GetElapsedTime());
  952. delete[] sizeArray;
  953. }
  954. int TestMemory()
  955. {
  956. EA::UnitTest::Report("TestMemory\n");
  957. int nErrorCount = 0;
  958. // Set up large aligned memory blocks for memory tests.
  959. // kBaseMemSize * 2 because we will set gMem1 to be kBaseMemSize bytes into pMem1Aligned so we can read bytes prior to the tested space.
  960. const size_t size = (kBaseMemSize * 2) + kBaseMemAlignment;
  961. uint8_t* pMem1Unaligned = new uint8_t[size];
  962. uint8_t* pMem2Unaligned = new uint8_t[size];
  963. if(pMem1Unaligned && pMem2Unaligned)
  964. {
  965. memset(pMem1Unaligned, kByte1, size);
  966. memset(pMem2Unaligned, kByte2, size);
  967. // Set gMem1/gMem2 to be kBaseMemSize bytes into pMem1Unaligned and be of alignment = kBaseMemAlignment.
  968. gMem1 = (uint8_t*)(((uintptr_t)pMem1Unaligned + kBaseMemSize + (kBaseMemAlignment - 1)) & ~(kBaseMemAlignment - 1));
  969. gMem2 = (uint8_t*)(((uintptr_t)pMem2Unaligned + kBaseMemSize + (kBaseMemAlignment - 1)) & ~(kBaseMemAlignment - 1));
  970. nErrorCount += TestEAAlloca();
  971. nErrorCount += TestEAMalloca();
  972. nErrorCount += TestMemset();
  973. nErrorCount += TestMemfill();
  974. nErrorCount += TestMemclear();
  975. nErrorCount += TestMemcheck();
  976. nErrorCount += TestMemchr();
  977. nErrorCount += TestMemcmp();
  978. nErrorCount += TestMemmem();
  979. nErrorCount += TestMemcpy();
  980. nErrorCount += TestMemmove();
  981. nErrorCount += TestTimingSafe();
  982. TestMemcpySpeed();
  983. TestMemmoveSpeed();
  984. TestMemsetSpeed();
  985. TestMemclearSpeed();
  986. EA_CACHE_PREFETCH_128(gMem1);
  987. EA_CACHE_ZERO_128(gMem1);
  988. delete[] pMem1Unaligned;
  989. delete[] pMem2Unaligned;
  990. }
  991. // template<size_t> StaticMemory
  992. struct MyClass{ char buffer[37]; };
  993. EA::StdC::StaticMemory<sizeof(MyClass)> mStaticMemory;
  994. MyClass* pClass = new(mStaticMemory.Memory()) MyClass;
  995. memset(pClass->buffer, 0, sizeof(pClass->buffer));
  996. EATEST_VERIFY(EA::StdC::Memcheck8(pClass->buffer, 0, sizeof(pClass->buffer)) == NULL);
  997. return nErrorCount;
  998. }
  999. #if defined(_MSC_VER)
  1000. #pragma warning(pop)
  1001. #endif