| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  cowdata.h                                                            */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #ifndef COWDATA_H
 | 
					
						
							|  |  |  | #define COWDATA_H
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/error/error_macros.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/os/memory.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/templates/safe_refcount.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | class Vector; | 
					
						
							|  |  |  | class String; | 
					
						
							| 
									
										
										
										
											2020-07-27 13:43:20 +03:00
										 |  |  | class Char16String; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | class CharString; | 
					
						
							|  |  |  | template <class T, class V> | 
					
						
							|  |  |  | class VMap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | #if !defined(NO_THREADS)
 | 
					
						
							|  |  |  | SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t) | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | class CowData { | 
					
						
							|  |  |  | 	template <class TV> | 
					
						
							|  |  |  | 	friend class Vector; | 
					
						
							|  |  |  | 	friend class String; | 
					
						
							| 
									
										
										
										
											2020-07-27 13:43:20 +03:00
										 |  |  | 	friend class Char16String; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	friend class CharString; | 
					
						
							|  |  |  | 	template <class TV, class VV> | 
					
						
							|  |  |  | 	friend class VMap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 	mutable T *_ptr = nullptr; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// internal helpers
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 	_FORCE_INLINE_ SafeNumeric<uint32_t> *_get_refcount() const { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (!_ptr) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return nullptr; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 		return reinterpret_cast<SafeNumeric<uint32_t> *>(_ptr) - 2; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ uint32_t *_get_size() const { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (!_ptr) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return nullptr; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return reinterpret_cast<uint32_t *>(_ptr) - 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ T *_get_data() const { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (!_ptr) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return nullptr; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return reinterpret_cast<T *>(_ptr); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { | 
					
						
							|  |  |  | 		return next_power_of_2(p_elements * sizeof(T)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { | 
					
						
							| 
									
										
										
										
											2020-03-11 21:01:02 +01:00
										 |  |  | #if defined(__GNUC__)
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		size_t o; | 
					
						
							|  |  |  | 		size_t p; | 
					
						
							| 
									
										
										
										
											2020-03-11 21:01:02 +01:00
										 |  |  | 		if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) { | 
					
						
							| 
									
										
										
										
											2018-10-04 18:54:20 +02:00
										 |  |  | 			*out = 0; | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		*out = next_power_of_2(o); | 
					
						
							| 
									
										
										
										
											2020-03-11 21:01:02 +01:00
										 |  |  | 		if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) { | 
					
						
							|  |  |  | 			return false; // No longer allocated here.
 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 		// Speed is more important than correctness here, do the operations unchecked
 | 
					
						
							| 
									
										
										
										
											2020-03-11 21:01:02 +01:00
										 |  |  | 		// and hope for the best.
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		*out = _get_alloc_size(p_elements); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void _unref(void *p_data); | 
					
						
							| 
									
										
										
										
											2018-07-29 16:57:05 +02:00
										 |  |  | 	void _ref(const CowData *p_from); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	void _ref(const CowData &p_from); | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 	uint32_t _copy_on_write(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	void operator=(const CowData<T> &p_from) { _ref(p_from); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ T *ptrw() { | 
					
						
							|  |  |  | 		_copy_on_write(); | 
					
						
							|  |  |  | 		return (T *)_get_data(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ const T *ptr() const { | 
					
						
							|  |  |  | 		return _get_data(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ int size() const { | 
					
						
							|  |  |  | 		uint32_t *size = (uint32_t *)_get_size(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (size) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 			return *size; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void clear() { resize(0); } | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 	_FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; } | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void set(int p_index, const T &p_elem) { | 
					
						
							|  |  |  | 		CRASH_BAD_INDEX(p_index, size()); | 
					
						
							|  |  |  | 		_copy_on_write(); | 
					
						
							|  |  |  | 		_get_data()[p_index] = p_elem; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ T &get_m(int p_index) { | 
					
						
							|  |  |  | 		CRASH_BAD_INDEX(p_index, size()); | 
					
						
							|  |  |  | 		_copy_on_write(); | 
					
						
							|  |  |  | 		return _get_data()[p_index]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ const T &get(int p_index) const { | 
					
						
							|  |  |  | 		CRASH_BAD_INDEX(p_index, size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return _get_data()[p_index]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Error resize(int p_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void remove(int p_index) { | 
					
						
							|  |  |  | 		ERR_FAIL_INDEX(p_index, size()); | 
					
						
							|  |  |  | 		T *p = ptrw(); | 
					
						
							|  |  |  | 		int len = size(); | 
					
						
							|  |  |  | 		for (int i = p_index; i < len - 1; i++) { | 
					
						
							|  |  |  | 			p[i] = p[i + 1]; | 
					
						
							| 
									
										
										
										
											2020-05-19 15:46:49 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		resize(len - 1); | 
					
						
							| 
									
										
										
										
											2020-05-19 15:46:49 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Error insert(int p_pos, const T &p_val) { | 
					
						
							|  |  |  | 		ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); | 
					
						
							|  |  |  | 		resize(size() + 1); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		for (int i = (size() - 1); i > p_pos; i--) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 			set(i, get(i - 1)); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		set(p_pos, p_val); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return OK; | 
					
						
							| 
									
										
										
										
											2020-05-19 15:46:49 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-07 17:02:55 +00:00
										 |  |  | 	int find(const T &p_val, int p_from = 0) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 	_FORCE_INLINE_ CowData() {} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	_FORCE_INLINE_ ~CowData(); | 
					
						
							|  |  |  | 	_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T> | 
					
						
							|  |  |  | void CowData<T>::_unref(void *p_data) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!p_data) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 	SafeNumeric<uint32_t> *refc = _get_refcount(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 	if (refc->decrement() > 0) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return; // still in use
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	// clean up
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 	if (!__has_trivial_destructor(T)) { | 
					
						
							|  |  |  | 		uint32_t *count = _get_size(); | 
					
						
							|  |  |  | 		T *data = (T *)(count + 1); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 		for (uint32_t i = 0; i < *count; ++i) { | 
					
						
							|  |  |  | 			// call destructors
 | 
					
						
							|  |  |  | 			data[i].~T(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// free mem
 | 
					
						
							|  |  |  | 	Memory::free_static((uint8_t *)p_data, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T> | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | uint32_t CowData<T>::_copy_on_write() { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!_ptr) { | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 19:22:13 +01:00
										 |  |  | 	SafeNumeric<uint32_t> *refc = _get_refcount(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 	uint32_t rc = refc->get(); | 
					
						
							|  |  |  | 	if (unlikely(rc > 1)) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		/* in use by more than me */ | 
					
						
							|  |  |  | 		uint32_t current_size = *_get_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 		new (mem_new - 2, sizeof(uint32_t), "") SafeNumeric<uint32_t>(1); //refcount
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		*(mem_new - 1) = current_size; //size
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		T *_data = (T *)(mem_new); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// initialize new elements
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 		if (__has_trivial_copy(T)) { | 
					
						
							|  |  |  | 			memcpy(mem_new, _ptr, current_size * sizeof(T)); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			for (uint32_t i = 0; i < current_size; i++) { | 
					
						
							|  |  |  | 				memnew_placement(&_data[i], T(_get_data()[i])); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_unref(_ptr); | 
					
						
							|  |  |  | 		_ptr = _data; | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		rc = 1; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 	return rc; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T> | 
					
						
							|  |  |  | Error CowData<T>::resize(int p_size) { | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 	int current_size = size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_size == current_size) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return OK; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_size == 0) { | 
					
						
							|  |  |  | 		// wants to clean up
 | 
					
						
							|  |  |  | 		_unref(_ptr); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		_ptr = nullptr; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return OK; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// possibly changing size, copy on write
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 	uint32_t rc = _copy_on_write(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 	size_t current_alloc_size = _get_alloc_size(current_size); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	size_t alloc_size; | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 	if (p_size > current_size) { | 
					
						
							|  |  |  | 		if (alloc_size != current_alloc_size) { | 
					
						
							|  |  |  | 			if (current_size == 0) { | 
					
						
							|  |  |  | 				// alloc from scratch
 | 
					
						
							|  |  |  | 				uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); | 
					
						
							|  |  |  | 				ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY); | 
					
						
							|  |  |  | 				*(ptr - 1) = 0; //size, currently none
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 				new (ptr - 2, sizeof(uint32_t), "") SafeNumeric<uint32_t>(1); //refcount
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 				_ptr = (T *)ptr; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 				uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 				ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 				new (_ptrnew - 2, sizeof(uint32_t), "") SafeNumeric<uint32_t>(rc); //refcount
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 				_ptr = (T *)(_ptrnew); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// construct the newly created elements
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 		if (!__has_trivial_constructor(T)) { | 
					
						
							|  |  |  | 			T *elems = _get_data(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 			for (int i = *_get_size(); i < p_size; i++) { | 
					
						
							|  |  |  | 				memnew_placement(&elems[i], T); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		*_get_size() = p_size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 	} else if (p_size < current_size) { | 
					
						
							| 
									
										
										
										
											2019-01-03 05:56:41 +01:00
										 |  |  | 		if (!__has_trivial_destructor(T)) { | 
					
						
							|  |  |  | 			// deinitialize no longer needed elements
 | 
					
						
							|  |  |  | 			for (uint32_t i = p_size; i < *_get_size(); i++) { | 
					
						
							|  |  |  | 				T *t = &_get_data()[i]; | 
					
						
							|  |  |  | 				t->~T(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 		if (alloc_size != current_alloc_size) { | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 			uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 			ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY); | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 			new (_ptrnew - 2, sizeof(uint32_t), "") SafeNumeric<uint32_t>(rc); //refcount
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 21:52:42 -03:00
										 |  |  | 			_ptr = (T *)(_ptrnew); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		*_get_size() = p_size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-07 17:02:55 +00:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | int CowData<T>::find(const T &p_val, int p_from) const { | 
					
						
							|  |  |  | 	int ret = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_from < 0 || size() == 0) { | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = p_from; i < size(); i++) { | 
					
						
							|  |  |  | 		if (get(i) == p_val) { | 
					
						
							|  |  |  | 			ret = i; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 16:57:05 +02:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | void CowData<T>::_ref(const CowData *p_from) { | 
					
						
							|  |  |  | 	_ref(*p_from); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | void CowData<T>::_ref(const CowData &p_from) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (_ptr == p_from._ptr) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return; // self assign, do nothing.
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_unref(_ptr); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	_ptr = nullptr; | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!p_from._ptr) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return; //nothing to do
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:31:30 +01:00
										 |  |  | 	if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		_ptr = p_from._ptr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T> | 
					
						
							|  |  |  | CowData<T>::~CowData() { | 
					
						
							|  |  |  | 	_unref(_ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #endif // COWDATA_H
 |