UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RecastAlloc.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// Modified version of Recast/Detour's source file
3
4//
5// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
6//
7// This software is provided 'as-is', without any express or implied
8// warranty. In no event will the authors be held liable for any damages
9// arising from the use of this software.
10// Permission is granted to anyone to use this software for any purpose,
11// including commercial applications, and to alter it and redistribute it
12// freely, subject to the following restrictions:
13// 1. The origin of this software must not be misrepresented; you must not
14// claim that you wrote the original software. If you use this software
15// in a product, an acknowledgment in the product documentation would be
16// appreciated but is not required.
17// 2. Altered source versions must be plainly marked as such, and must not be
18// misrepresented as being the original software.
19// 3. This notice may not be removed or altered from any source distribution.
20//
21
22#ifndef RECASTALLOC_H
23#define RECASTALLOC_H
24
25#include "CoreMinimal.h"
26
34
36// @param[in] size The size, in bytes of memory, to allocate.
37// @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
38// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
40typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
41
45typedef void (rcFreeFunc)(void* ptr);
46
51
57void* rcAlloc(int size, rcAllocHint hint);
58
62void rcFree(void* ptr);
63
64void rcMemCpy(void* dst, void* src, int size);
65
68{
69 int* m_data;
70 int m_size, m_cap;
71 inline rcIntArray(const rcIntArray&);
72 inline rcIntArray& operator=(const rcIntArray&);
73public:
74
76 inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
77
80 inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
81 inline ~rcIntArray() { rcFree(m_data); }
82
85 void resize(int n);
86
89 inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
90
93 inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
94
98 inline const int& operator[](int i) const { return m_data[i]; }
99
103 inline int& operator[](int i) { return m_data[i]; }
104
106 inline int size() const { return m_size; }
107
108 // @UE BEGIN: added contains()
109 bool contains(int n) const;
110 // @UE END
111};
112
115template<class T> class rcScopedDelete
116{
117 T* ptr;
118 int size;
119 inline T* operator=(T* p);
120public:
121
123 inline rcScopedDelete() : ptr(0) {}
124 inline rcScopedDelete(int n) { ptr = (T*)rcAlloc(sizeof(T)*n, RC_ALLOC_TEMP); size = n; }
125
128 inline rcScopedDelete(T* p) : ptr(p) {}
129 inline ~rcScopedDelete() { rcFree(ptr); ptr = 0; size = 0; }
130
133 inline operator T*() { return ptr; }
134
136 bool resizeGrow(int n);
137};
138
139template<class T>
141{
142 if (n > size)
143 {
144 T* newData = (T*)rcAlloc(sizeof(T) * n, RC_ALLOC_TEMP);
145 if (n && newData) rcMemCpy(newData, ptr, sizeof(T) * size);
146 rcFree(ptr);
147 ptr = newData;
148 size = n;
149 }
150
151 return (ptr != 0);
152}
153
158template<class T>
160{
161 int itemCount;
162 T* ptr;
163
164 // on purpose made private to restrict copying
165 inline T* operator=(T* p);
166public:
167
169 inline rcScopedStructArrayDelete(const int n) : itemCount(n) { ptr = (T*)rcAlloc(sizeof(T)*n, RC_ALLOC_TEMP); }
170
172 {
173 for (int itemIndex = 0; itemIndex < itemCount; ++itemIndex)
174 {
175 ptr[itemIndex].~T();
176 }
177 rcFree(ptr);
178 }
179
182 inline operator T*() { return ptr; }
183};
184
185#endif
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
rcAllocHint
Definition RecastAlloc.h:30
@ RC_ALLOC_TEMP
Memory used temporarily within a function.
Definition RecastAlloc.h:32
@ RC_ALLOC_PERM
Memory will persist after a function call.
Definition RecastAlloc.h:31
void() rcFreeFunc(void *ptr)
Definition RecastAlloc.h:45
NAVMESH_API void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc)
Definition RecastAlloc.cpp:43
void rcFree(void *ptr)
Definition RecastAlloc.cpp:61
void *() rcAllocFunc(int size, rcAllocHint hint)
A memory allocation function.
Definition RecastAlloc.h:40
void rcMemCpy(void *dst, void *src, int size)
Definition RecastAlloc.cpp:34
void * rcAlloc(int size, rcAllocHint hint)
Definition RecastAlloc.cpp:50
char * dst
Definition lz4.h:735
A simple dynamic array of integers.
Definition RecastAlloc.h:68
void resize(int n)
Definition RecastAlloc.cpp:81
int pop()
Definition RecastAlloc.h:93
~rcIntArray()
Definition RecastAlloc.h:81
rcIntArray()
Constructs an instance with an initial array size of zero.
Definition RecastAlloc.h:76
int & operator[](int i)
Definition RecastAlloc.h:103
rcIntArray(int n)
Definition RecastAlloc.h:80
void push(int item)
Definition RecastAlloc.h:89
int size() const
The current size of the integer array.
Definition RecastAlloc.h:106
bool contains(int n) const
Definition RecastAlloc.cpp:96
const int & operator[](int i) const
Definition RecastAlloc.h:98
Definition RecastAlloc.h:116
rcScopedDelete()
Constructs an instance with a null pointer.
Definition RecastAlloc.h:123
rcScopedDelete(T *p)
Definition RecastAlloc.h:128
rcScopedDelete(int n)
Definition RecastAlloc.h:124
~rcScopedDelete()
Definition RecastAlloc.h:129
bool resizeGrow(int n)
UE: resize and copy existing memory (n = element count), doesn't destruct elements!
Definition RecastAlloc.h:140
Definition RecastAlloc.h:160
rcScopedStructArrayDelete(const int n)
Constructs an array of instances of T.
Definition RecastAlloc.h:169
~rcScopedStructArrayDelete()
Definition RecastAlloc.h:171