Merge pull request #188 from kile0/kile/stl

Create an STL allocator for mimalloc
This commit is contained in:
Daan 2020-01-16 15:33:44 -08:00 committed by GitHub
commit 2d54553b7a
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 5 deletions

View file

@ -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"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View file

@ -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"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View file

@ -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"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View file

@ -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"> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View file

@ -73,6 +73,8 @@ terms of the MIT license. A copy of the license can be found in the file
#include <stdbool.h> // bool #include <stdbool.h> // bool
#ifdef __cplusplus #ifdef __cplusplus
#include <type_traits> // true_type
extern "C" { extern "C" {
#endif #endif
@ -335,5 +337,41 @@ mi_decl_export void* mi_new_aligned_nothrow(size_t n, size_t alignment) mi_attr_
} }
#endif #endif
#ifdef __cplusplus
// ------------------------------------------------------
// STL allocator - an extension to hook mimalloc into STL
// containers in place of std::allocator.
// ------------------------------------------------------
#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
#endif #endif

View file

@ -56,9 +56,9 @@ Enjoy!
### Releases ### Releases
* 2020-01-15, `v1.3.0`: stable release 1.3: bug fixes, improved randomness and stronger * 2020-01-15, `v1.3.0`: stable release 1.3: bug fixes, improved randomness and stronger
free list encoding in secure mode. free list encoding in secure mode.
* 2019-12-22, `v1.2.2`: stable release 1.2: minor updates.
* 2019-11-22, `v1.2.0`: stable release 1.2: bug fixes, improved secure mode (free list corruption checks, double free mitigation). Improved dynamic overriding on Windows. * 2019-11-22, `v1.2.0`: stable release 1.2: bug fixes, improved secure mode (free list corruption checks, double free mitigation). Improved dynamic overriding on Windows.
* 2019-10-07, `v1.1.0`: stable release 1.1. * 2019-10-07, `v1.1.0`: stable release 1.1.
* 2019-09-01, `v1.0.8`: pre-release 8: more robust windows dynamic overriding, initial huge page support. * 2019-09-01, `v1.0.8`: pre-release 8: more robust windows dynamic overriding, initial huge page support.

View file

@ -25,6 +25,11 @@ we therefore test the API over various inputs. Please add more tests :-)
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#ifdef __cplusplus
#include <vector>
#endif
#include "mimalloc.h" #include "mimalloc.h"
#include "mimalloc-internal.h" #include "mimalloc-internal.h"
@ -61,6 +66,8 @@ static int failed = 0;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool test_heap1(); bool test_heap1();
bool test_heap2(); bool test_heap2();
bool test_stl_allocator1();
bool test_stl_allocator2();
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Main testing // Main testing
@ -150,6 +157,9 @@ int main() {
mi_free(s); mi_free(s);
}); });
CHECK("stl_allocator1", test_stl_allocator1());
CHECK("stl_allocator2", test_stl_allocator2());
// --------------------------------------------------- // ---------------------------------------------------
// Done // Done
// ---------------------------------------------------[] // ---------------------------------------------------[]
@ -182,3 +192,27 @@ bool test_heap2() {
mi_free(p2); mi_free(p2);
return true; 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
}