1 /* 2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * 4 * All rights reserved. Distributed under the terms of the MIT License. 5 * 6 */ 7 8 9 #ifndef _SNET_BUFFER_H_ 10 #define _SNET_BUFFER_H_ 11 12 #include <util/list.h> 13 14 /* 15 * This is a simple data structure to hold network buffers. 16 * It drops many functionality that the Haiku net_buffer provides. 17 * 18 * - Inspired by linux sk_buff/bsd mbuf (put/pull) 19 * - Contiguoussafe (no push operation) 20 * 21 * So snet_buffers are ONLY meant to be used when: 22 * 1) You know exactily the maximun/final size of the frame 23 * before allocating it, and you will never exceed it. 24 * 2) You are not supposed to prepend data, only append. 25 * 26 */ 27 28 /* Configuration parameters */ 29 #define SNB_BUFFER_ATTACHED 30 //#define SNB_PERFORMS_OVERFLOW_CHECKS 31 //#define SNB_PERFORMS_POINTER_CHECKS 32 33 struct snet_buffer; 34 35 typedef struct snet_buffer snet_buffer; 36 37 /* Creates a snb_buffer allocating size space for its full content */ 38 snet_buffer* snb_create(uint16 size); 39 /* Free the snb_buffer*/ 40 void snb_free(snet_buffer* snb); 41 /* Free the snb_buffer*/ 42 void* snb_get(snet_buffer* snb); 43 /* Size of the snb_buffer*/ 44 uint16 snb_size(snet_buffer* snb); 45 46 /* Cookie of the snb_buffer*/ 47 void* snb_cookie(snet_buffer* snb); 48 /* Get Cookie of the snb_buffer*/ 49 void snb_set_cookie(snet_buffer* snb, void* cookie); 50 51 /* Place the memory given by data to the "tail" of the snb */ 52 void snb_put(snet_buffer* snb, void* data, uint16 size); 53 /* Returns a header chunk of size data */ 54 void* snb_pull(snet_buffer* snb, uint16 size); 55 /* Discards all data put or pulled from the buffer */ 56 void snb_reset(snet_buffer* snb); 57 58 /* Return true if we canot "put" more data in the buffer */ 59 bool snb_completed(snet_buffer* snb); 60 /* Return true if we cannot pull more more data from the buffer */ 61 bool snb_finished(snet_buffer* snb); 62 /* Return the amount of data we can still put in the buffer */ 63 bool snb_remaining_to_put(snet_buffer* snb); 64 /* Return the amount of data we can still pull in the buffer */ 65 bool snb_remaining_to_pull(snet_buffer* snb); 66 67 /* These to functions are provided to avoid memory fragmentation 68 * allocating and freeing many snb_buffers and its possible overhead. 69 * Thypical scenario would be 70 * that you create a snb_buffer to send data, once you send you free it, 71 * and need another one to hold the response. The idea would be once you send 72 * that buffer, to snb_park the buffer, and whenever you need to allocate another 73 * one snb_fetch it. That funcion will reuse most appropiated previous used one 74 * snb_buff by its memory use. 75 */ 76 void snb_park(struct list* l, snet_buffer* snb); 77 snet_buffer* snb_fetch(struct list* l, uint16 size); 78 79 80 81 #endif 82