FIDO-Raw

 view release on metacpan or  search on metacpan

deps/libcbor/src/cbor/common.h  view on Meta::CPAN


#ifndef LIBCBOR_COMMON_H
#define LIBCBOR_COMMON_H

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "cbor/configuration.h"
#include "data.h"

#ifdef __cplusplus
extern "C" {

/**
 * C++ is not a subset of C99 -- 'restrict' qualifier is not a part of the
 * language. This is a workaround to keep it in C headers -- compilers allow
 * linking non-restrict signatures with restrict implementations.
 *
 * If you know a nicer way, please do let me know.
 */
#define CBOR_RESTRICT_POINTER

#else

// MSVC + C++ workaround
#define CBOR_RESTRICT_POINTER CBOR_RESTRICT_SPECIFIER

#endif

static const uint8_t cbor_major_version = CBOR_MAJOR_VERSION;
static const uint8_t cbor_minor_version = CBOR_MINOR_VERSION;
static const uint8_t cbor_patch_version = CBOR_PATCH_VERSION;

#define CBOR_VERSION         \
  TO_STR(CBOR_MAJOR_VERSION) \
  "." TO_STR(CBOR_MINOR_VERSION) "." TO_STR(CBOR_PATCH_VERSION)
#define CBOR_HEX_VERSION \
  ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)

/* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing
 */
#ifdef DEBUG
#include <stdio.h>
#define debug_print(fmt, ...)                                           \
  do {                                                                  \
    if (DEBUG)                                                          \
      fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, \
              __VA_ARGS__);                                             \
  } while (0)
#else
#define debug_print(fmt, ...) \
  do {                        \
  } while (0)
#endif

#define TO_STR_(x) #x
#define TO_STR(x) TO_STR_(x) /* enables proper double expansion */

// Macro to short-circuit builder functions when memory allocation fails
#define _CBOR_NOTNULL(cbor_item) \
  do {                           \
    if (cbor_item == NULL) {     \
      return NULL;               \
    }                            \
  } while (0)

// Macro to short-circuit builders when memory allocation of nested data fails
#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer) \
  do {                                              \
    if (pointer == NULL) {                          \
      _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 *);

extern _cbor_malloc_t _cbor_malloc;
extern _cbor_realloc_t _cbor_realloc;
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
 * 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) \endrst
 *
 * @param custom_malloc malloc implementation
 * @param custom_realloc realloc implementation
 * @param custom_free free implementation
 */
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
 * ============================================================================
 */

/** Get the type of the item
 *
 * @param item[borrow]
 * @return The type



( run in 0.484 second using v1.01-cache-2.11-cpan-5a3173703d6 )