alignment.cpp 975 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "pls/internal/base/alignment.h"
#include "pls/internal/base/system_details.h"

namespace pls {
    namespace internal {
        namespace base {
            namespace alignment {
                void* allocate_aligned(size_t size) {
                    return aligned_alloc(system_details::CACHE_LINE_SIZE, size);
                }

                std::uintptr_t next_alignment(std::uintptr_t size) {
                    std::uintptr_t miss_alignment = size % base::system_details::CACHE_LINE_SIZE;
                    if (miss_alignment == 0) {
                        return size;
                    } else {
                        return size + (base::system_details::CACHE_LINE_SIZE - miss_alignment);
                    }
                }

                char* next_alignment(char* pointer) {
                    return reinterpret_cast<char*>(next_alignment(reinterpret_cast<std::uintptr_t >(pointer)));
                }
            }
        }
    }
}