guardedalloc_alignment_test.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. extern "C" {
  4. #include "BLI_utildefines.h"
  5. }
  6. #include "MEM_guardedalloc.h"
  7. #define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ((size_t)ptr % align, 0)
  8. namespace {
  9. void DoBasicAlignmentChecks(const int alignment)
  10. {
  11. int *foo, *bar;
  12. foo = (int *)MEM_mallocN_aligned(sizeof(int) * 10, alignment, "test");
  13. CHECK_ALIGNMENT(foo, alignment);
  14. bar = (int *)MEM_dupallocN(foo);
  15. CHECK_ALIGNMENT(bar, alignment);
  16. MEM_freeN(bar);
  17. foo = (int *)MEM_reallocN(foo, sizeof(int) * 5);
  18. CHECK_ALIGNMENT(foo, alignment);
  19. foo = (int *)MEM_recallocN(foo, sizeof(int) * 5);
  20. CHECK_ALIGNMENT(foo, alignment);
  21. MEM_freeN(foo);
  22. }
  23. } // namespace
  24. TEST(guardedalloc, LockfreeAlignedAlloc16)
  25. {
  26. DoBasicAlignmentChecks(16);
  27. }
  28. TEST(guardedalloc, GuardedAlignedAlloc16)
  29. {
  30. MEM_use_guarded_allocator();
  31. DoBasicAlignmentChecks(16);
  32. }
  33. // On Apple we currently support 16 bit alignment only.
  34. // Harmless for Blender, but would be nice to support
  35. // eventually.
  36. #ifndef __APPLE__
  37. TEST(guardedalloc, GuardedAlignedAlloc32)
  38. {
  39. MEM_use_guarded_allocator();
  40. DoBasicAlignmentChecks(32);
  41. }
  42. #endif