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

Commit

Permalink
Review updates:
Browse files Browse the repository at this point in the history
* Explicit call to ```ExtendablePoolAllocator()```
* Remove unnecessary virtuals
* Add asserts for memory allocation failure
  • Loading branch information
bremoran committed Jan 22, 2016
1 parent d61cdb2 commit 5872886
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions core-util/v2/detail/allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ContainerAllocator : public mbed::util::ExtendablePoolAllocator {
public:
ContainerAllocator(size_t initial_elements, size_t new_pool_elements, size_t element_size,
UAllocTraits_t alloc_traits, unsigned alignment = MBED_UTIL_POOL_ALLOC_DEFAULT_ALIGN)
: mbed::util::ExtendablePoolAllocator()
{
this->init(initial_elements, new_pool_elements, element_size, alloc_traits, alignment);
}
Expand Down
10 changes: 5 additions & 5 deletions core-util/v2/detail/capture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#ifndef FUNCTIONAL_DETAIL_CAPTURE_HPP
#define FUNCTIONAL_DETAIL_CAPTURE_HPP

#include <type_traits>
#include <tuple>

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

virtual ReturnType operator () (ArgTypes&&... Args) {
ReturnType operator () (ArgTypes&&... Args) {
return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), forward<ArgTypes>(Args)...);
}
template <size_t... S>
inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) {
return f(forward<CapturedTypes>(std::get<S>(storage))..., forward<ArgTypes>(Args)...);
}

virtual ContainerAllocator * get_allocator() {
ContainerAllocator * get_allocator() {
return & Allocator;
}
protected:
Expand All @@ -84,15 +83,15 @@ class CaptureLast <ReturnType(ArgTypes...), Allocator, CapturedTypes...>
f(f), storage(CapturedArgs...)
{}

virtual ReturnType operator () (ArgTypes&&... Args) {
ReturnType operator () (ArgTypes&&... Args) {
return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), forward<ArgTypes>(Args)...);
}
template <size_t... S>
inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) {
return f(forward<ArgTypes>(Args)..., forward<CapturedTypes>(std::get<S>(storage))...);
}

virtual ContainerAllocator * get_allocator() {
ContainerAllocator * get_allocator() {
return & Allocator;
}
protected:
Expand Down Expand Up @@ -135,6 +134,7 @@ Function<ReturnType(ArgTypes...)> bind_last(Function<ReturnType(ArgTypes...)> &&
using CaptureFP = CaptureLast<ReturnType(ArgTypes...), FunctorFPAllocator, CapturedTypes...>;
static_assert(sizeof(CaptureFP) <= FUNCTOR_SIZE, "Size of bound arguments is too large" );
CaptureFP * newf = reinterpret_cast<CaptureFP *>(detail::FunctorFPAllocator.alloc());
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
new(newf) CaptureFP(f,forward<CapturedTypes>(CapturedArgs)...);
return Function<ReturnType(ArgTypes...)>(static_cast<FunctionInterface<ReturnType(ArgTypes...)>*>(newf));
}
Expand Down
19 changes: 18 additions & 1 deletion core-util/v2/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <stddef.h>
#include <new>
#include <utility>
#include <type_traits>
#include "core-util/atomic_ops.h"

namespace functional {
Expand Down Expand Up @@ -75,16 +74,31 @@ class Function <ReturnType(ArgTypes...)> {
* Since ```Function``` only contains a single pointer, only a null assignment is necessary.
*/
Function() : ref(nullptr) {}
/**
* Construct a Function from a FunctionInterface.
*
* This is an API that should only be used by Function and its helpers. It was specifically added to enable
* bind_first and bind_last.
*
* @param[in] f
*/
Function(detail::FunctionInterface<ReturnType(ArgTypes...)> *f) {
ref = f;
if (ref) {
ref->inc();
}
}
/**
* Move constructor
* This constructor steals the reference from the rvalue-reference Function
* without incrementing the reference count.
*/
Function(Function &&f): ref(f.ref) {}

Function(ReturnType (*f)(ArgTypes...)) {
typedef detail::StaticContainer<ReturnType(ArgTypes...)> staticFP;
staticFP * newf = reinterpret_cast<staticFP *>(detail::StaticFPAllocator.alloc());
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
new(newf) staticFP(f);
ref = newf;
ref->inc();
Expand All @@ -97,6 +111,7 @@ class Function <ReturnType(ArgTypes...)> {
Function(C *o, ReturnType (C::*fp)(ArgTypes...)) {
typedef detail::MemberContainer<C, ReturnType(ArgTypes...)> memberFP;
memberFP * newf = reinterpret_cast<memberFP *>(detail::MemberFPAllocator.alloc());
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
new(newf) memberFP(o, fp);
ref = newf;
ref->inc();
Expand All @@ -113,6 +128,7 @@ class Function <ReturnType(ArgTypes...)> {
Function(const F &f) {
typedef detail::FunctorContainer<F, ReturnType(ArgTypes...), detail::FunctorFPAllocator> FunctorFP;
FunctorFP * newf = reinterpret_cast<FunctorFP *>(detail::FunctorFPAllocator.alloc());
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
new(newf) FunctorFP(f);
ref = newf;
ref->inc();
Expand All @@ -133,6 +149,7 @@ class Function <ReturnType(ArgTypes...)> {
typedef typename detail::CaptureFirst<ReturnType(ArgTypes...), detail::FunctorFPAllocator, CapturedTypes...> CaptureFP;
static_assert(sizeof(CaptureFP) <= FUNCTOR_SIZE, "Size of bound arguments is too large" );
CaptureFP * newf = reinterpret_cast<CaptureFP *>(detail::FunctorFPAllocator.alloc());
CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed");
new(newf) CaptureFP(t, f);
ref = newf;
ref->inc();
Expand Down

0 comments on commit 5872886

Please sign in to comment.