/* ---------------------------------------------------------------------
   (c) Emmanuel Delahaye 2002
   Project      : CLIB
   Function     : Compteur 64 bits
   Module       : C64
   File         : C64.C
   Created      : 17-12-2002
   Modified     : 17-12-2002
   --------------------------------------------------------------------- */
#ifdef __cplusplus
#error This source file is not C++ but rather C. Please use a C-compiler
#endif

/* ---------------------------------------------------------------------
   Log

   0.0 - 17-12-2002 Created

   --------------------------------------------------------------------- */

#include <limits.h>

#include "ed/inc/c64.h"

/* macros ============================================================== */

#define DBG 0

#if DBG
#define MAX 7
#else
#define MAX ULONG_MAX
#endif

/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */
/* private functions =================================================== */
/* internal public functions =========================================== */
/* public variables ==================================================== */
/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   c64_init()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void c64_init (c64_s * this)
{
   if (this)
   {
      this->l = 0;
      this->h = 0;
      this->lock_l = 0;
      this->lock_h = 0;
   }
}

/* ---------------------------------------------------------------------
   c64_inc()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
int c64_inc (c64_s * this)
{
   int err = -1;

   if (this)
   {
      err = (int) this->locked;

      if (!err)
      {
         if (this->lock_h)
         {
            this->lock_l = this->l == MAX;

            if (this->lock_l)
            {
               err = 1;
               this->locked = 1;
            }
         }

         if (!this->lock_l)
         {
            this->l++;
#if DBG
            if (this->l == (MAX + 1))
            {
               this->l = 0;
            }
#endif
            if (this->l == 0)
            {
               this->lock_h = this->h == MAX;

               if (!this->lock_h)
               {
                  this->h++;
#if DBG
                  if (this->h == (MAX + 1))
                  {
                     this->h = 0;
                  }
#endif
               }
            }
         }
      }
   }
   return err;
}
