blob: 309a3df6a834eaef6736730b11c95f396dbd338d [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- 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// ---
32// Author: Sanjay Ghemawat
33//
34// Check memalign related routines.
35//
36// We can't really do a huge amount of checking, but at the very
37// least, the following code checks that return values are properly
38// aligned, and that writing into the objects works.
39
40#include "config_for_unittests.h"
41
42// Complicated ordering requirements. tcmalloc.h defines (indirectly)
43// _POSIX_C_SOURCE, which it needs so stdlib.h defines posix_memalign.
44// unistd.h, on the other hand, requires _POSIX_C_SOURCE to be unset,
45// at least on Mac OS X, in order to define getpagesize. The solution
46// is to #include unistd.h first. This is safe because unistd.h
47// doesn't sub-include stdlib.h, so we'll still get posix_memalign
48// when we #include stdlib.h. Blah.
49#ifdef HAVE_UNISTD_H
50#include <unistd.h> // for getpagesize()
51#endif
52#include "tcmalloc.h" // must come early, to pick up posix_memalign
53#include <assert.h>
54#include <stdlib.h> // defines posix_memalign
55#include <stdio.h> // for the printf at the end
56#ifdef HAVE_STDINT_H
57#include <stdint.h> // for uintptr_t
58#endif
59#ifdef HAVE_UNISTD_H
60#include <unistd.h> // for getpagesize()
61#endif
62// Malloc can be in several places on older versions of OS X.
63#if defined(HAVE_MALLOC_H)
64#include <malloc.h> // for memalign() and valloc()
65#elif defined(HAVE_MALLOC_MALLOC_H)
66#include <malloc/malloc.h>
67#elif defined(HAVE_SYS_MALLOC_H)
68#include <sys/malloc.h>
69#endif
70#include "base/basictypes.h"
71#include "base/logging.h"
72#include "tests/testutil.h"
73
74
75// Return the next interesting size/delta to check. Returns -1 if no more.
76static int NextSize(int size) {
77 if (size < 100) {
78 return size+1;
79 } else if (size < 1048576) {
80 // Find next power of two
81 int power = 1;
82 while (power < size) {
83 power <<= 1;
84 }
85
86 // Yield (power-1, power, power+1)
87 if (size < power-1) {
88 return power-1;
89 } else if (size == power-1) {
90 return power;
91 } else {
92 assert(size == power);
93 return power+1;
94 }
95 } else {
96 return -1;
97 }
98}
99
100// Shortform for cast
101static uintptr_t Number(void* p) {
102 return reinterpret_cast<uintptr_t>(p);
103}
104
105// Check alignment
106static void CheckAlignment(void* p, int align) {
107 if ((Number(p) & (align-1)) != 0)
108 LOG(FATAL, "wrong alignment; wanted 0x%x; got %p\n", align, p);
109}
110
111// Fill a buffer of the specified size with a predetermined pattern
112static void Fill(void* p, int n, char seed) {
113 unsigned char* buffer = reinterpret_cast<unsigned char*>(p);
114 for (int i = 0; i < n; i++) {
115 buffer[i] = ((seed + i) & 0xff);
116 }
117}
118
119// Check that the specified buffer has the predetermined pattern
120// generated by Fill()
121static bool Valid(const void* p, int n, char seed) {
122 const unsigned char* buffer = reinterpret_cast<const unsigned char*>(p);
123 for (int i = 0; i < n; i++) {
124 if (buffer[i] != ((seed + i) & 0xff)) {
125 return false;
126 }
127 }
128 return true;
129}
130
131int main(int argc, char** argv) {
132 SetTestResourceLimit();
133
134 // Try allocating data with a bunch of alignments and sizes
135 for (int a = 1; a < 1048576; a *= 2) {
136 for (int s = 0; s != -1; s = NextSize(s)) {
137 void* ptr = memalign(a, s);
138 CheckAlignment(ptr, a);
139 Fill(ptr, s, 'x');
140 CHECK(Valid(ptr, s, 'x'));
141 free(ptr);
142
143 if ((a >= sizeof(void*)) && ((a & (a-1)) == 0)) {
144 CHECK(posix_memalign(&ptr, a, s) == 0);
145 CheckAlignment(ptr, a);
146 Fill(ptr, s, 'y');
147 CHECK(Valid(ptr, s, 'y'));
148 free(ptr);
149 }
150 }
151 }
152
153 {
154 // Check various corner cases
155 void* p1 = memalign(1<<20, 1<<19);
156 void* p2 = memalign(1<<19, 1<<19);
157 void* p3 = memalign(1<<21, 1<<19);
158 CheckAlignment(p1, 1<<20);
159 CheckAlignment(p2, 1<<19);
160 CheckAlignment(p3, 1<<21);
161 Fill(p1, 1<<19, 'a');
162 Fill(p2, 1<<19, 'b');
163 Fill(p3, 1<<19, 'c');
164 CHECK(Valid(p1, 1<<19, 'a'));
165 CHECK(Valid(p2, 1<<19, 'b'));
166 CHECK(Valid(p3, 1<<19, 'c'));
167 free(p1);
168 free(p2);
169 free(p3);
170 }
171
172 {
173 // posix_memalign
174 void* ptr;
175 CHECK(posix_memalign(&ptr, 0, 1) == EINVAL);
176 CHECK(posix_memalign(&ptr, sizeof(void*)/2, 1) == EINVAL);
177 CHECK(posix_memalign(&ptr, sizeof(void*)+1, 1) == EINVAL);
178 CHECK(posix_memalign(&ptr, 4097, 1) == EINVAL);
179
180 // Grab some memory so that the big allocation below will definitely fail.
181 void* p_small = malloc(4*1048576);
182 CHECK(p_small != NULL);
183
184 // Make sure overflow is returned as ENOMEM
185 const size_t zero = 0;
186 static const size_t kMinusNTimes = 10;
187 for ( size_t i = 1; i < kMinusNTimes; ++i ) {
188 int r = posix_memalign(&ptr, 1024, zero - i);
189 CHECK(r == ENOMEM);
190 }
191
192 free(p_small);
193 }
194
195 const int pagesize = getpagesize();
196 {
197 // valloc
198 for (int s = 0; s != -1; s = NextSize(s)) {
199 void* p = valloc(s);
200 CheckAlignment(p, pagesize);
201 Fill(p, s, 'v');
202 CHECK(Valid(p, s, 'v'));
203 free(p);
204 }
205 }
206
207 {
208 // pvalloc
209 for (int s = 0; s != -1; s = NextSize(s)) {
210 void* p = pvalloc(s);
211 CheckAlignment(p, pagesize);
212 int alloc_needed = ((s + pagesize - 1) / pagesize) * pagesize;
213 Fill(p, alloc_needed, 'x');
214 CHECK(Valid(p, alloc_needed, 'x'));
215 free(p);
216 }
217 }
218
219 printf("PASS\n");
220 return 0;
221}