Skip to content

Commit adce1b9

Browse files
committed
Make test compile faster.
1 parent 6dbf08a commit adce1b9

37 files changed

Lines changed: 614 additions & 150 deletions

File tree

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
449449
target_compile_definitions(${TESTNAME} PRIVATE ${DEFINES})
450450
endif()
451451

452+
# Link the pre-compiled test library into all tests. For tests
453+
# that include snmalloc.h directly the linker will simply discard
454+
# the duplicate inline definitions (ODR-safe).
455+
target_link_libraries(${TESTNAME} snmalloc-testlib-${TAG})
456+
452457
if (${TEST} MATCHES "release-.*")
453458
message(VERBOSE "Adding test: ${TESTNAME} only for release configs")
454459
add_test(NAME ${TESTNAME} COMMAND ${TESTNAME} CONFIGURATIONS "Release")
@@ -480,6 +485,18 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
480485
endforeach()
481486
endfunction()
482487

488+
function(build_test_library TAG DEFINES)
489+
set(LIBNAME snmalloc-testlib-${TAG})
490+
add_library(${LIBNAME} STATIC src/test/snmalloc_testlib.cc)
491+
target_link_libraries(${LIBNAME} PRIVATE snmalloc)
492+
set_target_properties(${LIBNAME} PROPERTIES INTERFACE_LINK_LIBRARIES "")
493+
target_compile_definitions(${LIBNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP}")
494+
if (NOT DEFINES STREQUAL " ")
495+
target_compile_definitions(${LIBNAME} PRIVATE ${DEFINES})
496+
endif()
497+
add_warning_flags(${LIBNAME})
498+
endfunction()
499+
483500
if(NOT (DEFINED SNMALLOC_LINKER_FLAVOUR) OR ("${SNMALLOC_LINKER_FLAVOUR}" MATCHES "^$"))
484501
# Linker not specified externally; probe to see if we can make lld work
485502
set(CMAKE_REQUIRED_LINK_OPTIONS -fuse-ld=lld -Wl,--icf=all)
@@ -622,6 +639,7 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
622639
set(DEFINES " ")
623640
endif()
624641

642+
build_test_library(${FLAVOUR} ${DEFINES})
625643
make_tests(${FLAVOUR} ${DEFINES})
626644
endforeach()
627645
endif()

src/snmalloc/ds/ds.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
#include "entropy.h"
1212
#include "mpmcstack.h"
1313
#include "pagemap.h"
14+
#include "pool.h"
15+
#include "pooled.h"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "../ds/ds.h"
3+
#include "ds.h"
44
#include "pooled.h"
55

66
namespace snmalloc
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

3-
#include "../ds/ds.h"
4-
#include "backend_concept.h"
3+
#include "ds.h"
54

65
namespace snmalloc
76
{

src/snmalloc/ds_core/defines.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ namespace snmalloc
123123
static constexpr bool Debug = true;
124124
#endif
125125

126-
// Forwards reference so that the platform can define how to handle errors.
126+
// Forward references so that the platform can define how to handle
127+
// errors and messages. Definitions are provided in pal/pal.h once
128+
// DefaultPal is known.
127129
[[noreturn]] SNMALLOC_COLD void error(const char* const str);
130+
void message_impl(const char* const str);
128131
} // namespace snmalloc
129132

130133
#define TOSTRING(expr) TOSTRING2(expr)
@@ -213,21 +216,21 @@ namespace snmalloc
213216

214217
namespace snmalloc
215218
{
219+
template<typename... Args>
220+
SNMALLOC_FAST_PATH_INLINE void UNUSED(Args&&...)
221+
{}
222+
216223
/**
217-
* Forward declaration so that this can be called before the pal header is
218-
* included.
224+
* Forward declaration so that this can be called before helpers.h is
225+
* included (e.g. in SNMALLOC_ASSERT macros expanded inside bits.h).
219226
*/
220227
template<size_t BufferSize = 1024, typename... Args>
221228
[[noreturn]] inline void report_fatal_error(Args... args);
222229

223230
/**
224-
* Forward declaration so that this can be called before the pal header is
231+
* Forward declaration so that this can be called before helpers.h is
225232
* included.
226233
*/
227234
template<size_t BufferSize = 1024, typename... Args>
228235
inline void message(Args... args);
229-
230-
template<typename... Args>
231-
SNMALLOC_FAST_PATH_INLINE void UNUSED(Args&&...)
232-
{}
233236
} // namespace snmalloc

src/snmalloc/ds_core/helpers.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "bits.h"
44
#include "snmalloc/ds_core/defines.h"
5+
#include "snmalloc/ds_core/tid.h"
56
#include "snmalloc/stl/array.h"
67
#include "snmalloc/stl/type_traits.h"
78
#include "snmalloc/stl/utility.h"
@@ -339,6 +340,38 @@ namespace snmalloc
339340
}
340341
};
341342

343+
/**
344+
* Report a fatal error via a PAL-specific error reporting mechanism. This
345+
* takes a format string and a set of arguments. The format string indicates
346+
* the remaining arguments with "{}". This could be extended later to
347+
* support indexing fairly easily, if we ever want to localise these error
348+
* messages.
349+
*
350+
* The following are supported as arguments:
351+
*
352+
* - Characters (`char`), printed verbatim.
353+
* - Strings Literals (`const char*` or `const char[]`), printed verbatim.
354+
* - Raw pointers (void*), printed as hex strings.
355+
* - Integers (convertible to `size_t`), printed as hex strings.
356+
*
357+
* These types should be sufficient for allocator-related error messages.
358+
*/
359+
template<size_t BufferSize, typename... Args>
360+
[[noreturn]] inline void report_fatal_error(Args... args)
361+
{
362+
MessageBuilder<BufferSize> msg{stl::forward<Args>(args)...};
363+
error(msg.get_message());
364+
}
365+
366+
template<size_t BufferSize, typename... Args>
367+
inline void message(Args... args)
368+
{
369+
MessageBuilder<BufferSize> msg{stl::forward<Args>(args)...};
370+
MessageBuilder<BufferSize> msg_tid{
371+
"{}: {}", debug_get_tid(), msg.get_message()};
372+
message_impl(msg_tid.get_message());
373+
}
374+
342375
/**
343376
* Convenience type that has no fields / methods.
344377
*/

src/snmalloc/ds_core/redblacktree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "snmalloc/ds_core/concept.h"
34
#include "snmalloc/stl/array.h"
45

56
#include <stddef.h>

src/snmalloc/global/globalalloc.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,13 @@ namespace snmalloc
342342
aligned_size(align, size));
343343
}
344344

345-
SNMALLOC_FAST_PATH_INLINE void dealloc(void* p)
345+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void dealloc(void* p)
346346
{
347347
ThreadAlloc::get().dealloc<ThreadAlloc::CheckInit>(p);
348348
}
349349

350-
SNMALLOC_FAST_PATH_INLINE void dealloc(void* p, size_t size)
350+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void
351+
dealloc(void* p, size_t size)
351352
{
352353
check_size(p, size);
353354
ThreadAlloc::get().dealloc<ThreadAlloc::CheckInit>(p);
@@ -360,14 +361,15 @@ namespace snmalloc
360361
ThreadAlloc::get().dealloc<ThreadAlloc::CheckInit>(p);
361362
}
362363

363-
SNMALLOC_FAST_PATH_INLINE void dealloc(void* p, size_t size, size_t align)
364+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void
365+
dealloc(void* p, size_t size, size_t align)
364366
{
365367
auto rsize = aligned_size(align, size);
366368
check_size(p, rsize);
367369
ThreadAlloc::get().dealloc<ThreadAlloc::CheckInit>(p);
368370
}
369371

370-
SNMALLOC_FAST_PATH_INLINE void debug_teardown()
372+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void debug_teardown()
371373
{
372374
return ThreadAlloc::teardown();
373375
}

src/snmalloc/global/libc.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ namespace snmalloc::libc
2424
return snmalloc::external_pointer<OnePastEnd>(ptr);
2525
}
2626

27-
SNMALLOC_FAST_PATH_INLINE void* malloc(size_t size)
27+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void* malloc(size_t size)
2828
{
2929
return snmalloc::alloc(size);
3030
}
3131

32-
SNMALLOC_FAST_PATH_INLINE void free(void* ptr)
32+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void free(void* ptr)
3333
{
3434
dealloc(ptr);
3535
}
3636

37-
SNMALLOC_FAST_PATH_INLINE void free_sized(void* ptr, size_t size)
37+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void
38+
free_sized(void* ptr, size_t size)
3839
{
3940
dealloc(ptr, size);
4041
}
4142

42-
SNMALLOC_FAST_PATH_INLINE void* calloc(size_t nmemb, size_t size)
43+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void*
44+
calloc(size_t nmemb, size_t size)
4345
{
4446
bool overflow = false;
4547
size_t sz = bits::umul(size, nmemb, overflow);
@@ -50,7 +52,8 @@ namespace snmalloc::libc
5052
return alloc<Zero>(sz);
5153
}
5254

53-
SNMALLOC_FAST_PATH_INLINE void* realloc(void* ptr, size_t size)
55+
SNMALLOC_USED_FUNCTION SNMALLOC_FAST_PATH_INLINE void*
56+
realloc(void* ptr, size_t size)
5457
{
5558
// Glibc treats
5659
// realloc(p, 0) as free(p)

src/snmalloc/mem/corealloc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "check_init.h"
55
#include "freelist.h"
66
#include "metadata.h"
7-
#include "pool.h"
87
#include "remotecache.h"
98
#include "sizeclasstable.h"
109
#include "snmalloc/stl/new.h"

0 commit comments

Comments
 (0)