update test files and overriding

This commit is contained in:
daan 2019-07-22 01:36:16 -07:00
parent 6d9fab5af4
commit 219d46ff0c
9 changed files with 67 additions and 41 deletions

View File

@ -172,23 +172,14 @@
<Command>COPY /Y $(SolutionDir)..\..\bin\mimalloc-redirect.dll $(OutputPath)</Command> <Command>COPY /Y $(SolutionDir)..\..\bin\mimalloc-redirect.dll $(OutputPath)</Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\test\main-override.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\test\main-override.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="mimalloc-override.vcxproj"> <ProjectReference Include="mimalloc-override.vcxproj">
<Project>{abb5eae7-b3e6-432e-b636-333449892ea7}</Project> <Project>{abb5eae7-b3e6-432e-b636-333449892ea7}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\test\main-override.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -18,8 +18,5 @@
<ClCompile Include="..\..\test\main-override.cpp"> <ClCompile Include="..\..\test\main-override.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\test\main-override.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -144,14 +144,14 @@
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\test\main-override.cpp" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="mimalloc.vcxproj"> <ProjectReference Include="mimalloc.vcxproj">
<Project>{abb5eae7-b3e6-432e-b636-333449892ea6}</Project> <Project>{abb5eae7-b3e6-432e-b636-333449892ea6}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\test\main-override-static.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -15,7 +15,7 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\test\main-override.cpp"> <ClCompile Include="..\..\test\main-override-static.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>

View File

@ -69,11 +69,15 @@ including this header is not necessary.
#define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o) #define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o)
// ------------------------------------------------------ // -----------------------------------------------------------------
// With a C++ compiler we override the new/delete operators. // With a C++ compiler we can override all the new/delete operators
// by defining 'MIMALLOC_DEFINE_NEW_DELETE' in some source file and
// then including this header file. This is not needed when linking
// statically with the mimalloc library, but it can be more performant
// on Windows when using dynamic overiding as well.
// see <https://en.cppreference.com/w/cpp/memory/new/operator_new> // see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
// ------------------------------------------------------ // -----------------------------------------------------------------
#ifdef __cplusplus #if defined(__cplusplus) && defined(MIMALLOC_DEFINE_NEW_DELETE)
#include <new> #include <new>
void operator delete(void* p) noexcept { mi_free(p); }; void operator delete(void* p) noexcept { mi_free(p); };

View File

@ -24,14 +24,21 @@ target_link_libraries(dynamic-override PUBLIC mimalloc)
add_executable(dynamic-override-cxx main-override.cpp) add_executable(dynamic-override-cxx main-override.cpp)
target_link_libraries(dynamic-override-cxx PUBLIC mimalloc) target_link_libraries(dynamic-override-cxx PUBLIC mimalloc)
# with a static library # overriding with a static object file works reliable as the symbols in the
# object file have priority over those in library files
add_executable(static-override-obj main-override.c ${MIMALLOC_TARGET_DIR}/mimalloc.o)
target_include_directories(static-override-obj PUBLIC ${MIMALLOC_TARGET_DIR}/include)
target_link_libraries(static-override-obj PUBLIC pthread)
# overriding with a static library works too if using the `mimalloc-override.h`
# header to redefine malloc/free.
add_executable(static-override-static main-override-static.c)
target_link_libraries(static-override-static PUBLIC mimalloc-static)
# overriding with a static library: this may not work if the library is linked too late
add_executable(static-override main-override.c) add_executable(static-override main-override.c)
target_link_libraries(static-override PUBLIC mimalloc-static) target_link_libraries(static-override PUBLIC mimalloc-static)
add_executable(static-override-cxx main-override.cpp) add_executable(static-override-cxx main-override.cpp)
target_link_libraries(static-override-cxx PUBLIC mimalloc-static) target_link_libraries(static-override-cxx PUBLIC mimalloc-static)
# and with a static object file
add_executable(static-override-obj main-override.c ${MIMALLOC_TARGET_DIR}/mimalloc.o)
target_include_directories(static-override-obj PUBLIC ${MIMALLOC_TARGET_DIR}/include)
target_link_libraries(static-override-obj PUBLIC pthread)

View File

@ -0,0 +1,31 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <mimalloc.h>
#include <mimalloc-override.h> // redefines malloc etc.
int main() {
mi_version();
void* p1 = malloc(78);
void* p2 = malloc(24);
free(p1);
p1 = malloc(8);
//char* s = strdup("hello\n");
free(p2);
p2 = malloc(16);
p1 = realloc(p1, 32);
free(p1);
free(p2);
//free(s);
//mi_collect(true);
/* now test if override worked by allocating/freeing across the api's*/
//p1 = mi_malloc(32);
//free(p1);
//p2 = malloc(32);
//mi_free(p2);
mi_stats_print(NULL);
return 0;
}

View File

@ -3,11 +3,10 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
//#include <mimalloc.h> #include <mimalloc.h>
int main() { int main() {
//mi_stats_reset(); mi_version(); // ensure mimalloc library is linked
void* p1 = malloc(78); void* p1 = malloc(78);
void* p2 = malloc(24); void* p2 = malloc(24);
free(p1); free(p1);
@ -26,6 +25,6 @@ int main() {
//free(p1); //free(p1);
//p2 = malloc(32); //p2 = malloc(32);
//mi_free(p2); //mi_free(p2);
mi_stats_print(NULL);
return 0; return 0;
} }

View File

@ -4,8 +4,6 @@
#include <string.h> #include <string.h>
#include <mimalloc.h> #include <mimalloc.h>
#include <mimalloc-override.h>
#include <new> #include <new>
static void* p = malloc(8); static void* p = malloc(8);
@ -25,15 +23,14 @@ public:
int main() { int main() {
//mi_malloc_override(); mi_version();
mi_stats_reset();
atexit(free_p); atexit(free_p);
void* p1 = malloc(78); void* p1 = malloc(78);
void* p2 = _aligned_malloc(24,16); void* p2 = mi_malloc_aligned(16,24);
free(p1); free(p1);
p1 = malloc(8); p1 = malloc(8);
char* s = _strdup("hello\n"); char* s = mi_strdup("hello\n");
_aligned_free(p2); mi_free(p2);
p2 = malloc(16); p2 = malloc(16);
p1 = realloc(p1, 32); p1 = realloc(p1, 32);
free(p1); free(p1);