Skip to content

iceoryx_utils/internal/relocatable_pointer/atomic_relocatable_pointer.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::rp

Classes🔗

Name
class iox::rp::AtomicRelocatablePointer
minimalistic relocatable pointer that can be written and read atomically and can be stored safely in shared memory. As the basic RelocatablePointer, it must point to something in the same shared memory segment as itself since the internally used offset must be an invariant different across adress spaces. Rationale: the default RelocatablePointer cannot be used in an atomic since the copy ctor is nontrivial.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP
#define IOX_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP

#include <atomic>
#include <limits>

namespace iox
{
namespace rp
{
template <typename T>
class AtomicRelocatablePointer
{
  public:
    using offset_t = std::ptrdiff_t;
    static constexpr offset_t NULL_POINTER_OFFSET = std::numeric_limits<offset_t>::max();

    AtomicRelocatablePointer(const T* ptr = nullptr) noexcept;

    AtomicRelocatablePointer(const AtomicRelocatablePointer&) = delete;
    AtomicRelocatablePointer& operator=(const AtomicRelocatablePointer& other) = delete;
    AtomicRelocatablePointer(AtomicRelocatablePointer&& other) = delete;
    AtomicRelocatablePointer& operator=(AtomicRelocatablePointer&& other) = delete;


    AtomicRelocatablePointer& operator=(const T* ptr) noexcept;

    T* operator->() const noexcept;

    T& operator*() const noexcept;

    operator T*() const noexcept;

  private:
    std::atomic<offset_t> m_offset{NULL_POINTER_OFFSET};

    inline T* computeRawPtr() const noexcept;

    inline offset_t computeOffset(const void* ptr) const noexcept;
};
} // namespace rp
} // namespace iox

#include "iceoryx_utils/internal/relocatable_pointer/atomic_relocatable_pointer.inl"

#endif // IOX_UTILS_RELOCATABLE_POINTER_ATOMIC_RELOCATABLE_POINTER_HPP

Updated on 17 June 2021 at 11:15:26 CEST