2 * Acess2 Networking Stack
3 * - By John Hodge (thePowersGang)
6 * - Scatter-gather handling
9 #include "include/buffer.h"
27 tIPStackBuffer *IPStack_Buffer_CreateBuffer(int MaxBuffers)
31 ret = malloc( sizeof(*ret) + MaxBuffers * sizeof(ret->SubBuffers[0]) );
32 ret->MaxSubBufffers = MaxBuffers;
35 memset(ret->SubBuffers, 0, MaxBuffers * sizeof(ret->SubBuffers[0]));
39 void IPStack_Buffer_DestroyBuffer(tIPStackBuffer *Buffer)
41 // TODO: Fire callbacks?
42 Buffer->MaxSubBufffers = 0;
43 Buffer->nSubBuffers = 0;
47 void IPStack_Buffer_AppendSubBuffer(tIPStackBuffer *Buffer,
48 size_t HeaderLen, size_t FooterLen, const void *Data,
49 tIPStackBufferCb Cb, void *Arg
52 if( Buffer->nSubBuffers == Buffer->MaxSubBufffers ) {
54 Log_Error("IPStack", "Buffer %p only had %i sub-buffers allocated, which was not enough",
55 Buffer, Buffer->MaxSubBufffers);
59 int index = Buffer->nSubBuffers++;
60 Buffer->TotalLength += HeaderLen + FooterLen;
62 Buffer->SubBuffers[index].Data = Data;
63 Buffer->SubBuffers[index].PreLength = HeaderLen;
64 Buffer->SubBuffers[index].PostLength = FooterLen;
67 size_t IPStack_Buffer_GetLength(tIPStackBuffer *Buffer)
69 return Buffer->TotalLength;
72 void *IPStack_Buffer_CompactBuffer(tIPStackBuffer *Buffer, size_t *Length)
75 ret = malloc(Buffer->TotalLength);
81 *Length = Buffer->TotalLength;
84 for( int i = Buffer->nSubBuffers; i --; )
87 Buffer->SubBuffers[i].Data,
88 Buffer->SubBuffers[i].PreLength
90 dest += Buffer->SubBuffers[i].PreLength;
92 for( int i = 0; i < Buffer->nSubBuffers; i ++ )
94 if( Buffer->SubBuffers[i].PostLength )
97 (Uint8*)Buffer->SubBuffers[i].Data + Buffer->SubBuffers[i].PreLength,
98 Buffer->SubBuffers[i].PostLength
100 dest += Buffer->SubBuffers[i].PreLength;
106 int IPStack_Buffer_GetBuffer(tIPStackBuffer *Buffer, int Index, size_t *Length, const void **DataPtr)
108 if( Index == -1 ) Index = 0;
110 if( Index >= Buffer->nSubBuffers*2 ) {
114 if( Index > Buffer->nSubBuffers )
117 Index -= Buffer->nSubBuffers;
119 // Bit of a hack to avoid multiple calls which return a zero length
120 while( !Buffer->SubBuffers[Index].PostLength )
122 if( Index++ == Buffer->nSubBuffers )
126 *DataPtr = (Uint8*)Buffer->SubBuffers[Index].Data + Buffer->SubBuffers[Index].PreLength;
127 *Length = Buffer->SubBuffers[Index].PostLength;
129 return (Index + 1) + Buffer->nSubBuffers;
133 Index = Buffer->nSubBuffers - Index;
135 *DataPtr = Buffer->SubBuffers[Index].Data;
136 *Length = Buffer->SubBuffers[Index].PreLength;
137 return Buffer->nSubBuffers - (Index - 1);