alignment.cpp 998 Bytes
Newer Older
1 2 3 4
#include "pls/internal/base/alignment.h"
#include "pls/internal/base/system_details.h"

namespace pls {
5 6 7
namespace internal {
namespace base {
namespace alignment {
8

9 10 11 12
void *allocate_aligned(size_t size) {
  return aligned_alloc(system_details::CACHE_LINE_SIZE, size);
}

13 14
system_details::pointer_t next_alignment(system_details::pointer_t size) {
  system_details::pointer_t miss_alignment = size % base::system_details::CACHE_LINE_SIZE;
15 16 17 18 19 20 21
  if (miss_alignment == 0) {
    return size;
  } else {
    return size + (base::system_details::CACHE_LINE_SIZE - miss_alignment);
  }
}

22 23 24 25 26 27 28 29 30
system_details::pointer_t previous_alignment(system_details::pointer_t size) {
  system_details::pointer_t miss_alignment = size % base::system_details::CACHE_LINE_SIZE;
  if (miss_alignment == 0) {
    return size;
  } else {
    return size - miss_alignment;
  }
}

31
char *next_alignment(char *pointer) {
32
  return reinterpret_cast<char *>(next_alignment(reinterpret_cast<system_details::pointer_t >(pointer)));
33
}
34

35 36 37
}
}
}
38
}