comparison network/Lib/RingBuff.c @ 110:4eb5a746d7af avr-http

Import avrusbmodem code minus the USB bits. Not built yet.
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Sep 2012 21:49:05 +0800
parents
children
comparison
equal deleted inserted replaced
109:315850d48eec 110:4eb5a746d7af
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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* const 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* const Buffer,
44 RingBuff_Data_t Data)
45 {
46 BUFF_ATOMIC_BLOCK
47 {
48 #if defined(BUFF_DROPOLD)
49 if (Buffer->Elements == BUFF_LENGTH)
50 {
51 Buffer->OutPtr++;
52
53 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
54 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
55 }
56 else
57 {
58 Buffer->Elements++;
59 }
60 #elif defined(BUFF_DROPNEW)
61 if (Buffer->Elements == BUFF_LENGTH)
62 return;
63
64 Buffer->Elements++;
65 #elif defined(BUFF_NODROPCHECK)
66 Buffer->Elements++;
67 #endif
68
69 *(Buffer->InPtr) = Data;
70 Buffer->InPtr++;
71
72 if (Buffer->InPtr == &Buffer->Buffer[BUFF_LENGTH])
73 Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
74 }
75 }
76
77 RingBuff_Data_t Buffer_GetElement(RingBuff_t* const Buffer)
78 {
79 RingBuff_Data_t BuffData;
80
81 BUFF_ATOMIC_BLOCK
82 {
83 #if defined(BUFF_EMPTYRETURNSZERO)
84 if (!(Buffer->Elements))
85 return 0;
86 #elif !defined(BUFF_NOEMPTYCHECK)
87 #error No empty buffer check behavior specified.
88 #endif
89
90 BuffData = *(Buffer->OutPtr);
91
92 Buffer->OutPtr++;
93 Buffer->Elements--;
94
95 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
96 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
97 }
98
99 return BuffData;
100 }
101
102 #if defined(BUFF_USEPEEK)
103 RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* const Buffer)
104 {
105 RingBuff_Data_t BuffData;
106
107 BUFF_ATOMIC_BLOCK
108 {
109 #if defined(BUFF_EMPTYRETURNSZERO)
110 if (!(Buffer->Elements))
111 return 0;
112 #elif !defined(BUFF_NOEMPTYCHECK)
113 #error No empty buffer check behavior specified.
114 #endif
115
116 BuffData = *(Buffer->OutPtr);
117 }
118
119 return BuffData;
120 }
121 #endif