Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add debug_getTotalAllocatedHybridObjects()
  • Loading branch information
mrousavy committed Mar 3, 2026
commit d57be7a7993b718481245765c960cf59d373b296
13 changes: 12 additions & 1 deletion packages/react-native-nitro-modules/cpp/core/HybridObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@

#include "HybridObject.hpp"
#include "CommonGlobals.hpp"
#include "HybridNitroModulesProxy.hpp"
#include "JSIConverter.hpp"
#include "NitroDefines.hpp"

namespace margelo::nitro {

HybridObject::HybridObject(const char* NON_NULL name) : HybridObjectPrototype(), _name(name) {}
HybridObject::HybridObject(const char* NON_NULL name) : HybridObjectPrototype(), _name(name) {
#ifdef NITRO_DEBUG
HybridNitroModulesProxy::debug_notifyHybridObjectAllocated();
#endif
}

HybridObject::~HybridObject() {
#ifdef NITRO_DEBUG
HybridNitroModulesProxy::debug_notifyHybridObjectDeallocated();
#endif
}

std::string HybridObject::toString() {
return "[HybridObject " + std::string(_name) + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class HybridObject : public virtual jsi::NativeState, public HybridObjectPrototy
* Called when no more references to the given `HybridObject` exist in both C++ and JS.
* JS might keep references for longer, as it is a garbage collected language.
*/
~HybridObject() override = default;
~HybridObject() override;
/**
* HybridObjects cannot be copied.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void HybridNitroModulesProxy::loadHybridMethods() {

prototype.registerHybridGetter("buildType", &HybridNitroModulesProxy::getBuildType);
prototype.registerHybridGetter("version", &HybridNitroModulesProxy::getVersion);

prototype.registerHybridGetter("debug_getTotalAllocatedHybridObjects", &HybridNitroModulesProxy::debug_getTotalAllocatedHybridObjects);
});
}

Expand Down Expand Up @@ -94,4 +96,20 @@ std::string HybridNitroModulesProxy::getVersion() {
return NITRO_VERSION;
}

// Allocation tests
static std::atomic_size_t _hybridObjectInstancesCount{0};
double HybridNitroModulesProxy::debug_getTotalAllocatedHybridObjects() {
size_t count = _hybridObjectInstancesCount.load(std::memory_order_relaxed);
return static_cast<double>(count);
}

void HybridNitroModulesProxy::debug_notifyHybridObjectAllocated() {
_hybridObjectInstancesCount.fetch_add(1, std::memory_order_relaxed);
}

void HybridNitroModulesProxy::debug_notifyHybridObjectDeallocated() {
_hybridObjectInstancesCount.fetch_sub(1, std::memory_order_relaxed);
}


} // namespace margelo::nitro
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class HybridNitroModulesProxy final : public HybridObject {
std::string getBuildType();
std::string getVersion();

// Allocation tests
double debug_getTotalAllocatedHybridObjects();
static void debug_notifyHybridObjectAllocated();
static void debug_notifyHybridObjectDeallocated();

private:
static constexpr auto TAG = "NitroModulesProxy";
};
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-nitro-modules/src/NitroModulesProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ export interface NitroModulesProxy
* owns it - not JS.
*/
createNativeArrayBuffer(size: number): ArrayBuffer

/**
* Gets a total number of currently allocated {@linkcode HybridObject}s.
* @internal
*/
debug_getTotalAllocatedHybridObjects(): number
}
Loading