iobuf.h
Go to the documentation of this file.00001 #ifndef _GPXE_IOBUF_H
00002 #define _GPXE_IOBUF_H
00003
00004
00005
00006
00007
00008
00009
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011
00012 #include <stdint.h>
00013 #include <assert.h>
00014 #include <gpxe/list.h>
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define IOB_ALIGN 2048
00026
00027
00028
00029
00030
00031
00032
00033
00034 #define IOB_ZLEN 64
00035
00036
00037
00038
00039
00040
00041
00042
00043 struct io_buffer {
00044
00045
00046
00047
00048
00049
00050 struct list_head list;
00051
00052
00053 void *head;
00054
00055 void *data;
00056
00057 void *tail;
00058
00059 void *end;
00060 };
00061
00062
00063
00064
00065
00066
00067
00068
00069 static inline void * iob_reserve ( struct io_buffer *iobuf, size_t len ) {
00070 iobuf->data += len;
00071 iobuf->tail += len;
00072 return iobuf->data;
00073 }
00074 #define iob_reserve( iobuf, len ) ( { \
00075 void *__result; \
00076 __result = iob_reserve ( (iobuf), (len) ); \
00077 assert ( (iobuf)->tail <= (iobuf)->end ); \
00078 __result; } )
00079
00080
00081
00082
00083
00084
00085
00086
00087 static inline void * iob_push ( struct io_buffer *iobuf, size_t len ) {
00088 iobuf->data -= len;
00089 return iobuf->data;
00090 }
00091 #define iob_push( iobuf, len ) ( { \
00092 void *__result; \
00093 __result = iob_push ( (iobuf), (len) ); \
00094 assert ( (iobuf)->data >= (iobuf)->head ); \
00095 __result; } )
00096
00097
00098
00099
00100
00101
00102
00103
00104 static inline void * iob_pull ( struct io_buffer *iobuf, size_t len ) {
00105 iobuf->data += len;
00106 assert ( iobuf->data <= iobuf->tail );
00107 return iobuf->data;
00108 }
00109 #define iob_pull( iobuf, len ) ( { \
00110 void *__result; \
00111 __result = iob_pull ( (iobuf), (len) ); \
00112 assert ( (iobuf)->data <= (iobuf)->tail ); \
00113 __result; } )
00114
00115
00116
00117
00118
00119
00120
00121
00122 static inline void * iob_put ( struct io_buffer *iobuf, size_t len ) {
00123 void *old_tail = iobuf->tail;
00124 iobuf->tail += len;
00125 return old_tail;
00126 }
00127 #define iob_put( iobuf, len ) ( { \
00128 void *__result; \
00129 __result = iob_put ( (iobuf), (len) ); \
00130 assert ( (iobuf)->tail <= (iobuf)->end ); \
00131 __result; } )
00132
00133
00134
00135
00136
00137
00138
00139 static inline void iob_unput ( struct io_buffer *iobuf, size_t len ) {
00140 iobuf->tail -= len;
00141 }
00142 #define iob_unput( iobuf, len ) do { \
00143 iob_unput ( (iobuf), (len) ); \
00144 assert ( (iobuf)->tail >= (iobuf)->data ); \
00145 } while ( 0 )
00146
00147
00148
00149
00150
00151
00152 static inline void iob_empty ( struct io_buffer *iobuf ) {
00153 iobuf->tail = iobuf->data;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 static inline size_t iob_len ( struct io_buffer *iobuf ) {
00163 return ( iobuf->tail - iobuf->data );
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 static inline size_t iob_headroom ( struct io_buffer *iobuf ) {
00173 return ( iobuf->data - iobuf->head );
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 static inline size_t iob_tailroom ( struct io_buffer *iobuf ) {
00183 return ( iobuf->end - iobuf->tail );
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 static inline void iob_populate ( struct io_buffer *iobuf,
00198 void *data, size_t len, size_t max_len ) {
00199 iobuf->head = iobuf->data = data;
00200 iobuf->tail = ( data + len );
00201 iobuf->end = ( data + max_len );
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 #define iob_disown( iobuf ) ( { \
00220 struct io_buffer *__iobuf = (iobuf); \
00221 (iobuf) = NULL; \
00222 __iobuf; } )
00223
00224 extern struct io_buffer * __malloc alloc_iob ( size_t len );
00225 extern void free_iob ( struct io_buffer *iobuf );
00226 extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
00227 extern int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len );
00228
00229 #endif