Merge branch 'master' into blender2.8
[blender.git] / tests / gtests / guardedalloc / guardedalloc_alignment_test.cc
1 /* Apache License, Version 2.0 */
2
3 #include "testing/testing.h"
4
5 extern "C" {
6 #include "BLI_utildefines.h"
7 }
8
9 #include "MEM_guardedalloc.h"
10
11 #define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ((size_t)ptr % align, 0)
12
13 namespace {
14
15 void DoBasicAlignmentChecks(const int alignment)
16 {
17         int *foo, *bar;
18
19         foo = (int *) MEM_mallocN_aligned(sizeof(int) * 10, alignment, "test");
20         CHECK_ALIGNMENT(foo, alignment);
21
22         bar = (int *) MEM_dupallocN(foo);
23         CHECK_ALIGNMENT(bar, alignment);
24         MEM_freeN(bar);
25
26         foo = (int *) MEM_reallocN(foo, sizeof(int) * 5);
27         CHECK_ALIGNMENT(foo, alignment);
28
29         foo = (int *) MEM_recallocN(foo, sizeof(int) * 5);
30         CHECK_ALIGNMENT(foo, alignment);
31
32         MEM_freeN(foo);
33 }
34
35 }  // namespace
36
37 TEST(guardedalloc, LockfreeAlignedAlloc16)
38 {
39         DoBasicAlignmentChecks(16);
40 }
41
42 TEST(guardedalloc, GuardedAlignedAlloc16)
43 {
44         MEM_use_guarded_allocator();
45         DoBasicAlignmentChecks(16);
46 }
47
48 // On Apple we currently support 16 bit alignment only.
49 // Harmless for Blender, but would be nice to support
50 // eventually.
51 #ifndef __APPLE__
52 TEST(guardedalloc, GuardedAlignedAlloc32)
53 {
54         MEM_use_guarded_allocator();
55         DoBasicAlignmentChecks(32);
56 }
57 #endif