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