serial.h 3.1 KB

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