Committer:
mbed714
Date:
Sat Sep 18 23:05:49 2010 +0000
Revision:
0:d616ece2d859

        

Who changed what in which revision?

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