i am not sure if anyone is intrested
-
- Member
- Posts: 50
- Joined: Sat Mar 21, 2009 9:42 pm
i am not sure if anyone is intrested
i have some kernel compatable libarys
a string libary source and header i wrote myself
and a malloc libary and header
a string libary source and header i wrote myself
and a malloc libary and header
- Attachments
-
- STRLIB.CPP
- string libary source
- (1.24 KiB) Downloaded 130 times
-
- STRLIB.H
- string header file
- (321 Bytes) Downloaded 91 times
-
- MAL.H
- malloc header file
- (77 Bytes) Downloaded 150 times
-
- Member
- Posts: 50
- Joined: Sat Mar 21, 2009 9:42 pm
Re: i am not sure if anyone is intrested
malloc libary:
- Attachments
-
- MAL.C
- malloc libary source
- (164 Bytes) Downloaded 173 times
- JackScott
- Member
- Posts: 1035
- Joined: Thu Dec 21, 2006 3:03 am
- Location: Hobart, Australia
- Mastodon: https://aus.social/@jackscottau
- Matrix: @JackScottAU:matrix.org
- GitHub: https://github.com/JackScottAU
- Contact:
Re: i am not sure if anyone is intrested
I haven't had more than a quick look through the source code, but I have a few recommendations:
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?
- 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.
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
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.
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: i am not sure if anyone is intrested
They appear to work at a glance of the code, but they're extremely simple.
- steveklabnik
- Member
- Posts: 72
- Joined: Wed Jan 28, 2009 4:30 pm
Re: i am not sure if anyone is intrested
Or, you know.Make it work.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.
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
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;
}
}
Code: Select all
int address = 0x00F;
void *m_malloc(int size)
{
return ((void*)address += size);
}
void m_free(void *p)
{
}
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.
Shame on you!Troy Martin wrote:They appear to work at a glance of the code, but they're extremely simple.
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: i am not sure if anyone is intrested
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.
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
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
EDIT: exception to the strncpy, which I fixed. I don't know why I care to find this fun
- Attachments
-
- string.c
- WTFPL String Functions For Great Good
- (1.27 KiB) Downloaded 141 times
Last edited by Wilkie on Sun Mar 22, 2009 8:45 pm, edited 1 time in total.
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: i am not sure if anyone is intrested
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
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: i am not sure if anyone is intrested
Oh, yes. I have skills.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
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: i am not sure if anyone is intrested
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
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...
so... you just use REPNE SCASB and then NOT CX and then DEC CX. CX is the string length.
For more details...
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:
Code: Select all
VALUE = ~VALUE + 1
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
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...
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...
Every good solution is obvious once you've found it.