RecastAlloc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include <stddef.h>
  21. /// Provides hint values to the memory allocator on how long the
  22. /// memory is expected to be used.
  23. enum rcAllocHint
  24. {
  25. RC_ALLOC_PERM, ///< Memory will persist after a function call.
  26. RC_ALLOC_TEMP ///< Memory used temporarily within a function.
  27. };
  28. /// A memory allocation function.
  29. // @param[in] size The size, in bytes of memory, to allocate.
  30. // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
  31. // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
  32. /// @see rcAllocSetCustom
  33. typedef void* (rcAllocFunc)(size_t size, rcAllocHint hint);
  34. /// A memory deallocation function.
  35. /// @param[in] ptr A pointer to a memory block previously allocated using #rcAllocFunc.
  36. /// @see rcAllocSetCustom
  37. typedef void (rcFreeFunc)(void* ptr);
  38. /// Sets the base custom allocation functions to be used by Recast.
  39. /// @param[in] allocFunc The memory allocation function to be used by #rcAlloc
  40. /// @param[in] freeFunc The memory de-allocation function to be used by #rcFree
  41. void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
  42. /// Allocates a memory block.
  43. /// @param[in] size The size, in bytes of memory, to allocate.
  44. /// @param[in] hint A hint to the allocator on how long the memory is expected to be in use.
  45. /// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
  46. /// @see rcFree
  47. void* rcAlloc(size_t size, rcAllocHint hint);
  48. /// Deallocates a memory block.
  49. /// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc.
  50. /// @see rcAlloc
  51. void rcFree(void* ptr);
  52. /// A simple dynamic array of integers.
  53. class rcIntArray
  54. {
  55. int* m_data;
  56. int m_size, m_cap;
  57. void doResize(int n);
  58. // Explicitly disabled copy constructor and copy assignment operator.
  59. rcIntArray(const rcIntArray&);
  60. rcIntArray& operator=(const rcIntArray&);
  61. public:
  62. /// Constructs an instance with an initial array size of zero.
  63. rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
  64. /// Constructs an instance initialized to the specified size.
  65. /// @param[in] n The initial size of the integer array.
  66. rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
  67. ~rcIntArray() { rcFree(m_data); }
  68. /// Specifies the new size of the integer array.
  69. /// @param[in] n The new size of the integer array.
  70. void resize(int n)
  71. {
  72. if (n > m_cap)
  73. doResize(n);
  74. m_size = n;
  75. }
  76. /// Push the specified integer onto the end of the array and increases the size by one.
  77. /// @param[in] item The new value.
  78. void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
  79. /// Returns the value at the end of the array and reduces the size by one.
  80. /// @return The value at the end of the array.
  81. int pop()
  82. {
  83. if (m_size > 0)
  84. m_size--;
  85. return m_data[m_size];
  86. }
  87. /// The value at the specified array index.
  88. /// @warning Does not provide overflow protection.
  89. /// @param[in] i The index of the value.
  90. const int& operator[](int i) const { return m_data[i]; }
  91. /// The value at the specified array index.
  92. /// @warning Does not provide overflow protection.
  93. /// @param[in] i The index of the value.
  94. int& operator[](int i) { return m_data[i]; }
  95. /// The current size of the integer array.
  96. int size() const { return m_size; }
  97. };
  98. /// A simple helper class used to delete an array when it goes out of scope.
  99. /// @note This class is rarely if ever used by the end user.
  100. template<class T> class rcScopedDelete
  101. {
  102. T* ptr;
  103. public:
  104. /// Constructs an instance with a null pointer.
  105. inline rcScopedDelete() : ptr(0) {}
  106. /// Constructs an instance with the specified pointer.
  107. /// @param[in] p An pointer to an allocated array.
  108. inline rcScopedDelete(T* p) : ptr(p) {}
  109. inline ~rcScopedDelete() { rcFree(ptr); }
  110. /// The root array pointer.
  111. /// @return The root array pointer.
  112. inline operator T*() { return ptr; }
  113. private:
  114. // Explicitly disabled copy constructor and copy assignment operator.
  115. rcScopedDelete(const rcScopedDelete&);
  116. rcScopedDelete& operator=(const rcScopedDelete&);
  117. };
  118. #endif