Hi. I've been wanting to wrap up oids into OCaml, and I'm wondering a few things about the following statement in gdk.mx:
@item oid: Unique @strong{long int} values uses as object identifier. Highest bit cleared always. Thus, oids-s are 31-bit numbers on 32-bit systems, and 63-bit numbers on 64-bit systems.
Moreover, the oid type is typedefed in the following way:
#ifdef MONET_OID32 #define SIZEOF_OID SIZEOF_INT typedef unsigned int oid; #else #define SIZEOF_OID SIZEOF_SIZE_T typedef size_t oid; #endif
I understood that MONET_OID32 was the exception rather than the rule, so I'm assuming, for now, that oid is size_t. But then, I'm wondering why the highest bit is always cleared. Moreover, OCaml has a way to deal with their integers which is rather peculiar. They are long ints, but the lowest bit has a peculiar meaning. If the lowest bit is 0, then this long int is in fact a pointer to an OCaml value in the OCaml heap. If the lowest bit is 1, then shift the value one bit, and you have the Ocaml integer. This scheme takes advantage of the alignement of OCaml values in the OCaml heap. So ocaml integers are 31-bit on 32-bit systems, and 63-bit on 64 bit systems. The difference is that the lowest bit is set, whereas it's the highest bit that is cleared for oids. Therefore, to convert oids to ocaml ints and conversely, I wrote:
#define ocamlmonetdb5_Val_oid(x) ((value) ((((oid) x) << 1) + 1)) #define ocamlmonetdb5_Oid_val(x) (((oid) x) >> 1)
Two questions: -1- This seems to work, but I'd appreciate if you could double-check this. Just to check if I understood correctly the bit layout of oids. -2- I'm wondering why the highest bit of oids is always cleared. This doesn't seem to be implied by the fact that oids are typedefed size_t. So what's the rationale? All the best, -- Guillaume Yziquel http://yziquel.homelinux.org/