// // Copyright (c) 2009-2010 Mikko Mononen [email protected] // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages // arising from the use of this software. // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // 1. The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software // in a product, an acknowledgment in the product documentation would be // appreciated but is not required. // 2. Altered source versions must be plainly marked as such, and must not be // misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. // #ifndef RECASTALLOC_H #define RECASTALLOC_H #include "RecastAssert.h" #include <stdlib.h> #include <stdint.h> /// Provides hint values to the memory allocator on how long the /// memory is expected to be used. enum rcAllocHint { … }; /// A memory allocation function. // @param[in] size The size, in bytes of memory, to allocate. // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use. // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed. /// @see rcAllocSetCustom rcAllocFunc; /// A memory deallocation function. /// @param[in] ptr A pointer to a memory block previously allocated using #rcAllocFunc. /// @see rcAllocSetCustom rcFreeFunc; /// Sets the base custom allocation functions to be used by Recast. /// @param[in] allocFunc The memory allocation function to be used by #rcAlloc /// @param[in] freeFunc The memory de-allocation function to be used by #rcFree /// /// @see rcAlloc, rcFree void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc); /// Allocates a memory block. /// /// @param[in] size The size, in bytes of memory, to allocate. /// @param[in] hint A hint to the allocator on how long the memory is expected to be in use. /// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed. /// /// @see rcFree, rcAllocSetCustom void* rcAlloc(size_t size, rcAllocHint hint); /// Deallocates a memory block. If @p ptr is NULL, this does nothing. /// /// @warning This function leaves the value of @p ptr unchanged. So it still /// points to the same (now invalid) location, and not to null. /// /// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc. /// /// @see rcAlloc, rcAllocSetCustom void rcFree(void* ptr); /// An implementation of operator new usable for placement new. The default one is part of STL (which we don't use). /// rcNewTag is a dummy type used to differentiate our operator from the STL one, in case users import both Recast /// and STL. struct rcNewTag { … }; inline void* operator new(size_t, const rcNewTag&, void* p) { … } inline void operator delete(void*, const rcNewTag&, void*) { … } /// Signed to avoid warnnings when comparing to int loop indexes, and common error with comparing to zero. /// MSVC2010 has a bug where ssize_t is unsigned (!!!). rcSizeType; #define RC_SIZE_MAX … /// Macros to hint to the compiler about the likeliest branch. Please add a benchmark that demonstrates a performance /// improvement before introducing use cases. #if defined(__GNUC__) || defined(__clang__) #define rcLikely(x) … #define rcUnlikely(x) … #else #define rcLikely … #define rcUnlikely … #endif /// Variable-sized storage type. Mimics the interface of std::vector<T> with some notable differences: /// * Uses rcAlloc()/rcFree() to handle storage. /// * No support for a custom allocator. /// * Uses signed size instead of size_t to avoid warnings in for loops: "for (int i = 0; i < foo.size(); i++)" /// * Omits methods of limited utility: insert/erase, (bad performance), at (we don't use exceptions), operator=. /// * assign() and the pre-sizing constructor follow C++11 semantics -- they don't construct a temporary if no value is provided. /// * push_back() and resize() support adding values from the current vector. Range-based constructors and assign(begin, end) do not. /// * No specialization for bool. template <typename T, rcAllocHint H> class rcVectorBase { … }; template<typename T, rcAllocHint H> bool rcVectorBase<T, H>::reserve(rcSizeType count) { … } template <typename T, rcAllocHint H> T* rcVectorBase<T, H>::allocate_and_copy(rcSizeType size) { … } template <typename T, rcAllocHint H> void rcVectorBase<T, H>::assign(const T* begin, const T* end) { … } template <typename T, rcAllocHint H> void rcVectorBase<T, H>::push_back(const T& value) { … } template <typename T, rcAllocHint H> rcSizeType rcVectorBase<T, H>::get_new_capacity(rcSizeType min_capacity) { … } template <typename T, rcAllocHint H> void rcVectorBase<T, H>::resize_impl(rcSizeType size, const T* value) { … } template <typename T, rcAllocHint H> void rcVectorBase<T, H>::swap(rcVectorBase<T, H>& other) { … } // static template <typename T, rcAllocHint H> void rcVectorBase<T, H>::construct_range(T* begin, T* end) { … } // static template <typename T, rcAllocHint H> void rcVectorBase<T, H>::construct_range(T* begin, T* end, const T& value) { … } // static template <typename T, rcAllocHint H> void rcVectorBase<T, H>::copy_range(T* dst, const T* begin, const T* end) { … } template <typename T, rcAllocHint H> void rcVectorBase<T, H>::destroy_range(rcSizeType begin, rcSizeType end) { … } template <typename T> class rcTempVector : public rcVectorBase<T, RC_ALLOC_TEMP> { … }; template <typename T> class rcPermVector : public rcVectorBase<T, RC_ALLOC_PERM> { … }; /// Legacy class. Prefer rcVector<int>. class rcIntArray { … }; /// A simple helper class used to delete an array when it goes out of scope. /// @note This class is rarely if ever used by the end user. template<class T> class rcScopedDelete { … }; #endif