Advertisement
Gistrec

Nano STL Allocator

May 3rd, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #ifndef NANOSTL_ALLOCATOR_H_
  2. #define NANOSTL_ALLOCATOR_H_
  3.  
  4. #ifdef NANOSTL_DEBUG
  5. #include <iostream>
  6. #endif
  7.  
  8. namespace nanostl {
  9.  
  10. typedef unsigned long long size_type;
  11.  
  12. #ifdef __clang__
  13. #pragma clang diagnostic push
  14. #if __has_warning("-Wzero-as-null-pointer-constant")
  15. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  16. #endif
  17. #endif
  18.  
  19. ///
  20. /// allocator class implementaion without libc function
  21. ///
  22. template <typename T>
  23. class allocator {
  24.  public:
  25.   typedef T value_type;
  26.   typedef T* pointer;
  27.   typedef const T* const_pointer;
  28.   typedef T& reference;
  29.   typedef const T& const_reference;
  30.  
  31.   allocator() {}
  32.  
  33.   T* allocate(size_type n, const void* hint = 0) {
  34.     (void)hint;  // Ignore `hint' for a while.
  35.     if (n < 1) {
  36.       return 0;
  37.     }
  38.  
  39. #ifdef NANOSTL_DEBUG
  40.     std::cerr << "allocator::allocate: n " << n << std::endl;
  41. #endif
  42.  
  43.     return new T[n];
  44.   }
  45.  
  46.   void deallocate(T* p, size_type n) {
  47.     (void)n;
  48.     delete[] p;
  49.   }
  50.  
  51.  private:
  52. };
  53.  
  54. #ifdef __clang__
  55. #pragma clang diagnostic pop
  56. #endif
  57.  
  58. }  // namespace nanostl
  59.  
  60. #endif  // NANOSTL_ALLOCATOR_H_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement