RecastAlloc.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef RECASTALLOC_H
  19. #define RECASTALLOC_H
  20. /// Provides hint values to the memory allocator on how long the
  21. /// memory is expected to be used.
  22. enum rcAllocHint
  23. {
  24. RC_ALLOC_PERM, ///< Memory will persist after a function call.
  25. RC_ALLOC_TEMP ///< Memory used temporarily within a function.
  26. };
  27. /// A memory allocation function.
  28. // @param[in] size The size, in bytes of memory, to allocate.
  29. // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
  30. // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
  31. /// @see rcAllocSetCustom
  32. typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
  33. /// A memory deallocation function.
  34. /// @param[in] ptr A pointer to a memory block previously allocated using #rcAllocFunc.
  35. /// @see rcAllocSetCustom
  36. typedef void (rcFreeFunc)(void* ptr);
  37. /// Sets the base custom allocation functions to be used by Recast.
  38. /// @param[in] allocFunc The memory allocation function to be used by #rcAlloc
  39. /// @param[in] freeFunc The memory de-allocation function to be used by #rcFree
  40. void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
  41. /// Allocates a memory block.
  42. /// @param[in] size The size, in bytes of memory, to allocate.
  43. /// @param[in] hint A hint to the allocator on how long the memory is expected to be in use.
  44. /// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
  45. /// @see rcFree
  46. void* rcAlloc(int size, rcAllocHint hint);
  47. /// Deallocates a memory block.
  48. /// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc.
  49. /// @see rcAlloc
  50. void rcFree(void* ptr);
  51. /// A simple dynamic array of integers.
  52. class rcIntArray
  53. {
  54. int* m_data;
  55. int m_size, m_cap;
  56. inline rcIntArray(const rcIntArray&);
  57. inline rcIntArray& operator=(const rcIntArray&);
  58. public:
  59. /// Constructs an instance with an initial array size of zero.
  60. inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
  61. /// Constructs an instance initialized to the specified size.
  62. /// @param[in] n The initial size of the integer array.
  63. inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
  64. inline ~rcIntArray() { rcFree(m_data); }
  65. /// Specifies the new size of the integer array.
  66. /// @param[in] n The new size of the integer array.
  67. void resize(int n);
  68. /// Push the specified integer onto the end of the array and increases the size by one.
  69. /// @param[in] item The new value.
  70. inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
  71. /// Returns the value at the end of the array and reduces the size by one.
  72. /// @return The value at the end of the array.
  73. inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
  74. /// The value at the specified array index.
  75. /// @warning Does not provide overflow protection.
  76. /// @param[in] i The index of the value.
  77. inline const int& operator[](int i) const { return m_data[i]; }
  78. /// The value at the specified array index.
  79. /// @warning Does not provide overflow protection.
  80. /// @param[in] i The index of the value.
  81. inline int& operator[](int i) { return m_data[i]; }
  82. /// The current size of the integer array.
  83. inline int size() const { return m_size; }
  84. };
  85. /// A simple helper class used to delete an array when it goes out of scope.
  86. /// @note This class is rarely if ever used by the end user.
  87. template<class T> class rcScopedDelete
  88. {
  89. T* ptr;
  90. inline T* operator=(T* p);
  91. public:
  92. /// Constructs an instance with a null pointer.
  93. inline rcScopedDelete() : ptr(0) {}
  94. /// Constructs an instance with the specified pointer.
  95. /// @param[in] p An pointer to an allocated array.
  96. inline rcScopedDelete(T* p) : ptr(p) {}
  97. inline ~rcScopedDelete() { rcFree(ptr); }
  98. /// The root array pointer.
  99. /// @return The root array pointer.
  100. inline operator T*() { return ptr; }
  101. };
  102. #endif