This source file includes following definitions.
- NextSize
- Number
- CheckAlignment
- Fill
- Valid
- main
#include "config_for_unittests.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "tcmalloc.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(HAVE_MALLOC_H)
#include <malloc.h>
#elif defined(HAVE_MALLOC_MALLOC_H)
#include <malloc/malloc.h>
#elif defined(HAVE_SYS_MALLOC_H)
#include <sys/malloc.h>
#endif
#include "base/basictypes.h"
#include "base/logging.h"
#include "tests/testutil.h"
static int NextSize(int size) {
if (size < 100) {
return size+1;
} else if (size < 1048576) {
int power = 1;
while (power < size) {
power <<= 1;
}
if (size < power-1) {
return power-1;
} else if (size == power-1) {
return power;
} else {
assert(size == power);
return power+1;
}
} else {
return -1;
}
}
static uintptr_t Number(void* p) {
return reinterpret_cast<uintptr_t>(p);
}
static void CheckAlignment(void* p, int align) {
if ((Number(p) & (align-1)) != 0)
LOG(FATAL, "wrong alignment; wanted 0x%x; got %p\n", align, p);
}
static void Fill(void* p, int n, char seed) {
unsigned char* buffer = reinterpret_cast<unsigned char*>(p);
for (int i = 0; i < n; i++) {
buffer[i] = ((seed + i) & 0xff);
}
}
static bool Valid(const void* p, int n, char seed) {
const unsigned char* buffer = reinterpret_cast<const unsigned char*>(p);
for (int i = 0; i < n; i++) {
if (buffer[i] != ((seed + i) & 0xff)) {
return false;
}
}
return true;
}
int main(int argc, char** argv) {
SetTestResourceLimit();
for (int a = 1; a < 1048576; a *= 2) {
for (int s = 0; s != -1; s = NextSize(s)) {
void* ptr = memalign(a, s);
CheckAlignment(ptr, a);
Fill(ptr, s, 'x');
CHECK(Valid(ptr, s, 'x'));
free(ptr);
if ((a >= sizeof(void*)) && ((a & (a-1)) == 0)) {
CHECK(posix_memalign(&ptr, a, s) == 0);
CheckAlignment(ptr, a);
Fill(ptr, s, 'y');
CHECK(Valid(ptr, s, 'y'));
free(ptr);
}
}
}
{
void* p1 = memalign(1<<20, 1<<19);
void* p2 = memalign(1<<19, 1<<19);
void* p3 = memalign(1<<21, 1<<19);
CheckAlignment(p1, 1<<20);
CheckAlignment(p2, 1<<19);
CheckAlignment(p3, 1<<21);
Fill(p1, 1<<19, 'a');
Fill(p2, 1<<19, 'b');
Fill(p3, 1<<19, 'c');
CHECK(Valid(p1, 1<<19, 'a'));
CHECK(Valid(p2, 1<<19, 'b'));
CHECK(Valid(p3, 1<<19, 'c'));
free(p1);
free(p2);
free(p3);
}
{
void* ptr;
CHECK(posix_memalign(&ptr, 0, 1) == EINVAL);
CHECK(posix_memalign(&ptr, sizeof(void*)/2, 1) == EINVAL);
CHECK(posix_memalign(&ptr, sizeof(void*)+1, 1) == EINVAL);
CHECK(posix_memalign(&ptr, 4097, 1) == EINVAL);
void* p_small = malloc(4*1048576);
CHECK(p_small != NULL);
const size_t zero = 0;
static const size_t kMinusNTimes = 10;
for ( size_t i = 1; i < kMinusNTimes; ++i ) {
int r = posix_memalign(&ptr, 1024, zero - i);
CHECK(r == ENOMEM);
}
free(p_small);
}
const int pagesize = getpagesize();
{
for (int s = 0; s != -1; s = NextSize(s)) {
void* p = valloc(s);
CheckAlignment(p, pagesize);
Fill(p, s, 'v');
CHECK(Valid(p, s, 'v'));
free(p);
}
}
{
for (int s = 0; s != -1; s = NextSize(s)) {
void* p = pvalloc(s);
CheckAlignment(p, pagesize);
int alloc_needed = ((s + pagesize - 1) / pagesize) * pagesize;
Fill(p, alloc_needed, 'x');
CHECK(Valid(p, alloc_needed, 'x'));
free(p);
}
}
printf("PASS\n");
return 0;
}