Page 1 of 1
i am not sure if anyone is intrested
Posted: Sat Mar 21, 2009 9:51 pm
by smeezekitty
i have some kernel compatable libarys
a string libary source and header i wrote myself
and a malloc libary and header
Re: i am not sure if anyone is intrested
Posted: Sat Mar 21, 2009 9:54 pm
by smeezekitty
malloc libary:
Re: i am not sure if anyone is intrested
Posted: Sat Mar 21, 2009 10:09 pm
by JackScott
I haven't had more than a quick look through the source code, but I have a few recommendations:
- Tabs. Add some tabs within the function blocks and so on, so that you (or rather, other people) can see the flow of the code properly. It doesn't matter whether you use tabs or spaces, but using neither is a sin.
- Same thing for comments. Add some, it'll improve readability a lot. We'd like to know what, how and why a function does something, though if the code is clear enough sometimes the how part can be left out.
- Whitespace. Put space between the functions, different blocks, parts of a block that do different things. It just breaks it up and makes it easier to read.
It's more important to do these things in libraries than in normal code even, since a library is going to be shared. If nobody can understand what the code is doing, how are they meant to use it properly?
I might play around with the actual code later and see what I can get it to do. I'm not in front of a compiler at the moment.
On a more pedantic note, what is the license of these libraries? You haven't specified one, so full copyright remains to you at the moment.
It might sound a bit like I'm being discouraging, but I don't want to be. It's just hard to read at the moment, that's all. Can you imagine anybody bothering to read a Shakespeare play if the handwriting was a 4 year olds?
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 2:00 am
by purage
You should further develop the malloc (and not call it malloc) function to actually reserve the memory as well. A check to see if you are out of memory would be nice too.
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 12:08 pm
by Troy Martin
They appear to work at a glance of the code, but they're extremely simple.
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 7:00 pm
by steveklabnik
purage wrote:You should further develop the malloc (and not call it malloc) function to actually reserve the memory as well. A check to see if you are out of memory would be nice too.
Or, you know.Make it work.
But basically, I agree with JackScott. Get some comments and whitespace in there! Nobody will use your code if they can't read it or understand it.
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 7:14 pm
by Wilkie
Code: Select all
int address = 0x00F;
void *m_malloc(int size){
address = address + size+3;
return ((void *)address);
}
void m_free(char *p){
if(p){
p = (char *) 0x00;
}
}
Could be optimized to:
Code: Select all
int address = 0x00F;
void *m_malloc(int size)
{
return ((void*)address += size);
}
void m_free(void *p)
{
}
This is equivalent. Note, all values in C are passed by value, and your free does nothing. (It also doesn't matter if p is valid, so your condition is moot)
Also, why the size + 3? Call it a curiosity, but please indulge. And why does your malloc return the end of the allocated space?
If you'd like to learn how to implement a real malloc, you can look that up.
Troy Martin wrote:They appear to work at a glance of the code, but they're extremely simple.
Shame on you!
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 7:57 pm
by Troy Martin
My definition of "work" means that it compiles
Therefore, a bare bones kernel that loads and halts works! so it's a working kernel!
EDIT: Four minutes in notepad and I wrote a better and more efficient strlower. Change a few characters and it's a strupper.
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 8:15 pm
by Wilkie
Ten minutes and I wrote a better str everything than that. Do whatever you want with this code, it is WTFPL'd
. All functions were tested and
do work.
EDIT: exception to the strncpy, which I fixed. I don't know why I care to find this fun
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 8:30 pm
by Troy Martin
Code: Select all
<Mr_Piranha> int strlen(char *s)
<Mr_Piranha> {
<Mr_Piranha> char *o;
<Mr_Piranha> o=s;
<Mr_Piranha> if(*s)
<Mr_Piranha> {
<Mr_Piranha> while(1)
<Mr_Piranha> {
<Mr_Piranha> if(*s) s++;
<Mr_Piranha> else
<Mr_Piranha> break;
<Mr_Piranha> }
<Mr_Piranha>
<Mr_Piranha> }
<Mr_Piranha> return (int)(s-o);
<Mr_Piranha> }
<Mr_Piranha> heh heh
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 8:33 pm
by piranha
Troy Martin wrote:Code: Select all
<Mr_Piranha> int strlen(char *s)
<Mr_Piranha> {
<Mr_Piranha> char *o;
<Mr_Piranha> o=s;
<Mr_Piranha> if(*s)
<Mr_Piranha> {
<Mr_Piranha> while(1)
<Mr_Piranha> {
<Mr_Piranha> if(*s) s++;
<Mr_Piranha> else
<Mr_Piranha> break;
<Mr_Piranha> }
<Mr_Piranha>
<Mr_Piranha> }
<Mr_Piranha> return (int)(s-o);
<Mr_Piranha> }
<Mr_Piranha> heh heh
Oh, yes. I have skills.
-JL
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 8:47 pm
by Troy Martin
On-topic: is there an x86 assembly instruction for incrementing CX instead of decrementing with a rep prefix?
Re: i am not sure if anyone is intrested
Posted: Sun Mar 22, 2009 9:16 pm
by Wilkie
Ahhh... I see what you want to do there. It is actually the way it is typically done. Note, that a REP will ONLY decrement CX... but that's not a bad thing since you can exploit the fact that it merely compares CX with 0 and you can initialize to a negative number.
SCASB is your friend, here. Set CX to -1 (which unsigned is very large) and compare a string with the byte 0. Use REPNE and CX will be decremented so that it is 2 off of the actual string length when it finally finds the NULL. (note: we are not counting the null itself or the fact we started at -1; that makes 2 off)
Note that for negative values, the positive component is found by:
so...
Code: Select all
STRLEN = ~VALUE + 1 - 2 = ~VALUE - 1
so... you just use REPNE SCASB and then NOT CX and then DEC CX. CX is the string length.
For more details...
Re: i am not sure if anyone is intrested
Posted: Mon Mar 23, 2009 8:46 am
by Solar
No license statement in the code. This makes it effectively unusable from a strictly legal standpoint.
strcmp() and strncmp() make completely unnecessary additions...?!?
strcat() does call strlen() on the source string once for every character copied.
The strupr() / strlwr() functions couldn't have been implemented any worse way I could think of. (Nested loops containing a conditional?
)
No "const" or comment in sight.
Sizes given in int (should be size_t, i.e. unsigned int usually).
We're not in school here, but that code won't get you through class. This has nothing to do with bragging, or "competitive bashing", and I am not perfect either, but you might want to have a look at the
PDCLib string functions for comparison...