• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libKriging / libKriging / 20211252547

14 Dec 2025 05:03PM UTC coverage: 67.712% (+30.3%) from 37.371%
20211252547

push

github

web-flow
Merge pull request #303 from libKriging/multistart-optim_jemalloc

Multistart optim

3946 of 4387 new or added lines in 30 files covered. (89.95%)

20 existing lines in 5 files now uncovered.

8403 of 12410 relevant lines covered (67.71%)

53457.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.0
/src/lib/lkalloc.cpp
1
#include "libKriging/utils/lkalloc.hpp"
2

3
// #define LIBKRIGING_DEBUG_ALLOC
4

5
#ifdef LIBKRIGING_DEBUG_ALLOC
6
#include <iostream>
7
#include <unordered_set>
8
#include <mutex>
9
#endif
10

11
#include <cassert>
12
#include <cstdlib>
13
#include <mutex>
14

15
// When USE_JEMALLOC is enabled, jemalloc's malloc/free override the standard ones via linking
16
// No need to explicitly call je_malloc/je_free
17

18
#if WIN32
19
#define _AMD64_
20
#include <libloaderapi.h>
21
#include <string>
22

23
HMODULE g_Handle;
24

25
EXTERN_C BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_opt_ LPVOID lpvReserved) {
26
  UNREFERENCED_PARAMETER(lpvReserved);
27

28
  if (fdwReason == DLL_PROCESS_ATTACH) {
29
    DisableThreadLibraryCalls(hinstDLL);
30
    g_Handle = hinstDLL;
31
  }
32

33
  return TRUE;
34
}
35

36
std::string dllName() {
37
  TCHAR dllName[MAX_PATH + 1];
38
  GetModuleFileName(g_Handle, dllName, MAX_PATH);
39
  return dllName;
40
}
41
#else
42
#include <string>
43
std::string dllName() {
×
44
  return {"<undefined>"};
×
45
}
46
#endif
47

48
namespace lkalloc {
49

50
void* (*custom_malloc)(size_t) = nullptr;
51
void (*custom_free)(void*) = nullptr;
52

53
#ifdef LIBKRIGING_DEBUG_ALLOC
54
std::unordered_set<void*> seens;
55
std::mutex debug_mutex;
56
#endif
57

58
LIBKRIGING_EXPORT
UNCOV
59
void* malloc(size_t n_bytes) {
×
60
#ifdef LIBKRIGING_DEBUG_ALLOC
61
  static int count = 0;
62
  {
63
    std::lock_guard<std::mutex> lock(debug_mutex);
64
    ++count;
65
  }
66
  // std::cout << "Using lkalloc allocator " /* << custom_malloc */ << " (#" << count << ") in " << dllName() << "\n";
67
#endif
UNCOV
68
  void* mem_ptr = nullptr;
×
UNCOV
69
  if (custom_malloc) {
×
UNCOV
70
    mem_ptr = (*custom_malloc)(n_bytes);
×
71
  } else {
72
#ifdef _MSC_VER
73
    const size_t alignment = (n_bytes >= size_t(1024)) ? size_t(32) : size_t(16);
74
    mem_ptr = _aligned_malloc(n_bytes, alignment);
75
#else
UNCOV
76
    mem_ptr = ::malloc(n_bytes);
×
77
#endif
78
  }
79
#ifdef LIBKRIGING_DEBUG_ALLOC
80
  {
81
    std::lock_guard<std::mutex> lock(debug_mutex);
82
    seens.insert(mem_ptr);
83
  }
84
#endif
UNCOV
85
  return mem_ptr;
×
86
}
87

88
LIBKRIGING_EXPORT
UNCOV
89
void free(void* mem_ptr) {
×
90
#ifdef LIBKRIGING_DEBUG_ALLOC
91
  static int count = 0;
92
  {
93
    std::lock_guard<std::mutex> lock(debug_mutex);
94
    ++count;
95
    // std::cout << "Using lkalloc deallocator " /* << custom_free */ << " (#" << count << ") in " << dllName() << "\n";
96
    if (seens.find(mem_ptr) == seens.end()) {
97
      std::cout << "### (#" << count << ") lkalloc allocator has never seen " << mem_ptr << " ##" << std::endl;
98
      return;
99
    }
100
  }
101
#endif
UNCOV
102
  if (custom_free) {
×
UNCOV
103
    (*custom_free)(mem_ptr);
×
104
  } else {
105
#ifdef _MSC_VER
106
    return _aligned_free(mem_ptr);
107
#else
UNCOV
108
    ::free(mem_ptr);
×
109
#endif
110
  }
111
#ifdef LIBKRIGING_DEBUG_ALLOC
112
  {
113
    std::lock_guard<std::mutex> lock(debug_mutex);
114
    seens.erase(mem_ptr);
115
  }
116
#endif
UNCOV
117
}
×
118

119
LIBKRIGING_EXPORT
120
void set_allocation_functions(void* (*allocator)(size_t), void (*deallocator)(void*)) {
14✔
121
  assert(custom_malloc == nullptr && custom_free == nullptr);
14✔
122
  assert(allocator != nullptr && deallocator != nullptr);
14✔
123
  custom_malloc = allocator;
14✔
124
  custom_free = deallocator;
14✔
125
}
14✔
126

127
LIBKRIGING_EXPORT
128
void unset_allocation_functions() {
×
129
  assert(custom_malloc != nullptr && custom_free != nullptr);
×
130
  custom_malloc = nullptr;
×
131
  custom_free = nullptr;
×
132
}
×
133

134
}  // namespace lkalloc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc