1 /*
2 (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
3 (c) Copyright 2000-2004 Convergence (integrated media) GmbH
4
5 All rights reserved.
6
7 Written by Denis Oliver Kropp <dok@directfb.org>,
8 Andreas Hundt <andi@fischlustig.de>,
9 Sven Neumann <neo@directfb.org>,
10 Ville Syrjälä <syrjala@sci.fi> and
11 Claudio Ciccani <klan@users.sf.net>.
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the
25 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.
27 */
28
29 #ifndef __DIRECT__SERIAL_H__
30 #define __DIRECT__SERIAL_H__
31
32 #include <direct/types.h>
33 #include <direct/debug.h>
34
35 struct __D_DirectSerial {
36 int magic;
37
38 u32 value;
39 u32 overflow;
40 };
41
42 static __inline__ void
direct_serial_init(DirectSerial * serial)43 direct_serial_init( DirectSerial *serial )
44 {
45 D_ASSERT( serial != NULL );
46
47 serial->value = 0;
48 serial->overflow = 0;
49
50 D_MAGIC_SET( serial, DirectSerial );
51 }
52
53 static __inline__ void
direct_serial_deinit(DirectSerial * serial)54 direct_serial_deinit( DirectSerial *serial )
55 {
56 D_MAGIC_CLEAR( serial );
57 }
58
59 static __inline__ void
direct_serial_increase(DirectSerial * serial)60 direct_serial_increase( DirectSerial *serial )
61 {
62 D_MAGIC_ASSERT( serial, DirectSerial );
63
64 if (! ++serial->value)
65 serial->overflow++;
66 }
67
68 static __inline__ void
direct_serial_copy(DirectSerial * serial,const DirectSerial * source)69 direct_serial_copy( DirectSerial *serial, const DirectSerial *source )
70 {
71 D_MAGIC_ASSERT( serial, DirectSerial );
72 D_MAGIC_ASSERT( source, DirectSerial );
73
74 serial->value = source->value;
75 serial->overflow = source->overflow;
76 }
77
78 static __inline__ bool
direct_serial_check(DirectSerial * serial,const DirectSerial * source)79 direct_serial_check( DirectSerial *serial, const DirectSerial *source )
80 {
81 D_MAGIC_ASSERT( serial, DirectSerial );
82 D_MAGIC_ASSERT( source, DirectSerial );
83
84 if (serial->overflow < source->overflow)
85 return false;
86 else if (serial->overflow == source->overflow && serial->value < source->value)
87 return false;
88
89 D_ASSUME( serial->value == source->value );
90
91 return true;
92 }
93
94 static __inline__ bool
direct_serial_update(DirectSerial * serial,const DirectSerial * source)95 direct_serial_update( DirectSerial *serial, const DirectSerial *source )
96 {
97 D_MAGIC_ASSERT( serial, DirectSerial );
98 D_MAGIC_ASSERT( source, DirectSerial );
99
100 if (serial->overflow < source->overflow) {
101 serial->overflow = source->overflow;
102 serial->value = source->value;
103
104 return true;
105 }
106 else if (serial->overflow == source->overflow && serial->value < source->value) {
107 serial->value = source->value;
108
109 return true;
110 }
111
112 D_ASSUME( serial->value == source->value );
113
114 return false;
115 }
116
117 #endif
118
119