irrAllocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
  4. #ifndef __IRR_ALLOCATOR_H_INCLUDED__
  5. #define __IRR_ALLOCATOR_H_INCLUDED__
  6. #include "irrTypes.h"
  7. #include <new>
  8. // necessary for older compilers
  9. #include <memory.h>
  10. namespace irr
  11. {
  12. namespace core
  13. {
  14. #ifdef DEBUG_CLIENTBLOCK
  15. #undef DEBUG_CLIENTBLOCK
  16. #define DEBUG_CLIENTBLOCK new
  17. #endif
  18. //! Very simple allocator implementation, containers using it can be used across dll boundaries
  19. template<typename T>
  20. class irrAllocator
  21. {
  22. public:
  23. //! Destructor
  24. virtual ~irrAllocator() {}
  25. //! Allocate memory for an array of objects
  26. T* allocate(size_t cnt)
  27. {
  28. return (T*)internal_new(cnt* sizeof(T));
  29. }
  30. //! Deallocate memory for an array of objects
  31. void deallocate(T* ptr)
  32. {
  33. internal_delete(ptr);
  34. }
  35. //! Construct an element
  36. void construct(T* ptr, const T&e)
  37. {
  38. new ((void*)ptr) T(e);
  39. }
  40. //! Destruct an element
  41. void destruct(T* ptr)
  42. {
  43. ptr->~T();
  44. }
  45. protected:
  46. virtual void* internal_new(size_t cnt)
  47. {
  48. return operator new(cnt);
  49. }
  50. virtual void internal_delete(void* ptr)
  51. {
  52. operator delete(ptr);
  53. }
  54. };
  55. //! Fast allocator, only to be used in containers inside the same memory heap.
  56. /** Containers using it are NOT able to be used it across dll boundaries. Use this
  57. when using in an internal class or function or when compiled into a static lib */
  58. template<typename T>
  59. class irrAllocatorFast
  60. {
  61. public:
  62. //! Allocate memory for an array of objects
  63. T* allocate(size_t cnt)
  64. {
  65. return (T*)operator new(cnt* sizeof(T));
  66. }
  67. //! Deallocate memory for an array of objects
  68. void deallocate(T* ptr)
  69. {
  70. operator delete(ptr);
  71. }
  72. //! Construct an element
  73. void construct(T* ptr, const T&e)
  74. {
  75. new ((void*)ptr) T(e);
  76. }
  77. //! Destruct an element
  78. void destruct(T* ptr)
  79. {
  80. ptr->~T();
  81. }
  82. };
  83. #ifdef DEBUG_CLIENTBLOCK
  84. #undef DEBUG_CLIENTBLOCK
  85. #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
  86. #endif
  87. //! defines an allocation strategy (used only by irr::array so far)
  88. enum eAllocStrategy
  89. {
  90. ALLOC_STRATEGY_SAFE = 0, // increase size by 1
  91. ALLOC_STRATEGY_DOUBLE = 1, // double size when under 500 elements, beyond that increase by 1/4th size. Plus a small constant.
  92. ALLOC_STRATEGY_SQRT = 2 // not implemented
  93. };
  94. } // end namespace core
  95. } // end namespace irr
  96. #endif