7 // --- Ring Buffers ---
8 tRingBuffer *RingBuffer_Create(size_t Space)
10 tRingBuffer *ret = malloc(sizeof(tRingBuffer)+Space);
17 size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
19 if(Length > Buffer->Length) Length = Buffer->Length;
21 if( Buffer->Start + Length > Buffer->Space )
23 int endData = Buffer->Space - Buffer->Start;
24 memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
25 memcpy(Dest + endData, &Buffer->Data, Length - endData);
29 memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
31 Buffer->Length -= Length;
35 size_t RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length)
37 size_t bufEnd = Buffer->Start + Buffer->Length;
38 size_t endSpace = Buffer->Space - bufEnd;
41 if(Length > Buffer->Space - Buffer->Length)
42 Length = Buffer->Space - Buffer->Length;
46 memcpy( &Buffer->Data[bufEnd], Source, endSpace );
47 memcpy( Buffer->Data, Source + endSpace, Length - endSpace );
48 Buffer->Length = Length - endSpace;
52 memcpy( &Buffer->Data[bufEnd], Source, Length );
53 Buffer->Length += Length;