1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "cyclic.h" 
 
#define CYCLIC_HASH_SIZE 0x1000 
 
static CYCLIC *cyclic_hash[CYCLIC_HASH_SIZE]; 
 
static size_t cyclic_hash_func(CYCLIC *c) 
{ 
  size_t h; 
 
  h = PTR_TO_INT(c->id); 
  h *= 33; 
  h ^= PTR_TO_INT(c->a); 
  h *= 33; 
  h ^= PTR_TO_INT(c->b); 
  h *= 33; 
  h ^= PTR_TO_INT(c->d); 
  h *= 33; 
  h ^= PTR_TO_INT(c->th); 
 
#if SIZEOF_CHAR_P > 4 
  h ^= h>>8; 
#endif 
  /* Fold h. This is to retain as many bits of h as possible. 
   * 
   * NB: The "magic" constant below has a 1 bit every 10 bits 
   *     starting at the least significant, and is == 1 when 
   *     shifted right 20 bits. Note also that 32 - 20 == 12 
   *     and 1<<12 == 0x1000 == CYCLIC_HASH_SIZE. 
   * 
   *     The multiplication has the effect of accumulating 
   *     the segments of 10 bits of h in the most significant 
   *     segment, which is then shifted down. 
   */ 
  h *= 0x100401; 
  h >>= 20; 
 
  return h & (CYCLIC_HASH_SIZE-1); 
} 
 
void low_unlink_cyclic(CYCLIC *c) 
{ 
  size_t h; 
  CYCLIC **p; 
 
  h = cyclic_hash_func(c); 
 
  for(p=cyclic_hash+h;*p;p=&(p[0]->next)) 
  { 
    if(c == *p) 
    { 
      *p=c->next; 
#ifdef CYCLIC_DEBUG 
      fprintf (stderr, "%s: END_CYCLIC a=%p b=%p: no cycle\n", c->id, c->a, c->b); 
#endif 
      return; 
    } 
  } 
#ifdef PIKE_DEBUG 
  Pike_fatal("Unlink cyclic on lost cyclic struct (%s).\n", c->id); 
#else 
  Pike_fatal("Unlink cyclic on lost cyclic struct.\n"); 
#endif 
} 
 
PMOD_EXPORT void unlink_cyclic(CYCLIC *c) 
{ 
  UNSET_ONERROR(c->onerr); 
  low_unlink_cyclic(c); 
} 
 
void *low_begin_cyclic(CYCLIC *c, 
                       char *id, 
                       void *th, 
                       void *a, 
                       void *b, 
                       void *d) 
{ 
  size_t h; 
  void *ret = 0; 
  CYCLIC *p; 
 
  c->ret = (void *)(ptrdiff_t)1; 
  c->a = a; 
  c->b = b; 
  c->d = d; 
  c->id = id; 
  c->th = th; 
 
  h = cyclic_hash_func(c); 
 
  for(p=cyclic_hash[h];p;p=p->next) 
  { 
    if(a == p->a && b==p->b && id==p->id && th==p->th && d==p->d) 
    { 
#ifdef CYCLIC_DEBUG 
      fprintf (stderr, "%s: BEGIN_CYCLIC a=%p b=%p d=%p: found cycle\n", 
               id, a, b, d); 
#endif 
      c->ret = ret = p->ret; 
      break; 
    } 
  } 
 
  c->next = cyclic_hash[h]; 
  cyclic_hash[h] = c; 
#ifdef CYCLIC_DEBUG 
  if (!ret) fprintf (stderr, "%s: BEGIN_CYCLIC a=%p b=%p d=%p: no cycle\n", 
                     id, a, b, d); 
#endif 
  return ret; 
} 
 
PMOD_EXPORT void *begin_cyclic(CYCLIC *c, 
                               char *id, 
                               void *th, 
                               void *a, 
                               void *b, 
                               void *d) 
{ 
  void *ret = low_begin_cyclic(c, id, th, a, b, d); 
  SET_ONERROR(c->onerr, low_unlink_cyclic, c); 
  return ret; 
}