mem.h

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - nano IP stack
00004 
00005 File        : mem.h
00006 
00007 Description : Memory Management
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2007 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 
00029 ##############################################################################*/
00030 
00031 #ifndef _NIP_MEM_H
00032  #define _NIP_MEM_H
00033 
00034 #include "nip_init.h"
00035 #include "nip_error.h"
00036 
00037 /** Maximum number of memory blocks. Raising the number above 255 may lead to
00038   * an increase of used program memory, due to larger identifiers. Look at the
00039   * definition of nip_mem_handle_t for further explanation.
00040   */
00041 #ifndef NIP_MEM_MAX_BLOCKS
00042         #define NIP_MEM_MAX_BLOCKS 255
00043 #endif
00044 
00045 /** Number of bytes reserved for dynamic buffer management. The default value
00046  * is 600 Bytes which is assumed to be needed for the reception of datagrams
00047  * with the standard maximum IP size of 576 Bytes.
00048  * \note Not all of the reservied memory will actually be available for
00049  * allocation, as some of it will be spend on management information.
00050  */
00051 #ifndef NIP_MEM_DYNAMIC_SIZE
00052         #define NIP_MEM_DYNAMIC_SIZE 600
00053 #endif
00054 
00055 
00056 #ifndef NIP_MEM_DEBUG
00057         #define NIP_MEM_DEBUG   0
00058 #endif
00059 
00060 
00061 // Define type for memory size
00062 #if ( NIP_MEM_DYNAMIC_SIZE <= 256 )
00063         typedef uint8_t nip_mem_size_t;
00064         typedef uint8_t nip_mem_cnt_t;
00065 #elif ( NIP_MEM_DYNAMIC_SIZE <= 65563 )
00066         typedef uint16_t nip_mem_size_t;
00067         typedef uint16_t nip_mem_cnt_t;
00068 #else
00069         typedef uint32_t nip_mem_size_t;
00070         typedef uint32_t nip_mem_cnt_t;
00071 #endif
00072 
00073 
00074 /** States of memory allocation function */
00075 typedef enum
00076 {
00077         NIP_MEM_ALLOC_DEFRAG,
00078         NIP_MEM_ALLOC_SIZE,
00079         NIP_MEM_ALLOC_MIN_SIZE,
00080         NIP_MEM_ALLOC_BLOCK_SIZE,
00081         NIP_MEM_ALLOC_FAIL,
00082 }
00083 #ifndef DOXYGEN
00084 __attribute__((packed))
00085 #endif
00086 nip_mem_alloc_stat_t;
00087 
00088 
00089 
00090 /** Handle for block of dynamically managed memory. */
00091 #if ( NIP_MEM_MAX_BLOCKS < 256 )
00092         typedef uint8_t nip_mem_handle_t;
00093 #elif ( NIP_MEM_MAX_BLOCKS < 65536 )
00094         typedef uint16_t nip_mem_handle_t;
00095 #else
00096         typedef uint32_t nip_mem_handle_t;
00097 #endif
00098 
00099 /** Type definition for memory block flags */
00100 typedef uint8_t nip_mem_flags_t;
00101 
00102 #define NIP_MEM_NULL        0x00  /**< handle for undefined memory block */
00103 
00104 /** \name Flags for block of dynamically managed memory. */
00105 //@{
00106 #define NIP_MEM_FLG_USED    0x01  /**< Block in use */
00107 #define NIP_MEM_FLG_LOCKED  0x02  /**< Block locked and may not be moved/defragmented at this time. */
00108 #define NIP_MEM_FLG_DELREAD 0x04  /**< Data will be deleted automatically when read from block. */
00109 
00110 #define NIP_MEM_FLGS_UNUSED 0x00  /**< Flag configuration for unused block */
00111 //@}
00112 
00113 /** Header for block of dynamically managed memory */
00114 typedef struct
00115 {
00116         nip_mem_handle_t id;         //!< Unique identifier. 1 <= id <= NIP_MEM_MAX_BLOCKS
00117         nip_mem_size_t   res_length; //!< reserved length. Size of reserved memory for this block. (incl. this header)
00118         nip_mem_size_t   min_length; //!< minimum length. Amount of memory that is guaranteed to be reserved for this block's data.
00119         nip_mem_size_t   used_length;//!< used length. Amount of memory currently used for data.
00120         nip_mem_flags_t  flags;      //!< flags to control access to the block \todo use bit masks (see below) to save program memory
00121 /*      struct
00122         {
00123                 uint8_t used:1;
00124                 uint8_t locked:1;
00125                 uint8_t delread:1;
00126                 uint8_t unused_flag:1;
00127                 uint8_t unused_flag:1;
00128                 uint8_t unused_flag:1;
00129                 uint8_t unused_flag:1;
00130                 uint8_t unused_flag:1;
00131         }flags;      //!< flags to control access to the block
00132 */
00133 
00134 
00135 }
00136 #ifndef DOXYGEN
00137 __attribute__((packed))
00138 #endif
00139 nip_mem_block_t;
00140 
00141 /** Structure to handle pointers to managed or unmanaged memory. If the id
00142  * attribute of the structure is NIP_MEM_NULL, then ptr holds an actual pointer,
00143  * for direct usage. If id is not NIP_MEM_NULL, ptr defines and offset from the
00144  * beginning of the data part of the managed memory block specified by id.
00145  */
00146 typedef struct
00147 {
00148         nip_mem_handle_t id;   /**< ID of memory block or NIP_MEM_NULL */
00149         uint8_t          *ptr; /**< offset in block memory, or pointer to data if id
00150                                 * is NIP_MEM_NULL */
00151 }
00152 #ifndef DOXYGEN
00153 __attribute__((packed))
00154 #endif
00155 nip_mem_ptr_t;
00156 
00157 #define NIP_MEM_BLOCK_USED( b )   (b->flags & NIP_MEM_FLG_USED)
00158 #define NIP_MEM_BLOCK_LOCKED( b ) (b->flags & NIP_MEM_FLG_LOCKED)
00159 
00160 void nip_mem_init();
00161 nip_mem_handle_t nip_mem_alloc( nip_mem_size_t size, nip_mem_size_t min_length, nip_mem_flags_t flags, nip_mem_size_t *res_length);
00162 void nip_mem_free( nip_mem_handle_t block_id );
00163 nip_error_t nip_mem_set_min_length( nip_mem_handle_t block_id, nip_mem_size_t min_length );
00164 nip_error_t nip_mem_write( nip_mem_handle_t block_id, void *buf, nip_mem_size_t size );
00165 nip_mem_size_t nip_mem_read_at_pos( nip_mem_handle_t block_id, uint8_t *buf,
00166                                     nip_mem_size_t size, nip_mem_size_t pos);
00167 nip_mem_size_t nip_mem_read( nip_mem_handle_t block_id, uint8_t *buf, nip_mem_size_t size );
00168 void *nip_mem_obtain_ptr( nip_mem_handle_t block_id );
00169 void nip_mem_release_block( nip_mem_handle_t block_id );
00170 void *nip_mem_ptr( nip_mem_ptr_t *p );
00171 void nip_mem_release_ptr( nip_mem_ptr_t *p );
00172 nip_mem_size_t nip_mem_buf_used( nip_mem_handle_t block_id );
00173 void nip_mem_set_used( nip_mem_handle_t block_id, nip_mem_size_t new_size );
00174 nip_mem_size_t nip_mem_move( nip_mem_handle_t dst_block, nip_mem_handle_t src_block, nip_mem_size_t size );
00175 nip_error_t nip_mem_insert( nip_mem_ptr_t *dst, nip_mem_ptr_t *src, nip_mem_size_t len );
00176 nip_error_t nip_mem_cut( nip_mem_ptr_t *location, nip_mem_size_t len );
00177 #endif /* _NIP_MEM_H */

Generated on Thu Jul 10 01:09:29 2008 for NIP by  doxygen 1.5.5