
#ifndef _G_REF_COUNT_NVOC_H_
#define _G_REF_COUNT_NVOC_H_

// Version of generated metadata structures
#ifdef NVOC_METADATA_VERSION
#undef NVOC_METADATA_VERSION
#endif
#define NVOC_METADATA_VERSION 2

#include "nvoc/runtime.h"
#include "nvoc/rtti.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * SPDX-FileCopyrightText: Copyright (c) 2013-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#pragma once
#include "g_ref_count_nvoc.h"

#ifndef REF_COUNT_H
#define REF_COUNT_H

/****************** Resource Manager Defines and Structures *****************\
*                                                                            *
*       Defines and structures used for the Reference-Counting Object.       *
*                                                                            *
\****************************************************************************/

#include "containers/map.h"
#include "nvoc/object.h"

#define NV_REQUESTER_INIT               NV_U64_MIN
#define NV_REQUESTER_RM                 NV_U64_MAX
#define NV_REQUESTER_CLIENT_OBJECT(c,o) (((NvU64)(c) << 32) | o)

typedef enum
{
    REFCNT_STATE_DEFAULT  = 0,
    REFCNT_STATE_ENABLED,
    REFCNT_STATE_DISABLED,
    REFCNT_STATE_ERROR,
} REFCNT_STATE;

typedef struct
{
    NvU32 numReferences;
} REFCNT_REQUESTER_ENTRY, *PREFCNT_REQUESTER_ENTRY;

MAKE_MAP(REFCNT_REQUESTER_ENTRY_MAP, REFCNT_REQUESTER_ENTRY);

struct OBJREFCNT;

#ifndef __nvoc_class_id_OBJREFCNT
#define __nvoc_class_id_OBJREFCNT 0xf89281u
typedef struct OBJREFCNT OBJREFCNT;
#endif /* __nvoc_class_id_OBJREFCNT */



//
// XXX-IOM:
//  These callback types are good candidates to be replaced with IOM
//  functionality, where small derived classes can be created on a 'callback'
//  base interface, should that become more practical (currently, adding any
//  kind of class still requires a non-trivial amount of boilerplate to wire
//  up).
//
typedef NV_STATUS RefcntStateChangeCallback(struct OBJREFCNT *, Dynamic *,
                                            REFCNT_STATE, REFCNT_STATE);

typedef void RefcntResetCallback(struct OBJREFCNT *, Dynamic *, NvU64);


// Private field names are wrapped in PRIVATE_FIELD, which does nothing for
// the matching C source file, but causes diagnostics to be issued if another
// source file references the field.
#ifdef NVOC_REF_COUNT_H_PRIVATE_ACCESS_ALLOWED
#define PRIVATE_FIELD(x) x
#else
#define PRIVATE_FIELD(x) NVOC_PRIVATE_FIELD(x)
#endif


// Metadata with per-class RTTI with ancestor(s)
struct NVOC_METADATA__OBJREFCNT;
struct NVOC_METADATA__Object;


struct OBJREFCNT {

    // Metadata starts with RTTI structure.
    union {
         const struct NVOC_METADATA__OBJREFCNT *__nvoc_metadata_ptr;
         const struct NVOC_RTTI *__nvoc_rtti;
    };

    // Parent (i.e. superclass or base class) objects
    struct Object __nvoc_base_Object;

    // Ancestor object pointers for `staticCast` feature
    struct Object *__nvoc_pbase_Object;    // obj super
    struct OBJREFCNT *__nvoc_pbase_OBJREFCNT;    // refcnt

    // 1 PDB property
    NvBool PDB_PROP_REFCNT_ALLOW_RECURSIVE_REQUESTS;

    // Data members
    Dynamic *pParent;
    NvU32 tag;
    REFCNT_REQUESTER_ENTRY_MAP requesterTree;
    REFCNT_STATE state;
    NvU32 count;
    RefcntStateChangeCallback *refcntStateChangeCallback;
    RefcntResetCallback *refcntResetCallback;
};


// Metadata with per-class RTTI with ancestor(s)
struct NVOC_METADATA__OBJREFCNT {
    const struct NVOC_RTTI rtti;
    const struct NVOC_METADATA__Object metadata__Object;
};

#ifndef __nvoc_class_id_OBJREFCNT
#define __nvoc_class_id_OBJREFCNT 0xf89281u
typedef struct OBJREFCNT OBJREFCNT;
#endif /* __nvoc_class_id_OBJREFCNT */

// Casting support
extern const struct NVOC_CLASS_DEF __nvoc_class_def_OBJREFCNT;

#define __staticCast_OBJREFCNT(pThis) \
    ((pThis)->__nvoc_pbase_OBJREFCNT)

#ifdef __nvoc_ref_count_h_disabled
#define __dynamicCast_OBJREFCNT(pThis) ((OBJREFCNT*) NULL)
#else //__nvoc_ref_count_h_disabled
#define __dynamicCast_OBJREFCNT(pThis) \
    ((OBJREFCNT*) __nvoc_dynamicCast(staticCast((pThis), Dynamic), classInfo(OBJREFCNT)))
#endif //__nvoc_ref_count_h_disabled

// Property macros
#define PDB_PROP_REFCNT_ALLOW_RECURSIVE_REQUESTS_BASE_CAST
#define PDB_PROP_REFCNT_ALLOW_RECURSIVE_REQUESTS_BASE_NAME PDB_PROP_REFCNT_ALLOW_RECURSIVE_REQUESTS


NV_STATUS __nvoc_objCreateDynamic_OBJREFCNT(Dynamic**, Dynamic*, NvU32, va_list);

NV_STATUS __nvoc_objCreate_OBJREFCNT(OBJREFCNT**, Dynamic*, NvU32, Dynamic *pParent, NvU32 tag, RefcntStateChangeCallback *pStateChangeCallback, RefcntResetCallback *pResetCallback);
#define __objCreate_OBJREFCNT(__nvoc_ppNewObj, __nvoc_pParent, __nvoc_createFlags, pParent, tag, pStateChangeCallback, pResetCallback) \
    __nvoc_objCreate_OBJREFCNT((__nvoc_ppNewObj), staticCast((__nvoc_pParent), Dynamic), (__nvoc_createFlags), pParent, tag, pStateChangeCallback, pResetCallback)


// Wrapper macros for implementation functions
NV_STATUS refcntConstruct_IMPL(struct OBJREFCNT *pRefcnt, Dynamic *pParent, NvU32 tag, RefcntStateChangeCallback *pStateChangeCallback, RefcntResetCallback *pResetCallback);
#define __nvoc_refcntConstruct(pRefcnt, pParent, tag, pStateChangeCallback, pResetCallback) refcntConstruct_IMPL(pRefcnt, pParent, tag, pStateChangeCallback, pResetCallback)

void refcntDestruct_IMPL(struct OBJREFCNT *pRefcnt);
#define __nvoc_refcntDestruct(pRefcnt) refcntDestruct_IMPL(pRefcnt)

NV_STATUS refcntRequestReference_IMPL(struct OBJREFCNT *pRefcnt, NvU64 arg2, NvU32 arg3, NvBool arg4);
#ifdef __nvoc_ref_count_h_disabled
static inline NV_STATUS refcntRequestReference(struct OBJREFCNT *pRefcnt, NvU64 arg2, NvU32 arg3, NvBool arg4) {
    NV_ASSERT_FAILED_PRECOMP("OBJREFCNT was disabled!");
    return NV_ERR_NOT_SUPPORTED;
}
#else // __nvoc_ref_count_h_disabled
#define refcntRequestReference(pRefcnt, arg2, arg3, arg4) refcntRequestReference_IMPL(pRefcnt, arg2, arg3, arg4)
#endif // __nvoc_ref_count_h_disabled

NV_STATUS refcntReleaseReferences_IMPL(struct OBJREFCNT *pRefcnt, NvU64 arg2, NvBool arg3);
#ifdef __nvoc_ref_count_h_disabled
static inline NV_STATUS refcntReleaseReferences(struct OBJREFCNT *pRefcnt, NvU64 arg2, NvBool arg3) {
    NV_ASSERT_FAILED_PRECOMP("OBJREFCNT was disabled!");
    return NV_ERR_NOT_SUPPORTED;
}
#else // __nvoc_ref_count_h_disabled
#define refcntReleaseReferences(pRefcnt, arg2, arg3) refcntReleaseReferences_IMPL(pRefcnt, arg2, arg3)
#endif // __nvoc_ref_count_h_disabled

NV_STATUS refcntReset_IMPL(struct OBJREFCNT *pRefcnt, NvBool arg2);
#ifdef __nvoc_ref_count_h_disabled
static inline NV_STATUS refcntReset(struct OBJREFCNT *pRefcnt, NvBool arg2) {
    NV_ASSERT_FAILED_PRECOMP("OBJREFCNT was disabled!");
    return NV_ERR_NOT_SUPPORTED;
}
#else // __nvoc_ref_count_h_disabled
#define refcntReset(pRefcnt, arg2) refcntReset_IMPL(pRefcnt, arg2)
#endif // __nvoc_ref_count_h_disabled


// Wrapper macros for halified functions

// Dispatch functions
// Virtual method declarations and/or inline definitions
// Exported method declarations and/or inline definitions
// HAL method declarations without bodies
// Inline HAL method definitions
// Static dispatch method declarations
// Static inline method definitions
#undef PRIVATE_FIELD


#endif // REF_COUNT_H

#ifdef __cplusplus
} // extern "C"
#endif

#endif // _G_REF_COUNT_NVOC_H_
