Compare commits
3 Commits
cb2810051d
...
7a2774b09d
Author | SHA1 | Date | |
---|---|---|---|
7a2774b09d | |||
51b550ae26 | |||
2c671d65c4 |
@ -1,6 +1,7 @@
|
||||
// No copyright
|
||||
#include "core/luajit/context.hh"
|
||||
|
||||
#include "iface/common/leak_detector.hh"
|
||||
#include "iface/subsys/concurrency.hh"
|
||||
#include "iface/subsys/parallelism.hh"
|
||||
|
||||
@ -50,8 +51,11 @@ void TaskContext::Push(const nf7::Value& v) noexcept {
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
class ContextImpl final : public Context {
|
||||
class ContextImpl final :
|
||||
public Context,
|
||||
private LeakDetector<ContextImpl<T>> {
|
||||
public:
|
||||
ContextImpl(const char* name, Kind kind, Env& env)
|
||||
: Context(name, kind), tasq_(env.Get<T>()) {
|
||||
@ -93,6 +97,7 @@ class ContextImpl final : public Context {
|
||||
private:
|
||||
std::shared_ptr<T> tasq_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<Context> Context::Create(Env& env, Kind kind) {
|
||||
switch (kind) {
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "core/luajit/context_test.hh"
|
||||
|
||||
|
||||
namespace {
|
||||
class LuaJIT_Lambda : public nf7::core::luajit::test::ContextFixture {
|
||||
public:
|
||||
using ContextFixture::ContextFixture;
|
||||
@ -55,6 +56,7 @@ class LuaJIT_Lambda : public nf7::core::luajit::test::ContextFixture {
|
||||
EXPECT_EQ(sut->abortCount(), expectAbort);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
||||
TEST_P(LuaJIT_Lambda, Run) {
|
||||
|
@ -8,11 +8,15 @@
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
#include "iface/common/leak_detector.hh"
|
||||
|
||||
#include "core/luajit/context.hh"
|
||||
|
||||
namespace nf7::core::luajit {
|
||||
|
||||
class Thread : public std::enable_shared_from_this<Thread> {
|
||||
class Thread :
|
||||
public std::enable_shared_from_this<Thread>,
|
||||
private LeakDetector<Thread> {
|
||||
public:
|
||||
struct DoNotCallConstructorDirectly { };
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "core/luajit/context_test.hh"
|
||||
|
||||
|
||||
namespace {
|
||||
class LuaJIT_Thread : public nf7::core::luajit::test::ContextFixture {
|
||||
public:
|
||||
using ContextFixture::ContextFixture;
|
||||
@ -32,6 +33,7 @@ class LuaJIT_Thread : public nf7::core::luajit::test::ContextFixture {
|
||||
EXPECT_EQ(called, 1);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
||||
TEST_P(LuaJIT_Thread, ResumeWithSingleReturn) {
|
||||
|
@ -13,6 +13,7 @@ target_sources(nf7_iface
|
||||
common/container.hh
|
||||
common/exception.hh
|
||||
common/future.hh
|
||||
common/leak_detector.hh
|
||||
common/observer.hh
|
||||
common/task.hh
|
||||
common/task_context.hh
|
||||
@ -33,6 +34,7 @@ target_sources(nf7_iface_test
|
||||
common/dealer_test.cc
|
||||
common/container_test.cc
|
||||
common/future_test.cc
|
||||
common/leak_detector_test.cc
|
||||
common/observer_test.cc
|
||||
common/observer_test.hh
|
||||
common/task_test.cc
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
class Object {
|
||||
public:
|
||||
virtual ~Object() = default;
|
||||
@ -30,6 +31,7 @@ class BRecursive : public IB {
|
||||
private:
|
||||
std::shared_ptr<IB> b_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(SimpleContainer, FetchIsolated) {
|
||||
SUT sut {{
|
||||
|
45
iface/common/leak_detector.hh
Normal file
45
iface/common/leak_detector.hh
Normal file
@ -0,0 +1,45 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
namespace nf7 {
|
||||
#if !defined(NDEBUG)
|
||||
|
||||
template <typename T>
|
||||
class LeakDetector {
|
||||
public:
|
||||
LeakDetector() { (void) checker_; ++count_; }
|
||||
virtual ~LeakDetector() noexcept { --count_; }
|
||||
|
||||
private:
|
||||
struct Checker {
|
||||
~Checker() {
|
||||
if (count_ > 0) {
|
||||
std::cerr << "LEAK DETECTED: " << typeid(T).name() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
static uint64_t count() noexcept { return count_; }
|
||||
|
||||
private:
|
||||
static inline Checker checker_;
|
||||
static inline std::atomic<uint64_t> count_ = 0;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
template <typename T>
|
||||
class LeakDetector {
|
||||
public:
|
||||
static uint64_t count() noexcept { return 0; }
|
||||
};
|
||||
|
||||
#endif
|
||||
} // namespace nf7
|
21
iface/common/leak_detector_test.cc
Normal file
21
iface/common/leak_detector_test.cc
Normal file
@ -0,0 +1,21 @@
|
||||
// No copyright
|
||||
#include "iface/common/leak_detector.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace {
|
||||
class A : private nf7::LeakDetector<A> { };
|
||||
} // namespace
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
TEST(LeakDetector, Counter) {
|
||||
{
|
||||
A a;
|
||||
EXPECT_EQ(nf7::LeakDetector<A>::count(), 1);
|
||||
}
|
||||
EXPECT_EQ(nf7::LeakDetector<A>::count(), 0);
|
||||
}
|
||||
#endif
|
@ -6,13 +6,14 @@
|
||||
#include <vector>
|
||||
|
||||
#include "iface/common/dealer.hh"
|
||||
#include "iface/common/leak_detector.hh"
|
||||
#include "iface/common/exception.hh"
|
||||
#include "iface/common/observer.hh"
|
||||
#include "iface/common/value.hh"
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class Lambda {
|
||||
class Lambda : private LeakDetector<Lambda> {
|
||||
public:
|
||||
Lambda() = delete;
|
||||
Lambda(const std::shared_ptr<Taker<Value>>& taker,
|
||||
|
Loading…
x
Reference in New Issue
Block a user