00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00038
00039
00040
00041 #ifndef NIP_MEM_MAX_BLOCKS
00042 #define NIP_MEM_MAX_BLOCKS 255
00043 #endif
00044
00045
00046
00047
00048
00049
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
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
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
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
00100 typedef uint8_t nip_mem_flags_t;
00101
00102 #define NIP_MEM_NULL 0x00
00103
00104
00105
00106 #define NIP_MEM_FLG_USED 0x01
00107 #define NIP_MEM_FLG_LOCKED 0x02
00108 #define NIP_MEM_FLG_DELREAD 0x04
00109
00110 #define NIP_MEM_FLGS_UNUSED 0x00
00111
00112
00113
00114 typedef struct
00115 {
00116 nip_mem_handle_t id;
00117 nip_mem_size_t res_length;
00118 nip_mem_size_t min_length;
00119 nip_mem_size_t used_length;
00120 nip_mem_flags_t flags;
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 }
00136 #ifndef DOXYGEN
00137 __attribute__((packed))
00138 #endif
00139 nip_mem_block_t;
00140
00141
00142
00143
00144
00145
00146 typedef struct
00147 {
00148 nip_mem_handle_t id;
00149 uint8_t *ptr;
00150
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