Merge pull request #93 from ros2/malloc_experiment

Prevent malloc(0) in memory strategy
This commit is contained in:
Jackie Kay 2015-08-25 16:43:06 -07:00
commit 5a22594174
2 changed files with 13 additions and 4 deletions

View file

@ -321,7 +321,9 @@ protected:
// TODO(wjwwood): Avoid redundant malloc's
subscriber_handles.subscribers = memory_strategy_->borrow_handles(
HandleType::subscription_handle, max_number_of_subscriptions);
if (subscriber_handles.subscribers == NULL) {
if (subscriber_handles.subscribers == NULL &&
max_number_of_subscriptions > 0)
{
// TODO(wjwwood): Use a different error here? maybe std::bad_alloc?
throw std::runtime_error("Could not malloc for subscriber pointers.");
}
@ -341,7 +343,9 @@ protected:
service_handles.service_count = number_of_services;
service_handles.services =
memory_strategy_->borrow_handles(HandleType::service_handle, number_of_services);
if (service_handles.services == NULL) {
if (service_handles.services == NULL &&
number_of_services > 0)
{
// TODO(esteve): Use a different error here? maybe std::bad_alloc?
throw std::runtime_error("Could not malloc for service pointers.");
}
@ -359,7 +363,7 @@ protected:
client_handles.client_count = number_of_clients;
client_handles.clients =
memory_strategy_->borrow_handles(HandleType::client_handle, number_of_clients);
if (client_handles.clients == NULL) {
if (client_handles.clients == NULL && number_of_clients > 0) {
// TODO: Use a different error here? maybe std::bad_alloc?
throw std::runtime_error("Could not malloc for client pointers.");
}
@ -378,7 +382,9 @@ protected:
guard_condition_handles.guard_condition_count = number_of_guard_conds;
guard_condition_handles.guard_conditions =
memory_strategy_->borrow_handles(HandleType::guard_condition_handle, number_of_guard_conds);
if (guard_condition_handles.guard_conditions == NULL) {
if (guard_condition_handles.guard_conditions == NULL &&
number_of_guard_conds > 0)
{
// TODO(wjwwood): Use a different error here? maybe std::bad_alloc?
throw std::runtime_error("Could not malloc for guard condition pointers.");
}

View file

@ -56,6 +56,9 @@ public:
virtual void * alloc(size_t size)
{
if (size == 0) {
return NULL;
}
return std::malloc(size);
}