2014-06-19 08:48:41 +00:00
|
|
|
/* Apache License, Version 2.0 */
|
|
|
|
|
2014-06-19 06:45:00 +00:00
|
|
|
#include "testing/testing.h"
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ(0, (size_t)ptr % align)
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
void DoBasicAlignmentChecks(const int alignment)
|
|
|
|
{
|
|
|
|
int *foo, *bar;
|
2014-06-19 06:45:00 +00:00
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
foo = (int *) MEM_mallocN_aligned(sizeof(int) * 10, alignment, "test");
|
|
|
|
CHECK_ALIGNMENT(foo, alignment);
|
2014-06-19 06:45:00 +00:00
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
bar = (int *) MEM_dupallocN(foo);
|
|
|
|
CHECK_ALIGNMENT(bar, alignment);
|
|
|
|
MEM_freeN(bar);
|
2014-06-19 06:45:00 +00:00
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
foo = (int *) MEM_reallocN(foo, sizeof(int) * 5);
|
|
|
|
CHECK_ALIGNMENT(foo, alignment);
|
2014-06-19 06:45:00 +00:00
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
foo = (int *) MEM_recallocN(foo, sizeof(int) * 5);
|
|
|
|
CHECK_ALIGNMENT(foo, alignment);
|
2014-06-19 06:45:00 +00:00
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
MEM_freeN(foo);
|
2014-06-19 06:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
TEST(guardedalloc, LockfreeAlignedAlloc16)
|
|
|
|
{
|
|
|
|
DoBasicAlignmentChecks(16);
|
2014-06-19 06:45:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 06:47:56 +00:00
|
|
|
TEST(guardedalloc, GuardedAlignedAlloc16)
|
|
|
|
{
|
|
|
|
MEM_use_guarded_allocator();
|
|
|
|
DoBasicAlignmentChecks(16);
|
2014-06-19 06:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// On Apple we currently support 16 bit alignment only.
|
|
|
|
// Harmless for Blender, but would be nice to support
|
|
|
|
// eventually.
|
|
|
|
#ifndef __APPLE__
|
2014-06-19 06:47:56 +00:00
|
|
|
TEST(guardedalloc, GuardedAlignedAlloc32)
|
|
|
|
{
|
|
|
|
MEM_use_guarded_allocator();
|
|
|
|
DoBasicAlignmentChecks(16);
|
2014-06-19 06:45:00 +00:00
|
|
|
}
|
|
|
|
#endif
|