ObjectUtils.h#

Fully qualified name: carb/ObjectUtils.h

File members: carb/ObjectUtils.h

// SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.

#pragma once

#include "IObject.h"

#include <atomic>

namespace carb
{

template <class T>
void deleteHandler(T* ptr)
{
    delete ptr;
}

} // namespace carb

#define CARB_IOBJECT_IMPL                                                                                              \
public:                                                                                                                \
                                                                                                                \
    size_t addRef() override                                                                                           \
    {                                                                                                                  \
        size_t prev = m_refCount.fetch_add(1, std::memory_order_relaxed);                                              \
        CARB_ASSERT(prev != 0); /* resurrected item if this occurs */                                                  \
        return prev + 1;                                                                                               \
    }                                                                                                                  \
                                                                                                                       \
                                                                                                                \
    size_t release() override                                                                                          \
    {                                                                                                                  \
        size_t prev = m_refCount.fetch_sub(1, std::memory_order_release);                                              \
        CARB_ASSERT(prev != 0); /* double release if this occurs */                                                    \
        if (prev == 1)                                                                                                 \
        {                                                                                                              \
            std::atomic_thread_fence(std::memory_order_acquire);                                                       \
            carb::deleteHandler(this);                                                                                 \
        }                                                                                                              \
        return prev - 1;                                                                                               \
    }                                                                                                                  \
                                                                                                                       \
private:                                                                                                               \
    std::atomic_size_t m_refCount{ 1 };