omni/extras/ForceLink.h
File members: omni/extras/ForceLink.h
// Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once
#include "../core/Platform.h"
#if OMNI_PLATFORM_WINDOWS
// see below for why we unfortunately either need this or to forward declare SetLastError()
// directly here instead of using something like strerror_s() on Windows.
# include "../../carb/CarbWindows.h"
#endif
namespace omni
{
namespace extras
{
class ForceSymbolLink
{
public:
ForceSymbolLink(void* ptr)
{
#if OMNI_PLATFORM_WINDOWS
// On Windows, we unfortunately can't call into something like strerror_s() to
// accomplish this task because the CRT is statically linked to the module that
// will be using this. That would make the function we're passing the symbol
// to local and therefore the symbol will still be discardable. Instead, we'll
// pass the address to SetLastError() which will always be available from 'kernel32'.
SetLastError(static_cast<DWORD>(reinterpret_cast<uintptr_t>(ptr)));
#else
char* str;
CARB_UNUSED(str);
str = strerror(static_cast<int>(reinterpret_cast<uintptr_t>(ptr)));
#endif
}
};
} // namespace extras
} // namespace omni
#define OMNI_FORCE_SYMBOL_LINK(symbol_, tag_) \
static omni::extras::ForceSymbolLink CARB_JOIN( \
g_forceLink##tag_##_, CARB_JOIN(CARB_JOIN(__COUNTER__, _), __LINE__))(&(symbol_))