mirror of
https://github.com/microsoft/mimalloc.git
synced 2024-12-27 13:33:18 +08:00
add stl mimalloc wrapper
This commit is contained in:
parent
49acc88924
commit
743e891738
@ -187,6 +187,7 @@ install(TARGETS mimalloc-static EXPORT mimalloc DESTINATION ${mi_install_dir})
|
||||
install(FILES include/mimalloc.h DESTINATION ${mi_install_dir}/include)
|
||||
install(FILES include/mimalloc-override.h DESTINATION ${mi_install_dir}/include)
|
||||
install(FILES include/mimalloc-new-delete.h DESTINATION ${mi_install_dir}/include)
|
||||
install(FILES include/mimalloc-stl-allocator.h DESTINATION ${mi_install_dir}/include)
|
||||
install(FILES cmake/mimalloc-config.cmake DESTINATION ${mi_install_dir}/cmake)
|
||||
install(FILES cmake/mimalloc-config-version.cmake DESTINATION ${mi_install_dir}/cmake)
|
||||
install(EXPORT mimalloc DESTINATION ${mi_install_dir}/cmake)
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@ -214,6 +214,7 @@
|
||||
<ClInclude Include="..\..\include\mimalloc-atomic.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-new-delete.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-override.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-stl-allocator.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-types.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@ -239,6 +239,7 @@
|
||||
<ClInclude Include="$(ProjectDir)..\..\include\mimalloc-override.h" />
|
||||
<ClInclude Include="$(ProjectDir)..\..\include\mimalloc-types.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-new-delete.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-stl-allocator.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@ -214,6 +214,7 @@
|
||||
<ClInclude Include="..\..\include\mimalloc-atomic.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-new-delete.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-override.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-stl-allocator.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-types.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@ -239,6 +239,7 @@
|
||||
<ClInclude Include="$(ProjectDir)..\..\include\mimalloc-override.h" />
|
||||
<ClInclude Include="$(ProjectDir)..\..\include\mimalloc-types.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-new-delete.h" />
|
||||
<ClInclude Include="..\..\include\mimalloc-stl-allocator.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
44
include/mimalloc-stl-allocator.h
Normal file
44
include/mimalloc-stl-allocator.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
#ifndef MIMALLOC_STL_ALLOCATOR_H
|
||||
#define MIMALLOC_STL_ALLOCATOR_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
/* ----------------------------------------------------------------------------
|
||||
This header can be used to hook mimalloc into STL containers in place of
|
||||
std::allocator.
|
||||
-----------------------------------------------------------------------------*/
|
||||
#include <mimalloc.h>
|
||||
#include <type_traits> // true_type
|
||||
|
||||
#pragma warning(disable: 4100)
|
||||
|
||||
template <class T>
|
||||
struct mi_stl_allocator {
|
||||
typedef T value_type;
|
||||
|
||||
using propagate_on_container_copy_assignment = std::true_type;
|
||||
using propagate_on_container_move_assignment = std::true_type;
|
||||
using propagate_on_container_swap = std::true_type;
|
||||
using is_always_equal = std::true_type;
|
||||
|
||||
mi_stl_allocator() noexcept {}
|
||||
mi_stl_allocator(const mi_stl_allocator& other) noexcept {}
|
||||
template <class U>
|
||||
mi_stl_allocator(const mi_stl_allocator<U>& other) noexcept {}
|
||||
|
||||
T* allocate(size_t n, const void* hint = 0) {
|
||||
return (T*)mi_mallocn(n, sizeof(T));
|
||||
}
|
||||
|
||||
void deallocate(T* p, size_t n) {
|
||||
mi_free(p);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T1, class T2>
|
||||
bool operator==(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) noexcept { return true; }
|
||||
template <class T1, class T2>
|
||||
bool operator!=(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) noexcept { return false; }
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // MIMALLOC_STL_ALLOCATOR_H
|
@ -25,8 +25,10 @@ we therefore test the API over various inputs. Please add more tests :-)
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <vector>
|
||||
#include "mimalloc.h"
|
||||
#include "mimalloc-internal.h"
|
||||
#include "mimalloc-stl-allocator.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test macros: CHECK(name,predicate) and CHECK_BODY(name,body)
|
||||
@ -61,6 +63,8 @@ static int failed = 0;
|
||||
// ---------------------------------------------------------------------------
|
||||
bool test_heap1();
|
||||
bool test_heap2();
|
||||
bool test_stl_allocator1();
|
||||
bool test_stl_allocator2();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main testing
|
||||
@ -150,6 +154,9 @@ int main() {
|
||||
mi_free(s);
|
||||
});
|
||||
|
||||
CHECK("stl_allocator1", test_stl_allocator1());
|
||||
CHECK("stl_allocator2", test_stl_allocator2());
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Done
|
||||
// ---------------------------------------------------[]
|
||||
@ -182,3 +189,27 @@ bool test_heap2() {
|
||||
mi_free(p2);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool test_stl_allocator1() {
|
||||
#ifdef __cplusplus
|
||||
std::vector<int, mi_stl_allocator<int>> vec;
|
||||
vec.push_back(1);
|
||||
vec.pop_back();
|
||||
return vec.size() == 0;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool test_stl_allocator2() {
|
||||
#ifdef __cplusplus
|
||||
struct some_struct { int i; int j; double z; };
|
||||
|
||||
std::vector<some_struct, mi_stl_allocator<some_struct>> vec;
|
||||
vec.push_back(some_struct());
|
||||
vec.pop_back();
|
||||
return vec.size() == 0;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user