Advertisement
Guest User

Untitled

a guest
Jun 5th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #pragma once
  2. #ifndef PAGE_LOCKED_ALLOCATOR_H
  3. #define PAGE_LOCKED_ALLOCATOR_H
  4.  
  5. struct page_locked_allocator_impl
  6. {
  7. static void* allocation_impl(size_t bytes);
  8. static void deallocate_impl(void* p, size_t bytes);
  9. static void page_locked_increment(int inc);
  10. };
  11.  
  12. template <class T>
  13. struct page_locked_allocator : private page_locked_allocator_impl {
  14. typedef T value_type;
  15. page_locked_allocator() = default;
  16. template <class U> constexpr page_locked_allocator(const page_locked_allocator<U>&) noexcept
  17. {
  18.  
  19. }
  20. T* allocate(const std::size_t n)
  21. {
  22. if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
  23. {
  24. throw std::bad_alloc();
  25. }
  26. //if (auto p = static_cast<T*>(std::malloc(n * sizeof(T))))
  27. if (auto p = static_cast<T*>(page_locked_allocator_impl::allocation_impl(n * sizeof(T))))
  28. {
  29. return p;
  30. }
  31. throw std::bad_alloc();
  32. }
  33. static void deallocate(T* p, const std::size_t bytes) noexcept
  34. {
  35. //std::free(p);
  36. page_locked_allocator_impl::deallocate_impl(p, bytes);
  37. }
  38. };
  39. template <class T, class U>
  40. bool operator==(const page_locked_allocator<T>&, const page_locked_allocator<U>&) { return true; }
  41. template <class T, class U>
  42. bool operator!=(const page_locked_allocator<T>&, const page_locked_allocator<U>&) { return false; }
  43.  
  44. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement