Page 2 of 3

Re: your favorite function

Posted: Sun Nov 15, 2009 4:11 pm
by Mark139
Great idea for a post. My favorite function is the one I've just written. Div_mod in ARM assembler.

Re: your favorite function

Posted: Sun Nov 15, 2009 4:37 pm
by Combuster
overall, sprintf. cout or printf are too limited as to where the information ends up, and that's not just the case for OS development.

Re: your favorite function

Posted: Sun Nov 15, 2009 8:20 pm
by geppyfx
(Sorry if this is a pointless thread)
This is truly amazing how many people found this tread non pointless.
your favorite function
I'll give you this one. Its very subjective. Physical memory allocation function for me. Everything will fall apart without it.
the function you use the most
Appears most in the source? Executed most by CPU? Most time consuming(longest name) to type?
could not live without
Seriosly, which function can you remove and keep functionality of the kernel at the same level?

This is probably the first time time I regret that Combuster is no longer moderator. But since he is no longer with power he also decided to post while not having to say anything useful (my believe that thread is indeed pointless).

Re: your favorite function

Posted: Sun Nov 15, 2009 8:36 pm
by 01000101
- Moved to General Programming.
Carry on. :wink:

Re: your favorite function

Posted: Sun Nov 15, 2009 8:42 pm
by earlz
umm.. in my kernel I have this file called `kdebug.c` which is nothing but special debugging functions. Among my most commonly used was `kputs_x_y` which would print a string at specified x and y coordinates without buffering. Also I used a thing called `stopints();hlt();` which would help me when debugging when getting page faults and other problems with printing to the screen and also I had a PANIC macro which was pretty amazing also lol

btw, this thread is pointless, but geppyfx isn't right.

Re: your favorite function

Posted: Sun Nov 15, 2009 9:05 pm
by NickJohnson
For me, definitely signal() - my kernel function that sends a preemptive signal (16 byte synchronous message that changes control flow) to a process. It's used for not only all IPC, which in a microkernel is everything, but also IRQ handling, page fault handling, math emulation, and even efficient userspace threading. It's both the most complex system call and at the heart of other calls like fork() and exit(). It even uses itself for error handling. 57 well commented lines of awesome. :D

Re: your favorite function

Posted: Mon Nov 16, 2009 2:40 am
by qw

Code: Select all

int main(int argc, char **argv)
What else?

Re: your favorite function

Posted: Mon Nov 16, 2009 6:01 am
by Solar

Code: Select all

#include <sstream>

#define SSTR( x ) dynamic_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str()
It's a nice augmentation to the operator<<() already mentioned. In some places you can't really use it, because what is expected is a (C or C++) string, and operator<<() works only on streams.

This little piece of macro magic allows you to create that string on the fly using operator<<() without having to fumble around with ostringstream yourself. Especially useful with C-style log functions.

Example:

Code: Select all

call_to_foo( SSTR( "Output: a[1] is " << a[1] << ", and the answer to all questions is " << 42 ) );

Re: your favorite function

Posted: Mon Nov 16, 2009 6:30 am
by Combuster
geppyfx wrote:This is probably the first time time I regret that Combuster is no longer moderator. But since he is no longer with power he also decided to post while not having to say anything useful (my believe that thread is indeed pointless).
Even as a moderator, I would have let this run freely. In fact *you* would have been the more likely target for going offtopic.

If this thread was truly pointless, there would be nothing to learn. Do you know what the function Colonel Kernel mentioned does?

Re: your favorite function

Posted: Mon Nov 16, 2009 7:05 am
by qw
Solar wrote:

Code: Select all

#include <sstream>

#define SSTR( x ) dynamic_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str()
I'm probably missing some C++ subtlety here, but what is the reason for casting ostringstream to ostringstream& ? I tried to compile with and without the cast. Without cast, it lead to the error "'struct std::basic_ostream<char, td::char_traits<char> >' has no member named 'str'" but I don't understand why the reference does have a member named 'str' and the struct itself doesn't.

Re: your favorite function

Posted: Mon Nov 16, 2009 7:29 am
by Solar
Hobbes wrote:I'm probably missing some C++ subtlety here...
Just like me the first time around. :twisted:
...but what is the reason for casting ostringstream to ostringstream& ?
No such thing. ;-) There is some tricky type conversion going on.

You need ostringstream& so you can call .str() at the end.

But you need ostream& so that e.g. char* is handled correctly (and not printed as a pointer value as it would with ostringstream&). That's why the operator<<( std::dec ) is in there - it works for ostringstream, and returns ostream&.

After you fed x to the ostream&, you have to convert it back to ostringstream& so you can call .str().

Ugly as all macros, but both valid and useful C++ code like all good macros. Tested on various platforms, too. 8)

Re: your favorite function

Posted: Mon Nov 16, 2009 7:35 am
by Solar
Oh, and I forgot its trusted sidekick. Less impressive and convoluted, but just as useful.

Code: Select all

#define THROW( exc, code, message ) throw exc( SSTR( code << ": (" << __FLIE__ << ", line " << __LINE__ << ": " << message ) );

Re: your favorite function

Posted: Mon Nov 16, 2009 7:35 am
by qw
Ah, silly me. The error message already told me. I overlooked the exact return type of operator<<(). Thanks!

Re: your favorite function

Posted: Tue Nov 17, 2009 8:52 am
by geppyfx
Combuster wrote:
geppyfx wrote:If this thread was truly pointless, there would be nothing to learn. Do you know what the function Colonel Kernel mentioned does?
Does that mean sending messages is more important than memory allocations?
Or do you suggest I learn ObjC? Maybe objc_msgSend is a macro or compiler/language related function? How come programming language related issues belong to osdev site not to mention "OS Development" forum(before it was moved).
In fact *you* would have been the more likely target for going offtopic.
It seems that all *you* can do is to argue about previously mentioned printf function. While the OP asks to present your own function.
overall, sprintf. cout or printf are too limited as to where the information ends up, and that's not just the case for OS development.
If we had a poll I'd give 1 vote to each of these: efficiently coded cli, kitchen_sink(), hack_the_gibson()

Re: your favorite function

Posted: Tue Nov 17, 2009 10:07 am
by Combuster
must...not...feed...troll

I rest my case.