TestMemory.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. int nErrorCount = 0;
  957. // Set up large aligned memory blocks for memory tests.
  958. // kBaseMemSize * 2 because we will set gMem1 to be kBaseMemSize bytes into pMem1Aligned so we can read bytes prior to the tested space.
  959. const size_t size = (kBaseMemSize * 2) + kBaseMemAlignment;
  960. uint8_t* pMem1Unaligned = new uint8_t[size];
  961. uint8_t* pMem2Unaligned = new uint8_t[size];
  962. if(pMem1Unaligned && pMem2Unaligned)
  963. {
  964. memset(pMem1Unaligned, kByte1, size);
  965. memset(pMem2Unaligned, kByte2, size);
  966. // Set gMem1/gMem2 to be kBaseMemSize bytes into pMem1Unaligned and be of alignment = kBaseMemAlignment.
  967. gMem1 = (uint8_t*)(((uintptr_t)pMem1Unaligned + kBaseMemSize + (kBaseMemAlignment - 1)) & ~(kBaseMemAlignment - 1));
  968. gMem2 = (uint8_t*)(((uintptr_t)pMem2Unaligned + kBaseMemSize + (kBaseMemAlignment - 1)) & ~(kBaseMemAlignment - 1));
  969. nErrorCount += TestEAAlloca();
  970. nErrorCount += TestEAMalloca();
  971. nErrorCount += TestMemset();
  972. nErrorCount += TestMemfill();
  973. nErrorCount += TestMemclear();
  974. nErrorCount += TestMemcheck();
  975. nErrorCount += TestMemchr();
  976. nErrorCount += TestMemcmp();
  977. nErrorCount += TestMemmem();
  978. nErrorCount += TestMemcpy();
  979. nErrorCount += TestMemmove();
  980. nErrorCount += TestTimingSafe();
  981. TestMemcpySpeed();
  982. TestMemmoveSpeed();
  983. TestMemsetSpeed();
  984. TestMemclearSpeed();
  985. EA_CACHE_PREFETCH_128(gMem1);
  986. EA_CACHE_ZERO_128(gMem1);
  987. delete[] pMem1Unaligned;
  988. delete[] pMem2Unaligned;
  989. }
  990. // template<size_t> StaticMemory
  991. struct MyClass{ char buffer[37]; };
  992. EA::StdC::StaticMemory<sizeof(MyClass)> mStaticMemory;
  993. MyClass* pClass = new(mStaticMemory.Memory()) MyClass;
  994. memset(pClass->buffer, 0, sizeof(pClass->buffer));
  995. EATEST_VERIFY(EA::StdC::Memcheck8(pClass->buffer, 0, sizeof(pClass->buffer)) == NULL);
  996. return nErrorCount;
  997. }
  998. #if defined(_MSC_VER)
  999. #pragma warning(pop)
  1000. #endif