Allocator.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "GarbageCollector.h"
  3. namespace il2cpp
  4. {
  5. namespace gc
  6. {
  7. template<typename T>
  8. class Allocator
  9. {
  10. public:
  11. typedef size_t size_type;
  12. typedef ptrdiff_t difference_type;
  13. typedef T* pointer;
  14. typedef const T* const_pointer;
  15. typedef T& reference;
  16. typedef const T& const_reference;
  17. typedef T value_type;
  18. typedef Allocator<T> allocator_type;
  19. Allocator() {}
  20. Allocator(const Allocator&) {}
  21. pointer allocate(size_type n, const void * = 0)
  22. {
  23. T* t = (T*)GarbageCollector::AllocateFixed(n * sizeof(T), 0);
  24. return t;
  25. }
  26. void deallocate(void* p, size_type)
  27. {
  28. if (p)
  29. {
  30. GarbageCollector::FreeFixed(p);
  31. }
  32. }
  33. pointer address(reference x) const { return &x; }
  34. const_pointer address(const_reference x) const { return &x; }
  35. Allocator<T>& operator=(const Allocator&) { return *this; }
  36. void construct(pointer p, const T& val) { new((T*)p)T(val); }
  37. void destroy(pointer p) { p->~T(); }
  38. size_type max_size() const { return size_t(-1); }
  39. template<class U>
  40. struct rebind { typedef Allocator<U> other; };
  41. template<class U>
  42. Allocator(const Allocator<U>&) {}
  43. template<class U>
  44. Allocator& operator=(const Allocator<U>&) { return *this; }
  45. };
  46. }
  47. }