Get unique IDs by reading the Instruction Pointer Register

Started by
27 comments, last by Tispe 11 years, 1 month ago

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!

Advertisement
This... is a horrible idea...

You have 3 'button' widgets; you run the code to get the instruction pointer - what makes you think this will be different for each instance, given they are running the same code...

Just use an int and have someone increment it each time you create a control... if you really think 32bit is too small then you could always use a 64bit int..

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
}

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.

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.

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

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.

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.

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.

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

This topic is closed to new replies.

Advertisement