Index | FAQ | Search | Memberlist | Usergroups | Profile | Log in to check your private messages | Log in | Register

Static and Const

| Inside3d Forums Forum Index -> Engine Programming

View previous topic :: View next topic
Author | Message
--- | ---
Baker Joined: 14 Mar 2006 Posts: 1538 |

Posted: Thu Jul 08, 2010 8:35 pm Post subject: Static and Const |

I would like to get my source codes to all use static for all functions never called externally and const for all non-modified variables for the function declarations. DarkPlaces, for example, the entire source code is like that:

Code:
static qfile_t* FS_SysOpen (const char* filepath, const char* mode, qboolean nonblocking)

Does anyone know of any options in Visual Studio, Visual Studio Express or compiler options that allow the identification of such? Or even a 3rd party utility [or even a gcc compiler option].
I'd like to immediately upon seeing a function declaration know its scope and determine any inputs that could be modified.
Thanks in advance for any suggestions. I sure don't want to do my entire source code manually except as a very last resort.
[I don't know if getting my entire source using static and const for function declarations would make it compile any smaller or more optimized but I'm shooting for being able to more quickly understand the inputs, outputs and changes that are occurring and whether it is being used outside the source file.]
_________________
Tomorrow Never Dies. I feel this Tomorrow knocking on the door ...

Back to top |

Spike Joined: 05 Nov 2004 Posts: 944 Location: UK |

Posted: Thu Jul 08, 2010 9:36 pm Post subject: |

just put static+const infront of everything then keep removing them until it compiles. :P gcc will inline static stuff more agressively, so it'll probably come out slightly larger if you have many moderate sized functions that are called from multiple places.
it won't do much with const other than place it in rodata instead of data.
Using the restrict keyword will give more performance than const when used in the right places.
_________________
What's a signature?

Back to top |

Baker Joined: 14 Mar 2006 Posts: 1538 |

Posted: Thu Jul 08, 2010 9:51 pm Post subject: |

Spike wrote:
just put static+const infront of everything then keep removing them until it compiles.

100 or so source files ... gah. That'd take a while.
Well ... if it proves to be my only option ...
_________________
Tomorrow Never Dies. I feel this Tomorrow knocking on the door ...

Back to top |

mh Joined: 12 Jan 2008 Posts: 909 |

Posted: Thu Jul 08, 2010 9:55 pm Post subject: |

You can do it in 2008 (right-click on a function, find all references) or 2010 (right-click, find all references OR view call hierarchy). Both are great for doing smaller more piecemeal items of work in the IDE (such as removing individual global variables) but something scriptable would definitely be preferable for this.
If it was me I would probably grab the source code for something like Artistic Style or a C compiler, modify it slightly to just dump out a text file containing all function names (it will already have code for identifying these), then drop that text file into Excel and build a script that will grep all of your source files for the names.
_________________
DirectQ Engine - New release 1.8.666a, 9th August 2010
MHQuake Blog (General)
Direct3D 8 Quake Engines

Back to top |

Spike Joined: 05 Nov 2004 Posts: 944 Location: UK |

Posted: Thu Jul 08, 2010 11:40 pm Post subject: |

well, for each line containing a function declaration, grep through the headers for an identical lline (with added semi-colon). if you don't find any, its safe to add static, and if it isn't safe, then you need to fix something anyway, tbh. const is a real pain, by the way.
_________________
What's a signature?

Back to top |

Baker Joined: 14 Mar 2006 Posts: 1538 |

Posted: Fri Jul 09, 2010 12:00 am Post subject: |

Spike wrote:
const is a real pain, by the way.

You mean identifying instances where const should be used in a function declaration or some twist I might not be thinking of like where the compiler might miss something?
_________________
Tomorrow Never Dies. I feel this Tomorrow knocking on the door ...

Back to top |

Sajt Joined: 16 Oct 2004 Posts: 1026 |

Posted: Fri Jul 09, 2010 12:09 am Post subject: |

Const can be a pain, yes... sometimes your struct contains one or two little things that should be exempt from const (like reference counters or generated indices used in serialization). In C++ you have the mutable keyword but that's a further hack (though I wouldn't hesitate to use it in my case). "Proper" design probably requires you to split those into separate structs, keep them in separate lists, make a thousand extra allocs, and blow everything to pieces, but that's not usually practical.
_________________
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.

Back to top |

Spike Joined: 05 Nov 2004 Posts: 944 Location: UK |

Posted: Fri Jul 09, 2010 2:03 am Post subject: |

char *strstr(const char *in, const char *match); so, urm, how does that work? :P With string management, I quite often change a character mid-string to 0 to terminate it so I can use some libc function or whatever without having to duplicate it etc (then restore it after). Can't do that if its const, yet the string is unmodified when its returned.
With const in parts of the engine, you quickly find that you need to make everything else const too. If you can pick out your leaf functions then sure, but noone claimed that Quake was clean.
And then because you use tricks like temporarily setting chars mid-string to 0, or passing to library functions that arn't const-safe, or your own functions that take an argument to enable modifications, or... etc etc.
Don't get me wrong, const is a useful keyword. Its just that you generally need to rewrite half your code in order to use it properly (or to have written your code using const as it was written).
An example from FTE:
I have mesh buffers. They basically contain pointers to vertex+coord+texcoord data. A few of my load functions will play with this data in various ways (its used for buffers. the mesh object contains pointers to data, whether its filled in yet or not, an alternative example would be quake's water being split into multiple smaller fragments). This prevents me from using const on my mesh objects.
And because my mesh objects arn't const, I can't make my static const arrays (eg index arrays) const either.
Now I could rewrite my code to use different memory representations between finished meshes and proto-meshes just to avoid issues with const. But frankly I'm too lazy.
offtopic but somewhat related: the wip branch of fteqcc supports the static keyword. it also supports '#pragma defaultstatic 1' and a matching nonstatic keyword to go with it.
_________________
What's a signature?

Back to top |

Baker Joined: 14 Mar 2006 Posts: 1538 |

Posted: Sat Jul 17, 2010 8:11 pm Post subject: |

I've just discovered that XCode (OS compiler) hates it if static functions aren't at the top --- not a warning, an error. Wouldn't be shocking if gcc hates it too. MSVC6 doesn't care, but I don't think I feel like moving everything static to the top of every source file.
It doesn't even save a single byte in file size doing static (just makes the code easier to understand and maybe possibly might minimally benefit from compiler optimization.)
_________________
Tomorrow Never Dies. I feel this Tomorrow knocking on the door ...

Back to top |

Spike Joined: 05 Nov 2004 Posts: 944 Location: UK |

Posted: Sun Jul 18, 2010 6:34 am Post subject: |

you need to prototype things correctly still, but you shouldn't need static stuff first (older versions of gcc are known to not inline if they are not first, but that shouldn't affect permitted C syntax). _________________ What's a signature?

Back to top |

Display posts from previous:

| Inside3d Forums Forum Index -> Engine Programming | All times are GMT
Page 1 of 1

Jump to:

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2004 phpBB Group