memalign_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2004, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. //
  33. // Check memalign related routines.
  34. //
  35. // We can't really do a huge amount of checking, but at the very
  36. // least, the following code checks that return values are properly
  37. // aligned, and that writing into the objects works.
  38. #include "config_for_unittests.h"
  39. // Complicated ordering requirements. tcmalloc.h defines (indirectly)
  40. // _POSIX_C_SOURCE, which it needs so stdlib.h defines posix_memalign.
  41. // unistd.h, on the other hand, requires _POSIX_C_SOURCE to be unset,
  42. // at least on Mac OS X, in order to define getpagesize. The solution
  43. // is to #include unistd.h first. This is safe because unistd.h
  44. // doesn't sub-include stdlib.h, so we'll still get posix_memalign
  45. // when we #include stdlib.h. Blah.
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h> // for getpagesize()
  48. #endif
  49. #include "tcmalloc.h" // must come early, to pick up posix_memalign
  50. #include <assert.h>
  51. #include <stdlib.h> // defines posix_memalign
  52. #include <stdio.h> // for the printf at the end
  53. #ifdef HAVE_STDINT_H
  54. #include <stdint.h> // for uintptr_t
  55. #endif
  56. #ifdef HAVE_UNISTD_H
  57. #include <unistd.h> // for getpagesize()
  58. #endif
  59. // Malloc can be in several places on older versions of OS X.
  60. #if defined(HAVE_MALLOC_H)
  61. #include <malloc.h> // for memalign() and valloc()
  62. #elif defined(HAVE_MALLOC_MALLOC_H)
  63. #include <malloc/malloc.h>
  64. #elif defined(HAVE_SYS_MALLOC_H)
  65. #include <sys/malloc.h>
  66. #endif
  67. #include "base/basictypes.h"
  68. #include "base/logging.h"
  69. #include "tests/testutil.h"
  70. // Return the next interesting size/delta to check. Returns -1 if no more.
  71. static int NextSize(int size) {
  72. if (size < 100) {
  73. return size+1;
  74. } else if (size < 1048576) {
  75. // Find next power of two
  76. int power = 1;
  77. while (power < size) {
  78. power <<= 1;
  79. }
  80. // Yield (power-1, power, power+1)
  81. if (size < power-1) {
  82. return power-1;
  83. } else if (size == power-1) {
  84. return power;
  85. } else {
  86. assert(size == power);
  87. return power+1;
  88. }
  89. } else {
  90. return -1;
  91. }
  92. }
  93. // Shortform for cast
  94. static uintptr_t Number(void* p) {
  95. return reinterpret_cast<uintptr_t>(p);
  96. }
  97. // Check alignment
  98. static void CheckAlignment(void* p, int align) {
  99. if ((Number(p) & (align-1)) != 0)
  100. LOG(FATAL, "wrong alignment; wanted 0x%x; got %p\n", align, p);
  101. }
  102. // Fill a buffer of the specified size with a predetermined pattern
  103. static void Fill(void* p, int n, char seed) {
  104. unsigned char* buffer = reinterpret_cast<unsigned char*>(p);
  105. for (int i = 0; i < n; i++) {
  106. buffer[i] = ((seed + i) & 0xff);
  107. }
  108. }
  109. // Check that the specified buffer has the predetermined pattern
  110. // generated by Fill()
  111. static bool Valid(const void* p, int n, char seed) {
  112. const unsigned char* buffer = reinterpret_cast<const unsigned char*>(p);
  113. for (int i = 0; i < n; i++) {
  114. if (buffer[i] != ((seed + i) & 0xff)) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. int main(int argc, char** argv) {
  121. SetTestResourceLimit();
  122. // Try allocating data with a bunch of alignments and sizes
  123. for (int a = 1; a < 1048576; a *= 2) {
  124. for (int s = 0; s != -1; s = NextSize(s)) {
  125. void* ptr = memalign(a, s);
  126. CheckAlignment(ptr, a);
  127. Fill(ptr, s, 'x');
  128. CHECK(Valid(ptr, s, 'x'));
  129. free(ptr);
  130. if ((a >= sizeof(void*)) && ((a & (a-1)) == 0)) {
  131. CHECK(posix_memalign(&ptr, a, s) == 0);
  132. CheckAlignment(ptr, a);
  133. Fill(ptr, s, 'y');
  134. CHECK(Valid(ptr, s, 'y'));
  135. free(ptr);
  136. }
  137. }
  138. }
  139. {
  140. // Check various corner cases
  141. void* p1 = memalign(1<<20, 1<<19);
  142. void* p2 = memalign(1<<19, 1<<19);
  143. void* p3 = memalign(1<<21, 1<<19);
  144. CheckAlignment(p1, 1<<20);
  145. CheckAlignment(p2, 1<<19);
  146. CheckAlignment(p3, 1<<21);
  147. Fill(p1, 1<<19, 'a');
  148. Fill(p2, 1<<19, 'b');
  149. Fill(p3, 1<<19, 'c');
  150. CHECK(Valid(p1, 1<<19, 'a'));
  151. CHECK(Valid(p2, 1<<19, 'b'));
  152. CHECK(Valid(p3, 1<<19, 'c'));
  153. free(p1);
  154. free(p2);
  155. free(p3);
  156. }
  157. {
  158. // posix_memalign
  159. void* ptr;
  160. CHECK(posix_memalign(&ptr, 0, 1) == EINVAL);
  161. CHECK(posix_memalign(&ptr, sizeof(void*)/2, 1) == EINVAL);
  162. CHECK(posix_memalign(&ptr, sizeof(void*)+1, 1) == EINVAL);
  163. CHECK(posix_memalign(&ptr, 4097, 1) == EINVAL);
  164. // Grab some memory so that the big allocation below will definitely fail.
  165. void* p_small = malloc(4*1048576);
  166. CHECK(p_small != NULL);
  167. // Make sure overflow is returned as ENOMEM
  168. const size_t zero = 0;
  169. static const size_t kMinusNTimes = 10;
  170. for ( size_t i = 1; i < kMinusNTimes; ++i ) {
  171. int r = posix_memalign(&ptr, 1024, zero - i);
  172. CHECK(r == ENOMEM);
  173. }
  174. free(p_small);
  175. }
  176. const int pagesize = getpagesize();
  177. {
  178. // valloc
  179. for (int s = 0; s != -1; s = NextSize(s)) {
  180. void* p = valloc(s);
  181. CheckAlignment(p, pagesize);
  182. Fill(p, s, 'v');
  183. CHECK(Valid(p, s, 'v'));
  184. free(p);
  185. }
  186. }
  187. {
  188. // pvalloc
  189. for (int s = 0; s != -1; s = NextSize(s)) {
  190. void* p = pvalloc(s);
  191. CheckAlignment(p, pagesize);
  192. int alloc_needed = ((s + pagesize - 1) / pagesize) * pagesize;
  193. Fill(p, alloc_needed, 'x');
  194. CHECK(Valid(p, alloc_needed, 'x'));
  195. free(p);
  196. }
  197. }
  198. printf("PASS\n");
  199. return 0;
  200. }