JSONAllocator.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef JSON_ALLOCATOR_H
  2. #define JSON_ALLOCATOR_H
  3. #include "JSONStats.h"
  4. #if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
  5. #include <cstddef>
  6. //need these for the json_nothrow
  7. #include "JSONDefs/Visual_C.h"
  8. #include "JSONDefs/GNU_C.h"
  9. #include "JSONDefs/Unknown_C.h"
  10. class JSONAllocatorRelayer {
  11. public:
  12. static void * alloc(size_t bytes) json_nothrow json_hot;
  13. static void dealloc(void * ptr) json_nothrow json_hot;
  14. };
  15. template <class T> class json_allocator;
  16. // specialize for void:
  17. template <> class json_allocator<void> {
  18. public:
  19. typedef void* pointer;
  20. typedef const void* const_pointer;
  21. // reference to void members are impossible.
  22. typedef void value_type;
  23. template <class U> struct rebind { typedef json_allocator<U> other; };
  24. };
  25. template <class T> class json_allocator {
  26. public:
  27. typedef size_t size_type;
  28. typedef ptrdiff_t difference_type;
  29. typedef T* pointer;
  30. typedef const T* const_pointer;
  31. typedef T& reference;
  32. typedef const T& const_reference;
  33. typedef T value_type;
  34. template <class U> struct rebind { typedef json_allocator<U> other; };
  35. //LIBJSON_OBJECT(json_allocator);
  36. inline json_allocator() json_nothrow {
  37. //LIBJSON_CTOR;
  38. }
  39. inline json_allocator(const json_allocator&) json_nothrow {
  40. //LIBJSON_COPY_CTOR;
  41. }
  42. template <class U> inline json_allocator(const json_allocator<U>&) json_nothrow {
  43. //LIBJSON_COPY_CTOR;
  44. }
  45. inline ~json_allocator() json_nothrow {
  46. //LIBJSON_DTOR;
  47. }
  48. inline pointer address(reference x) const { return &x; }
  49. inline const_pointer address(const_reference x) const { return &x; }
  50. inline pointer allocate(size_type n, json_allocator<void>::const_pointer = 0) json_hot {
  51. return (pointer)JSONAllocatorRelayer::alloc(n * sizeof(T));
  52. }
  53. inline void deallocate(pointer p, size_type) json_hot {
  54. JSONAllocatorRelayer::dealloc(p);
  55. }
  56. inline size_type max_size() const json_nothrow { return 0xEFFFFFFF; }
  57. inline void construct(pointer p, const T& val){
  58. new(p)T(val);
  59. };
  60. inline void destroy(pointer p){
  61. ((T*)p) -> ~T();
  62. }
  63. };
  64. template <class T1, class T2> inline bool operator==(const json_allocator<T1>&, const json_allocator<T2>&) json_nothrow { return true; }
  65. template <class T1, class T2> inline bool operator!=(const json_allocator<T1>&, const json_allocator<T2>&) json_nothrow { return false; }
  66. #endif
  67. #endif