your favorite function

Programming, for all ages and all languages.
Mark139
Member
Member
Posts: 39
Joined: Mon Jan 15, 2007 2:32 pm

Re: your favorite function

Post by Mark139 »

Great idea for a post. My favorite function is the one I've just written. Div_mod in ARM assembler.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: your favorite function

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: your favorite function

Post 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).
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: your favorite function

Post by 01000101 »

- Moved to General Programming.
Carry on. :wink:
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: your favorite function

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: your favorite function

Post 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
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: your favorite function

Post by qw »

Code: Select all

int main(int argc, char **argv)
What else?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: your favorite function

Post 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 ) );
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: your favorite function

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: your favorite function

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: your favorite function

Post 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)
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: your favorite function

Post 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 ) );
Last edited by Solar on Mon Nov 16, 2009 7:36 am, edited 1 time in total.
Every good solution is obvious once you've found it.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: your favorite function

Post by qw »

Ah, silly me. The error message already told me. I overlooked the exact return type of operator<<(). Thanks!
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: your favorite function

Post 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()
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: your favorite function

Post by Combuster »

must...not...feed...troll

I rest my case.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply