OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 1:55 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Most reused/versatile code
PostPosted: Sun Feb 22, 2015 3:18 pm 
Offline
Member
Member

Joined: Thu May 06, 2010 4:34 am
Posts: 116
Location: Leiden, The Netherlands
What piece of code that you have written did you reuse most often?

For me that would be my (doubly) linked list implementation, which I wrote for my previous kernel and I have since used it for:
  • My current kernel
  • Dutch computer science olympiad assignments
  • My OS's GUI library and server
  • A VT100 emulator firmware for a very old terminal (Z80 based)
  • The software for a high-altitude balloon tracker ( linux on arm )
  • Lots of small projects
I wrote it in such a way that I could use it in all of these projects without any modifications

_________________
posnk ( a simple unix clone )
twitter profile - security research, die shots and IC reverse engineering, low level stuff


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Sun Feb 22, 2015 6:26 pm 
Offline
Member
Member
User avatar

Joined: Wed Aug 21, 2013 3:53 am
Posts: 449
Location: Asia, Singapore
This:
Code:
/** NOTE: This function trashes the current position in file **/
/** Does no error checking either **/
long int GetFileSize(FILE* FilePointer)  { 
      fseek(FilePointer, 0L, SEEK_END);
      long int ReturnVal = ftell(FilePointer);
      fseek(FilePointer, 0L, SEEK_SET);
      return ReturnVal;
}


(Everywhere?)

_________________
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Sun Feb 22, 2015 7:12 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
Spaces and semicolons probably beat everything else.


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 12:14 am 
Offline
Member
Member

Joined: Wed Oct 30, 2013 1:57 pm
Posts: 306
Location: Germany
Newlines. They still count as code and they beat semicolons (comments and blank lines ...) :D

Jokes aside, probably the code proposed by Bender is the one that I've used the most, too. Did anyone propose it to the guys who maintain glibc?

_________________
managarm


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 1:06 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
I doubt it. After all, a single system call will give you the file size, and a lot of other information about a file, without trashing the position of the file pointer.

See also: https://www.securecoding.cert.org/confl ... w/42729539


Last edited by iansjack on Mon Feb 23, 2015 1:29 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 1:09 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
data structures and algorithms should be reused most, due to its nature and pattern of an application.
Other than that, it's debug/logging routines.


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 1:58 am 
Offline
Member
Member

Joined: Thu Jul 05, 2012 5:12 am
Posts: 923
Location: Finland
This is not exactly on-topic but I have reimplemented "print decimal numbers" in assembly many times and it is always painful. My latest implementation printed numbers like this:

Code:
                         "0"
                      "1234"    special case!
                    "12 123"
                   "123 123"
                 "1 123 123"
            "12 123 123 123"
"18 446 744 073 709 551 615"


And this is only for unsigned integers (from 8 to 64 bits) but still...

_________________
Undefined behavior since 2012


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 2:34 am 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
Oh really? Programming non-trivial things in assembly is painful? If we only had been told before!

_________________
Developer of tyndur - community OS of Lowlevel (German)


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 3:49 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
iansjack wrote:
I doubt it. After all, a single system call will give you the file size, and a lot of other information about a file, without trashing the position of the file pointer.

See also: https://www.securecoding.cert.org/confl ... w/42729539
I don't agree that "do this with posix" is a correct answer, because that would actually make the code less portable. The fact that posix ignores the "b" mode means that all files have all the properties of both text and binary files and therefore must support SEEK_END and character-accurate reporting. In addition, "need not support" does mean that fseek-ftell pairs work de-facto on other platforms - most importantly of which is windows, even though it might pose an implementation risk on operating systems you won't practically encounter.

In other words. KISS wins.

_________________
"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 ]


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 4:04 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Combuster wrote:
In other words. KISS wins.

I quite agree. Most platforms provide a single API call to determine file size and this is the one to use (KISS). There is no guaranteed portable way to determine file size.

I'd also comment that resusable code most emphatically should include error checking, preferably wouldn't have side effects, and you would hope it would work with files larger than 2GB :).


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 4:38 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
iansjack wrote:
There is no guaranteed portable way to determine file size.


Unless your fgetc()/fread() returns/sets EOF on a binary file later than the actual end of file is reached and unless it's not a real binary file, there certainly is.


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 5:14 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
alexfru wrote:
iansjack wrote:
There is no guaranteed portable way to determine file size.

Unless....

Exactly.

I'm well aware that with a real file you can just read all the bytes until the end of the file. I'd rather stick needles in my eye.

Let's face it, if the OS provides an API to obtain the files size it's crazy not to use it.


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 5:33 am 
Offline
Member
Member

Joined: Thu Jul 05, 2012 5:12 am
Posts: 923
Location: Finland
iansjack wrote:
Let's face it, if the OS provides an API to obtain the files size it's crazy not to use it.


Are you serious? I heard that writing portable code might be important for projects written in C. If we broke compatibility, it would be reasonable to do it thoroughly. It is quite lame to do it on such a small scale.

_________________
Undefined behavior since 2012


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Mon Feb 23, 2015 5:39 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
iansjack wrote:
alexfru wrote:
iansjack wrote:
There is no guaranteed portable way to determine file size.

Unless....

Exactly.

I'm well aware that with a real file you can just read all the bytes until the end of the file. I'd rather stick needles in my eye.


fseek() to positions 0, 1, 2, 4, 8, etc, fgetc(), check for errors. You get the top bit of the size. Similarly you get the rest.


Top
 Profile  
 
 Post subject: Re: Most reused/versatile code
PostPosted: Tue Feb 24, 2015 3:43 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
I think for me the thing I find the least in existing libraries is having an idmap. A class that maps instances to IDs. You use it for any kind of hard interface where you want to give out handles and validate them when they come back - kernel boundary, remote calls, ... Biggest property is that they are usually small and that they are not reused or moved until freed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group