内存分配器

一个从硬盘上取空间的STL内存空间分配器

浪尽此生 提交于 2020-01-07 18:39:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近在读侯捷写的《STL源码剖析》。 看完STL的内存空间分配器这章。在这章里,作者开玩笑的说,你甚至可以写一个直接从硬盘上取空间的配置器。我想,我确实可以写这样的分配器。然后今天就动手写了一个。不过这个分配器只是写着玩玩,不仅效率奇低,还有很多BUG。所以大家可以看着玩玩,可千万别使用啊。 #ifndef __ALLOCATOR_H__ #define __ALLOCATOR_H__ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #include <string.h> #include <new> #include <cstddef> #include <cstdlib> #include <climits> #include <iostream> using namespace std; namespace Costaxu { #define BUFFER_FILE_NAME "/tmp/costaxu_buffer" #define BUFFER_SIZE 1024*1024*1024 template <class T> inline T* _hd_allocate