STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
memp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 
33 #ifndef LWIP_HDR_MEMP_H
34 #define LWIP_HDR_MEMP_H
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* run once with empty definition to handle all custom includes in lwippools.h */
41 #define LWIP_MEMPOOL(name,num,size,desc)
42 #include "lwip/priv/memp_std.h"
43 
44 /* Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */
45 typedef enum {
46 #define LWIP_MEMPOOL(name,num,size,desc) MEMP_##name,
47 #include "lwip/priv/memp_std.h"
49 } memp_t;
50 
51 #include "lwip/priv/memp_priv.h"
52 
53 /* Private mempools example:
54  * .h: only when pool is used in multiple .c files: LWIP_MEMPOOL_PROTOTYPE(my_private_pool);
55  * .c:
56  * - in global variables section: LWIP_MEMPOOL_DECLARE(my_private_pool, 10, sizeof(foo), "Some description")
57  * - call ONCE before using pool (e.g. in some init() function): LWIP_MEMPOOL_INIT(my_private_pool);
58  * - allocate: void* my_new_mem = LWIP_MEMPOOL_ALLOC(my_private_pool);
59  * - free: LWIP_MEMPOOL_FREE(my_private_pool, my_new_mem);
60  *
61  * To relocate a pool, declare it as extern in cc.h. Example for GCC:
62  * extern u8_t __attribute__((section(".onchip_mem"))) memp_memory_my_private_pool[];
63  */
64 
65 extern const struct memp_desc *memp_pools[MEMP_MAX];
66 
67 #define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name
68 
69 #if MEMP_MEM_MALLOC
70 
71 #include "lwip/mem.h"
72 
73 #define memp_init()
74 #define memp_malloc(type) mem_malloc(memp_pools[type]->size)
75 #define memp_free(type, mem) mem_free(mem)
76 
77 #define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \
78  const struct memp_desc memp_ ## name = { \
79  LWIP_MEM_ALIGN_SIZE(size) \
80  };
81 
82 #define LWIP_MEMPOOL_INIT(name)
83 #define LWIP_MEMPOOL_ALLOC(name) mem_malloc(memp_ ## name.size)
84 #define LWIP_MEMPOOL_FREE(name, x) mem_free(x)
85 
86 #else /* MEMP_MEM_MALLOC */
87 
88 #define LWIP_MEMPOOL_DECLARE(name,num,size,desc) u8_t memp_memory_ ## name ## _base \
89  [((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))]; \
90  \
91  static struct memp *memp_tab_ ## name; \
92  \
93  const struct memp_desc memp_ ## name = { \
94  LWIP_MEM_ALIGN_SIZE(size), \
95  (num), \
96  DECLARE_LWIP_MEMPOOL_DESC(desc) \
97  memp_memory_ ## name ## _base, \
98  &memp_tab_ ## name \
99  };
100 
101 #define LWIP_MEMPOOL_INIT(name) memp_init_pool(&memp_ ## name)
102 #define LWIP_MEMPOOL_ALLOC(name) memp_malloc_pool(&memp_ ## name)
103 #define LWIP_MEMPOOL_FREE(name, x) memp_free_pool(&memp_ ## name, (x))
104 
105 #if MEM_USE_POOLS
106 
107 struct memp_malloc_helper
108 {
109  memp_t poolnr;
110 #if MEMP_OVERFLOW_CHECK
111  u16_t size;
112 #endif /* MEMP_OVERFLOW_CHECK */
113 };
114 
115 #endif /* MEM_USE_POOLS */
116 
117 void memp_init(void);
118 
119 #if MEMP_OVERFLOW_CHECK
120 void *memp_malloc_fn(memp_t type, const char* file, const int line);
121 #define memp_malloc(t) memp_malloc_fn((t), __FILE__, __LINE__)
122 #else
123 void *memp_malloc(memp_t type);
124 #endif
125 void memp_free(memp_t type, void *mem);
126 
127 #endif /* MEMP_MEM_MALLOC */
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* LWIP_HDR_MEMP_H */
134 
Definition: memp.h:48
const struct memp_desc * memp_pools[MEMP_MAX]
Definition: memp.c:75
memp_t
Definition: memp.h:45
void * memp_malloc(memp_t type)
Definition: memp.c:303
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
void memp_init(void)
Definition: memp.c:232
Definition: mem.c:179
unsigned short u16_t
Definition: cc.h:40