Forked from the DieterGraef Library

Fork of F7_Ethernet by Dieter Graef

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mem.h Source File

mem.h

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  * 
00005  * Redistribution and use in source and binary forms, with or without modification, 
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission. 
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  * 
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 #ifndef __LWIP_MEM_H__
00033 #define __LWIP_MEM_H__
00034 
00035 #include "lwip/opt.h"
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 #if MEM_LIBC_MALLOC
00042 
00043 #include <stddef.h> /* for size_t */
00044 
00045 typedef size_t mem_size_t;
00046 #define MEM_SIZE_F SZT_F
00047 
00048 /* aliases for C library malloc() */
00049 #define mem_init()
00050 /* in case C library malloc() needs extra protection,
00051  * allow these defines to be overridden.
00052  */
00053 #ifndef mem_free
00054 #define mem_free free
00055 #endif
00056 #ifndef mem_malloc
00057 #define mem_malloc malloc
00058 #endif
00059 #ifndef mem_calloc
00060 #define mem_calloc calloc
00061 #endif
00062 /* Since there is no C library allocation function to shrink memory without
00063    moving it, define this to nothing. */
00064 #ifndef mem_trim
00065 #define mem_trim(mem, size) (mem)
00066 #endif
00067 #else /* MEM_LIBC_MALLOC */
00068 
00069 /* MEM_SIZE would have to be aligned, but using 64000 here instead of
00070  * 65535 leaves some room for alignment...
00071  */
00072 #if MEM_SIZE > 64000L
00073 typedef u32_t mem_size_t;
00074 #define MEM_SIZE_F U32_F
00075 #else
00076 typedef u16_t mem_size_t;
00077 #define MEM_SIZE_F U16_F
00078 #endif /* MEM_SIZE > 64000 */
00079 
00080 #if MEM_USE_POOLS
00081 /** mem_init is not used when using pools instead of a heap */
00082 #define mem_init()
00083 /** mem_trim is not used when using pools instead of a heap:
00084     we can't free part of a pool element and don't want to copy the rest */
00085 #define mem_trim(mem, size) (mem)
00086 #else /* MEM_USE_POOLS */
00087 /* lwIP alternative malloc */
00088 void  mem_init(void);
00089 void *mem_trim(void *mem, mem_size_t size);
00090 #endif /* MEM_USE_POOLS */
00091 void *mem_malloc(mem_size_t size);
00092 void *mem_calloc(mem_size_t count, mem_size_t size);
00093 void  mem_free(void *mem);
00094 #endif /* MEM_LIBC_MALLOC */
00095 
00096 /** Calculate memory size for an aligned buffer - returns the next highest
00097  * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
00098  * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
00099  */
00100 #ifndef LWIP_MEM_ALIGN_SIZE
00101 #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1) & ~(MEM_ALIGNMENT-1))
00102 #endif
00103 
00104 /** Calculate safe memory size for an aligned buffer when using an unaligned
00105  * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
00106  * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
00107  */
00108 #ifndef LWIP_MEM_ALIGN_BUFFER
00109 #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1))
00110 #endif
00111 
00112 /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
00113  * so that ADDR % MEM_ALIGNMENT == 0
00114  */
00115 #ifndef LWIP_MEM_ALIGN
00116 #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
00117 #endif
00118 
00119 #ifdef __cplusplus
00120 }
00121 #endif
00122 
00123 #endif /* __LWIP_MEM_H__ */