201 lines
4.8 KiB
C++
201 lines
4.8 KiB
C++
#pragma once
|
||
|
||
#include <QVector>
|
||
#include <QMutex>
|
||
#include <QSharedPointer>
|
||
#include <functional>
|
||
#include "BZStruct.h"
|
||
|
||
/**
|
||
* @brief 对象池模板类,用于重用对象以减少内存分配开销
|
||
*/
|
||
template<typename T>
|
||
class ObjectPool {
|
||
public:
|
||
ObjectPool(size_t initialSize = 100, size_t maxSize = 1000)
|
||
: m_maxSize(maxSize) {
|
||
// 预分配初始对象
|
||
for (size_t i = 0; i < initialSize; ++i) {
|
||
m_pool.append(new T());
|
||
}
|
||
}
|
||
|
||
~ObjectPool() {
|
||
QMutexLocker lock(&m_mutex);
|
||
for (auto obj : m_pool) {
|
||
delete obj;
|
||
}
|
||
m_pool.clear();
|
||
}
|
||
|
||
/**
|
||
* @brief 从对象池获取对象
|
||
*/
|
||
QSharedPointer<T> acquire() {
|
||
QMutexLocker lock(&m_mutex);
|
||
|
||
if (!m_pool.isEmpty()) {
|
||
T* obj = m_pool.takeLast();
|
||
return QSharedPointer<T>(obj, [this](T* obj) { this->release(obj); });
|
||
} else if (m_totalCreated < m_maxSize) {
|
||
m_totalCreated++;
|
||
T* obj = new T();
|
||
return QSharedPointer<T>(obj, [this](T* obj) { this->release(obj); });
|
||
}
|
||
|
||
// 如果池为空且达到最大限制,创建临时对象(不会被回收)
|
||
return QSharedPointer<T>(new T());
|
||
}
|
||
|
||
/**
|
||
* @brief 获取当前池大小
|
||
*/
|
||
size_t size() const {
|
||
QMutexLocker lock(&m_mutex);
|
||
return m_pool.size();
|
||
}
|
||
|
||
/**
|
||
* @brief 获取已创建对象总数
|
||
*/
|
||
size_t totalCreated() const {
|
||
QMutexLocker lock(&m_mutex);
|
||
return m_totalCreated;
|
||
}
|
||
|
||
private:
|
||
void release(T* obj) {
|
||
QMutexLocker lock(&m_mutex);
|
||
if (m_pool.size() < m_maxSize) {
|
||
// 重置对象状态
|
||
*obj = T();
|
||
m_pool.append(obj);
|
||
} else {
|
||
delete obj;
|
||
}
|
||
}
|
||
|
||
private:
|
||
QVector<T*> m_pool;
|
||
mutable QMutex m_mutex;
|
||
size_t m_maxSize;
|
||
size_t m_totalCreated = 0;
|
||
};
|
||
|
||
/**
|
||
* @brief BigOrderInfo对象池单例
|
||
*/
|
||
class BigOrderInfoPool {
|
||
public:
|
||
static BigOrderInfoPool& instance() {
|
||
static BigOrderInfoPool pool;
|
||
return pool;
|
||
}
|
||
|
||
QSharedPointer<BigOrderInfo> acquire() {
|
||
return m_pool.acquire();
|
||
}
|
||
|
||
size_t poolSize() const {
|
||
return m_pool.size();
|
||
}
|
||
|
||
size_t totalCreated() const {
|
||
return m_pool.totalCreated();
|
||
}
|
||
|
||
private:
|
||
BigOrderInfoPool() : m_pool(100, 500) {} // 初始100个,最大500个
|
||
|
||
private:
|
||
ObjectPool<BigOrderInfo> m_pool;
|
||
};
|
||
|
||
/**
|
||
* @brief 内存使用监控器
|
||
*/
|
||
class MemoryMonitor {
|
||
public:
|
||
static MemoryMonitor& instance() {
|
||
static MemoryMonitor monitor;
|
||
return monitor;
|
||
}
|
||
|
||
/**
|
||
* @brief 记录内存分配
|
||
*/
|
||
void recordAllocation(size_t bytes, const QString& type) {
|
||
QMutexLocker lock(&m_mutex);
|
||
m_allocatedMemory[type] += bytes;
|
||
m_totalAllocated += bytes;
|
||
|
||
// 检查内存使用是否超过阈值
|
||
if (m_totalAllocated > m_memoryThreshold && !m_alertTriggered) {
|
||
emitMemoryAlert();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 记录内存释放
|
||
*/
|
||
void recordDeallocation(size_t bytes, const QString& type) {
|
||
QMutexLocker lock(&m_mutex);
|
||
m_allocatedMemory[type] -= bytes;
|
||
m_totalAllocated -= bytes;
|
||
}
|
||
|
||
/**
|
||
* @brief 获取当前内存使用情况
|
||
*/
|
||
QMap<QString, size_t> getMemoryUsage() const {
|
||
QMutexLocker lock(&m_mutex);
|
||
return m_allocatedMemory;
|
||
}
|
||
|
||
/**
|
||
* @brief 获取总内存使用量
|
||
*/
|
||
size_t getTotalMemoryUsage() const {
|
||
QMutexLocker lock(&m_mutex);
|
||
return m_totalAllocated;
|
||
}
|
||
|
||
/**
|
||
* @brief 设置内存告警阈值
|
||
*/
|
||
void setMemoryThreshold(size_t threshold) {
|
||
QMutexLocker lock(&m_mutex);
|
||
m_memoryThreshold = threshold;
|
||
}
|
||
|
||
private:
|
||
void emitMemoryAlert() {
|
||
m_alertTriggered = true;
|
||
// 这里可以发送信号或记录日志
|
||
qWarning() << "Memory usage alert: Total allocated" << m_totalAllocated << "bytes";
|
||
|
||
// 记录内存使用详情
|
||
for (auto it = m_allocatedMemory.begin(); it != m_allocatedMemory.end(); ++it) {
|
||
if (it.value() > 0) {
|
||
qWarning() << " " << it.key() << ":" << it.value() << "bytes";
|
||
}
|
||
}
|
||
}
|
||
|
||
private:
|
||
mutable QMutex m_mutex;
|
||
QMap<QString, size_t> m_allocatedMemory;
|
||
size_t m_totalAllocated = 0;
|
||
size_t m_memoryThreshold = 100 * 1024 * 1024; // 100MB默认阈值
|
||
bool m_alertTriggered = false;
|
||
};
|
||
|
||
// 内存分配跟踪宏
|
||
#ifdef MEMORY_TRACKING
|
||
#define TRACK_MEMORY_ALLOC(bytes, type) MemoryMonitor::instance().recordAllocation(bytes, type)
|
||
#define TRACK_MEMORY_FREE(bytes, type) MemoryMonitor::instance().recordDeallocation(bytes, type)
|
||
#else
|
||
#define TRACK_MEMORY_ALLOC(bytes, type)
|
||
#define TRACK_MEMORY_FREE(bytes, type)
|
||
#endif
|