Add tests cases for mimalloc static initialization order

This commit is contained in:
Frank Richter 2021-12-28 14:35:11 +01:00
parent 164b3a9667
commit c8a70b25ae
4 changed files with 134 additions and 0 deletions

38
test/static-user.cpp Normal file
View file

@ -0,0 +1,38 @@
/* ----------------------------------------------------------------------------
Copyright (c) 2021 Frank Richter
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/
#include "static-user.h"
#include "mimalloc.h"
struct HeapWrapper
{
struct Container
{
mi_heap_t *heap;
};
Container *container;
HeapWrapper()
{
container = mi_malloc_tp(Container);
container->heap = mi_heap_new();
}
~HeapWrapper()
{
mi_heap_destroy(container->heap);
mi_free(container);
}
};
static HeapWrapper static_heap;
void static_user_ref()
{
auto *p = mi_heap_malloc(static_heap.container->heap, 123);
mi_free(p);
}

42
test/static-user.h Normal file
View file

@ -0,0 +1,42 @@
/* ----------------------------------------------------------------------------
Copyright (c) 2021 Frank Richter
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/
#ifndef STATIC_USER_H_
#define STATIC_USER_H_
#if defined(_MSC_VER) || defined(__MINGW32__)
#define DLLEXPORT __declspec(dllexport)
#define DLLIMPORT __declspec(dllimport)
#elif defined(__GNUC__)
#define DLLEXPORT __attribute__((visibility("default")))
#define DLLIMPORT
#else
#define DLLEXPORT
#define DLLIMPORT
#endif
#if defined(STATIC_USER_SHARED)
#if defined(STATIC_USER_BUILD)
#define STATIC_USER_API DLLEXPORT
#else
#define STATIC_USER_API DLLIMPORT
#endif
#else
#define STATIC_USER_API
#endif
#if defined(__cplusplus)
extern "C" {
#endif // defined(__cplusplus)
STATIC_USER_API void static_user_ref();
#if defined(__cplusplus)
} // extern "C"
#endif // defined(__cplusplus)
#endif // STATIC_USER_H_

25
test/test-static-user.c Normal file
View file

@ -0,0 +1,25 @@
/* ----------------------------------------------------------------------------
Copyright (c) 2018-2020, Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/
#include "mimalloc.h"
#include "static-user.h"
#include "testhelper.h"
// ---------------------------------------------------------------------------
// Main testing
// ---------------------------------------------------------------------------
int main(void) {
mi_option_disable(mi_option_verbose);
// The real test is whether the test executable crashes at exit
static_user_ref();
// ---------------------------------------------------
// Done
// ---------------------------------------------------[]
return print_test_summary();
}