Snmalloc內(nèi)存分配器
snmalloc 是一個研究性質(zhì)的內(nèi)存分配器。 其主要設(shè)計特點是:
- 由分配它的同一線程釋放的內(nèi)存不需要任何同步操作。
- 在最初分配它的不同線程中釋放內(nèi)存,不占用任何鎖,而是使用新穎的消息傳遞方案將內(nèi)存返回到原始分配器,在那里它被回收。
- 分配器使用大范圍的頁面來減少所需的元數(shù)據(jù)量。
在 Windows 中構(gòu)建
依賴于 Visual Studio 2017.
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
cmake --build . --config Debug
cmake --build . --config Release
cmake --build . --config RelWithDebInfo
如果是在 IDE 中操作的話,你可以忽視最后三步。Visual Studio 構(gòu)建使用一個獨立的目錄來為每個構(gòu)建配置保存二進(jìn)制文件。
此外你可以參考下一部分內(nèi)容來使用 Visual Studio 編譯器和 Ninja 構(gòu)建。
在 Mac OS X 、Linux 和 FreeBSD 中構(gòu)建
Snmalloc 依賴很少,包括 CMake, Ninja, Clang 6.0 以及一個 C++17 標(biāo)準(zhǔn)庫。當(dāng)前不推薦使用 GCC 編譯,因為 GCC 在 COMDAT 中缺少對指定變量 selectany 屬性的支持。但是可以在 GCC-7 中構(gòu)建,但需要預(yù)先設(shè)置一些全局變量
構(gòu)建調(diào)試配置:
mkdir build
cd build
cmake -G Ninja .. -DCMAKE_BUILD_TYPE=Debug
ninja
構(gòu)建發(fā)行配置:
mkdir build
cd build
cmake -G Ninja .. -DCMAKE_BUILD_TYPE=Release
ninja
開啟優(yōu)化器,但包含調(diào)試配置:
mkdir build
cd build
cmake -G Ninja .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
ninja
在 ELF 平臺上,上述構(gòu)建將生成一個二進(jìn)制文件 libsnmallocshim.so 。這個文件可以使用 LD_PRELOAD來用該分配器替代系統(tǒng)分配器,如下:
LD_PRELOAD=/usr/local/lib/libsnmallocshim.so ninja
CMake Feature Flags
These can be added to your cmake command line.
-DUSE_SNMALLOC_STATS=ON // Track allocation stats
-DUSE_MEASURE=ON // Measure performance with histograms
The project can be included in other CMake projects as a header only library:
set(SNMALLOC_ONLY_HEADER_LIBRARY ON)
add_subdirectory([...]/snmalloc EXCLUDE_FROM_ALL)
This has a single build target snmalloc_lib, which includes the necessary compiler and linker flags, to use snmalloc as a header-only library.
