1c87c5fbaSopenharmony_ci/*
2c87c5fbaSopenharmony_ci * coap_mem.h -- CoAP memory handling
3c87c5fbaSopenharmony_ci *
4c87c5fbaSopenharmony_ci * Copyright (C) 2010-2011,2014-2015 Olaf Bergmann <bergmann@tzi.org>
5c87c5fbaSopenharmony_ci *
6c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
7c87c5fbaSopenharmony_ci *
8c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms
9c87c5fbaSopenharmony_ci * of use.
10c87c5fbaSopenharmony_ci */
11c87c5fbaSopenharmony_ci
12c87c5fbaSopenharmony_ci/**
13c87c5fbaSopenharmony_ci * @file coap_mem.h
14c87c5fbaSopenharmony_ci * @brief CoAP memory handling
15c87c5fbaSopenharmony_ci */
16c87c5fbaSopenharmony_ci
17c87c5fbaSopenharmony_ci#ifndef COAP_MEM_H_
18c87c5fbaSopenharmony_ci#define COAP_MEM_H_
19c87c5fbaSopenharmony_ci
20c87c5fbaSopenharmony_ci#include "coap3/coap_oscore.h"
21c87c5fbaSopenharmony_ci#include <stdlib.h>
22c87c5fbaSopenharmony_ci
23c87c5fbaSopenharmony_ci#ifndef WITH_LWIP
24c87c5fbaSopenharmony_ci/**
25c87c5fbaSopenharmony_ci * Initializes libcoap's memory management.
26c87c5fbaSopenharmony_ci * This function must be called once before coap_malloc() can be used on
27c87c5fbaSopenharmony_ci * constrained devices.
28c87c5fbaSopenharmony_ci */
29c87c5fbaSopenharmony_civoid coap_memory_init(void);
30c87c5fbaSopenharmony_ci#endif /* WITH_LWIP */
31c87c5fbaSopenharmony_ci
32c87c5fbaSopenharmony_ci/**
33c87c5fbaSopenharmony_ci * Type specifiers for coap_malloc_type(). Memory objects can be typed to
34c87c5fbaSopenharmony_ci * facilitate arrays of type objects to be used instead of dynamic memory
35c87c5fbaSopenharmony_ci * management on constrained devices.
36c87c5fbaSopenharmony_ci */
37c87c5fbaSopenharmony_citypedef enum {
38c87c5fbaSopenharmony_ci  COAP_STRING,
39c87c5fbaSopenharmony_ci  COAP_ATTRIBUTE_NAME,
40c87c5fbaSopenharmony_ci  COAP_ATTRIBUTE_VALUE,
41c87c5fbaSopenharmony_ci  COAP_PACKET,
42c87c5fbaSopenharmony_ci  COAP_NODE,
43c87c5fbaSopenharmony_ci  COAP_CONTEXT,
44c87c5fbaSopenharmony_ci  COAP_ENDPOINT,
45c87c5fbaSopenharmony_ci  COAP_PDU,
46c87c5fbaSopenharmony_ci  COAP_PDU_BUF,
47c87c5fbaSopenharmony_ci  COAP_RESOURCE,
48c87c5fbaSopenharmony_ci  COAP_RESOURCEATTR,
49c87c5fbaSopenharmony_ci  COAP_DTLS_SESSION,
50c87c5fbaSopenharmony_ci  COAP_SESSION,
51c87c5fbaSopenharmony_ci  COAP_OPTLIST,
52c87c5fbaSopenharmony_ci  COAP_CACHE_KEY,
53c87c5fbaSopenharmony_ci  COAP_CACHE_ENTRY,
54c87c5fbaSopenharmony_ci  COAP_LG_XMIT,
55c87c5fbaSopenharmony_ci  COAP_LG_CRCV,
56c87c5fbaSopenharmony_ci  COAP_LG_SRCV,
57c87c5fbaSopenharmony_ci  COAP_DIGEST_CTX,
58c87c5fbaSopenharmony_ci  COAP_SUBSCRIPTION,
59c87c5fbaSopenharmony_ci  COAP_DTLS_CONTEXT,
60c87c5fbaSopenharmony_ci  COAP_OSCORE_COM,
61c87c5fbaSopenharmony_ci  COAP_OSCORE_SEN,
62c87c5fbaSopenharmony_ci  COAP_OSCORE_REC,
63c87c5fbaSopenharmony_ci  COAP_OSCORE_EX,
64c87c5fbaSopenharmony_ci  COAP_OSCORE_EP,
65c87c5fbaSopenharmony_ci  COAP_OSCORE_BUF,
66c87c5fbaSopenharmony_ci  COAP_COSE,
67c87c5fbaSopenharmony_ci} coap_memory_tag_t;
68c87c5fbaSopenharmony_ci
69c87c5fbaSopenharmony_ci#ifndef WITH_LWIP
70c87c5fbaSopenharmony_ci
71c87c5fbaSopenharmony_ci/**
72c87c5fbaSopenharmony_ci * Allocates a chunk of @p size bytes and returns a pointer to the newly
73c87c5fbaSopenharmony_ci * allocated memory. The @p type is used to select the appropriate storage
74c87c5fbaSopenharmony_ci * container on constrained devices. The storage allocated by coap_malloc_type()
75c87c5fbaSopenharmony_ci * must be released with coap_free_type().
76c87c5fbaSopenharmony_ci *
77c87c5fbaSopenharmony_ci * @param type The type of object to be stored.
78c87c5fbaSopenharmony_ci * @param size The number of bytes requested.
79c87c5fbaSopenharmony_ci * @return     A pointer to the allocated storage or @c NULL on error.
80c87c5fbaSopenharmony_ci */
81c87c5fbaSopenharmony_civoid *coap_malloc_type(coap_memory_tag_t type, size_t size);
82c87c5fbaSopenharmony_ci
83c87c5fbaSopenharmony_ci/**
84c87c5fbaSopenharmony_ci * Reallocates a chunk @p p of bytes created by coap_malloc_type() or
85c87c5fbaSopenharmony_ci * coap_realloc_type() and returns a pointer to the newly allocated memory of
86c87c5fbaSopenharmony_ci * @p size.
87c87c5fbaSopenharmony_ci * Only COAP_STRING type is supported.
88c87c5fbaSopenharmony_ci *
89c87c5fbaSopenharmony_ci * Note: If there is an error, @p p will separately need to be released by
90c87c5fbaSopenharmony_ci * coap_free_type().
91c87c5fbaSopenharmony_ci *
92c87c5fbaSopenharmony_ci * @param type The type of object to be stored.
93c87c5fbaSopenharmony_ci * @param p    A pointer to memory that was allocated by coap_malloc_type().
94c87c5fbaSopenharmony_ci * @param size The number of bytes requested.
95c87c5fbaSopenharmony_ci * @return     A pointer to the allocated storage or @c NULL on error.
96c87c5fbaSopenharmony_ci */
97c87c5fbaSopenharmony_civoid *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
98c87c5fbaSopenharmony_ci
99c87c5fbaSopenharmony_ci/**
100c87c5fbaSopenharmony_ci * Releases the memory that was allocated by coap_malloc_type(). The type tag @p
101c87c5fbaSopenharmony_ci * type must be the same that was used for allocating the object pointed to by
102c87c5fbaSopenharmony_ci * @p .
103c87c5fbaSopenharmony_ci *
104c87c5fbaSopenharmony_ci * @param type The type of the object to release.
105c87c5fbaSopenharmony_ci * @param p    A pointer to memory that was allocated by coap_malloc_type().
106c87c5fbaSopenharmony_ci */
107c87c5fbaSopenharmony_civoid coap_free_type(coap_memory_tag_t type, void *p);
108c87c5fbaSopenharmony_ci
109c87c5fbaSopenharmony_ci/**
110c87c5fbaSopenharmony_ci * Wrapper function to coap_malloc_type() for backwards compatibility.
111c87c5fbaSopenharmony_ci */
112c87c5fbaSopenharmony_ciCOAP_STATIC_INLINE void *
113c87c5fbaSopenharmony_cicoap_malloc(size_t size) {
114c87c5fbaSopenharmony_ci  return coap_malloc_type(COAP_STRING, size);
115c87c5fbaSopenharmony_ci}
116c87c5fbaSopenharmony_ci
117c87c5fbaSopenharmony_ci/**
118c87c5fbaSopenharmony_ci * Wrapper function to coap_free_type() for backwards compatibility.
119c87c5fbaSopenharmony_ci */
120c87c5fbaSopenharmony_ciCOAP_STATIC_INLINE void
121c87c5fbaSopenharmony_cicoap_free(void *object) {
122c87c5fbaSopenharmony_ci  coap_free_type(COAP_STRING, object);
123c87c5fbaSopenharmony_ci}
124c87c5fbaSopenharmony_ci
125c87c5fbaSopenharmony_ci#endif /* not WITH_LWIP */
126c87c5fbaSopenharmony_ci
127c87c5fbaSopenharmony_ci#ifdef WITH_LWIP
128c87c5fbaSopenharmony_ci
129c87c5fbaSopenharmony_ci#include <lwip/memp.h>
130c87c5fbaSopenharmony_ci
131c87c5fbaSopenharmony_ci/* no initialization needed with lwip (or, more precisely: lwip must be
132c87c5fbaSopenharmony_ci * completely initialized anyway by the time coap gets active)  */
133c87c5fbaSopenharmony_ciCOAP_STATIC_INLINE void
134c87c5fbaSopenharmony_cicoap_memory_init(void) {}
135c87c5fbaSopenharmony_ci
136c87c5fbaSopenharmony_ci/* It would be nice to check that size equals the size given at the memp
137c87c5fbaSopenharmony_ci * declaration, but i currently don't see a standard way to check that without
138c87c5fbaSopenharmony_ci * sourcing the custom memp pools and becoming dependent of its syntax
139c87c5fbaSopenharmony_ci */
140c87c5fbaSopenharmony_ci#define coap_malloc_type(type, asize) \
141c87c5fbaSopenharmony_ci  (((asize) <= memp_pools[MEMP_ ## type]->size) ? \
142c87c5fbaSopenharmony_ci   memp_malloc(MEMP_ ## type) : NULL)
143c87c5fbaSopenharmony_ci#define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
144c87c5fbaSopenharmony_ci
145c87c5fbaSopenharmony_ci/* As these are fixed size, return value if already defined */
146c87c5fbaSopenharmony_ci#define coap_realloc_type(type, p, asize) \
147c87c5fbaSopenharmony_ci  ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize))
148c87c5fbaSopenharmony_ci
149c87c5fbaSopenharmony_ci/* Those are just here to make uri.c happy where string allocation has not been
150c87c5fbaSopenharmony_ci * made conditional.
151c87c5fbaSopenharmony_ci */
152c87c5fbaSopenharmony_ciCOAP_STATIC_INLINE void *
153c87c5fbaSopenharmony_cicoap_malloc(size_t size) {
154c87c5fbaSopenharmony_ci  (void)size;
155c87c5fbaSopenharmony_ci  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
156c87c5fbaSopenharmony_ci}
157c87c5fbaSopenharmony_ci
158c87c5fbaSopenharmony_ciCOAP_STATIC_INLINE void
159c87c5fbaSopenharmony_cicoap_free(void *pointer) {
160c87c5fbaSopenharmony_ci  (void)pointer;
161c87c5fbaSopenharmony_ci  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
162c87c5fbaSopenharmony_ci}
163c87c5fbaSopenharmony_ci
164c87c5fbaSopenharmony_ci#endif /* WITH_LWIP */
165c87c5fbaSopenharmony_ci
166c87c5fbaSopenharmony_ci#endif /* COAP_MEM_H_ */
167