Skip to content

Commit

Permalink
ensure malloc and free are called in only one location
Browse files Browse the repository at this point in the history
  • Loading branch information
wjwwood committed May 11, 2018
1 parent 1f9f4d1 commit 266ef58
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 159 deletions.
15 changes: 8 additions & 7 deletions include/rcutils/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C"

/// Encapsulation of an allocator.
/**
* The default allocator uses std::malloc(), std::free(), std::calloc(), and std::realloc().
* The default allocator uses malloc(), free(), calloc(), and realloc().
* It can be obtained using rcutils_get_default_allocator().
*
* The allocator should be trivially copyable.
Expand All @@ -47,17 +47,17 @@ typedef struct rcutils_allocator_t
/// Allocate memory, given a size and the `state` pointer.
/** An error should be indicated by returning `NULL`. */
void * (*allocate)(size_t size, void * state);
/// Deallocate previously allocated memory, mimicking std::free().
/// Deallocate previously allocated memory, mimicking free().
/** Also takes the `state` pointer. */
void (* deallocate)(void * pointer, void * state);
/// Reallocate if possible, otherwise it deallocates and allocates.
/**
* Also takes the `state` pointer.
*
* If unsupported then do deallocate and then allocate.
* This should behave as std::realloc() does, as opposed to posix's
* This should behave as realloc() does, as opposed to posix's
* [reallocf](https://linux.die.net/man/3/reallocf), i.e. the memory given
* by pointer will not be free'd automatically if std::realloc() fails.
* by pointer will not be free'd automatically if realloc() fails.
* For reallocf-like behavior use rcutils_reallocf().
* This function must be able to take an input pointer of `NULL` and succeed.
*/
Expand Down Expand Up @@ -87,9 +87,10 @@ rcutils_get_zero_initialized_allocator(void);
/**
* This defaults to:
*
* - allocate = wraps std::malloc()
* - deallocate = wraps std::free()
* - reallocate = wrapps std::realloc()
* - allocate = wraps malloc()
* - deallocate = wraps free()
* - reallocate = wraps realloc()
* - zero_allocate = wraps calloc()
* - state = `NULL`
*
* <hr>
Expand Down
42 changes: 0 additions & 42 deletions include/rcutils/concat.h

This file was deleted.

10 changes: 8 additions & 2 deletions include/rcutils/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C"
#include <stdbool.h>
#include <stddef.h>

#include "rcutils/allocator.h"
#include "rcutils/macros.h"
#include "rcutils/visibility_control.h"

Expand Down Expand Up @@ -108,17 +109,22 @@ rcutils_is_readable_and_writable(const char * abs_path);
/// Return newly allocated string with arguments separated by correct delimiter for the platform.
/**
* This function allocates memory and returns it to the caller.
* It is up to the caller to release the memory once it is done with it by calling `free`.
* It is up to the caller to release the memory once it is done with it by
* calling `deallocate` on the same allocator passed here.
*
* \param[in] left_hand_path
* \param[in] right_hand_path
* \param[in] allocator
* \return char * concatenated path on success
* NULL on invalid arguments
* NULL on failure
*/
RCUTILS_PUBLIC
char *
rcutils_join_path(const char * left_hand_path, const char * right_hand_path);
rcutils_join_path(
const char * left_hand_path,
const char * right_hand_path,
rcutils_allocator_t allocator);

#if __cplusplus
}
Expand Down
53 changes: 0 additions & 53 deletions src/concat.c

This file was deleted.

12 changes: 8 additions & 4 deletions src/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
extern "C"
{
#endif
#include "rcutils/filesystem.h"

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -26,8 +27,8 @@ extern "C"
#else
#include <direct.h>
#endif // _WIN32
#include "rcutils/concat.h"
#include "rcutils/filesystem.h"

#include "rcutils/format_string.h"

bool
rcutils_get_cwd(char * buffer, size_t max_length)
Expand Down Expand Up @@ -142,7 +143,10 @@ rcutils_is_readable_and_writable(const char * abs_path)
}

char *
rcutils_join_path(const char * left_hand_path, const char * right_hand_path)
rcutils_join_path(
const char * left_hand_path,
const char * right_hand_path,
rcutils_allocator_t allocator)
{
if (NULL == left_hand_path) {
return NULL;
Expand All @@ -157,7 +161,7 @@ rcutils_join_path(const char * left_hand_path, const char * right_hand_path)
const char * delimiter = "/";
#endif // _WIN32

return rcutils_concat(left_hand_path, right_hand_path, delimiter);
return rcutils_format_string(allocator, "%s%s%s", left_hand_path, delimiter, right_hand_path);
}

#if __cplusplus
Expand Down
49 changes: 0 additions & 49 deletions test/test_concat.cpp

This file was deleted.

6 changes: 4 additions & 2 deletions test/test_logging_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <string.h>

#include "rcutils/allocator.h"
#include "rcutils/logging_macros.h"
#include "rcutils/types/rcutils_ret.h"

Expand All @@ -32,15 +33,16 @@ void custom_handler(
const rcutils_log_location_t * location,
int severity, const char * name, const char * format, va_list * args)
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();
g_log_calls += 1;
g_last_log_event.location = location;
g_last_log_event.severity = severity;
g_last_log_event.name = name ? name : "";
if (g_last_log_event.message) {
free(g_last_log_event.message);
allocator.deallocate(g_last_log_event.message, allocator.state);
}
const size_t size = 1024;
g_last_log_event.message = malloc(size);
g_last_log_event.message = allocator.allocate(size, allocator.state);
vsnprintf(g_last_log_event.message, size, format, *args);
}

Expand Down

0 comments on commit 266ef58

Please sign in to comment.