37 #include "os/memory.h" 38 #include "error_macros.h" 39 #include "safe_refcount.h" 57 _FORCE_INLINE_
int* _get_size()
const {
61 return reinterpret_cast<int*
>((uint8_t*)_ptr-
sizeof(
int));
64 _FORCE_INLINE_ T* _get_data()
const {
68 return reinterpret_cast<T*
>(_ptr);
72 _FORCE_INLINE_
size_t _get_alloc_size(
size_t p_elements)
const {
74 return nearest_power_of_2(p_elements*
sizeof(T)+
sizeof(
SafeRefCount)+
sizeof(
int));
77 _FORCE_INLINE_
bool _get_alloc_size_checked(
size_t p_elements,
size_t *out)
const {
78 #if defined(_add_overflow) && defined(_mul_overflow) 81 if (_mul_overflow(p_elements,
sizeof(T), &o))
return false;
82 if (_add_overflow(o,
sizeof(
SafeRefCount)+
sizeof(
int), &p))
return false;
83 *out = nearest_power_of_2(p);
88 *out = _get_alloc_size(p_elements);
93 void _unref(
void *p_data);
95 void _copy_from(
const Vector& p_from);
96 void _copy_on_write();
100 _FORCE_INLINE_ T *ptr() {
if (!_ptr)
return NULL; _copy_on_write();
return (T*)_get_data(); }
101 _FORCE_INLINE_
const T *ptr()
const {
if (!_ptr)
return NULL;
return _get_data(); }
104 _FORCE_INLINE_
void clear() { resize(0); }
106 _FORCE_INLINE_
int size()
const {
107 int* size = _get_size();
113 _FORCE_INLINE_
bool empty()
const {
return _ptr == 0; }
114 Error resize(
int p_size);
115 bool push_back(T p_elem);
117 void remove(
int p_index);
118 void erase(
const T& p_val) {
int idx = find(p_val);
if (idx>=0)
remove(idx); };
122 template <
class T_val>
123 int find(
const T_val& p_val)
const;
125 void set(
int p_index,T p_elem);
126 T
get(
int p_index)
const;
128 inline T& operator[](
int p_index) {
130 if (p_index<0 || p_index>=size()) {
132 ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
137 return _get_data()[p_index];
140 inline const T& operator[](
int p_index)
const {
142 if (p_index<0 || p_index>=size()) {
143 const T& aux=*((T*)0);
144 ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
147 return _get_data()[p_index];
150 Error insert(
int p_pos,
const T& p_val);
158 T *data = &operator[](0);
160 sorter.sort(data,len);
165 sort_custom<_DefaultComparator<T> >();
168 void ordered_insert(
const T& p_val) {
170 for (i=0; i<size(); i++) {
172 if (p_val <
operator[](i)) {
179 void operator=(
const Vector& p_from);
183 _FORCE_INLINE_ ~Vector();
199 int *count = (
int*)(src+1);
200 T *data = (T*)(count+1);
202 for (
int i=0;i<*count;i++) {
208 memfree((uint8_t*)p_data-
sizeof(
int)-
sizeof(
SafeRefCount));
218 if (_get_refcount()->
get() > 1 ) {
220 void* mem_new = memalloc(_get_alloc_size(*_get_size()));
223 int * _size = (
int*)(src_new+1);
226 T*_data=(T*)(_size+1);
229 for (
int i=0;i<*_size;i++) {
231 memnew_placement(&_data[i], T( _get_data()[i] ) );
240 template<
class T>
template<
class T_val>
247 for (
int i=0; i<size(); i++) {
249 if (
operator[](i) == p_val) {
261 ERR_FAIL_COND_V(p_size<0,ERR_INVALID_PARAMETER);
277 ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
283 void* ptr=memalloc(alloc_size);
284 ERR_FAIL_COND_V( !ptr ,ERR_OUT_OF_MEMORY);
285 _ptr=(T*)((uint8_t*)ptr+
sizeof(int)+
sizeof(
SafeRefCount));
286 _get_refcount()->init();
290 void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-
sizeof(int)-
sizeof(
SafeRefCount), alloc_size);
291 ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
292 _ptr=(T*)((uint8_t*)_ptrnew+
sizeof(int)+
sizeof(
SafeRefCount));
296 T*elems = _get_data();
298 for (
int i=*_get_size();i<p_size;i++) {
300 memnew_placement(&elems[i], T) ;
305 }
else if (p_size<size()) {
308 for (
int i=p_size;i<*_get_size();i++) {
310 T* t = &_get_data()[i];
314 void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-
sizeof(int)-
sizeof(
SafeRefCount), alloc_size);
315 ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY);
317 _ptr=(T*)((uint8_t*)_ptrnew+
sizeof(int)+
sizeof(
SafeRefCount));
330 for(
int i=0;i<size()/2;i++) {
332 SWAP(
operator[](i),
operator[](size()-i-1) );
339 operator[](p_index)=p_elem;
345 return operator[](p_index);
351 Error err = resize(size()+1);
352 ERR_FAIL_COND_V( err,
true )
353 set(size()-1,p_elem);
362 ERR_FAIL_INDEX(p_index, size());
365 for (
int i=p_index; i<len-1; i++) {
376 if (_ptr == p_from._ptr)
385 if (p_from._get_refcount()->ref())
400 ERR_FAIL_INDEX_V(p_pos,size()+1,ERR_INVALID_PARAMETER);
402 for (
int i=(size()-1);i>p_pos;i--)
413 _copy_from( p_from );
Definition: safe_refcount.h:336