Uncrustified

This commit is contained in:
Jackie Kay 2015-07-13 14:49:08 -07:00
parent 41cc5324f4
commit 0557b115bc
4 changed files with 44 additions and 61 deletions

View file

@ -23,8 +23,8 @@ namespace rclcpp
{ {
namespace executor namespace executor
{ {
struct AnyExecutable struct AnyExecutable
{ {
AnyExecutable() AnyExecutable()
: subscription(0), timer(0), callback_group(0), node(0) : subscription(0), timer(0), callback_group(0), node(0)
@ -37,9 +37,9 @@ namespace executor
// These are used to keep the scope on the containing items // These are used to keep the scope on the containing items
rclcpp::callback_group::CallbackGroup::SharedPtr callback_group; rclcpp::callback_group::CallbackGroup::SharedPtr callback_group;
rclcpp::node::Node::SharedPtr node; rclcpp::node::Node::SharedPtr node;
}; };
typedef std::shared_ptr<AnyExecutable> AnyExecutableSharedPtr; typedef std::shared_ptr<AnyExecutable> AnyExecutableSharedPtr;
} }
} }

View file

@ -120,15 +120,13 @@ public:
void void
set_memory_strategy(memory_strategy::MemoryStrategySharedPtr memory_strategy) set_memory_strategy(memory_strategy::MemoryStrategySharedPtr memory_strategy)
{ {
if (memory_strategy == nullptr) if (memory_strategy == nullptr) {
{
throw std::runtime_error("Received NULL memory strategy in executor."); throw std::runtime_error("Received NULL memory strategy in executor.");
} }
_memory_strategy = memory_strategy; _memory_strategy = memory_strategy;
} }
protected: protected:
void void
execute_any_executable(AnyExecutableSharedPtr & any_exec) execute_any_executable(AnyExecutableSharedPtr & any_exec)
{ {

View file

@ -20,12 +20,12 @@ namespace rclcpp
{ {
// TODO move HandleType somewhere where it makes sense // TODO move HandleType somewhere where it makes sense
enum class HandleType{subscriber_handle, service_handle, client_handle, guard_condition_handle}; enum class HandleType {subscriber_handle, service_handle, client_handle, guard_condition_handle};
namespace executor namespace executor
{ {
class Executor; class Executor;
}; }
namespace memory_strategy namespace memory_strategy
{ {
@ -36,13 +36,12 @@ class MemoryStrategy
friend class executor::Executor; friend class executor::Executor;
public: public:
virtual void ** borrow_handles(HandleType type, size_t number_of_handles)
virtual void** borrow_handles(HandleType type, size_t number_of_handles)
{ {
return static_cast<void**>(rcl_malloc(sizeof(void *) * number_of_handles)); return static_cast<void **>(rcl_malloc(sizeof(void *) * number_of_handles));
} }
virtual void return_handles(HandleType type, void** handles) virtual void return_handles(HandleType type, void ** handles)
{ {
this->rcl_free(handles); this->rcl_free(handles);
} }
@ -52,20 +51,18 @@ public:
return executor::AnyExecutableSharedPtr(new executor::AnyExecutable); return executor::AnyExecutableSharedPtr(new executor::AnyExecutable);
} }
virtual void *rcl_malloc(size_t size) virtual void * rcl_malloc(size_t size)
{ {
return std::malloc(size); return std::malloc(size);
} }
virtual void rcl_free(void *ptr) virtual void rcl_free(void * ptr)
{ {
return std::free(ptr); return std::free(ptr);
} }
protected: protected:
private: private:
}; };
typedef std::shared_ptr<MemoryStrategy> MemoryStrategySharedPtr; typedef std::shared_ptr<MemoryStrategy> MemoryStrategySharedPtr;

View file

@ -1,4 +1,3 @@
// Copyright 2015 Open Source Robotics Foundation, Inc. // Copyright 2015 Open Source Robotics Foundation, Inc.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -31,7 +30,6 @@ class StaticMemoryStrategy : public memory_strategy::MemoryStrategy
{ {
public: public:
StaticMemoryStrategy() StaticMemoryStrategy()
{ {
memset(_memory_pool, 0, _pool_size); memset(_memory_pool, 0, _pool_size);
@ -42,21 +40,18 @@ public:
_pool_seq = 0; _pool_seq = 0;
_exec_seq = 0; _exec_seq = 0;
for (size_t i = 0; i < _pool_size; ++i) for (size_t i = 0; i < _pool_size; ++i) {
{
_memory_map[_memory_pool[i]] = 0; _memory_map[_memory_pool[i]] = 0;
} }
for (size_t i = 0; i < _max_executables; ++i) for (size_t i = 0; i < _max_executables; ++i) {
{
_executable_pool[i] = std::make_shared<executor::AnyExecutable>(executor::AnyExecutable()); _executable_pool[i] = std::make_shared<executor::AnyExecutable>(executor::AnyExecutable());
} }
} }
void** borrow_handles(HandleType type, size_t number_of_handles) void ** borrow_handles(HandleType type, size_t number_of_handles)
{
switch(type)
{ {
switch (type) {
case HandleType::subscriber_handle: case HandleType::subscriber_handle:
return _subscriber_pool; return _subscriber_pool;
case HandleType::service_handle: case HandleType::service_handle:
@ -71,10 +66,9 @@ public:
throw std::runtime_error("Unrecognized enum, could not borrow handle memory."); throw std::runtime_error("Unrecognized enum, could not borrow handle memory.");
} }
void return_handles(HandleType type, void** handles) void return_handles(HandleType type, void ** handles)
{
switch(type)
{ {
switch (type) {
case HandleType::subscriber_handle: case HandleType::subscriber_handle:
memset(_subscriber_pool, 0, _max_subscribers); memset(_subscriber_pool, 0, _max_subscribers);
break; break;
@ -95,8 +89,7 @@ public:
executor::AnyExecutableSharedPtr instantiate_next_executable() executor::AnyExecutableSharedPtr instantiate_next_executable()
{ {
if (_exec_seq >= _max_executables) if (_exec_seq >= _max_executables) {
{
// wrap around // wrap around
_exec_seq = 0; _exec_seq = 0;
} }
@ -106,18 +99,16 @@ public:
return _executable_pool[prev_exec_seq]; return _executable_pool[prev_exec_seq];
} }
void *rcl_malloc(size_t size) void * rcl_malloc(size_t size)
{ {
// Extremely naive static allocation strategy // Extremely naive static allocation strategy
// Keep track of block size at a given pointer // Keep track of block size at a given pointer
if (_pool_seq + size > _pool_size) if (_pool_seq + size > _pool_size) {
{
// Start at 0 // Start at 0
_pool_seq = 0; _pool_seq = 0;
} }
void *ptr = _memory_pool[_pool_seq]; void * ptr = _memory_pool[_pool_seq];
if (_memory_map.count(ptr) == 0) if (_memory_map.count(ptr) == 0) {
{
// We expect to have the state for all blocks pre-mapped into _memory_map // We expect to have the state for all blocks pre-mapped into _memory_map
throw std::runtime_error("Unexpected pointer in rcl_malloc."); throw std::runtime_error("Unexpected pointer in rcl_malloc.");
} }
@ -127,10 +118,9 @@ public:
return _memory_pool[prev_pool_seq]; return _memory_pool[prev_pool_seq];
} }
void rcl_free(void *ptr) void rcl_free(void * ptr)
{
if (_memory_map.count(ptr) == 0)
{ {
if (_memory_map.count(ptr) == 0) {
// We expect to have the state for all blocks pre-mapped into _memory_map // We expect to have the state for all blocks pre-mapped into _memory_map
throw std::runtime_error("Unexpected pointer in rcl_free."); throw std::runtime_error("Unexpected pointer in rcl_free.");
} }
@ -139,8 +129,6 @@ public:
} }
protected: protected:
private: private:
static const size_t _pool_size = 1024; static const size_t _pool_size = 1024;
static const size_t _max_subscribers = 10; static const size_t _max_subscribers = 10;
@ -149,17 +137,17 @@ private:
static const size_t _max_guard_conditions = 50; static const size_t _max_guard_conditions = 50;
static const size_t _max_executables = 1; static const size_t _max_executables = 1;
void *_memory_pool[_pool_size]; void * _memory_pool[_pool_size];
void *_subscriber_pool[_max_subscribers]; void * _subscriber_pool[_max_subscribers];
void *_service_pool[_max_services]; void * _service_pool[_max_services];
void *_client_pool[_max_clients]; void * _client_pool[_max_clients];
void *_guard_condition_pool[_max_guard_conditions]; void * _guard_condition_pool[_max_guard_conditions];
executor::AnyExecutableSharedPtr _executable_pool[_max_executables]; executor::AnyExecutableSharedPtr _executable_pool[_max_executables];
size_t _pool_seq; size_t _pool_seq;
size_t _exec_seq; size_t _exec_seq;
std::map<void*, size_t> _memory_map; std::map<void *, size_t> _memory_map;
}; };
} }