Re: detecting if an atom is a pointer

new topic     » goto parent     » topic index » view thread      » older message » newer message

Chris Bensler wrote:
> 
> The problem:
>   A set of routines that accept an atom as a parameter, which could be a
>   pointer
> or a type identifier.
> 
> The type identifiers are Eu's C type definitions..
>   C_INT, C_FLOAT, etc..
> All the Eu definitions are in the format #0F00000F
> where #0F000000 represents the type modifier and #0000000F represents the
> atomic
> size.
> I've expanded the definitions to support compound C types. (structs, unions,
> arrays and bitfields)
> The mask format for the expanded identifiers is #CFFFFFFF
> As you can see, the expanded identifiers use pretty much all the bits of the
> atom. (bits 27 and 28 are not used)
> 
> My first thought was to use the pointer alignment to detect the difference,
> since an identifier will always use at least 1 of the aligned bits for the
> type
> size. This is flawed however because the type size could be 8, which would use
> 4 bits and the 3 aligned bits would be 0. Additionally, it would be preferable
> if it will handle third party allocations as well as Eu pointers, and AFAIK
> there is no guarantee that third party pointers will be aligned at all.
> 
> So the question: is there a way that I can determine if an atom is a pointer
> or an identifier?
> 
> In my testing it appears that the pointers never use all the high bits. Is
> this
> because Eu's heaps? or OS heaps? Would it be safe to use those bits to detect
> identifiers?
> 
> Chris Bensler
> ~ The difference between ordinary and extraordinary is that little extra ~
> <a href="http://empire.iwireweb.com">http://empire.iwireweb.com</a> - Empire
> for Euphoria

Hi Chris,

Welcome to the world of trying to tell the difference between a pointer
and an integer.
There's only three ways:
Identify the pointer by name, ie "Pointer1", etc, and id normal
integers by other types of names, "myvalue" etc.
or
By making it part of a sequence: {TYPE_POINTER, value}
or
Make the least bit always a 'zero' for pointers, and make it always
a 'one' for non-pointers.  This of course also means that you
might have to change your type identifier integers, to make them
always odd...ie if you have #80 you'll have to change it to #81
and make a cross ref table or possibly just adapt that to your
program or some other idea.  This way you know if it's a pointer
by checking the LSB of the value.



Take care,
Al

E boa sorte com sua programacao Euphoria!


My bumper sticker: "I brake for LED's"

 From "Black Knight":
"I can live with losing the good fight,
 but i can not live without fighting it".
"Well on second thought, maybe not."

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu