Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8_4] improve error handling and performance of mimalloc #254

Merged
merged 8 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions System/Memory/fast_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern void fast_delete (void* ptr);

extern int mem_used ();
extern void mem_info ();
extern void mem_init ();
void* alloc_check (const char* msg, void* ptr, size_t* sp);

/******************************************************************************
Expand Down
3 changes: 3 additions & 0 deletions System/Memory/impl/fast_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ break_stub (void* ptr) {
}
#endif

void
mem_init () {}

/******************************************************************************
* Redefine standard new and delete
******************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions System/Memory/impl/je_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ fast_delete (void* ptr) {
free (ptr);
}

void
mem_init () {}

/******************************************************************************
* Statistics
******************************************************************************/
Expand Down
45 changes: 33 additions & 12 deletions System/Memory/impl/mi_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,16 @@
#include "assert.h"
#include "basic.hpp"
#include "fast_alloc.hpp"
#include <errno.h>
#include <mimalloc.h>

/*****************************************************************************/
// General purpose fast allocation routines
/*****************************************************************************/

void*
safe_malloc (size_t sz) {
void* ptr= mi_malloc (mi_good_size (sz));
if (ptr == NULL) {
cerr << "Fatal error: out of memory\n";
abort ();
}
return ptr;
}

void*
fast_alloc (size_t sz) {
return safe_malloc (sz);
return mi_malloc (sz);
}

void
Expand All @@ -40,7 +31,7 @@ fast_free (void* ptr, size_t sz) {

void*
fast_new (size_t s) {
return safe_malloc (s);
return mi_malloc (s);
}

void
Expand Down Expand Up @@ -73,3 +64,33 @@ mem_info () {
cout << "\n---------------- memory statistics ----------------\n";
cout << "malloc overrided:" << mi_is_redirected () << "\n";
}

void
mem_err_handler (int err, void* arg) {
switch (err) {
case ENOMEM:
cerr << "Fatal error: out of memory\n";
break;
case EINVAL:
cerr << "Fatal error: invalid pointer\n";
break;
case EFAULT:
cerr << "Corrupted heap. Please check whether there are objects used after "
"free.\n";
break;
case EOVERFLOW:
cerr << "Fatal error: input argument of allocator is too large\n";
break;
case EAGAIN:
cerr << "Double free detected\n";
break;
default:
break;
}
abort ();
}

void
mem_init () {
mi_register_error (mem_err_handler, NULL);
}
1 change: 1 addition & 0 deletions System/Misc/sys_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ namespace lolly {
void
init_tbox () {
if (!tb_init (tb_null, tb_null)) exit (-1);
mem_init ();
}

string
Expand Down
1 change: 1 addition & 0 deletions tests/Kernel/Abstractions/blackbox_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "a_lolly_test.hpp"
#include "blackbox.hpp"
#include "string.hpp"
#include <climits>

blackbox b0;
blackbox b1 = close_box<SI> (-2147483648); // int
Expand Down
1 change: 1 addition & 0 deletions tests/Kernel/Containers/hashmap_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "a_lolly_test.hpp"
#include "hashmap.hpp"
#include <string>

TEST_CASE ("test_resize") {
auto hm= hashmap<int, int> (0, 10);
Expand Down
2 changes: 2 additions & 0 deletions tests/Kernel/Containers/hashset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ TEST_CASE ("test contains") {
set->insert ("Hello");
CHECK_EQ (set->contains ("Hello"), true);
CHECK_EQ (set->contains ("hello"), false);
auto empty= hashset<string> ();
CHECK_EQ (set->contains ("hello"), false);
}

TEST_CASE ("test init") {
Expand Down
1 change: 1 addition & 0 deletions tests/Kernel/Containers/hashtree_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "a_lolly_test.hpp"
#include "hashtree.hpp"
#include "string.hpp"
#include <string>

TEST_CASE ("Test the add_child function") {
hashtree<int, string> t;
Expand Down
1 change: 0 additions & 1 deletion tests/Kernel/Types/string_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "a_lolly_test.hpp"
#include "string.hpp"
#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>

static ankerl::nanobench::Bench bench;
Expand Down
2 changes: 1 addition & 1 deletion tests/System/Classes/file_url_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* \author Darcy
* \date 2019-2023
*/
#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "url.hpp"

#if defined(OS_MINGW) || defined(OS_WIN)
Expand Down
1 change: 1 addition & 0 deletions tests/System/Classes/tm_timer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "lolly/system/timer.hpp"
#include "string.hpp"
#include "tm_timer.hpp"
#include <cstring>

#include "tbox/tbox.h"

Expand Down
3 changes: 2 additions & 1 deletion tests/System/Classes/url_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* \author Darcy
* \date 2019-2023
*/
#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "sys_utils.hpp"
#include "url.hpp"

url ustc_edu = url_system ("https://ustc.edu.cn");
Expand Down
13 changes: 5 additions & 8 deletions tests/System/Files/file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* \author Darcy
* \date 2019-2023
*/
#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "file.hpp"
#include "sys_utils.hpp"
#include "tbox/tbox.h"
#include <cstring>

url unix_root= url_system ("/");
url xmake_lua= url_pwd () * "xmake.lua";
Expand Down Expand Up @@ -259,15 +261,10 @@ TEST_CASE ("load_string from newly created file") {
const char* s2 = "hello world";
tb_size_t size = strlen (s2);
tb_byte_t* buffer= (tb_byte_t*) tb_malloc_bytes (size);
int seek = 0;
while (seek < size) {
tb_byte_t c = s2[seek];
buffer[seek]= c;
seek++;
}
memcpy (buffer, s2, size);

auto file_ref= tb_file_init (s1, TB_FILE_MODE_RW);
tb_file_writ (file_ref, buffer, strlen (s2));
tb_file_writ (file_ref, buffer, size);
string s;
CHECK (!load_string (u1, s, false));
string_eq (s, string ("hello world"));
Expand Down
11 changes: 0 additions & 11 deletions tests/System/Memory/fast_alloc_test.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
#include "a_lolly_test.hpp"
#include "fast_alloc.hpp"
#include "tm_timer.hpp"
#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>
static ankerl::nanobench::Bench bench;

int
usec_diff (time_t start, time_t end) {
if (start <= end) {
return end - start;
}
else {
return end + 1000000 - start;
}
}

struct Complex {
public:
double re, im;
Expand Down
2 changes: 1 addition & 1 deletion tests/System/Misc/sys_utils_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "sys_utils.hpp"

TEST_MEMORY_LEAK_INIT
Expand Down
2 changes: 0 additions & 2 deletions tests/a_lolly_test.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include "doctest/doctest.h"
#include "lolly_doctests.hpp"
2 changes: 2 additions & 0 deletions tests/a_tbox_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "doctest/doctest.h"
#include "lolly_doctests.hpp"
#include "sys_utils.hpp"
#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>

int
main (int argc, char** argv) {
Expand Down
3 changes: 2 additions & 1 deletion tests/lolly/data/numeral_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "lolly/data/numeral.hpp"
#include <climits>
#include <cstdint>

using namespace lolly::data;
Expand Down
2 changes: 1 addition & 1 deletion tests/lolly/data/unicode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "lolly/data/unicode.hpp"

using lolly::data::has_cjk_unified_ideographs;
Expand Down
2 changes: 1 addition & 1 deletion tests/lolly/hash/md5_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "file.hpp"
#include "lolly/hash/md5.hpp"

Expand Down
2 changes: 1 addition & 1 deletion tests/lolly/hash/sha_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "file.hpp"
#include "lolly/hash/sha.hpp"

Expand Down
2 changes: 1 addition & 1 deletion tests/lolly/io/http_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* \date 2023
*/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "file.hpp"
#include "generic_tree.hpp"
#include "hashmap.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/lolly/system/subprocess_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "a_tbox_main.cpp"
#include "a_lolly_test.hpp"
#include "lolly/system/subprocess.hpp"
#include "sys_utils.hpp"

Expand Down
27 changes: 23 additions & 4 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function add_test_target(filepath)
local testname = path.basename(filepath)
target(testname) do
set_group("tests")
add_deps("liblolly")
add_deps("test_base")
set_languages("c++17")
set_policy("check.auto_ignore_flags", false)

Expand Down Expand Up @@ -241,9 +241,7 @@ function add_test_target(filepath)

if is_plat("wasm") then
add_cxxflags("-s DISABLE_EXCEPTION_CATCHING=0")
add_ldflags("--preload-file xmake.lua")
add_ldflags("--preload-file tests")
add_ldflags("--preload-file LICENSE")
set_values("wasm.preloadfiles", {"xmake.lua", "tests", "LICENSE"})
add_ldflags("-s DISABLE_EXCEPTION_CATCHING=0")
on_run(function (target)
node = os.getenv("EMSDK_NODE")
Expand Down Expand Up @@ -289,6 +287,27 @@ end


if has_config("enable_tests") then
target("test_base")do
set_kind("object")
add_deps("liblolly")
set_languages("c++17")
set_policy("check.auto_ignore_flags", false)
add_packages("tbox")
add_packages("doctest")
add_packages("nanobench")

if is_plat("windows") then
set_encodings("utf-8")
elseif is_plat("wasm") then
add_cxxflags("-s DISABLE_EXCEPTION_CATCHING=0")
end
add_includedirs("$(buildir)/L1")
add_includedirs(lolly_includedirs)
add_includedirs("tests")
add_forceincludes(path.absolute("$(buildir)/L1/config.h"))
add_files("tests/a_tbox_main.cpp")
end

cpp_tests_on_all_plat = os.files("tests/**_test.cpp")
for _, filepath in ipairs(cpp_tests_on_all_plat) do
add_test_target (filepath)
Expand Down