memset_test.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/mman.h>
  20. #include <sys/types.h>
  21. #include <memory>
  22. #include <cutils/memory.h>
  23. #include <gtest/gtest.h>
  24. #define FENCEPOST_LENGTH 8
  25. #define MAX_TEST_SIZE (64*1024)
  26. // Choose values that have no repeating byte values.
  27. #define MEMSET16_PATTERN 0xb139
  28. #define MEMSET32_PATTERN 0x48193a27
  29. enum test_e {
  30. MEMSET16 = 0,
  31. MEMSET32,
  32. };
  33. static int g_memset16_aligns[][2] = {
  34. { 2, 0 },
  35. { 4, 0 },
  36. { 8, 0 },
  37. { 16, 0 },
  38. { 32, 0 },
  39. { 64, 0 },
  40. { 128, 0 },
  41. { 4, 2 },
  42. { 8, 2 },
  43. { 8, 4 },
  44. { 8, 6 },
  45. { 128, 2 },
  46. { 128, 4 },
  47. { 128, 6 },
  48. { 128, 8 },
  49. { 128, 10 },
  50. { 128, 12 },
  51. { 128, 14 },
  52. { 128, 16 },
  53. };
  54. static int g_memset32_aligns[][2] = {
  55. { 4, 0 },
  56. { 8, 0 },
  57. { 16, 0 },
  58. { 32, 0 },
  59. { 64, 0 },
  60. { 128, 0 },
  61. { 8, 4 },
  62. { 128, 4 },
  63. { 128, 8 },
  64. { 128, 12 },
  65. { 128, 16 },
  66. };
  67. static size_t GetIncrement(size_t len, size_t min_incr) {
  68. if (len >= 4096) {
  69. return 1024;
  70. } else if (len >= 1024) {
  71. return 256;
  72. }
  73. return min_incr;
  74. }
  75. // Return a pointer into the current buffer with the specified alignment.
  76. static void *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) {
  77. uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
  78. if (alignment > 0) {
  79. // When setting the alignment, set it to exactly the alignment chosen.
  80. // The pointer returned will be guaranteed not to be aligned to anything
  81. // more than that.
  82. ptr += alignment - (ptr & (alignment - 1));
  83. ptr |= alignment | or_mask;
  84. }
  85. return reinterpret_cast<void*>(ptr);
  86. }
  87. static void SetFencepost(uint8_t *buffer) {
  88. for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
  89. buffer[i] = 0xde;
  90. buffer[i+1] = 0xad;
  91. }
  92. }
  93. static void VerifyFencepost(uint8_t *buffer) {
  94. for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
  95. if (buffer[i] != 0xde || buffer[i+1] != 0xad) {
  96. uint8_t expected_value;
  97. if (buffer[i] == 0xde) {
  98. i++;
  99. expected_value = 0xad;
  100. } else {
  101. expected_value = 0xde;
  102. }
  103. ASSERT_EQ(expected_value, buffer[i]);
  104. }
  105. }
  106. }
  107. void RunMemsetTests(test_e test_type, uint32_t value, int align[][2], size_t num_aligns) {
  108. size_t min_incr = 4;
  109. if (test_type == MEMSET16) {
  110. min_incr = 2;
  111. value |= value << 16;
  112. }
  113. std::unique_ptr<uint32_t[]> expected_buf(new uint32_t[MAX_TEST_SIZE/sizeof(uint32_t)]);
  114. for (size_t i = 0; i < MAX_TEST_SIZE/sizeof(uint32_t); i++) {
  115. expected_buf[i] = value;
  116. }
  117. // Allocate one large buffer with lots of extra space so that we can
  118. // guarantee that all possible alignments will fit.
  119. std::unique_ptr<uint8_t[]> buf(new uint8_t[3*MAX_TEST_SIZE]);
  120. uint8_t *buf_align;
  121. for (size_t i = 0; i < num_aligns; i++) {
  122. size_t incr = min_incr;
  123. for (size_t len = incr; len <= MAX_TEST_SIZE; len += incr) {
  124. incr = GetIncrement(len, min_incr);
  125. buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr(
  126. buf.get()+FENCEPOST_LENGTH, align[i][0], align[i][1]));
  127. SetFencepost(&buf_align[-FENCEPOST_LENGTH]);
  128. SetFencepost(&buf_align[len]);
  129. memset(buf_align, 0xff, len);
  130. if (test_type == MEMSET16) {
  131. android_memset16(reinterpret_cast<uint16_t*>(buf_align), value, len);
  132. } else {
  133. android_memset32(reinterpret_cast<uint32_t*>(buf_align), value, len);
  134. }
  135. ASSERT_EQ(0, memcmp(expected_buf.get(), buf_align, len))
  136. << "Failed size " << len << " align " << align[i][0] << " " << align[i][1] << "\n";
  137. VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
  138. VerifyFencepost(&buf_align[len]);
  139. }
  140. }
  141. }
  142. TEST(libcutils, android_memset16_non_zero) {
  143. RunMemsetTests(MEMSET16, MEMSET16_PATTERN, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
  144. }
  145. TEST(libcutils, android_memset16_zero) {
  146. RunMemsetTests(MEMSET16, 0, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
  147. }
  148. TEST(libcutils, android_memset32_non_zero) {
  149. RunMemsetTests(MEMSET32, MEMSET32_PATTERN, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
  150. }
  151. TEST(libcutils, android_memset32_zero) {
  152. RunMemsetTests(MEMSET32, 0, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
  153. }