From 3850c0712d6cef092113943869ca1a089f4eb47c Mon Sep 17 00:00:00 2001 From: Aurelia <3792458256@qq.com> Date: Thu, 5 Feb 2026 15:21:57 +0800 Subject: [PATCH] Add Allocator class for memory management --- test/core/allocator.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/core/allocator.h diff --git a/test/core/allocator.h b/test/core/allocator.h new file mode 100644 index 0000000..c15318a --- /dev/null +++ b/test/core/allocator.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +class Allocator { +private: + void* ptr; // 实际分配的内存指针 + size_t currentOffset; // 当前分配偏移量 + size_t totalSize; // 总分配大小 + + // 空闲块管理 + std::map freeBlocksByStart; // key: 起始地址, value: 块大小 + std::map> freeBlocksBySize; // key: 块大小, value: 起始地址集合 + +public: + Allocator(); + ~Allocator(); + + // 分配内存,返回起始地址偏移量 + size_t alloc(size_t size); + + // 释放内存 + void free(size_t addr, size_t size); + + // 获取对齐后的尺寸 + size_t getAlignedSize(size_t size); + + // 获取内存指针 + void* getPtr(); + + // 打印分配信息 + void info(); + +private: + // 合并相邻的空闲块 + void mergeFreeBlocks(); +};