5 * - Complex data type code
12 // --- Ring Buffers ---
13 tRingBuffer *RingBuffer_Create(size_t Space)
15 tRingBuffer *ret = malloc(sizeof(tRingBuffer)+Space);
22 size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
26 tmpLen = Buffer->Length; // Changed in Write, so cache it for our read
28 if(Length > tmpLen) Length = tmpLen;
30 if( Buffer->Start + Length > Buffer->Space )
32 int endData = Buffer->Space - Buffer->Start;
33 memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
34 memcpy((Uint8*)Dest + endData, Buffer->Data, Length - endData);
38 memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
42 SHORTLOCK( &Buffer->Lock );
43 Buffer->Start += Length;
44 if( Buffer->Start > Buffer->Space )
45 Buffer->Start -= Buffer->Space;
46 Buffer->Length -= Length;
47 SHORTREL( &Buffer->Lock );
52 size_t RingBuffer_Write(tRingBuffer *Buffer, const void *Source, size_t Length)
54 size_t bufEnd, endSpace;
55 size_t tmpLen, tmpStart;
57 // Cache Start and Length because _Read can change these
58 SHORTLOCK( &Buffer->Lock );
59 tmpStart = Buffer->Start;
60 tmpLen = Buffer->Length;
61 SHORTREL( &Buffer->Lock );
63 bufEnd = (tmpStart + Buffer->Length) % Buffer->Space;
64 endSpace = Buffer->Space - bufEnd;
67 if(Length > Buffer->Space - tmpLen) Length = Buffer->Space - tmpLen;
71 memcpy( &Buffer->Data[bufEnd], Source, endSpace );
72 memcpy( Buffer->Data, (Uint8*)Source + endSpace, Length - endSpace );
76 memcpy( &Buffer->Data[bufEnd], Source, Length );
80 SHORTLOCK( &Buffer->Lock );
81 Buffer->Length += Length;
82 SHORTREL( &Buffer->Lock );