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

Deprecate CBOR_CUSTOM_ALLOC #237

Merged
merged 10 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ commands:
steps:
- run: >
cmake -DWITH_TESTS=ON \
-DCBOR_CUSTOM_ALLOC=ON \
-DCMAKE_BUILD_TYPE=Debug \
-DSANITIZE=OFF \
-DCOVERAGE="${CMAKE_COVERAGE:='OFF'}" \
Expand Down
2 changes: 1 addition & 1 deletion Bazel.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ genrule(
"INITIAL_WD=`pwd`",
# Build libcbor library.
"cd `dirname $(location CMakeLists.txt)`",
"cmake -DCMAKE_BUILD_TYPE=Release -DCBOR_CUSTOM_ALLOC=ON .",
"cmake -DCMAKE_BUILD_TYPE=Release .",
"cmake --build .",
# Export the .a and .h files for cbor rule, below.
"cp src/libcbor.a src/cbor.h $$INITIAL_WD/$(RULEDIR)",
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Next
- [Reworked `cbor_serialize_alloc` to allocate the exact amount of memory necessary upfront](https://github.com/PJK/libcbor/pull/251)
- This should significantly speed up `cbor_serialize_alloc` for large items by avoiding multiple reallocation iterations
- Clients should not use the return value of `cbor_serialize_alloc`. It may be removed in the future.

- BUILD BREAKING: [Deprecate CBOR_CUSTOM_ALLOC](https://github.com/PJK/libcbor/pull/237)
- `cbor_set_allocs` will always be enabled from now on
- Note: The flag will be kept as a no-op triggering a warning when used for one version and then removed completely

0.9.0 (2021-11-14)
---------------------
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ if(BIG_ENDIAN)
endif()

option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
if(CBOR_CUSTOM_ALLOC)
message(WARNING
"CBOR_CUSTOM_ALLOC has been deprecated. Custom allocators are now enabled by default."
"The flag is a no-op and will be removed in the next version. "
"Please remove CBOR_CUSTOM_ALLOC from your build configuation.")
endif(CBOR_CUSTOM_ALLOC)

option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack")
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = CBOR_CUSTOM_ALLOC CBOR_RESTRICT_POINTER= CBOR_EXPORT
PREDEFINED = CBOR_RESTRICT_POINTER= CBOR_EXPORT

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
11 changes: 4 additions & 7 deletions doc/source/api/item_reference_counting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ If you have specific requirements, you should consider rolling your own driver f
Using custom allocator
^^^^^^^^^^^^^^^^^^^^^^^^

*libcbor* gives you with the ability to provide your own implementations of ``malloc``, ``realloc``, and ``free``. This can be useful if you are using a custom allocator throughout your application, or if you want to implement custom policies (e.g. tighter restrictions on the amount of allocated memory).
*libcbor* gives you with the ability to provide your own implementations of ``malloc``, ``realloc``, and ``free``.
This can be useful if you are using a custom allocator throughout your application,
or if you want to implement custom policies (e.g. tighter restrictions on the amount of allocated memory).

In order to use this feature, *libcbor* has to be compiled with the :doc:`appropriate flags </getting_started>`. You can verify the configuration using the ``CBOR_CUSTOM_ALLOC`` macro. A simple usage might be as follows:

.. code-block:: c

#if CBOR_CUSTOM_ALLOC
cbor_set_allocs(malloc, realloc, free);
#else
#error "libcbor built with support for custom allocation is required"
#endif
cbor_set_allocs(malloc, realloc, free);

.. doxygenfunction:: cbor_set_allocs

Expand Down
5 changes: 4 additions & 1 deletion doc/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ The following configuration options will also be defined as macros [#]_ in ``<cb
======================== ======================================================= ====================== =====================================================================================================================
Option Meaning Default Possible values
------------------------ ------------------------------------------------------- ---------------------- ---------------------------------------------------------------------------------------------------------------------
``CBOR_CUSTOM_ALLOC`` Enable custom allocator support ``OFF`` ``ON``, ``OFF``
``CBOR_PRETTY_PRINTER`` Include a pretty-printing routine ``ON`` ``ON``, ``OFF``
``CBOR_BUFFER_GROWTH`` Factor for buffer growth & shrinking ``2`` Decimals > 1
======================== ======================================================= ====================== =====================================================================================================================
Expand All @@ -64,6 +63,10 @@ Option Meaning

If you want to pass other custom configuration options, please refer to `<http://www.cmake.org/Wiki/CMake_Useful_Variables>`_.

.. warning::
``CBOR_CUSTOM_ALLOC`` has been `removed <https://github.com/PJK/libcbor/pull/237>`_.
Custom allocators (historically a controlled by a build flag) are always enabled.

**Building using make**

CMake will generate a Makefile and other configuration files for the build. As a rule of thumb, you should configure the
Expand Down
6 changes: 2 additions & 4 deletions examples/bazel/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ http_archive(
)

# libcbor
http_archive(
new_local_repository(
name = "libcbor",
build_file = "//third_party:libcbor.BUILD",
sha256 = "dd04ea1a7df484217058d389e027e7a0143a4f245aa18a9f89a5dd3e1a4fcc9a",
strip_prefix = "libcbor-0.8.0",
urls = ["https://github.com/PJK/libcbor/archive/refs/tags/v0.8.0.zip"],
path = "../../src",
)

5 changes: 2 additions & 3 deletions examples/bazel/src/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "cbor.h"

uint8_t cbor_version() {
return cbor_major_version;
void print_cbor_version() {
printf("libcbor v%d.%d.%d\n", cbor_major_version, cbor_minor_version, cbor_patch_version);
}

2 changes: 1 addition & 1 deletion examples/bazel/src/hello.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include <cstdint>

uint8_t cbor_version(void);
void print_cbor_version(void);

#endif // HELLO_H_
3 changes: 2 additions & 1 deletion examples/bazel/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <stdio.h>

int main() {
printf("Hello, v=%d\n", cbor_version());
print_cbor_version();

return 0;
}

20 changes: 11 additions & 9 deletions examples/bazel/third_party/libcbor.BUILD
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
cc_library(
name = "cbor",
srcs = glob([
"src/**/*.h",
"src/**/*.c",

"src/**/*.h",
"src/**/*.c",
]),
deps = [
"@libcbor_bazel_example//third_party/libcbor:config",
],
hdrs = [
"src/cbor.h",
],
"cbor.h",
] + glob([
"cbor/*.h",
]),
includes = [
"src",
"src/cbor",
"src/cbor/internal",
],
visibility = ["//visibility:public"],
deps = [
"@libcbor_bazel_example//third_party/libcbor:config",
],
)

1 change: 0 additions & 1 deletion examples/bazel/third_party/libcbor/cbor/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define CBOR_MINOR_VERSION 9
#define CBOR_PATCH_VERSION 0

#define CBOR_CUSTOM_ALLOC 1
#define CBOR_BUFFER_GROWTH 2
#define CBOR_MAX_STACK_SIZE 2048
#define CBOR_PRETTY_PRINTER 1
Expand Down
1 change: 0 additions & 1 deletion examples/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

int main(void) {
printf("Hello from libcbor %s\n", CBOR_VERSION);
printf("Custom allocation support: %s\n", CBOR_CUSTOM_ALLOC ? "yes" : "no");
printf("Pretty-printer support: %s\n", CBOR_PRETTY_PRINTER ? "yes" : "no");
printf("Buffer growth factor: %f\n", (float)CBOR_BUFFER_GROWTH);
}
2 changes: 1 addition & 1 deletion oss-fuzz/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
mkdir build
cd build
# We disable libcbor's default sanitizers since we'll be configuring them ourselves via CFLAGS.
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX="$WORK" -D CBOR_CUSTOM_ALLOC=ON -D SANITIZE=OFF ..
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX="$WORK" -D SANITIZE=OFF ..
make "-j$(nproc)"
make install

Expand Down
6 changes: 1 addition & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
set(SOURCES cbor.c cbor/streaming.c cbor/internal/encoders.c cbor/internal/builder_callbacks.c cbor/internal/loaders.c cbor/internal/memory_utils.c cbor/internal/stack.c cbor/internal/unicode.c cbor/encoding.c cbor/serialization.c cbor/arrays.c cbor/common.c cbor/floats_ctrls.c cbor/bytestrings.c cbor/callbacks.c cbor/strings.c cbor/maps.c cbor/tags.c cbor/ints.c)
set(SOURCES cbor.c allocators.c cbor/streaming.c cbor/internal/encoders.c cbor/internal/builder_callbacks.c cbor/internal/loaders.c cbor/internal/memory_utils.c cbor/internal/stack.c cbor/internal/unicode.c cbor/encoding.c cbor/serialization.c cbor/arrays.c cbor/common.c cbor/floats_ctrls.c cbor/bytestrings.c cbor/callbacks.c cbor/strings.c cbor/maps.c cbor/tags.c cbor/ints.c)

include(GNUInstallDirs)
include(JoinPaths)
include(CheckFunctionExists)
set(CMAKE_SKIP_BUILD_RPATH FALSE)

if (CBOR_CUSTOM_ALLOC)
LIST(APPEND SOURCES allocators.c)
endif(CBOR_CUSTOM_ALLOC)

if (NOT DEFINED CMAKE_MACOSX_RPATH)
set(CMAKE_MACOSX_RPATH 0)
endif()
Expand Down
4 changes: 2 additions & 2 deletions src/cbor/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ cbor_item_t **cbor_array_handle(const cbor_item_t *item) {
}

cbor_item_t *cbor_new_definite_array(size_t size) {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);
cbor_item_t **data = _cbor_alloc_multiple(sizeof(cbor_item_t *), size);
_CBOR_DEPENDENT_NOTNULL(item, data);
Expand All @@ -116,7 +116,7 @@ cbor_item_t *cbor_new_definite_array(size_t size) {
}

cbor_item_t *cbor_new_indefinite_array(void) {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);

*item = (cbor_item_t){
Expand Down
8 changes: 4 additions & 4 deletions src/cbor/bytestrings.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool cbor_bytestring_is_indefinite(const cbor_item_t *item) {
}

cbor_item_t *cbor_new_definite_bytestring(void) {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);
*item = (cbor_item_t){
.refcount = 1,
Expand All @@ -40,14 +40,14 @@ cbor_item_t *cbor_new_definite_bytestring(void) {
}

cbor_item_t *cbor_new_indefinite_bytestring(void) {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);
*item = (cbor_item_t){
.refcount = 1,
.type = CBOR_TYPE_BYTESTRING,
.metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE,
.length = 0}},
.data = _CBOR_MALLOC(sizeof(struct cbor_indefinite_string_data))};
.data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))};
_CBOR_DEPENDENT_NOTNULL(item, item->data);
*((struct cbor_indefinite_string_data *)item->data) =
(struct cbor_indefinite_string_data){
Expand All @@ -61,7 +61,7 @@ cbor_item_t *cbor_new_indefinite_bytestring(void) {
cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length) {
cbor_item_t *item = cbor_new_definite_bytestring();
_CBOR_NOTNULL(item);
void *content = _CBOR_MALLOC(length);
void *content = _cbor_malloc(length);
_CBOR_DEPENDENT_NOTNULL(item, content);
memcpy(content, handle, length);
cbor_bytestring_set_handle(item, content, length);
Expand Down
20 changes: 10 additions & 10 deletions src/cbor/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,29 @@ void cbor_decref(cbor_item_t **item_ref) {
{ break; }
case CBOR_TYPE_BYTESTRING: {
if (cbor_bytestring_is_definite(item)) {
_CBOR_FREE(item->data);
_cbor_free(item->data);
} else {
/* We need to decref all chunks */
cbor_item_t **handle = cbor_bytestring_chunks_handle(item);
for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
cbor_decref(&handle[i]);
_CBOR_FREE(
_cbor_free(
((struct cbor_indefinite_string_data *)item->data)->chunks);
_CBOR_FREE(item->data);
_cbor_free(item->data);
}
break;
}
case CBOR_TYPE_STRING: {
if (cbor_string_is_definite(item)) {
_CBOR_FREE(item->data);
_cbor_free(item->data);
} else {
/* We need to decref all chunks */
cbor_item_t **handle = cbor_string_chunks_handle(item);
for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
cbor_decref(&handle[i]);
_CBOR_FREE(
_cbor_free(
((struct cbor_indefinite_string_data *)item->data)->chunks);
_CBOR_FREE(item->data);
_cbor_free(item->data);
}
break;
}
Expand All @@ -124,7 +124,7 @@ void cbor_decref(cbor_item_t **item_ref) {
size_t size = cbor_array_size(item);
for (size_t i = 0; i < size; i++)
if (handle[i] != NULL) cbor_decref(&handle[i]);
_CBOR_FREE(item->data);
_cbor_free(item->data);
break;
}
case CBOR_TYPE_MAP: {
Expand All @@ -134,21 +134,21 @@ void cbor_decref(cbor_item_t **item_ref) {
cbor_decref(&handle->key);
if (handle->value != NULL) cbor_decref(&handle->value);
}
_CBOR_FREE(item->data);
_cbor_free(item->data);
break;
}
case CBOR_TYPE_TAG: {
if (item->metadata.tag_metadata.tagged_item != NULL)
cbor_decref(&item->metadata.tag_metadata.tagged_item);
_CBOR_FREE(item->data);
_cbor_free(item->data);
break;
}
case CBOR_TYPE_FLOAT_CTRL: {
/* Floats have combined allocation */
break;
}
}
_CBOR_FREE(item);
_cbor_free(item);
*item_ref = NULL;
}
}
Expand Down
34 changes: 9 additions & 25 deletions src/cbor/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ extern bool _cbor_enable_assert;
#define _CBOR_NODISCARD
#endif

typedef void *(*_cbor_malloc_t)(size_t);
typedef void *(*_cbor_realloc_t)(void *, size_t);
typedef void (*_cbor_free_t)(void *);

CBOR_EXPORT extern _cbor_malloc_t _cbor_malloc;
CBOR_EXPORT extern _cbor_realloc_t _cbor_realloc;
CBOR_EXPORT extern _cbor_free_t _cbor_free;

// Macro to short-circuit builder functions when memory allocation fails
#define _CBOR_NOTNULL(cbor_item) \
do { \
Expand All @@ -95,24 +103,12 @@ extern bool _cbor_enable_assert;
#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer) \
do { \
if (pointer == NULL) { \
_CBOR_FREE(cbor_item); \
_cbor_free(cbor_item); \
return NULL; \
} \
} while (0)

#if CBOR_CUSTOM_ALLOC

typedef void *(*_cbor_malloc_t)(size_t);
typedef void *(*_cbor_realloc_t)(void *, size_t);
typedef void (*_cbor_free_t)(void *);

CBOR_EXPORT extern _cbor_malloc_t _cbor_malloc;
CBOR_EXPORT extern _cbor_realloc_t _cbor_realloc;
CBOR_EXPORT extern _cbor_free_t _cbor_free;

/** Sets the memory management routines to use.
*
* Only available when `CBOR_CUSTOM_ALLOC` is truthy
*
* \rst
* .. warning:: This function modifies the global state and should therefore be
Expand All @@ -133,18 +129,6 @@ CBOR_EXPORT void cbor_set_allocs(_cbor_malloc_t custom_malloc,
_cbor_realloc_t custom_realloc,
_cbor_free_t custom_free);

#define _CBOR_MALLOC _cbor_malloc
#define _CBOR_REALLOC _cbor_realloc
#define _CBOR_FREE _cbor_free

#else

#define _CBOR_MALLOC malloc
#define _CBOR_REALLOC realloc
#define _CBOR_FREE free

#endif

/*
* ============================================================================
* Type manipulation
Expand Down
1 change: 0 additions & 1 deletion src/cbor/configuration.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define CBOR_MINOR_VERSION ${CBOR_VERSION_MINOR}
#define CBOR_PATCH_VERSION ${CBOR_VERSION_PATCH}

#cmakedefine01 CBOR_CUSTOM_ALLOC
#define CBOR_BUFFER_GROWTH ${CBOR_BUFFER_GROWTH}
#define CBOR_MAX_STACK_SIZE ${CBOR_MAX_STACK_SIZE}
#cmakedefine01 CBOR_PRETTY_PRINTER
Expand Down
Loading