Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic memory audit #102

Merged
merged 3 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ endif()
set(rcutils_sources
src/allocator.c
src/cmdline_parser.c
src/concat.c
src/error_handling.c
src/filesystem.c
src/find.c
Expand Down Expand Up @@ -213,13 +212,6 @@ if(BUILD_TESTING)
target_link_libraries(test_find ${PROJECT_NAME})
endif()

rcutils_custom_add_gtest(test_concat
test/test_concat.cpp
)
if(TARGET test_concat)
target_link_libraries(test_concat ${PROJECT_NAME})
endif()

rcutils_custom_add_gtest(test_string_array
test/test_string_array.cpp
)
Expand Down
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);

#ifdef __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);
}

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

This file was deleted.

Loading