Jump to content
  • Advertisement
Sign in to follow this  
Khaos Dragon

ways to cache code

This topic is 4867 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

I am wondering if there a way to do something like this in c++. What I want to do works similar to how a display list in opengl works, but I would like to be able to do this for a general case.
BeginCachingCode();
a bunch of lines of code blah
...
...
...
...
EndCachingCode();

; Then later you could do this...
RunCachedCode();

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SiCrane
Sounds like a job for a function.


What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function, if anyone is familiar with opengl display lists then this should make sense.

Share this post


Link to post
Share on other sites
I was thinking that BeginCaching could perhaps store the memory address of the next instruction, and that EndCaching could mark the memory address of the previous instruction as an endpoint to use when RunCachedCode is called. I am not sure if something like this is possible.

The method I describe here is not caching, but merely just stores and start and end addresses of the instructions in the code block.

Share this post


Link to post
Share on other sites
Quote:
Original post by Khaos Dragon
Quote:
Original post by SiCrane
Sounds like a job for a function.


What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function



Quote:
if anyone is familiar with opengl display lists then this should make sense.

Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:


BeginCachingCode();
// magical speed voodoo goes here
EndCachingCode();


Which doesn't help anyone.

Share this post


Link to post
Share on other sites
There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).

If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.

JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.

Share this post


Link to post
Share on other sites
Quote:
Original post by OrangyTang
Quote:
Original post by Khaos Dragon
Quote:
Original post by SiCrane
Sounds like a job for a function.


What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function



Quote:
if anyone is familiar with opengl display lists then this should make sense.

Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:


BeginCachingCode();
// magical speed voodoo goes here
EndCachingCode();


Which doesn't help anyone.



Hmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).

If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.

JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.


I have not used assembly much. However is it possible within assembly to get the address of the current instruction pointer?

Share this post


Link to post
Share on other sites
Quote:
Original post by Khaos Dragon
Hmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.

'Organisational reasons' is so horribly vauge that it could mean anything, but at a guess I'd suggest you look into function pointers and/or the listener pattern.

Share this post


Link to post
Share on other sites
I currently am using function pointers, but this requires me to force the user to define a function with specific class parameters where I like him to be able to pass whatever paramteres possible.

I suppose a function pointer with a void pointer as an argument would be good here?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!