5 * - Complex data type code
12 // --- Ring Buffers ---
13 tRingBuffer *RingBuffer_Create(size_t Space)
15 tRingBuffer *ret = calloc(1, sizeof(tRingBuffer)+Space);
20 void RingBuffer_Free(tRingBuffer *Buffer)
25 size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
29 tmpLen = Buffer->Length; // Changed in Write, so cache it for our read
31 if(Length > tmpLen) Length = tmpLen;
33 if( Buffer->Start + Length > Buffer->Space )
35 int endData = Buffer->Space - Buffer->Start;
36 memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
37 memcpy((Uint8*)Dest + endData, Buffer->Data, Length - endData);
41 memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
45 SHORTLOCK( &Buffer->Lock );
46 Buffer->Start += Length;
47 if( Buffer->Start > Buffer->Space )
48 Buffer->Start -= Buffer->Space;
49 Buffer->Length -= Length;
50 SHORTREL( &Buffer->Lock );
55 size_t RingBuffer_Write(tRingBuffer *Buffer, const void *Source, size_t Length)
57 size_t bufEnd, endSpace;
58 size_t tmpLen, tmpStart;
60 // Cache Start and Length because _Read can change these
61 SHORTLOCK( &Buffer->Lock );
62 tmpStart = Buffer->Start;
63 tmpLen = Buffer->Length;
64 SHORTREL( &Buffer->Lock );
66 bufEnd = (tmpStart + Buffer->Length) % Buffer->Space;
67 endSpace = Buffer->Space - bufEnd;
70 if(Length > Buffer->Space - tmpLen) Length = Buffer->Space - tmpLen;
74 memcpy( &Buffer->Data[bufEnd], Source, endSpace );
75 memcpy( Buffer->Data, (Uint8*)Source + endSpace, Length - endSpace );
79 memcpy( &Buffer->Data[bufEnd], Source, Length );
83 SHORTLOCK( &Buffer->Lock );
84 Buffer->Length += Length;
85 SHORTREL( &Buffer->Lock );