1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* The generic hashing primitive */
|
|
|
|
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "str.h"
|
|
|
|
|
|
|
|
static unsigned long hash_accu;
|
|
|
|
static long hash_univ_limit, hash_univ_count;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void hash_aux(value obj);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value hash_univ_param(value count, value limit, value obj) /* ML */
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
hash_univ_limit = Long_val(limit);
|
|
|
|
hash_univ_count = Long_val(count);
|
|
|
|
hash_accu = 0;
|
|
|
|
hash_aux(obj);
|
|
|
|
return Val_long(hash_accu & 0x3FFFFFFF);
|
|
|
|
/* The & has two purposes: ensure that the return value is positive
|
|
|
|
and give the same result on 32 bit and 64 bit architectures. */
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Alpha 65599
|
|
|
|
#define Beta 19
|
|
|
|
#define Combine(new) (hash_accu = hash_accu * Alpha + (new))
|
|
|
|
#define Combine_small(new) (hash_accu = hash_accu * Beta + (new))
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void hash_aux(value obj)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
unsigned char * p;
|
1995-07-27 10:41:09 -07:00
|
|
|
mlsize_t i, j;
|
1995-05-04 03:15:53 -07:00
|
|
|
tag_t tag;
|
|
|
|
|
|
|
|
hash_univ_limit--;
|
|
|
|
if (hash_univ_count < 0 || hash_univ_limit < 0) return;
|
|
|
|
|
|
|
|
if (Is_long(obj)) {
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine(Long_val(obj));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1995-07-13 02:02:41 -07:00
|
|
|
/* Pointers into the heap are well-structured blocks. So are atoms.
|
1995-05-04 03:15:53 -07:00
|
|
|
We can inspect the block contents. */
|
|
|
|
|
1995-07-13 02:02:41 -07:00
|
|
|
if (Is_atom(obj) || Is_young(obj) || Is_in_heap(obj)) {
|
1995-05-04 03:15:53 -07:00
|
|
|
tag = Tag_val(obj);
|
|
|
|
switch (tag) {
|
|
|
|
case String_tag:
|
|
|
|
hash_univ_count--;
|
|
|
|
i = string_length(obj);
|
|
|
|
for (p = &Byte_u(obj, 0); i > 0; i--, p++)
|
|
|
|
Combine_small(*p);
|
|
|
|
break;
|
|
|
|
case Double_tag:
|
|
|
|
/* For doubles, we inspect their binary representation, LSB first.
|
|
|
|
The results are consistent among all platforms with IEEE floats. */
|
|
|
|
hash_univ_count--;
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
1995-05-04 03:15:53 -07:00
|
|
|
for (p = &Byte_u(obj, sizeof(double) - 1), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p--, i--)
|
|
|
|
#else
|
|
|
|
for (p = &Byte_u(obj, 0), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p++, i--)
|
|
|
|
#endif
|
|
|
|
Combine_small(*p);
|
|
|
|
break;
|
1995-07-27 10:41:09 -07:00
|
|
|
case Double_array_tag:
|
|
|
|
hash_univ_count--;
|
|
|
|
for (j = 0; j < Bosize_val(obj); j += sizeof(double)) {
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
1995-07-27 10:41:09 -07:00
|
|
|
for (p = &Byte_u(obj, j + sizeof(double) - 1), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p--, i--)
|
|
|
|
#else
|
|
|
|
for (p = &Byte_u(obj, j), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p++, i--)
|
|
|
|
#endif
|
|
|
|
Combine_small(*p);
|
|
|
|
}
|
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
case Abstract_tag:
|
|
|
|
case Final_tag:
|
|
|
|
/* We don't know anything about the contents of the block.
|
|
|
|
Better do nothing. */
|
|
|
|
break;
|
1995-07-10 02:48:27 -07:00
|
|
|
case Infix_tag:
|
|
|
|
hash_aux(obj - Infix_offset_val(obj));
|
|
|
|
break;
|
1997-05-11 15:42:38 -07:00
|
|
|
case Object_tag:
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine(Oid_val(obj));
|
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
default:
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine_small(tag);
|
|
|
|
i = Wosize_val(obj);
|
|
|
|
while (i != 0) {
|
|
|
|
i--;
|
|
|
|
hash_aux(Field(obj, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, obj is a pointer outside the heap, to an object with
|
|
|
|
a priori unknown structure. Use its physical address as hash key. */
|
|
|
|
Combine((long) obj);
|
|
|
|
}
|