From 7994c0c6177a36d3525ace705ac599b66c606462 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Thu, 12 Apr 2018 18:42:10 -0400 Subject: [PATCH 1/4] Trim as much version(unittest) as possible from allocator --- .../allocator/building_blocks/allocator_list.d | 1 - .../allocator/building_blocks/bitmapped_block.d | 1 - .../building_blocks/fallback_allocator.d | 1 - .../allocator/building_blocks/free_list.d | 1 - .../allocator/building_blocks/free_tree.d | 2 -- .../building_blocks/kernighan_ritchie.d | 4 +--- .../allocator/building_blocks/scoped_allocator.d | 1 - std/experimental/allocator/common.d | 9 ++++++--- std/experimental/allocator/mallocator.d | 16 +++++++--------- 9 files changed, 14 insertions(+), 22 deletions(-) diff --git a/std/experimental/allocator/building_blocks/allocator_list.d b/std/experimental/allocator/building_blocks/allocator_list.d index 95f09628fc8..c45e296d3b3 100644 --- a/std/experimental/allocator/building_blocks/allocator_list.d +++ b/std/experimental/allocator/building_blocks/allocator_list.d @@ -7,7 +7,6 @@ module std.experimental.allocator.building_blocks.allocator_list; import std.experimental.allocator.building_blocks.null_allocator; import std.experimental.allocator.common; import std.experimental.allocator.gc_allocator; -version(unittest) import std.stdio; // Turn this on for debugging // debug = allocator_list; diff --git a/std/experimental/allocator/building_blocks/bitmapped_block.d b/std/experimental/allocator/building_blocks/bitmapped_block.d index 59888637588..7164cdc59ef 100644 --- a/std/experimental/allocator/building_blocks/bitmapped_block.d +++ b/std/experimental/allocator/building_blocks/bitmapped_block.d @@ -53,7 +53,6 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment import std.typecons : Ternary; import std.typecons : tuple, Tuple; - version(unittest) @system unittest { import std.algorithm.comparison : max; diff --git a/std/experimental/allocator/building_blocks/fallback_allocator.d b/std/experimental/allocator/building_blocks/fallback_allocator.d index 1ebaca171b3..beb19f14870 100644 --- a/std/experimental/allocator/building_blocks/fallback_allocator.d +++ b/std/experimental/allocator/building_blocks/fallback_allocator.d @@ -29,7 +29,6 @@ struct FallbackAllocator(Primary, Fallback) // Need both allocators to be stateless // This is to avoid using default initialized stateful allocators - version(unittest) static if (!stateSize!Primary && !stateSize!Fallback) @system unittest { diff --git a/std/experimental/allocator/building_blocks/free_list.d b/std/experimental/allocator/building_blocks/free_list.d index 4a1934750fc..c93b412b3a8 100644 --- a/std/experimental/allocator/building_blocks/free_list.d +++ b/std/experimental/allocator/building_blocks/free_list.d @@ -120,7 +120,6 @@ struct FreeList(ParentAllocator, _max = high; } - version(unittest) @system unittest { import std.experimental.allocator.common : chooseAtRuntime; diff --git a/std/experimental/allocator/building_blocks/free_tree.d b/std/experimental/allocator/building_blocks/free_tree.d index 92db3b4aeb4..0b777dc26a0 100644 --- a/std/experimental/allocator/building_blocks/free_tree.d +++ b/std/experimental/allocator/building_blocks/free_tree.d @@ -335,7 +335,6 @@ struct FreeTree(ParentAllocator) return true; } - version(unittest) @system unittest // test a few simple configurations { import std.experimental.allocator.gc_allocator; @@ -357,7 +356,6 @@ struct FreeTree(ParentAllocator) assert(a.formatSizes == "(_)", a.formatSizes); } - version(unittest) @system unittest // build a complex free tree { import std.experimental.allocator.gc_allocator, std.range; diff --git a/std/experimental/allocator/building_blocks/kernighan_ritchie.d b/std/experimental/allocator/building_blocks/kernighan_ritchie.d index 2630f33c4f7..190cd7274e4 100644 --- a/std/experimental/allocator/building_blocks/kernighan_ritchie.d +++ b/std/experimental/allocator/building_blocks/kernighan_ritchie.d @@ -6,7 +6,6 @@ module std.experimental.allocator.building_blocks.kernighan_ritchie; import std.experimental.allocator.building_blocks.null_allocator; //debug = KRRegion; -version(unittest) import std.conv : text; debug(KRRegion) import std.stdio; // KRRegion @@ -554,7 +553,6 @@ struct KRRegion(ParentAllocator = NullAllocator) } /// - version(unittest) @system unittest { import std.experimental.allocator.gc_allocator : GCAllocator; @@ -776,7 +774,7 @@ it actually returns memory to the operating system when possible. auto p = cast(KRRegion!()* ) store.ptr; import core.stdc.string : memcpy; import std.algorithm.mutation : move; - import std.conv : emplace; + import std.conv : text, emplace; memcpy(p, &alloc, alloc.sizeof); emplace(&alloc); diff --git a/std/experimental/allocator/building_blocks/scoped_allocator.d b/std/experimental/allocator/building_blocks/scoped_allocator.d index 87a0cf2bf26..7bc318b05ba 100644 --- a/std/experimental/allocator/building_blocks/scoped_allocator.d +++ b/std/experimental/allocator/building_blocks/scoped_allocator.d @@ -23,7 +23,6 @@ struct ScopedAllocator(ParentAllocator) static if (!stateSize!ParentAllocator) { // This test is available only for stateless allocators - version(unittest) @system unittest { testAllocator!(() => ScopedAllocator()); diff --git a/std/experimental/allocator/common.d b/std/experimental/allocator/common.d index f73fc7c8abb..7d17b10a1af 100644 --- a/std/experimental/allocator/common.d +++ b/std/experimental/allocator/common.d @@ -481,7 +481,6 @@ Forwards each of the methods in `funs` (if defined) to `member`. version(unittest) { - import std.experimental.allocator : RCIAllocator, RCISharedAllocator; package void testAllocator(alias make)() { @@ -608,9 +607,13 @@ version(unittest) } package void testAllocatorObject(RCAllocInterface)(RCAllocInterface a) - if (is(RCAllocInterface == RCIAllocator) - || is (RCAllocInterface == shared RCISharedAllocator)) { + // this used to be a template constraint, but moving it inside prevents + // unnecessary import of std.experimental.allocator + import std.experimental.allocator : RCIAllocator, RCISharedAllocator; + static assert(is(RCAllocInterface == RCIAllocator) + || is (RCAllocInterface == shared RCISharedAllocator)); + import std.conv : text; import std.math : isPowerOf2; import std.stdio : writeln, stderr; diff --git a/std/experimental/allocator/mallocator.d b/std/experimental/allocator/mallocator.d index 68c21f7ad31..a98a637b8b4 100644 --- a/std/experimental/allocator/mallocator.d +++ b/std/experimental/allocator/mallocator.d @@ -12,7 +12,6 @@ import std.experimental.allocator.common; */ struct Mallocator { - version(unittest) @system unittest { testAllocator!(() => Mallocator.instance); } /** @@ -363,10 +362,6 @@ version(LDC_AddressSanitizer) //... } -version(unittest) version(CRuntime_DigitalMars) -@nogc nothrow -size_t addr(ref void* ptr) { return cast(size_t) ptr; } - version(Posix) @nogc @system nothrow unittest { @@ -399,24 +394,27 @@ version(CRuntime_DigitalMars) { void* m; + @nogc nothrow + size_t addr() { return cast(size_t) m; } + m = _aligned_malloc(16, 0x10); if (m) { - assert((m.addr & 0xF) == 0); + assert((addr & 0xF) == 0); _aligned_free(m); } m = _aligned_malloc(16, 0x100); if (m) { - assert((m.addr & 0xFF) == 0); + assert((addr & 0xFF) == 0); _aligned_free(m); } m = _aligned_malloc(16, 0x1000); if (m) { - assert((m.addr & 0xFFF) == 0); + assert((addr & 0xFFF) == 0); _aligned_free(m); } @@ -425,7 +423,7 @@ version(CRuntime_DigitalMars) { assert((cast(size_t) m & 0xF) == 0); m = _aligned_realloc(m, 32, 0x10000); - if (m) assert((m.addr & 0xFFFF) == 0); + if (m) assert((addr & 0xFFFF) == 0); _aligned_free(m); } From 37ef806ab6e60730b60c8f8af01bf2fc087d0f6b Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 13 Apr 2018 09:03:10 -0400 Subject: [PATCH 2/4] improve internal test function addr to m_addr (and fix verbosity a bit) --- std/experimental/allocator/mallocator.d | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/std/experimental/allocator/mallocator.d b/std/experimental/allocator/mallocator.d index a98a637b8b4..2d3f851d9bc 100644 --- a/std/experimental/allocator/mallocator.d +++ b/std/experimental/allocator/mallocator.d @@ -394,27 +394,26 @@ version(CRuntime_DigitalMars) { void* m; - @nogc nothrow - size_t addr() { return cast(size_t) m; } + alias m_addr = () => cast(size_t) m; m = _aligned_malloc(16, 0x10); if (m) { - assert((addr & 0xF) == 0); + assert((m_addr & 0xF) == 0); _aligned_free(m); } m = _aligned_malloc(16, 0x100); if (m) { - assert((addr & 0xFF) == 0); + assert((m_addr & 0xFF) == 0); _aligned_free(m); } m = _aligned_malloc(16, 0x1000); if (m) { - assert((addr & 0xFFF) == 0); + assert((m_addr & 0xFFF) == 0); _aligned_free(m); } @@ -423,7 +422,7 @@ version(CRuntime_DigitalMars) { assert((cast(size_t) m & 0xF) == 0); m = _aligned_realloc(m, 32, 0x10000); - if (m) assert((addr & 0xFFFF) == 0); + if (m) assert((m_addr & 0xFFFF) == 0); _aligned_free(m); } From ba03157e34a87192323eaaa3d7f05c97e0ae5c26 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 13 Apr 2018 09:05:31 -0400 Subject: [PATCH 3/4] Remove shared from RCISharedAllocator, as it's already shared --- std/experimental/allocator/common.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/experimental/allocator/common.d b/std/experimental/allocator/common.d index 7d17b10a1af..d505669e106 100644 --- a/std/experimental/allocator/common.d +++ b/std/experimental/allocator/common.d @@ -612,7 +612,7 @@ version(unittest) // unnecessary import of std.experimental.allocator import std.experimental.allocator : RCIAllocator, RCISharedAllocator; static assert(is(RCAllocInterface == RCIAllocator) - || is (RCAllocInterface == shared RCISharedAllocator)); + || is (RCAllocInterface == RCISharedAllocator)); import std.conv : text; import std.math : isPowerOf2; From cdc5310dd79427eb2dbb00f6499d955f70488d56 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 13 Apr 2018 10:05:19 -0400 Subject: [PATCH 4/4] Fix issue with m_addr --- std/experimental/allocator/mallocator.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/experimental/allocator/mallocator.d b/std/experimental/allocator/mallocator.d index 2d3f851d9bc..ec1117b82d1 100644 --- a/std/experimental/allocator/mallocator.d +++ b/std/experimental/allocator/mallocator.d @@ -394,7 +394,7 @@ version(CRuntime_DigitalMars) { void* m; - alias m_addr = () => cast(size_t) m; + size_t m_addr() { return cast(size_t) m; } m = _aligned_malloc(16, 0x10); if (m)