78 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| 
 | |
| #pragma once
 | |
| 
 | |
| #if !defined(BAD_APPLE_OS_MEMORY_HPP_INCLUDED)
 | |
| #define BAD_APPLE_OS_MEMORY_HPP_INCLUDED
 | |
| 
 | |
| #include <cstddef>
 | |
| #include <cstring>
 | |
| #include <limits>
 | |
| #include <new>
 | |
| #include <type_traits>
 | |
| 
 | |
| namespace std
 | |
| {
 | |
| template<typename T>
 | |
| struct allocator
 | |
| {
 | |
|     using value_type = T;
 | |
|     using size_type = std::size_t;
 | |
|     using propagate_on_container_move_assignment = true_type;
 | |
| 
 | |
|     constexpr allocator() noexcept = default;
 | |
|     constexpr allocator(const allocator&) noexcept = default;
 | |
| 
 | |
|     template<typename U>
 | |
|     constexpr allocator(const allocator<U>&) noexcept {}
 | |
| 
 | |
|     [[nodiscard]] constexpr T* allocate(size_t n)
 | |
|     {
 | |
|         if (numeric_limits<size_t>::max() / sizeof(T) < n)
 | |
|         {
 | |
|             __ba_throw bad_array_new_length();
 | |
|         }
 | |
|         return static_cast<T*>(::operator new(n * sizeof(T))); // TODO: alignment for bigger types
 | |
|     }
 | |
| 
 | |
|     constexpr void deallocate(T* p, size_t n)
 | |
|     {
 | |
|         (void) n;
 | |
|         ::operator delete(p); // TODO: bigger alignments
 | |
|     }
 | |
| };
 | |
| 
 | |
| constexpr void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space) noexcept
 | |
| {
 | |
|     if (space < size) {
 | |
|         return nullptr;
 | |
|     }
 | |
|     std::size_t addr = __builtin_bit_cast(std::size_t, ptr);
 | |
|     if (addr % alignment != 0)
 | |
|     {
 | |
|         std::size_t offset = alignment + (addr % alignment);
 | |
|         if (space < size + offset)
 | |
|         {
 | |
|             return nullptr;
 | |
|         }
 | |
|         addr += offset;
 | |
|         space -= offset;
 | |
|         ptr = __builtin_bit_cast(void*, addr);
 | |
|     }
 | |
|     return ptr;
 | |
| }
 | |
| 
 | |
| #if defined(BASTL_EXTENSIONS)
 | |
| template<typename T>
 | |
| constexpr T alignUp(T value, T alignTo) noexcept
 | |
| {
 | |
|     if (value % alignTo != 0)
 | |
|     {
 | |
|         return value + alignTo - (value % alignTo);
 | |
|     }
 | |
|     return value;
 | |
| }
 | |
| #endif
 | |
| }
 | |
| 
 | |
| #endif // !defined(BAD_APPLE_OS_MEMORY_HPP_INCLUDED)
 |