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

make_shared() For Arrays #309

Merged
merged 62 commits into from
Apr 8, 2020

Commits on Nov 18, 2019

  1. Basic implementation

    AdamBucior authored Nov 18, 2019
    Configuration menu
    Copy the full SHA
    3e6a2ff View commit details
    Browse the repository at this point in the history
  2. Update yvals_core.h

    AdamBucior authored Nov 18, 2019
    Configuration menu
    Copy the full SHA
    52de1ba View commit details
    Browse the repository at this point in the history

Commits on Nov 19, 2019

  1. Configuration menu
    Copy the full SHA
    e169d41 View commit details
    Browse the repository at this point in the history
  2. Formatting

    AdamBucior authored Nov 19, 2019
    Configuration menu
    Copy the full SHA
    dad50ea View commit details
    Browse the repository at this point in the history

Commits on Nov 21, 2019

  1. Lots of Fixes

    AdamBucior authored Nov 21, 2019
    Configuration menu
    Copy the full SHA
    07aaa50 View commit details
    Browse the repository at this point in the history
  2. Transition LLVM-42404

    AdamBucior authored Nov 21, 2019
    Configuration menu
    Copy the full SHA
    ab24fa6 View commit details
    Browse the repository at this point in the history
  3. More Fixes

    AdamBucior authored Nov 21, 2019
    Configuration menu
    Copy the full SHA
    38e6b83 View commit details
    Browse the repository at this point in the history

Commits on Nov 28, 2019

  1. Configuration menu
    Copy the full SHA
    a586cd8 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    77db877 View commit details
    Browse the repository at this point in the history

Commits on Nov 29, 2019

  1. Configuration menu
    Copy the full SHA
    5bb85af View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    458424c View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    d778d04 View commit details
    Browse the repository at this point in the history

Commits on Dec 5, 2019

  1. Feature-test macro

    AdamBucior authored Dec 5, 2019
    Configuration menu
    Copy the full SHA
    53f3c4d View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    03bcabb View commit details
    Browse the repository at this point in the history
  3. Correct Encoding

    AdamBucior authored Dec 5, 2019
    Configuration menu
    Copy the full SHA
    11d8d93 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    e1fd4a1 View commit details
    Browse the repository at this point in the history
  5. Formatting

    AdamBucior authored Dec 5, 2019
    Configuration menu
    Copy the full SHA
    f929287 View commit details
    Browse the repository at this point in the history

Commits on Dec 6, 2019

  1. Configuration menu
    Copy the full SHA
    04d67e6 View commit details
    Browse the repository at this point in the history

Commits on Dec 12, 2019

  1. Configuration menu
    Copy the full SHA
    92ef947 View commit details
    Browse the repository at this point in the history
  2. Update yvals_core.h

    AdamBucior authored Dec 12, 2019
    Configuration menu
    Copy the full SHA
    8c98cf2 View commit details
    Browse the repository at this point in the history

Commits on Dec 14, 2019

  1. Configuration menu
    Copy the full SHA
    e2ba89b View commit details
    Browse the repository at this point in the history

Commits on Jan 29, 2020

  1. Configuration menu
    Copy the full SHA
    f64bfb0 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    fd8eef9 View commit details
    Browse the repository at this point in the history
  3. oh common clang-format

    AdamBucior authored Jan 29, 2020
    Configuration menu
    Copy the full SHA
    d5f0687 View commit details
    Browse the repository at this point in the history

Commits on Feb 3, 2020

  1. Formatting

    AdamBucior authored Feb 3, 2020
    Configuration menu
    Copy the full SHA
    e0d0f08 View commit details
    Browse the repository at this point in the history

Commits on Mar 2, 2020

  1. Configuration menu
    Copy the full SHA
    1726327 View commit details
    Browse the repository at this point in the history

Commits on Mar 3, 2020

  1. Configuration menu
    Copy the full SHA
    177bfa2 View commit details
    Browse the repository at this point in the history

Commits on Mar 4, 2020

  1. Configuration menu
    Copy the full SHA
    f0098a5 View commit details
    Browse the repository at this point in the history

Commits on Mar 5, 2020

  1. Adjust and test feature-test macros.

    Make __cpp_lib_shared_ptr_arrays vary with _HAS_CXX20 instead of
    __cpp_concepts (as the feature isn't inherently dependent on concepts).
    
    Now that more feature-test macros have varying values, extract them to
    separate preprocessor blocks where the logic is easier to read.
    StephanTLavavej committed Mar 5, 2020
    Configuration menu
    Copy the full SHA
    6722355 View commit details
    Browse the repository at this point in the history
  2. memory already includes xmemory, which includes xutility, which inclu…

    …des utility.
    
    Ironically, the STL's product code doesn't follow
    a coherent philosophy around "include what you use".
    StephanTLavavej committed Mar 5, 2020
    Configuration menu
    Copy the full SHA
    dcf7dbd View commit details
    Browse the repository at this point in the history
  3. Fix test typo affecting coverage.

    This was copy-pasted from make_shared, but it meant to test
    allocate_shared. Clang noticed that `a5` was unused.
    StephanTLavavej committed Mar 5, 2020
    Configuration menu
    Copy the full SHA
    7ae0911 View commit details
    Browse the repository at this point in the history

Commits on Mar 6, 2020

  1. Replace concepts usage with SFINAE.

    This is less elegant, but works for Clang 9 and EDG.
    StephanTLavavej committed Mar 6, 2020
    Configuration menu
    Copy the full SHA
    272afd6 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    bdc84c5 View commit details
    Browse the repository at this point in the history
  3. Fix sign-conversion warnings in the test.

    When constants like `2` are directly passed to functions taking
    `size_t`, the compiler can see that the value is being preserved,
    so it doesn't warn. However, when `2` is perfectly forwarded,
    only the type `int` is forwarded - the constant nature isn't part
    of the type system. So, the compiler will warn if it sees a forwarded
    `int` converted to `size_t`.
    
    The fix is to use `2u` etc. in the source code. `unsigned int`
    being converted to `size_t` is known to be value-preserving
    (for 64-bit `size_t`, it's a widening).
    StephanTLavavej committed Mar 6, 2020
    Configuration menu
    Copy the full SHA
    17f2ef6 View commit details
    Browse the repository at this point in the history
  4. Silence SAL annotation warning C28251 in the test.

    This is necessary whenever we replace `new` in test code.
    StephanTLavavej committed Mar 6, 2020
    Configuration menu
    Copy the full SHA
    11834ff View commit details
    Browse the repository at this point in the history
  5. Fix sign-conversion warning, use C++ casts.

    First, this was using functional-style C-semantics casts to convert
    from unsigned to signed. We conventionally avoid all C-semantics casts
    regardless of style (except `(void)`).
    
    Second, this was implicitly converting from signed back to unsigned,
    emitting warnings for which we attempt to be clean (even though
    they're off-by-default).
    
    Finally, this had unnecessary parentheses in the conditional operator;
    it's a judgement call, but stylistically we tend to avoid parentheses
    given these operators.
    StephanTLavavej committed Mar 6, 2020
    Configuration menu
    Copy the full SHA
    c80db82 View commit details
    Browse the repository at this point in the history
  6. FIXME: Silence buffer overrun warning C6386.

    This must be properly investigated before merging.
    StephanTLavavej committed Mar 6, 2020
    Configuration menu
    Copy the full SHA
    f762dee View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    e076167 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    07b7b16 View commit details
    Browse the repository at this point in the history

Commits on Mar 8, 2020

  1. Configuration menu
    Copy the full SHA
    42b09fe View commit details
    Browse the repository at this point in the history

Commits on Mar 11, 2020

  1. Configuration menu
    Copy the full SHA
    dbe217a View commit details
    Browse the repository at this point in the history
  2. Adjust comments and class/struct.

    In new code, we generally prefer to "detach" introductory comments
    from braces, especially when that reduces wrapping for the comment
    and the function parameters.
    
    Conversely, we generally prefer to attach TRANSITION comments to the
    affected lines, so we can easily see what needs to be adjusted
    when the mentioned bug is fixed.
    
    `_Uninitialized_rev_destroying_backout_al` is a class that uses
    private access control. Therefore, its comment should refer to it as a
    class. Additionally, we generally avoid relying on classes defaulting
    to private access control; this is one of the relatively few places
    where we prefer to repeat the default.
    StephanTLavavej committed Mar 11, 2020
    Configuration menu
    Copy the full SHA
    22b0262 View commit details
    Browse the repository at this point in the history
  3. Flip the specialization of _Ref_count_unbounded_array.

    This is a stylistic nitpick - when we have a primary template and a
    specialization, it's slightly more readable for the primary template to
    be the `true` case. That's because it's the one that mentions
    `is_trivially_destructible_v` here.
    
    (This diff looks messy, but it's purely moving code around.)
    StephanTLavavej committed Mar 11, 2020
    Configuration menu
    Copy the full SHA
    00454a2 View commit details
    Browse the repository at this point in the history

Commits on Mar 12, 2020

  1. Address some code review comments.

    Simplify `_Div_ceil`; still need to handle overflow here and elsewhere.
    
    Use RAII guards for reverse destruction. (Also use CTAD.)
    
    Drop unnecessary `if (_Size > 0)` branching.
    
    Comment `_Destroy_in_place(_Storage)` as being intentional.
    
    Drop unnecessary `static_cast<size_t>`.
    StephanTLavavej committed Mar 12, 2020
    Configuration menu
    Copy the full SHA
    68ad0e4 View commit details
    Browse the repository at this point in the history

Commits on Mar 13, 2020

  1. Configuration menu
    Copy the full SHA
    76a8e2c View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    1c9328e View commit details
    Browse the repository at this point in the history
  3. _Allocate_flexible_array refactoring, part 1.

    This carefully performs multiplication and addition overflow checks,
    and it avoids the use of `new[]` with aligned_storage_t that was
    emitting buffer overrun warnings. The new strategy is to give
    `_Ref_count_unbounded_array` storage for one element, and simply
    allocate additional space (via plain ::operator new) for additional
    elements if necessary. We need a bit of extra logic for alignment,
    performed with bitwise operations here (to avoid dividing and
    multiplying, even by a constant power of 2). (The concern is that if
    the refcount control block has an alignment of 8, and the elements have
    a size like 1, then the overall space requested might be an odd number
    like 1729, which might cause ::operator new to think that it doesn't
    need to return 8-aligned storage. By rounding up to the maximum
    alignment of the control block and the element, we avoid that issue.)
    
    This doesn't yet use an RAII guard for ::operator delete, but I plan
    to do so in the near future.
    
    This uses a non-_Ugly typedef element_type in internal code. This is
    slightly non-conventional, but safe (because it's a Standard typedef
    in shared_ptr, it can't be macroized). There's some existing precedent
    for this, and I wanted to reinforce that "this is the usual
    element_type definition that you've seen elsewhere".
    
    Part 2 will extend this to the allocator control blocks.
    StephanTLavavej committed Mar 13, 2020
    4 Configuration menu
    Copy the full SHA
    25d450f View commit details
    Browse the repository at this point in the history

Commits on Mar 17, 2020

  1. Configuration menu
    Copy the full SHA
    0d8aae3 View commit details
    Browse the repository at this point in the history

Commits on Mar 18, 2020

  1. Configuration menu
    Copy the full SHA
    eba51f7 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    a39cd1e View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    719ba7e View commit details
    Browse the repository at this point in the history

Commits on Mar 19, 2020

  1. _Allocate_flexible_array refactoring, part 2.

    This extends the refactoring to allocate_shared. It uses a "hybrid"
    approach, where the control block contains a union of one element
    (treated as a flexible array), giving the control block proper
    alignment, but storage for the control block is allocated as a
    sequence of `aligned_storage_t<_Align, _Align>` elements.
    
    This should resolve all overflow issues. Overflow checking is
    controllable because we don't need it during cleanup.
    
    Add comments citing N4849 [class.dtor]/7, explaining why these
    destructors must be defined with `{}` instead of `= default;`.
    StephanTLavavej committed Mar 19, 2020
    Configuration menu
    Copy the full SHA
    45742a4 View commit details
    Browse the repository at this point in the history

Commits on Mar 20, 2020

  1. Address all remaining code review feedback.

    Replace aligned_storage_t with _Alignas_storage_unit.
    
    Comment a meow_copy call within a meow_fill function.
    
    Use RAII guards for global delete and allocator deallocate.
    
    Respect fancy pointers.
    
    Use more CTAD.
    StephanTLavavej committed Mar 20, 2020
    Configuration menu
    Copy the full SHA
    21d9c48 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    7c6b5e4 View commit details
    Browse the repository at this point in the history

Commits on Apr 2, 2020

  1. Configuration menu
    Copy the full SHA
    b7ceade View commit details
    Browse the repository at this point in the history
  2. More code review improvements.

    Add `noexcept` to internal machinery.
    
    Instead of testing `(rank_v<_Ty>) > 0`, simply test `is_array_v<_Ty>`.
    
    Change `_Reverse_destroy_multidimensional_n_guard` to be an aggregate.
    (Note that Clang 10 doesn't support CTAD for aggregates.)
    
    Rework the optimization metaprogramming in
    `_Uninitialized_copy_multidimensional`. First,
    `_Ptr_copy_cat<_Ty*, _Ty*>::_Really_trivial` was incorrect. This is
    because `_Ptr_copy_cat` checks `is_trivially_assignable_v`, which will
    be false when `_Ty` is an array. Instead, we should test
    `is_trivial_v<_Ty>`. (We don't need the rest of what `_Ptr_copy_cat`
    does, because we're not working with user-defined iterators, and our
    source and destination are the same type.) Second, `is_trivial_v<_Ty>`
    can be the first thing we test; when we're working with trivial
    elements, we can perform a single `_Copy_memmove`, regardless of
    whether these are single-dimensional or multi-dimensional arrays.
    Finally, for `is_array_v<_Ty>`, it was strange (although correct) to
    construct an RAII guard for each iteration of the loop. We can lift
    this out and simply update the guard's index. (Same transformation for
    the following algorithms.)
    
    Similarly for `_Uninitialized_value_construct_multidimensional_n`,
    `_Use_memset_value_construct_v<_Ty*>` was incorrect as it checks
    `is_scalar` which is false for arrays. We need to use
    `remove_all_extents_t<_Ty>`, which I've been referring to as `_Item`.
    Also similarly, we can perform this `_Zero_range` optimization at the
    top level. Finally, we should consistently use `_Idx` instead of
    `_Count` to iterate (also changed below).
    
    In `_Uninitialized_fill_multidimensional_n`, add the
    `// intentionally copy, not fill` comment (previously applied to the
    allocator version).
    
    Mark `_Get_ptr()` as `_NODISCARD` and `noexcept`.
    
    Change pre-existing code for consistency: upgrade
    `_Ref_count_obj_alloc2` to `_Ref_count_obj_alloc3`. The difference is
    that we store _Rebind_alloc_t<_Alloc, _Ty> so we can directly use it
    later. (We still need to rebind for `_Delete_this`.) Also add a
    `static_assert` verifying that we're following the Standard's
    `remove_cv_t` wording.
    
    Change `_Reverse_destroy_multidimensional_n_al_guard` and its
    associated algorithms similarly to the non-allocator versions.
    
    In `_Uninitialized_copy_multidimensional_al`, change
    `_Uses_default_construct<_Alloc, _Ty*, _Ty>` to
    `_Uses_default_construct<_Alloc, _Item*, const _Item&>`. This is
    because the provided `_Alloc` is for the `_Item` (aka
    `remove_all_extents_t<_Ty>`) that we ultimately construct from a const
    lvalue.
    
    In `_Uninitialized_value_construct_multidimensional_n_al`, similarly
    change the `_Zero_range` metaprogramming.
    
    In `_Uninitialized_fill_multidimensional_n_al`, slightly adjust
    `_Uses_default_construct<_Alloc, _Ty*, _Ty>` to
    `_Uses_default_construct<_Alloc, _Ty*, const _Ty&>` because we
    construct from a const lvalue, and that distinction could be important.
    
    Change `_Ref_count_unbounded_array_alloc` to store
    `_Rebind_alloc_t<_Alloc, remove_all_extents_t<_Ty>>` so we don't need
    to rebind it later. Also `static_assert` `remove_cv_t`.
    
    Adjust the metaprogramming in `_Destroy()`.
    `is_trivially_destructible<_Element_type>` was correct (`_Element_type`
    removes the unbounded array, and the type trait "recurses" to the
    non-array item), but `is_trivially_destructible<_Item>` is what we're
    ultimately interested in, and will be more consistent with other
    control blocks.
    
    Also, `_Rebound` is never a reference now, and we have the `_Item`
    typedef.
    
    Fix `_Ref_count_bounded_array_alloc` comment; "for object" should be
    "for bounded array". Similarly change its implementation. Here,
    `is_trivially_destructible<_Ty>` was correct (`_Ty` is a bounded
    array), but`is_trivially_destructible<_Item>` lets us be consistent
    with the unbounded array control block.
    StephanTLavavej committed Apr 2, 2020
    Configuration menu
    Copy the full SHA
    c8b6d32 View commit details
    Browse the repository at this point in the history

Commits on Apr 7, 2020

  1. Configuration menu
    Copy the full SHA
    9959d21 View commit details
    Browse the repository at this point in the history
  2. Improve comments.

    StephanTLavavej committed Apr 7, 2020
    Configuration menu
    Copy the full SHA
    731ab48 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    d017e0a View commit details
    Browse the repository at this point in the history
  4. More test improvements.

    * Include <cstdlib> for malloc.
    * Include <new> for bad_alloc.
    * Include <string_view>, newly used.
    * The precise Word Of Power is that `operator new` is "replaced".
    * Use size_t for vector indices; this respects 64-bit (not that these
    vectors can be large). Additionally, we conventionally never say
    `unsigned` - we would say `unsigned int`.
    * Remove artificial scopes around try/catch - they already introduce
    scopes, so this was just extra indentation.
    * Instead of constructing a std::string in a catch block, compare
    against a string_view literal, avoiding dynamic memory allocation.
    * Conventionally use preincrement.
    StephanTLavavej committed Apr 7, 2020
    Configuration menu
    Copy the full SHA
    5b4eb44 View commit details
    Browse the repository at this point in the history
  5. Final test improvements.

    * Initialize InitialValue::value instead of reassigning it.
    
    * Test `canCreate > 0` instead of directly testing an integer for being
    nonzero.
    
    * assert_shared_use_get() is an observer.
    
    * Always use template argument deduction when calling
    assert_shared_use_get().
    
    * Add many missing calls to assert_shared_use_get().
    
    * Test plain int (and arrays of known/unknown bound) as it's sensitive
    to the difference between default-init and value-init. The other types
    tested have constructors.
    
    * Iterating through vectors with `int&` was correct, but `const auto&`
    is more conventional in our tests for pure observation. (This is more
    important for maps, where getting the element type wrong is a real
    possibility.)
    StephanTLavavej committed Apr 7, 2020
    Configuration menu
    Copy the full SHA
    0c0cb15 View commit details
    Browse the repository at this point in the history

Commits on Apr 8, 2020

  1. More code review feedback.

    * Reorder friendship to match the Standard's declaration order.
    
    * Extract `_Element_size` for simplicity.
    
    * Support extended alignment in `_Allocate_flexible_array`.
    
    * Add `_Deallocate_flexible_array` to handle extended alignment.
    
    * Mark guard constructors as TRANSITION, P1771R1.
    
    * Handle non-pointer iterators when calling `destroy_at()`.
    
    * Add `static_assert` to array control blocks.
    
    * Add `HighlyAligned` test coverage.
    
    * Other test improvements.
    StephanTLavavej committed Apr 8, 2020
    Configuration menu
    Copy the full SHA
    e7daab4 View commit details
    Browse the repository at this point in the history