Dependents: TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more
lwip/include/lwip/mem.h@0:ac1725ba162c, 2010-11-09 (annotated)
- Committer:
- segundo
- Date:
- Tue Nov 09 20:54:15 2010 +0000
- Revision:
- 0:ac1725ba162c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | /* |
segundo | 0:ac1725ba162c | 2 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
segundo | 0:ac1725ba162c | 3 | * All rights reserved. |
segundo | 0:ac1725ba162c | 4 | * |
segundo | 0:ac1725ba162c | 5 | * Redistribution and use in source and binary forms, with or without modification, |
segundo | 0:ac1725ba162c | 6 | * are permitted provided that the following conditions are met: |
segundo | 0:ac1725ba162c | 7 | * |
segundo | 0:ac1725ba162c | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
segundo | 0:ac1725ba162c | 9 | * this list of conditions and the following disclaimer. |
segundo | 0:ac1725ba162c | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
segundo | 0:ac1725ba162c | 11 | * this list of conditions and the following disclaimer in the documentation |
segundo | 0:ac1725ba162c | 12 | * and/or other materials provided with the distribution. |
segundo | 0:ac1725ba162c | 13 | * 3. The name of the author may not be used to endorse or promote products |
segundo | 0:ac1725ba162c | 14 | * derived from this software without specific prior written permission. |
segundo | 0:ac1725ba162c | 15 | * |
segundo | 0:ac1725ba162c | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
segundo | 0:ac1725ba162c | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
segundo | 0:ac1725ba162c | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
segundo | 0:ac1725ba162c | 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
segundo | 0:ac1725ba162c | 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
segundo | 0:ac1725ba162c | 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
segundo | 0:ac1725ba162c | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
segundo | 0:ac1725ba162c | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
segundo | 0:ac1725ba162c | 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
segundo | 0:ac1725ba162c | 25 | * OF SUCH DAMAGE. |
segundo | 0:ac1725ba162c | 26 | * |
segundo | 0:ac1725ba162c | 27 | * This file is part of the lwIP TCP/IP stack. |
segundo | 0:ac1725ba162c | 28 | * |
segundo | 0:ac1725ba162c | 29 | * Author: Adam Dunkels <adam@sics.se> |
segundo | 0:ac1725ba162c | 30 | * |
segundo | 0:ac1725ba162c | 31 | */ |
segundo | 0:ac1725ba162c | 32 | #ifndef __LWIP_MEM_H__ |
segundo | 0:ac1725ba162c | 33 | #define __LWIP_MEM_H__ |
segundo | 0:ac1725ba162c | 34 | |
segundo | 0:ac1725ba162c | 35 | #include "lwip/opt.h" |
segundo | 0:ac1725ba162c | 36 | |
segundo | 0:ac1725ba162c | 37 | #ifdef __cplusplus |
segundo | 0:ac1725ba162c | 38 | extern "C" { |
segundo | 0:ac1725ba162c | 39 | #endif |
segundo | 0:ac1725ba162c | 40 | |
segundo | 0:ac1725ba162c | 41 | #if MEM_LIBC_MALLOC |
segundo | 0:ac1725ba162c | 42 | |
segundo | 0:ac1725ba162c | 43 | #include <stddef.h> /* for size_t */ |
segundo | 0:ac1725ba162c | 44 | |
segundo | 0:ac1725ba162c | 45 | typedef size_t mem_size_t; |
segundo | 0:ac1725ba162c | 46 | |
segundo | 0:ac1725ba162c | 47 | /* aliases for C library malloc() */ |
segundo | 0:ac1725ba162c | 48 | #define mem_init() |
segundo | 0:ac1725ba162c | 49 | /* in case C library malloc() needs extra protection, |
segundo | 0:ac1725ba162c | 50 | * allow these defines to be overridden. |
segundo | 0:ac1725ba162c | 51 | */ |
segundo | 0:ac1725ba162c | 52 | #ifndef mem_free |
segundo | 0:ac1725ba162c | 53 | #define mem_free free |
segundo | 0:ac1725ba162c | 54 | #endif |
segundo | 0:ac1725ba162c | 55 | #ifndef mem_malloc |
segundo | 0:ac1725ba162c | 56 | #define mem_malloc malloc |
segundo | 0:ac1725ba162c | 57 | #endif |
segundo | 0:ac1725ba162c | 58 | #ifndef mem_calloc |
segundo | 0:ac1725ba162c | 59 | #define mem_calloc calloc |
segundo | 0:ac1725ba162c | 60 | #endif |
segundo | 0:ac1725ba162c | 61 | /* Since there is no C library allocation function to shrink memory without |
segundo | 0:ac1725ba162c | 62 | moving it, define this to nothing. */ |
segundo | 0:ac1725ba162c | 63 | #ifndef mem_trim |
segundo | 0:ac1725ba162c | 64 | #define mem_trim(mem, size) (mem) |
segundo | 0:ac1725ba162c | 65 | #endif |
segundo | 0:ac1725ba162c | 66 | #else /* MEM_LIBC_MALLOC */ |
segundo | 0:ac1725ba162c | 67 | |
segundo | 0:ac1725ba162c | 68 | /* MEM_SIZE would have to be aligned, but using 64000 here instead of |
segundo | 0:ac1725ba162c | 69 | * 65535 leaves some room for alignment... |
segundo | 0:ac1725ba162c | 70 | */ |
segundo | 0:ac1725ba162c | 71 | #if MEM_SIZE > 64000l |
segundo | 0:ac1725ba162c | 72 | typedef u32_t mem_size_t; |
segundo | 0:ac1725ba162c | 73 | #define MEM_SIZE_F U32_F |
segundo | 0:ac1725ba162c | 74 | #else |
segundo | 0:ac1725ba162c | 75 | typedef u16_t mem_size_t; |
segundo | 0:ac1725ba162c | 76 | #define MEM_SIZE_F U16_F |
segundo | 0:ac1725ba162c | 77 | #endif /* MEM_SIZE > 64000 */ |
segundo | 0:ac1725ba162c | 78 | |
segundo | 0:ac1725ba162c | 79 | #if MEM_USE_POOLS |
segundo | 0:ac1725ba162c | 80 | /** mem_init is not used when using pools instead of a heap */ |
segundo | 0:ac1725ba162c | 81 | #define mem_init() |
segundo | 0:ac1725ba162c | 82 | /** mem_trim is not used when using pools instead of a heap: |
segundo | 0:ac1725ba162c | 83 | we can't free part of a pool element and don't want to copy the rest */ |
segundo | 0:ac1725ba162c | 84 | #define mem_trim(mem, size) (mem) |
segundo | 0:ac1725ba162c | 85 | #else /* MEM_USE_POOLS */ |
segundo | 0:ac1725ba162c | 86 | /* lwIP alternative malloc */ |
segundo | 0:ac1725ba162c | 87 | void mem_init(void); |
segundo | 0:ac1725ba162c | 88 | void *mem_trim(void *mem, mem_size_t size); |
segundo | 0:ac1725ba162c | 89 | #endif /* MEM_USE_POOLS */ |
segundo | 0:ac1725ba162c | 90 | void *mem_malloc(mem_size_t size); |
segundo | 0:ac1725ba162c | 91 | void *mem_calloc(mem_size_t count, mem_size_t size); |
segundo | 0:ac1725ba162c | 92 | void mem_free(void *mem); |
segundo | 0:ac1725ba162c | 93 | #endif /* MEM_LIBC_MALLOC */ |
segundo | 0:ac1725ba162c | 94 | |
segundo | 0:ac1725ba162c | 95 | /** Calculate memory size for an aligned buffer - returns the next highest |
segundo | 0:ac1725ba162c | 96 | * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and |
segundo | 0:ac1725ba162c | 97 | * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4). |
segundo | 0:ac1725ba162c | 98 | */ |
segundo | 0:ac1725ba162c | 99 | #ifndef LWIP_MEM_ALIGN_SIZE |
segundo | 0:ac1725ba162c | 100 | #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1) & ~(MEM_ALIGNMENT-1)) |
segundo | 0:ac1725ba162c | 101 | #endif |
segundo | 0:ac1725ba162c | 102 | |
segundo | 0:ac1725ba162c | 103 | /** Calculate safe memory size for an aligned buffer when using an unaligned |
segundo | 0:ac1725ba162c | 104 | * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the |
segundo | 0:ac1725ba162c | 105 | * start (e.g. if buffer is u8_t[] and actual data will be u32_t*) |
segundo | 0:ac1725ba162c | 106 | */ |
segundo | 0:ac1725ba162c | 107 | #ifndef LWIP_MEM_ALIGN_BUFFER |
segundo | 0:ac1725ba162c | 108 | #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1)) |
segundo | 0:ac1725ba162c | 109 | #endif |
segundo | 0:ac1725ba162c | 110 | |
segundo | 0:ac1725ba162c | 111 | /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT |
segundo | 0:ac1725ba162c | 112 | * so that ADDR % MEM_ALIGNMENT == 0 |
segundo | 0:ac1725ba162c | 113 | */ |
segundo | 0:ac1725ba162c | 114 | #ifndef LWIP_MEM_ALIGN |
segundo | 0:ac1725ba162c | 115 | #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1))) |
segundo | 0:ac1725ba162c | 116 | #endif |
segundo | 0:ac1725ba162c | 117 | |
segundo | 0:ac1725ba162c | 118 | #ifdef __cplusplus |
segundo | 0:ac1725ba162c | 119 | } |
segundo | 0:ac1725ba162c | 120 | #endif |
segundo | 0:ac1725ba162c | 121 | |
segundo | 0:ac1725ba162c | 122 | #endif /* __LWIP_MEM_H__ */ |