Jump to content
  • Advertisement
Sign in to follow this  
Tispe

Get unique IDs by reading the Instruction Pointer Register

This topic is 1966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

 

I am currently writing an IMGUI implementation and I need to create an unique ID for each widget. I thought about using the Instruction Pointer (Program Counter) since it will be the same and be unique everytime I get to the same line of code.

 

But I don't know that much about assembly that I can inline and get it read. I googled this but it does not compile in VS2010:

void *ip = 0;
asm("movl %%eip, %0" : "=r"(ip) );

 

Does it seem like a good idea to generate unique IDs like this? And can someone help me get the asm working?

 

Cheers!

Edited by Tispe

Share this post


Link to post
Share on other sites
Advertisement

This is IMGUI, the EIP will not be the same for identical widgets because each widget will be called from different parts of the code.

 

DWORD ID = GetEIP();
if(DoButton(ID,Button1,.....))
{
     //code
}

ID=GetEIP();
if(DoButton(ID,Button2.....))
{
     //code
}

Share this post


Link to post
Share on other sites
A combination of line number, source file ID and loop index (if you're inside a for loop) would be easier to manage.

I'm sure you will have seen this:

http://sol.gfxile.net/imgui/ch04.html

You can get away with just incrementing (in code), but then you can never change the widget set while one of them is active or the IDs will go out of sync.

Share this post


Link to post
Share on other sites

I was hoping that I could macro something that would give me an ID.

 

if(DoButton(GetEIP(),Button1,.....))
{
     //code
}


if(DoButton(GetEIP(),Button2.....))
{
     //code
}

 

This would pass the same unique ID everytime the code goes through that code. I want to avoid hardcoding IDs or even pass the address of a static DummyVariable.

Edited by Tispe

Share this post


Link to post
Share on other sites

If you're ok with a non-portable extension VC and GCC provide the __COUNTER__ macro which does just what you're asking for.

Share this post


Link to post
Share on other sites

What I have tried now is

#ifdef IMGUI_SRC_ID
#define GEN_ID ((IMGUI_SRC_ID) + (__LINE__))
#else
#define GEN_ID (__LINE__)
#endif

And then in each cpp file I do this in increments of 10000, this will work since I dont have any source files with more then 10000 Lines of code.

#define IMGUI_SRC_ID 50000

 

But.... When I now call

if(DoButton(GEN_ID,.......))
{
      //code
}

The compiler says: cannot convert parameter 1 from 'long' to 'int &'. GEN_ID is defined as long :/

 

 

So I changed to

#ifdef IMGUI_SRC_ID
#define GEN_ID int(((IMGUI_SRC_ID) + (__LINE__)))
#else
#define GEN_ID int((__LINE__))
#endif

and took out the referencing.

Edited by Tispe

Share this post


Link to post
Share on other sites

When I wanted to make such a function for IMGUI I tried to use the __LINE__ makro too, but found out it didnt really work cause if you call some widget from two places it would get the same id again. I also considered mangling the __FILE__ makro but that got same problem and felt too complicated for the little gain. Also just adding to the line number seems to produce less unique numbers.

So I made a custom hash function which started with somehow combining the parentid and the number of needed child ids and then wrote a bunch of unittests to verify uniqueness and that led to a number of additions and bit twiddling tweaks to the function to make it behave as good as possible even on grandchild widgets, bit overflow and wrong inputs. I'm still not convinced its completely done so I'll be looking for more improvedments later.

Share this post


Link to post
Share on other sites

The compiler says: cannot convert parameter 1 from 'long' to 'int &'. GEN_ID is defined as long :/


Wait, why does the function take a reference to an int? It provides no benefit over passing the int by value, and will likely result in a penalty for the level of indirection. The only reason I can see for someone to do that is if the function modifies the referred to value.

Just making sure the function isn't now modifying a temporary value, thinking it is making a change to the value passed to the function.

Share this post


Link to post
Share on other sites

I still don't understand why you can't just say int id=random()*random()>>random() or something similar if you absolutely must have a totally random and unique ID every time and do checks to make sure you're not getting identical ids every time, and of course you can map the results of your random id assignment to some meaningful array of structs describing the widget or whatever

Edited by darkhaven3

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!