Browse Source

排除潜在的内存错误

tags/v2.9.7
tearshark 4 years ago
parent
commit
8a3b577147

+ 1
- 1
benchmark/benchmark_async_mem.cpp View File

@@ -7,7 +7,7 @@
#include "librf.h"
const size_t N = 5000000;
const size_t N = 2000000;
const size_t LOOP_COUNT = 50;
volatile size_t globalValue = 0;

+ 2
- 2
librf/src/asio_task_1.10.0.inl View File

@@ -38,7 +38,7 @@ namespace asio {

template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>(true))
: state_(state_type::typename _Alloc_state<state_type>(true))
{
}

@@ -71,7 +71,7 @@ namespace asio {

template<typename Allocator>
promise_handler(const rf_task_t<Allocator> &)
: state_(resumef::make_counted<state_type>(true))
: state_(state_type::typename _Alloc_state<state_type>(true))
{
}


+ 1
- 1
librf/src/asio_task_1.12.0.inl View File

@@ -23,7 +23,7 @@ namespace asio {
typedef resumef::state_t<result_type> state_type;

promise_handler_base()
: state_(resumef::make_counted<state_type>(true))
: state_(resumef::state_future_t::_Alloc_state<state_type>(true))
{
}


+ 0
- 6
librf/src/counted_ptr.h View File

@@ -82,12 +82,6 @@ RESUMEF_NS
T* _p = nullptr;
};
template <typename T, typename... Args>
counted_ptr<T> make_counted(Args&&... args)
{
return new T{std::forward<Args>(args)...};
}
template <typename T, typename U>
inline bool operator == (const counted_ptr<T>& _Left, const counted_ptr<U>& _Right)
{

+ 1
- 1
librf/src/generator.h View File

@@ -210,7 +210,7 @@ RESUMEF_NS
//在初始地址上构造state
{
state_type* st = new(ptr) state_type();
state_type* st = state_type::_Construct(ptr);
st->lock();
}

+ 1
- 1
librf/src/promise.inl View File

@@ -136,7 +136,7 @@ RESUMEF_NS

//在初始地址上构造state
{
state_type* st = new(ptr) state_type(_Size + _State_size);
state_type* st = state_future_t::_Construct<state_type>(ptr, _Size + _State_size);
st->lock();
}


+ 12
- 1
librf/src/state.cpp View File

@@ -27,7 +27,18 @@ RESUMEF_NS
_Alloc_char _Al;
return _Al.deallocate(reinterpret_cast<char*>(this), _Size);
}
state_generator_t* state_generator_t::_Alloc_state()
{
_Alloc_char _Al;
size_t _Size = _Align_size<state_generator_t>();
#if RESUMEF_DEBUG_COUNTER
std::cout << "state_generator_t::alloc, size=" << sizeof(state_generator_t) << std::endl;
#endif
char* _Ptr = _Al.allocate(_Size);
return new(_Ptr) state_generator_t();
}
void state_generator_t::destroy_deallocate()
{
size_t _Size = _Align_size<state_generator_t>();

+ 28
- 45
librf/src/state.h View File

@@ -65,6 +65,7 @@ RESUMEF_NS
{
private:
virtual void destroy_deallocate() override;
state_generator_t() = default;
public:
virtual void resume() override;
virtual bool has_handler() const noexcept override;
@@ -76,16 +77,13 @@ RESUMEF_NS
_coro = handler;
}
static state_generator_t * _Alloc_state()
#if RESUMEF_INLINE_STATE
static state_generator_t* _Construct(void* _Ptr)
{
_Alloc_char _Al;
size_t _Size = sizeof(state_generator_t);
#if RESUMEF_DEBUG_COUNTER
std::cout << "state_generator_t::alloc, size=" << sizeof(state_generator_t) << std::endl;
#endif
char* _Ptr = _Al.allocate(_Size);
return new(_Ptr) state_generator_t();
}
#endif
static state_generator_t* _Alloc_state();
};
struct state_future_t : public state_base_t
@@ -123,14 +121,7 @@ RESUMEF_NS
static_assert(alignof(bool) == 1);
static_assert(sizeof(std::atomic<initor_type>) == 1);
static_assert(alignof(std::atomic<initor_type>) == 1);
public:
state_future_t()
{
#if RESUMEF_DEBUG_COUNTER
_id = ++g_resumef_state_id;
#endif
_is_future = true;
}
protected:
explicit state_future_t(bool awaitor)
{
#if RESUMEF_DEBUG_COUNTER
@@ -138,7 +129,7 @@ RESUMEF_NS
#endif
_is_future = !awaitor;
}
public:
virtual void destroy_deallocate() override;
virtual void resume() override;
virtual bool has_handler() const noexcept override;
@@ -182,16 +173,29 @@ RESUMEF_NS
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
void promise_final_suspend(coroutine_handle<_PromiseT> handler);
#if RESUMEF_INLINE_STATE
template<class _Sty>
static _Sty* _Construct(void* _Ptr, size_t _Size)
{
_Sty* st = new(_Ptr) _Sty(false);
st->_alloc_size = _Size;
return st;
}
#endif
template<class _Sty>
static inline _Sty* _Alloc_state(bool awaitor)
{
_Alloc_char _Al;
size_t _Size = sizeof(_Sty);
size_t _Size = _Align_size<_Sty>();
#if RESUMEF_DEBUG_COUNTER
std::cout << "state_future_t::alloc, size=" << _Size << std::endl;
#endif
char* _Ptr = _Al.allocate(_Size);
return new(_Ptr) _Sty(awaitor);
_Sty* st = new(_Ptr) _Sty(awaitor);
st->_alloc_size = _Size;
return st;
}
};
@@ -202,15 +206,8 @@ RESUMEF_NS
using state_future_t::lock_type;
using value_type = _Ty;
explicit state_t(size_t alloc_size) :state_future_t()
{
_alloc_size = static_cast<uint32_t>(alloc_size);
}
explicit state_t(bool awaitor) :state_future_t(awaitor)
{
_alloc_size = sizeof(*this);
}
private:
explicit state_t(bool awaitor) :state_future_t(awaitor) {}
public:
~state_t()
{
@@ -260,15 +257,8 @@ RESUMEF_NS
using state_future_t::lock_type;
using value_type = _Ty;
using reference_type = _Ty&;
explicit state_t(size_t alloc_size) :state_future_t()
{
_alloc_size = static_cast<uint32_t>(alloc_size);
}
explicit state_t(bool awaitor) :state_future_t(awaitor)
{
_alloc_size = sizeof(*this);
}
private:
explicit state_t(bool awaitor) :state_future_t(awaitor) {}
public:
~state_t()
{
@@ -304,15 +294,8 @@ RESUMEF_NS
{
friend state_future_t;
using state_future_t::lock_type;
explicit state_t(size_t alloc_size) :state_future_t()
{
_alloc_size = static_cast<uint32_t>(alloc_size);
}
explicit state_t(bool awaitor) :state_future_t(awaitor)
{
_alloc_size = sizeof(*this);
}
private:
explicit state_t(bool awaitor) :state_future_t(awaitor) {}
public:
void future_await_resume();
template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>

+ 4
- 3
vs_proj/librf.vcxproj View File

@@ -28,19 +28,20 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>ClangCL</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>ClangCL</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
<EnableASAN>true</EnableASAN>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>ClangCL</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">

Loading…
Cancel
Save