i am not sure if anyone is intrested

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
smeezekitty
Member
Member
Posts: 50
Joined: Sat Mar 21, 2009 9:42 pm

i am not sure if anyone is intrested

Post by smeezekitty »

i have some kernel compatable libarys
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
smeezekitty
Member
Member
Posts: 50
Joined: Sat Mar 21, 2009 9:42 pm

Re: i am not sure if anyone is intrested

Post by smeezekitty »

malloc libary:
Attachments
MAL.C
malloc libary source
(164 Bytes) Downloaded 173 times
User avatar
JackScott
Member
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

Post by JackScott »

I haven't had more than a quick look through the source code, but I have a few recommendations:
  1. 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.
  2. 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.
  3. 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?
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: i am not sure if anyone is intrested

Post 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.
User avatar
Troy Martin
Member
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

Post by Troy Martin »

They appear to work at a glance of the code, but they're extremely simple.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
steveklabnik
Member
Member
Posts: 72
Joined: Wed Jan 28, 2009 4:30 pm

Re: i am not sure if anyone is intrested

Post 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.
User avatar
Wilkie
Member
Member
Posts: 44
Joined: Tue Aug 26, 2008 10:02 pm
Location: Land of the Dead
Contact:

Re: i am not sure if anyone is intrested

Post 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! :)
User avatar
Troy Martin
Member
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

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Wilkie
Member
Member
Posts: 44
Joined: Tue Aug 26, 2008 10:02 pm
Location: Land of the Dead
Contact:

Re: i am not sure if anyone is intrested

Post 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 :)
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.
User avatar
Troy Martin
Member
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

Post 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
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
piranha
Member
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

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Troy Martin
Member
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

Post by Troy Martin »

On-topic: is there an x86 assembly instruction for incrementing CX instead of decrementing with a rep prefix?
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Wilkie
Member
Member
Posts: 44
Joined: Tue Aug 26, 2008 10:02 pm
Location: Land of the Dead
Contact:

Re: i am not sure if anyone is intrested

Post 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:

Code: Select all

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

Re: i am not sure if anyone is intrested

Post 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. :shock:

The strupr() / strlwr() functions couldn't have been implemented any worse way I could think of. (Nested loops containing a conditional? :shock: )

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.
Post Reply