no message
This commit is contained in:
166
cocos/core/memop/CachedArray.h
Normal file
166
cocos/core/memop/CachedArray.h
Normal file
@@ -0,0 +1,166 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string.h>
|
||||
#include <climits>
|
||||
#include "base/Object.h"
|
||||
#include "base/TypeDef.h"
|
||||
#include "base/memory/Memory.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
template <typename T>
|
||||
class CachedArray final {
|
||||
public:
|
||||
explicit CachedArray(uint size = 1U) {
|
||||
_size = 0;
|
||||
_capacity = std::max(size, 1U);
|
||||
_array = ccnew T[_capacity];
|
||||
}
|
||||
|
||||
// The rule of five applies here
|
||||
~CachedArray() {
|
||||
CC_SAFE_DELETE_ARRAY(_array);
|
||||
}
|
||||
|
||||
CachedArray(const CachedArray &other)
|
||||
: _size(other._size), _capacity(other._capacity), _array(ccnew T[other._capacity]) {
|
||||
memcpy(_array, other._array, _size * sizeof(T));
|
||||
}
|
||||
|
||||
CachedArray &operator=(const CachedArray &other) {
|
||||
if (this != &other) {
|
||||
delete[] _array;
|
||||
_size = other._size;
|
||||
_capacity = other._capacity;
|
||||
_array = ccnew T[_capacity];
|
||||
memcpy(_array, other._array, _size * sizeof(T));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
CachedArray(CachedArray &&other) noexcept : _size(other._size), _capacity(other._capacity), _array(other._array) {
|
||||
other._size = 0;
|
||||
other._capacity = 0;
|
||||
other._array = nullptr;
|
||||
}
|
||||
|
||||
CachedArray &operator=(CachedArray &&other) noexcept {
|
||||
if (this != &other) {
|
||||
delete[] _array;
|
||||
_size = other._size;
|
||||
_capacity = other._capacity;
|
||||
_array = other._array;
|
||||
other._size = 0;
|
||||
other._capacity = 0;
|
||||
other._array = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Subscription operators
|
||||
T &operator[](uint index) {
|
||||
return _array[index];
|
||||
}
|
||||
|
||||
const T &operator[](uint index) const {
|
||||
return _array[index];
|
||||
}
|
||||
|
||||
inline void clear() { _size = 0; }
|
||||
inline uint size() const { return _size; }
|
||||
inline T pop() { return _array[--_size]; }
|
||||
|
||||
void reserve(uint size) {
|
||||
if (size > _capacity) {
|
||||
T *temp = _array;
|
||||
_array = ccnew T[size];
|
||||
memcpy(_array, temp, _capacity * sizeof(T));
|
||||
_capacity = size;
|
||||
delete[] temp;
|
||||
}
|
||||
}
|
||||
|
||||
void push(T item) {
|
||||
if (_size >= _capacity) {
|
||||
T *temp = _array;
|
||||
_array = ccnew T[_capacity * 2];
|
||||
memcpy(_array, temp, _capacity * sizeof(T));
|
||||
_capacity *= 2;
|
||||
delete[] temp;
|
||||
}
|
||||
_array[_size++] = item;
|
||||
}
|
||||
|
||||
void concat(const CachedArray<T> &array) {
|
||||
if (_size + array._size > _capacity) {
|
||||
T *temp = _array;
|
||||
uint size = std::max(_capacity * 2, _size + array._size);
|
||||
_array = ccnew T[size];
|
||||
memcpy(_array, temp, _size * sizeof(T));
|
||||
_capacity = size;
|
||||
delete[] temp;
|
||||
}
|
||||
memcpy(_array + _size, array._array, array._size * sizeof(T));
|
||||
_size += array._size;
|
||||
}
|
||||
|
||||
void concat(T *array, uint count) {
|
||||
if (_size + count > _capacity) {
|
||||
T *temp = _array;
|
||||
uint size = std::max(_capacity * 2, _size + count);
|
||||
_array = ccnew T[size];
|
||||
memcpy(_array, temp, _size * sizeof(T));
|
||||
_capacity = size;
|
||||
delete[] temp;
|
||||
}
|
||||
memcpy(_array + _size, array, count * sizeof(T));
|
||||
_size += count;
|
||||
}
|
||||
|
||||
void fastRemove(uint idx) {
|
||||
if (idx >= _size) {
|
||||
return;
|
||||
}
|
||||
_array[idx] = _array[--_size];
|
||||
}
|
||||
|
||||
uint indexOf(T item) {
|
||||
for (uint i = 0; i < _size; ++i) {
|
||||
if (_array[i] == item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return UINT_MAX;
|
||||
}
|
||||
|
||||
private:
|
||||
uint _size = 0;
|
||||
uint _capacity = 0;
|
||||
T *_array = nullptr;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
133
cocos/core/memop/Pool.h
Normal file
133
cocos/core/memop/Pool.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include "base/TypeDef.h"
|
||||
#include "base/std/container/vector.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
namespace memop {
|
||||
|
||||
template <typename T>
|
||||
class Pool final {
|
||||
public:
|
||||
using CtorFunc = std::function<T *()>;
|
||||
using DtorFunc = std::function<void(T *)>;
|
||||
/**
|
||||
* @en Constructor with the allocator of elements and initial pool size
|
||||
* @zh 使用元素的构造器和初始大小的构造函数
|
||||
* @param ctor The allocator of elements in pool, it's invoked directly without `new`
|
||||
* @param elementsPerBatch Initial pool size, this size will also be the incremental size when the pool is overloaded
|
||||
*/
|
||||
Pool(const CtorFunc &ctor, const DtorFunc &dtor, uint32_t elementsPerBatch)
|
||||
: _ctor(ctor),
|
||||
_dtor(dtor),
|
||||
_elementsPerBatch(std::max(elementsPerBatch, static_cast<uint32_t>(1))) {
|
||||
CC_ASSERT(_ctor);
|
||||
CC_ASSERT(_dtor);
|
||||
_nextAvail = static_cast<index_t>(_elementsPerBatch - 1);
|
||||
|
||||
for (uint32_t i = 0; i < _elementsPerBatch; ++i) {
|
||||
_freepool.push_back(ctor());
|
||||
}
|
||||
}
|
||||
|
||||
Pool(const Pool &) = delete;
|
||||
Pool(Pool &&) = delete;
|
||||
~Pool() = default;
|
||||
Pool &operator=(const Pool &) = delete;
|
||||
Pool &operator=(Pool &&) = delete;
|
||||
|
||||
/**
|
||||
* @en Take an object out of the object pool.
|
||||
* @zh 从对象池中取出一个对象。
|
||||
* @return An object ready for use. This function always return an object.
|
||||
*/
|
||||
T *alloc() {
|
||||
if (_nextAvail < 0) {
|
||||
_freepool.resize(_elementsPerBatch);
|
||||
for (uint32_t i = 0; i < _elementsPerBatch; ++i) {
|
||||
_freepool[i] = _ctor();
|
||||
}
|
||||
_nextAvail = static_cast<index_t>(_elementsPerBatch - 1);
|
||||
}
|
||||
|
||||
return _freepool[_nextAvail--];
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Put an object back into the object pool.
|
||||
* @zh 将一个对象放回对象池中。
|
||||
* @param obj The object to be put back into the pool
|
||||
*/
|
||||
void free(T *obj) {
|
||||
++_nextAvail;
|
||||
if (_nextAvail >= _freepool.size()) {
|
||||
_freepool.resize(_freepool.size() + 1);
|
||||
}
|
||||
_freepool[_nextAvail] = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Put multiple objects back into the object pool.
|
||||
* @zh 将一组对象放回对象池中。
|
||||
* @param objs An array of objects to be put back into the pool
|
||||
*/
|
||||
void freeArray(const ccstd::vector<T *> &objs) {
|
||||
_freepool.reserve(_nextAvail + 1 + objs.size());
|
||||
_freepool.insert(_freepool.begin() + _nextAvail + 1,
|
||||
objs.begin(), objs.end());
|
||||
_nextAvail += static_cast<index_t>(objs.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Destroy all elements and clear the pool.
|
||||
* @zh 释放对象池中所有资源并清空缓存池。
|
||||
* @param dtor The destructor function, it will be invoked for all elements in the pool
|
||||
*/
|
||||
void destroy() {
|
||||
for (int i = 0; i <= _nextAvail; ++i) {
|
||||
_dtor(_freepool[i]);
|
||||
}
|
||||
_nextAvail = -1;
|
||||
_freepool.clear();
|
||||
_freepool.shrink_to_fit();
|
||||
}
|
||||
|
||||
private:
|
||||
CtorFunc _ctor{nullptr};
|
||||
DtorFunc _dtor{nullptr};
|
||||
uint32_t _elementsPerBatch{0};
|
||||
index_t _nextAvail{-1};
|
||||
ccstd::vector<T *> _freepool;
|
||||
};
|
||||
|
||||
} // namespace memop
|
||||
|
||||
} // namespace cc
|
||||
105
cocos/core/memop/RecyclePool.h
Normal file
105
cocos/core/memop/RecyclePool.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include "base/std/container/vector.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
/**
|
||||
* @en Recyclable object pool. It's designed to be entirely reused each time.
|
||||
* There is no put and get method, each time you get the [[data]], you can use all elements as new.
|
||||
* You shouldn't simultaneously use the same RecyclePool in more than two overlapped logic.
|
||||
* Its size can be automatically incremented or manually resized.
|
||||
* @zh 循环对象池。这种池子被设计为每次使用都完整复用。
|
||||
* 它没有回收和提取的函数,通过获取 [[data]] 可以获取池子中所有元素,全部都应该被当做新对象来使用。
|
||||
* 开发者不应该在相互交叉的不同逻辑中同时使用同一个循环对象池。
|
||||
* 池子尺寸可以在池子满时自动扩充,也可以手动调整。
|
||||
* @see [[Pool]]
|
||||
*/
|
||||
template <typename T>
|
||||
class RecyclePool final {
|
||||
public:
|
||||
/**
|
||||
* @en Constructor with the allocator of elements and initial pool size, all elements will be pre-allocated.
|
||||
* @zh 使用元素的构造器和初始大小的构造函数,所有元素都会被预创建。
|
||||
* @param fn The allocator of elements in pool, it's invoked directly without `new`
|
||||
* @param size Initial pool size
|
||||
*/
|
||||
RecyclePool(const std::function<T *()> &fn, uint32_t size);
|
||||
|
||||
RecyclePool(const RecyclePool &) = delete;
|
||||
RecyclePool(RecyclePool &&) = delete;
|
||||
~RecyclePool() = default;
|
||||
RecyclePool &operator=(const RecyclePool &) = delete;
|
||||
RecyclePool &operator=(RecyclePool &&) = delete;
|
||||
|
||||
/**
|
||||
* @en The length of the object pool.
|
||||
* @zh 对象池大小。
|
||||
*/
|
||||
inline uint32_t getLength() const { return _count; }
|
||||
|
||||
/**
|
||||
* @en The underlying array of all pool elements.
|
||||
* @zh 实际对象池数组。
|
||||
*/
|
||||
inline const ccstd::vector<T *> &getData() const { return _data; }
|
||||
|
||||
/**
|
||||
* @en Resets the object pool. Only changes the length to 0
|
||||
* @zh 清空对象池。目前仅仅会设置尺寸为 0
|
||||
*/
|
||||
inline void reset() { _count = 0; }
|
||||
|
||||
/**
|
||||
* @en Resize the object poo, and fills with new created elements.
|
||||
* @zh 设置对象池大小,并填充新的元素。
|
||||
* @param size The new size of the pool
|
||||
*/
|
||||
void resize(uint32_t size);
|
||||
|
||||
/**
|
||||
* @en Expand the object pool, the size will be increment to current size times two, and fills with new created elements.
|
||||
* @zh 扩充对象池容量,会自动扩充尺寸到两倍,并填充新的元素。
|
||||
* @param idx
|
||||
*/
|
||||
void add();
|
||||
|
||||
/**
|
||||
* @en Remove an element of the object pool. This will also decrease size of the pool
|
||||
* @zh 移除对象池中的一个元素,同时会减小池子尺寸。
|
||||
* @param idx The index of the element to be removed
|
||||
*/
|
||||
void removeAt(uint32_t idx);
|
||||
|
||||
private:
|
||||
uint32_t _count{0};
|
||||
ccstd::vector<T *> _data;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user