It's been too long since I committed
[tpg/acess2.git] / Kernel / adt.c
1 /*
2  */
3 #include <acess.h>
4 #include <adt.h>
5
6 // === CODE ===
7 // --- Ring Buffers ---
8 tRingBuffer *RingBuffer_Create(size_t Space)
9 {
10         tRingBuffer     *ret = malloc(sizeof(tRingBuffer)+Space);
11         ret->Start = 0;
12         ret->Length = 0;
13         ret->Space = Space;
14         return ret;
15 }
16
17 size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
18 {
19         if(Length > Buffer->Length)     Length = Buffer->Length;
20         
21         if( Buffer->Start + Length > Buffer->Space )
22         {
23                  int    endData = Buffer->Space - Buffer->Start;
24                 memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
25                 memcpy(Dest + endData, &Buffer->Data, Length - endData);
26         }
27         else
28         {
29                 memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
30         }
31         Buffer->Length -= Length;
32         return Length;
33 }
34
35 size_t RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length)
36 {
37         size_t  bufEnd = Buffer->Start + Buffer->Length;
38         size_t  endSpace = Buffer->Space - bufEnd;
39         
40         // Force to bounds
41         if(Length > Buffer->Space - Buffer->Length)
42                 Length = Buffer->Space - Buffer->Length;
43         
44         if(endSpace < Length)
45         {
46                 memcpy( &Buffer->Data[bufEnd], Source, endSpace );
47                 memcpy( Buffer->Data, Source + endSpace, Length - endSpace );
48                 Buffer->Length = Length - endSpace;
49         }
50         else
51         {
52                 memcpy( &Buffer->Data[bufEnd], Source, Length );
53                 Buffer->Length += Length;
54         }
55         
56         return Length;
57 }

UCC git Repository :: git.ucc.asn.au