123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #ifndef _DEF_ALLOC_
- #define _DEF_ALLOC_ 1
- #include<iostream>
- #include<cstddef>
- #include<new>
- #include<climits>
- namespace nanxing
- {
- template<typename T>
- constexpr
- inline T* _allocate(ptrdiff_t size, T*) noexcept
- {
- T* temp;
- try
- {
- temp = (T*)(::operator new((size_t)(size * (sizeof(T)))));
- }
- catch(...)
- {
- return nullptr;
- }
- return temp;
- }
-
-
-
-
-
- template<typename _Up, typename... _Args>
- inline void _construct(_Up* __p, _Args&&... __args) noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
- {
- ::new((void *)__p) _Up(std::forward<_Args>(__args)...);
- }
-
- template<typename T>
- constexpr
- inline void _deallocate(T* buffer)
- {
- ::operator delete(buffer);
- }
- template<typename T>
- constexpr
- inline void _destory(T* ptr)
- {
- ptr->~T();
- }
- template<typename T>
- class allocator
- {
- public:
- using value_type = T;
- using pointer = T*;
- using const_pointer = const T*;
- using reference = T&;
- using size_type = size_t;
- using difference_type = ptrdiff_t;
- using const_reference = const T&;
-
- constexpr allocator() noexcept
- {
-
- }
- template<typename U> struct rebind
- {
- using other = allocator<U>;
- };
- constexpr pointer allocate(size_type n, const void* hint = 0)
- {
- return _allocate((difference_type)n, (pointer)0);
- }
-
- template<typename... _Args>
- void construct(pointer __p, _Args&&... __args) noexcept
- {
- _construct(__p,std::forward<_Args>(__args)...);
-
- }
-
-
-
-
-
- constexpr void deallocate(pointer p, size_type n=0)
- {
- _deallocate(p);
- }
- constexpr void destory(pointer p)
- {
- _destory(p);
- }
- constexpr pointer address(reference x)
- {
- return (pointer)&x;
- }
- constexpr const_pointer address(const_reference x)
- {
- return (const_pointer)&x;
- }
-
- constexpr size_type max_size()const
- {
- return size_type(UINT_MAX / sizeof(T));
- }
- };
- }
- #endif
|