Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit 5872886

Browse files
committed
Review updates:
* Explicit call to ```ExtendablePoolAllocator()``` * Remove unnecessary virtuals * Add asserts for memory allocation failure
1 parent d61cdb2 commit 5872886

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

core-util/v2/detail/allocators.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ContainerAllocator : public mbed::util::ExtendablePoolAllocator {
2727
public:
2828
ContainerAllocator(size_t initial_elements, size_t new_pool_elements, size_t element_size,
2929
UAllocTraits_t alloc_traits, unsigned alignment = MBED_UTIL_POOL_ALLOC_DEFAULT_ALIGN)
30+
: mbed::util::ExtendablePoolAllocator()
3031
{
3132
this->init(initial_elements, new_pool_elements, element_size, alloc_traits, alignment);
3233
}

core-util/v2/detail/capture.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#ifndef FUNCTIONAL_DETAIL_CAPTURE_HPP
1717
#define FUNCTIONAL_DETAIL_CAPTURE_HPP
1818

19-
#include <type_traits>
2019
#include <tuple>
2120

2221
#include "interface.hpp"
@@ -50,15 +49,15 @@ class CaptureFirst <ReturnType(ArgTypes...), Allocator, CapturedTypes...>
5049
f(f), storage(t)
5150
{}
5251

53-
virtual ReturnType operator () (ArgTypes&&... Args) {
52+
ReturnType operator () (ArgTypes&&... Args) {
5453
return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), forward<ArgTypes>(Args)...);
5554
}
5655
template <size_t... S>
5756
inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) {
5857
return f(forward<CapturedTypes>(std::get<S>(storage))..., forward<ArgTypes>(Args)...);
5958
}
6059

61-
virtual ContainerAllocator * get_allocator() {
60+
ContainerAllocator * get_allocator() {
6261
return & Allocator;
6362
}
6463
protected:
@@ -84,15 +83,15 @@ class CaptureLast <ReturnType(ArgTypes...), Allocator, CapturedTypes...>
8483
f(f), storage(CapturedArgs...)
8584
{}
8685

87-
virtual ReturnType operator () (ArgTypes&&... Args) {
86+
ReturnType operator () (ArgTypes&&... Args) {
8887
return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), forward<ArgTypes>(Args)...);
8988
}
9089
template <size_t... S>
9190
inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) {
9291
return f(forward<ArgTypes>(Args)..., forward<CapturedTypes>(std::get<S>(storage))...);
9392
}
9493

95-
virtual ContainerAllocator * get_allocator() {
94+
ContainerAllocator * get_allocator() {
9695
return & Allocator;
9796
}
9897
protected:
@@ -135,6 +134,7 @@ Function<ReturnType(ArgTypes...)> bind_last(Function<ReturnType(ArgTypes...)> &&
135134
using CaptureFP = CaptureLast<ReturnType(ArgTypes...), FunctorFPAllocator, CapturedTypes...>;
136135
static_assert(sizeof(CaptureFP) <= FUNCTOR_SIZE, "Size of bound arguments is too large" );
137136
CaptureFP * newf = reinterpret_cast<CaptureFP *>(detail::FunctorFPAllocator.alloc());
137+
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
138138
new(newf) CaptureFP(f,forward<CapturedTypes>(CapturedArgs)...);
139139
return Function<ReturnType(ArgTypes...)>(static_cast<FunctionInterface<ReturnType(ArgTypes...)>*>(newf));
140140
}

core-util/v2/functional.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <stddef.h>
3535
#include <new>
3636
#include <utility>
37-
#include <type_traits>
3837
#include "core-util/atomic_ops.h"
3938

4039
namespace functional {
@@ -75,16 +74,31 @@ class Function <ReturnType(ArgTypes...)> {
7574
* Since ```Function``` only contains a single pointer, only a null assignment is necessary.
7675
*/
7776
Function() : ref(nullptr) {}
77+
/**
78+
* Construct a Function from a FunctionInterface.
79+
*
80+
* This is an API that should only be used by Function and its helpers. It was specifically added to enable
81+
* bind_first and bind_last.
82+
*
83+
* @param[in] f
84+
*/
7885
Function(detail::FunctionInterface<ReturnType(ArgTypes...)> *f) {
7986
ref = f;
8087
if (ref) {
8188
ref->inc();
8289
}
8390
}
91+
/**
92+
* Move constructor
93+
* This constructor steals the reference from the rvalue-reference Function
94+
* without incrementing the reference count.
95+
*/
96+
Function(Function &&f): ref(f.ref) {}
8497

8598
Function(ReturnType (*f)(ArgTypes...)) {
8699
typedef detail::StaticContainer<ReturnType(ArgTypes...)> staticFP;
87100
staticFP * newf = reinterpret_cast<staticFP *>(detail::StaticFPAllocator.alloc());
101+
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
88102
new(newf) staticFP(f);
89103
ref = newf;
90104
ref->inc();
@@ -97,6 +111,7 @@ class Function <ReturnType(ArgTypes...)> {
97111
Function(C *o, ReturnType (C::*fp)(ArgTypes...)) {
98112
typedef detail::MemberContainer<C, ReturnType(ArgTypes...)> memberFP;
99113
memberFP * newf = reinterpret_cast<memberFP *>(detail::MemberFPAllocator.alloc());
114+
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
100115
new(newf) memberFP(o, fp);
101116
ref = newf;
102117
ref->inc();
@@ -113,6 +128,7 @@ class Function <ReturnType(ArgTypes...)> {
113128
Function(const F &f) {
114129
typedef detail::FunctorContainer<F, ReturnType(ArgTypes...), detail::FunctorFPAllocator> FunctorFP;
115130
FunctorFP * newf = reinterpret_cast<FunctorFP *>(detail::FunctorFPAllocator.alloc());
131+
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
116132
new(newf) FunctorFP(f);
117133
ref = newf;
118134
ref->inc();
@@ -133,6 +149,7 @@ class Function <ReturnType(ArgTypes...)> {
133149
typedef typename detail::CaptureFirst<ReturnType(ArgTypes...), detail::FunctorFPAllocator, CapturedTypes...> CaptureFP;
134150
static_assert(sizeof(CaptureFP) <= FUNCTOR_SIZE, "Size of bound arguments is too large" );
135151
CaptureFP * newf = reinterpret_cast<CaptureFP *>(detail::FunctorFPAllocator.alloc());
152+
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
136153
new(newf) CaptureFP(t, f);
137154
ref = newf;
138155
ref->inc();

0 commit comments

Comments
 (0)