5 * - Complex data type code
12 // --- Ring Buffers ---
13 tRingBuffer *RingBuffer_Create(size_t Space)
15 tRingBuffer *ret = calloc(1, sizeof(tRingBuffer)+Space);
20 size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
24 tmpLen = Buffer->Length; // Changed in Write, so cache it for our read
26 if(Length > tmpLen) Length = tmpLen;
28 if( Buffer->Start + Length > Buffer->Space )
30 int endData = Buffer->Space - Buffer->Start;
31 memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
32 memcpy((Uint8*)Dest + endData, Buffer->Data, Length - endData);
36 memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
40 SHORTLOCK( &Buffer->Lock );
41 Buffer->Start += Length;
42 if( Buffer->Start > Buffer->Space )
43 Buffer->Start -= Buffer->Space;
44 Buffer->Length -= Length;
45 SHORTREL( &Buffer->Lock );
50 size_t RingBuffer_Write(tRingBuffer *Buffer, const void *Source, size_t Length)
52 size_t bufEnd, endSpace;
53 size_t tmpLen, tmpStart;
55 // Cache Start and Length because _Read can change these
56 SHORTLOCK( &Buffer->Lock );
57 tmpStart = Buffer->Start;
58 tmpLen = Buffer->Length;
59 SHORTREL( &Buffer->Lock );
61 bufEnd = (tmpStart + Buffer->Length) % Buffer->Space;
62 endSpace = Buffer->Space - bufEnd;
65 if(Length > Buffer->Space - tmpLen) Length = Buffer->Space - tmpLen;
69 memcpy( &Buffer->Data[bufEnd], Source, endSpace );
70 memcpy( Buffer->Data, (Uint8*)Source + endSpace, Length - endSpace );
74 memcpy( &Buffer->Data[bufEnd], Source, Length );
78 SHORTLOCK( &Buffer->Lock );
79 Buffer->Length += Length;
80 SHORTREL( &Buffer->Lock );