Spell check all source code variables, comments and strings.
[pub/USBasp.git] / Projects / Benito / Lib / RingBuff.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "RingBuff.h"
32
33 void Buffer_Initialize(RingBuff_t* Buffer)
34 {
35 BUFF_ATOMIC_BLOCK
36 {
37 Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
38 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
39 Buffer->Elements = 0;
40 }
41 }
42
43 void Buffer_StoreElement(RingBuff_t* Buffer, RingBuff_Data_t Data)
44 {
45 BUFF_ATOMIC_BLOCK
46 {
47 #if defined(BUFF_DROPOLD)
48 if (Buffer->Elements == BUFF_LENGTH)
49 {
50 Buffer->OutPtr++;
51
52 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
53 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
54 }
55 else
56 {
57 Buffer->Elements++;
58 }
59 #elif defined(BUFF_DROPNEW)
60 if (Buffer->Elements == BUFF_LENGTH)
61 return;
62
63 Buffer->Elements++;
64 #elif defined(BUFF_NODROPCHECK)
65 Buffer->Elements++;
66 #endif
67
68 *(Buffer->InPtr) = Data;
69 Buffer->InPtr++;
70
71 if (Buffer->InPtr == &Buffer->Buffer[BUFF_LENGTH])
72 Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
73 }
74 }
75
76 RingBuff_Data_t Buffer_GetElement(RingBuff_t* Buffer)
77 {
78 RingBuff_Data_t BuffData;
79
80 BUFF_ATOMIC_BLOCK
81 {
82 #if defined(BUFF_EMPTYRETURNSZERO)
83 if (!(Buffer->Elements))
84 return 0;
85 #elif !defined(BUFF_NOEMPTYCHECK)
86 #error No empty buffer check behavior specified.
87 #endif
88
89 BuffData = *(Buffer->OutPtr);
90
91 Buffer->OutPtr++;
92 Buffer->Elements--;
93
94 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
95 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
96 }
97
98 return BuffData;
99 }
100
101 #if defined(BUFF_USEPEEK)
102 RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* Buffer)
103 {
104 RingBuff_Data_t BuffData;
105
106 BUFF_ATOMIC_BLOCK
107 {
108 #if defined(BUFF_EMPTYRETURNSZERO)
109 if (!(Buffer->Elements))
110 return 0;
111 #elif !defined(BUFF_NOEMPTYCHECK)
112 #error No empty buffer check behavior specified.
113 #endif
114
115 BuffData = *(Buffer->OutPtr);
116 }
117
118 return BuffData;
119 }
120 #endif