OSSP CVS Repository

ossp - Check-in [3734]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 3734
Date: 2004-Jan-10 23:18:48 (local)
2004-Jan-10 22:18:48 (UTC)
User:rse
Branch:
Comment: implement last component of v1 UUID generation: clock sequence
Tickets:
Inspections:
Files:
ossp-pkg/uuid/uuid.c      1.19 -> 1.20     44 inserted, 34 deleted

ossp-pkg/uuid/uuid.c 1.19 -> 1.20

--- uuid.c       2004/01/10 21:54:52     1.19
+++ uuid.c       2004/01/10 22:18:48     1.20
@@ -478,14 +478,19 @@
    (which in our case is 1us (= 1000ns) because we use gettimeofday(2) */
 #define UUIDS_PER_TICK 10
 
-/* INTERNAL: generate UUID version 1, time part */
-static uuid_rc_t uuid_generate_v1_time(uuid_t *uuid, unsigned int mode, va_list ap)
+/* INTERNAL: generate UUID version 1: time, clock and node based */
+static uuid_rc_t uuid_generate_v1(uuid_t *uuid, unsigned int mode, va_list ap)
 {
     struct timeval time_now;
     struct timeval tv;
     ui64_t t;
     ui64_t offset;
     ui64_t ov;
+    uuid_uint16_t clck;
+
+    /*
+     *  GENERATE TIME
+     */
 
     /* determine current system time and sequence counter */
     while (1) {
@@ -495,15 +500,10 @@
 
         /* check whether system time changed since last retrieve */
         if (!(   time_now.tv_sec  == uuid->time_last.tv_sec
-              && time_now.tv_usec == uuid->time_last.tv_usec)) {
+              && time_now.tv_usec == uuid->time_last.tv_usec))
             /* reset time sequence counter */
             uuid->time_seq = 0;
 
-            /* remember system time for next iteration */
-            uuid->time_last.tv_sec  = time_now.tv_sec;
-            uuid->time_last.tv_usec = time_now.tv_usec;
-        }
-
         /* until we are out of UUIDs per tick, increment
            the time/tick sequence counter and continue */
         if (uuid->time_seq < UUIDS_PER_TICK) {
@@ -555,19 +555,36 @@
     uuid->obj.time_low =
         (uuid_uint32_t)(ui64_i2n(ov) & 0xffffffff); /* all 32 bit */
 
-    return UUID_RC_OK;
-}
-
-/* INTERNAL: generate UUID version 1, clock part */
-static uuid_rc_t uuid_generate_v1_clock(uuid_t *uuid, unsigned int mode, va_list ap)
-{
-    /* FIXME */
-    return UUID_RC_OK;
-}
+    /*
+     *  GENERATE CLOCK
+     */
+
+    /* retrieve current clock sequence */
+    clck = ((uuid->obj.clock_seq_hi_and_reserved & ~((0x03) << 6)) << 8)
+           + uuid->obj.clock_seq_low;
+
+    /* generate new random clock sequence (initially or if the
+       time has stepped backwards) or else just increase it */
+    if (   clck == 0
+        || (   time_now.tv_sec < uuid->time_last.tv_sec
+            || (   time_now.tv_sec == uuid->time_last.tv_sec
+                && time_now.tv_usec < uuid->time_last.tv_usec)))
+        prng_data(uuid->prng, (void *)&clck, sizeof(clck));
+    else
+        clck++;
+    clck &= ~((0x03) << 6);
+
+    /* store back new clock sequence */
+    uuid->obj.clock_seq_hi_and_reserved =
+        (uuid->obj.clock_seq_hi_and_reserved & ((0x03) << 6))
+        | (uuid_uint8_t)((clck >> 8) & 0xff);
+    uuid->obj.clock_seq_low =
+        (uuid_uint8_t)(clck & 0xff);
+
+    /*
+     *  GENERATE NODE
+     */
 
-/* INTERNAL: generate UUID version 1, node part */
-static uuid_rc_t uuid_generate_v1_node(uuid_t *uuid, unsigned int mode, va_list ap)
-{
     if ((mode & UUID_MCASTRND) || (uuid->mac[0] & 0x80)) {
         /* use random multi-cast MAC address */
         prng_data(uuid->prng, (void *)&(uuid->obj.node), sizeof(uuid->obj.node));
@@ -577,21 +594,14 @@
         /* use real regular MAC address */
         memcpy(uuid->obj.node, uuid->mac, sizeof(uuid->mac));
     }
-    return UUID_RC_OK;
-}
-
-/* INTERNAL: generate UUID version 1: time, clock and node based */
-static uuid_rc_t uuid_generate_v1(uuid_t *uuid, unsigned int mode, va_list ap)
-{
-    uuid_rc_t rc;
 
-    /* generate individual parts of v1 UUID */
-    if ((rc = uuid_generate_v1_time (uuid, mode, ap)) != UUID_RC_OK)
-        return rc;
-    if ((rc = uuid_generate_v1_clock(uuid, mode, ap)) != UUID_RC_OK)
-        return rc;
-    if ((rc = uuid_generate_v1_node (uuid, mode, ap)) != UUID_RC_OK)
-        return rc;
+    /*
+     *  FINISH
+     */
+
+    /* remember current system time for next iteration */
+    uuid->time_last.tv_sec  = time_now.tv_sec;
+    uuid->time_last.tv_usec = time_now.tv_usec;
 
     /* brand with version and variant */
     uuid_brand(uuid, 1);

CVSTrac 2.0.1