libcbor 0.12.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Loading...
Searching...
No Matches
common.h File Reference
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "cbor/cbor_export.h"
#include "cbor/configuration.h"
#include "data.h"

Go to the source code of this file.

Macros

#define CBOR_RESTRICT_POINTER   CBOR_RESTRICT_SPECIFIER
 
#define CBOR_VERSION
 
#define CBOR_HEX_VERSION    ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)
 
#define debug_print(fmt, ...)
 
#define CBOR_ASSERT(e)
 
#define _CBOR_TEST_DISABLE_ASSERT(block)
 
#define _CBOR_TO_STR_(x)
 
#define _CBOR_TO_STR(x)
 
#define _CBOR_UNUSED
 
#define _CBOR_NODISCARD
 
#define _CBOR_UNREACHABLE
 
#define _CBOR_NOTNULL(cbor_item)
 
#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer)
 

Typedefs

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

Functions

void cbor_set_allocs (_cbor_malloc_t custom_malloc, _cbor_realloc_t custom_realloc, _cbor_free_t custom_free)
 Sets the memory management routines to use.
 
cbor_type cbor_typeof (const cbor_item_t *item)
 Get the type of the item.
 
bool cbor_isa_uint (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_negint (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_bytestring (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_string (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_array (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_map (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_tag (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_isa_float_ctrl (const cbor_item_t *item)
 Does the item have the appropriate major type?
 
bool cbor_is_int (const cbor_item_t *item)
 Is the item an integer, either positive or negative?
 
bool cbor_is_float (const cbor_item_t *item)
 Is the item an a floating point number?
 
bool cbor_is_bool (const cbor_item_t *item)
 Is the item an a boolean?
 
bool cbor_is_null (const cbor_item_t *item)
 Does this item represent null
 
bool cbor_is_undef (const cbor_item_t *item)
 Does this item represent undefined
 
cbor_item_tcbor_incref (cbor_item_t *item)
 Increases the item's reference count by one.
 
void cbor_decref (cbor_item_t **item)
 Decreases the item's reference count by one, deallocating the item if needed.
 
void cbor_intermediate_decref (cbor_item_t *item)
 Decreases the item's reference count by one, deallocating the item if needed.
 
size_t cbor_refcount (const cbor_item_t *item)
 Get the item's reference count.
 
cbor_item_tcbor_move (cbor_item_t *item)
 Provides CPP-like move construct.
 

Variables

_cbor_malloc_t _cbor_malloc
 
_cbor_realloc_t _cbor_realloc
 
_cbor_free_t _cbor_free
 

Macro Definition Documentation

◆ _CBOR_DEPENDENT_NOTNULL

#define _CBOR_DEPENDENT_NOTNULL ( cbor_item,
pointer )
Value:
do { \
if (pointer == NULL) { \
_cbor_free(cbor_item); \
return NULL; \
} \
} while (0)

Definition at line 119 of file common.h.

◆ _CBOR_NODISCARD

#define _CBOR_NODISCARD

Definition at line 93 of file common.h.

◆ _CBOR_NOTNULL

#define _CBOR_NOTNULL ( cbor_item)
Value:
do { \
if (cbor_item == NULL) { \
return NULL; \
} \
} while (0)

Definition at line 111 of file common.h.

◆ _CBOR_TEST_DISABLE_ASSERT

#define _CBOR_TEST_DISABLE_ASSERT ( block)
Value:
do { \
block \
} while (0)

Definition at line 74 of file common.h.

◆ _CBOR_TO_STR

#define _CBOR_TO_STR ( x)
Value:
_CBOR_TO_STR_(x) /* enables proper double expansion */
#define _CBOR_TO_STR_(x)
Definition common.h:80

Definition at line 81 of file common.h.

◆ _CBOR_TO_STR_

#define _CBOR_TO_STR_ ( x)
Value:
#x

Definition at line 80 of file common.h.

◆ _CBOR_UNREACHABLE

#define _CBOR_UNREACHABLE

Definition at line 99 of file common.h.

◆ _CBOR_UNUSED

#define _CBOR_UNUSED

Definition at line 92 of file common.h.

◆ CBOR_ASSERT

#define CBOR_ASSERT ( e)

Definition at line 73 of file common.h.

◆ CBOR_HEX_VERSION

#define CBOR_HEX_VERSION    ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)

Definition at line 47 of file common.h.

◆ CBOR_RESTRICT_POINTER

#define CBOR_RESTRICT_POINTER   CBOR_RESTRICT_SPECIFIER

Definition at line 36 of file common.h.

◆ CBOR_VERSION

#define CBOR_VERSION
Value:
_CBOR_TO_STR(CBOR_MAJOR_VERSION) \
"." _CBOR_TO_STR(CBOR_MINOR_VERSION) "." _CBOR_TO_STR(CBOR_PATCH_VERSION)
#define _CBOR_TO_STR(x)
Definition common.h:81

Definition at line 44 of file common.h.

◆ debug_print

#define debug_print ( fmt,
... )
Value:
do { \
} while (0)

Definition at line 70 of file common.h.

Typedef Documentation

◆ _cbor_free_t

typedef void(* _cbor_free_t) (void *)

Definition at line 104 of file common.h.

◆ _cbor_malloc_t

typedef void *(* _cbor_malloc_t) (size_t)

Definition at line 102 of file common.h.

◆ _cbor_realloc_t

typedef void *(* _cbor_realloc_t) (void *, size_t)

Definition at line 103 of file common.h.

Function Documentation

◆ cbor_decref()

void cbor_decref ( cbor_item_t ** item)

Decreases the item's reference count by one, deallocating the item if needed.

In case the item is deallocated, the reference count of all items this item references will also be cbor_decref 'ed recursively.

Parameters
itemReference to an item. Will be set to NULL if deallocated

Definition at line 83 of file common.c.

◆ cbor_incref()

cbor_item_t * cbor_incref ( cbor_item_t * item)

Increases the item's reference count by one.

Constant complexity; items referring to this one or items being referred to are not updated.

This function can be used to extend reference counting to client code.

Parameters
itemReference to an item
Returns
The input item

Definition at line 78 of file common.c.

◆ cbor_intermediate_decref()

void cbor_intermediate_decref ( cbor_item_t * item)

Decreases the item's reference count by one, deallocating the item if needed.

Convenience wrapper for cbor_decref when its set-to-null behavior is not needed

Parameters
itemReference to an item

Definition at line 154 of file common.c.

◆ cbor_is_bool()

bool cbor_is_bool ( const cbor_item_t * item)

Is the item an a boolean?

Parameters
itemthe item
Returns
Is the item a boolean?

Definition at line 60 of file common.c.

◆ cbor_is_float()

bool cbor_is_float ( const cbor_item_t * item)

Is the item an a floating point number?

Parameters
itemthe item
Returns
Is the item a floating point number?

Definition at line 74 of file common.c.

◆ cbor_is_int()

bool cbor_is_int ( const cbor_item_t * item)

Is the item an integer, either positive or negative?

Parameters
itemthe item
Returns
Is the item an integer, either positive or negative?

Definition at line 56 of file common.c.

◆ cbor_is_null()

bool cbor_is_null ( const cbor_item_t * item)

Does this item represent null

embed:rst:leading-asterisk 
* .. warning:: This is in no way related to the value of the pointer. Passing a
*  null pointer will most likely result in a crash.
* 
Parameters
itemthe item
Returns
Is the item (CBOR logical) null?

Definition at line 66 of file common.c.

◆ cbor_is_undef()

bool cbor_is_undef ( const cbor_item_t * item)

Does this item represent undefined

embed:rst:leading-asterisk 
* .. warning:: Care must be taken to distinguish nulls and undefined values in
*  C.
* 
Parameters
itemthe item
Returns
Is the item (CBOR logical) undefined?

Definition at line 70 of file common.c.

◆ cbor_isa_array()

bool cbor_isa_array ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item an CBOR_TYPE_ARRAY?

Definition at line 38 of file common.c.

◆ cbor_isa_bytestring()

bool cbor_isa_bytestring ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_BYTESTRING?

Definition at line 30 of file common.c.

◆ cbor_isa_float_ctrl()

bool cbor_isa_float_ctrl ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_FLOAT_CTRL?

Definition at line 50 of file common.c.

◆ cbor_isa_map()

bool cbor_isa_map ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_MAP?

Definition at line 42 of file common.c.

◆ cbor_isa_negint()

bool cbor_isa_negint ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_NEGINT?

Definition at line 26 of file common.c.

◆ cbor_isa_string()

bool cbor_isa_string ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_STRING?

Definition at line 34 of file common.c.

◆ cbor_isa_tag()

bool cbor_isa_tag ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item a CBOR_TYPE_TAG?

Definition at line 46 of file common.c.

◆ cbor_isa_uint()

bool cbor_isa_uint ( const cbor_item_t * item)

Does the item have the appropriate major type?

Parameters
itemthe item
Returns
Is the item an CBOR_TYPE_UINT?

Definition at line 22 of file common.c.

◆ cbor_move()

cbor_item_t * cbor_move ( cbor_item_t * item)

Provides CPP-like move construct.

Decreases the reference count by one, but does not deallocate the item even if its refcount reaches zero. This is useful for passing intermediate values to functions that increase reference count. Should only be used with functions that incref their arguments.

embed:rst:leading-asterisk 
* .. warning:: If the item is moved without correctly increasing the reference
*  count afterwards, the memory will be leaked.
* 
Parameters
itemReference to an item
Returns
the item with reference count decreased by one

Definition at line 158 of file common.c.

◆ cbor_refcount()

size_t cbor_refcount ( const cbor_item_t * item)

Get the item's reference count.

embed:rst:leading-asterisk 
* .. warning:: This does *not* account for transitive references.
* 
Todo
Add some inline examples for reference counting
Parameters
itemthe item
Returns
the reference count

Definition at line 156 of file common.c.

◆ cbor_set_allocs()

void cbor_set_allocs ( _cbor_malloc_t custom_malloc,
_cbor_realloc_t custom_realloc,
_cbor_free_t custom_free )

Sets the memory management routines to use.

By default, libcbor will use the standard library malloc, realloc, and free.

embed:rst:leading-asterisk 
* .. warning:: This function modifies the global state and should therefore be
*  used accordingly. Changing the memory handlers while allocated items exist
*  will result in a ``free``/``malloc`` mismatch. This function is not thread
*  safe with respect to both itself and all the other *libcbor* functions that
*  work with the heap.
*
* .. note:: `realloc` implementation must correctly support `NULL` reallocation
*  (see e.g. http://en.cppreference.com/w/c/memory/realloc)
* 
Parameters
custom_mallocmalloc implementation
custom_reallocrealloc implementation
custom_freefree implementation

Definition at line 14 of file allocators.c.

◆ cbor_typeof()

cbor_type cbor_typeof ( const cbor_item_t * item)

Get the type of the item.

Parameters
item
Returns
The type

Definition at line 54 of file common.c.

Variable Documentation

◆ _cbor_free

_cbor_free_t _cbor_free
extern

Definition at line 12 of file allocators.c.

◆ _cbor_malloc

_cbor_malloc_t _cbor_malloc
extern

Definition at line 10 of file allocators.c.

◆ _cbor_realloc

_cbor_realloc_t _cbor_realloc
extern

Definition at line 11 of file allocators.c.