Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Apr 2014, 17:14
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
unsorted C macros for small/fast static apps
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 2 [26 Posts]   Goto page: Previous 1, 2
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Thu 06 Dec 2012, 00:33    Post subject:  

just need to figure out how to deal with blah-0041.2 and blah-041.1
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Ibidem

Joined: 25 May 2010
Posts: 430
Location: State of Jefferson

PostPosted: Thu 06 Dec 2012, 19:24    Post subject:  

technosaurus wrote:
just need to figure out how to deal with blah-0041.2 and blah-041.1

If you look in the manpage, that isn't ambiguous: numbers starting with 0 are considered to have an implied decimal point before the first 0, so the more leading zeros, the smaller.
Since it diverges at 00 vs 04, anything after the decimal point is ignored.

But there is another bug in the version I just posted:
compare 2004 204, and my code assumes it's dealing with a leading 0 Embarassed
I have an idea for fixing it, but haven't done so yet.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Wed 30 Jan 2013, 03:58    Post subject:  

simple qsort example, takes all arguments and prints sorted tab separated list
Code:
#include <string.h>

static inline int cmp(const void *a, const void *b){
   return strcmp(*(const char **)a, *(const char **)b);
}

int main(int argc, char *argv[]){
    qsort(++argv, --argc, sizeof(char *), cmp);
    while (argc){
      write(1,argv[0],strlen(argv[0]));
      write(1,(--argc && argv++)?"\t":"\n",1);
   }
}

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Thu 07 Feb 2013, 00:07    Post subject:  

a faster (but bigger) strlen -needs work (misses \0 on multiples of Cool

Code:
size_t strlen(char *s){
size_t *v,ret;
v=(size_t  *)s;
while (!(~((((*v & 0x7F7F7F7F) + 0x7F7F7F7F) | *v) | 0x7F7F7F7F))) v++; /*64bit size_t?*/
ret=(char *)v-s;
while (s[++ret]);
return ret;
}


notes: need to use an unsigned int or 0 is 111111111111.....
maybe use:
((*v>>24)&&(*v&0xFF00FFFF))&&((*v&0xFFFF00FF)&&(*v<<24))
because the compiler can combine some of these operations and/or thread them

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Sat 16 Feb 2013, 18:04    Post subject:  

here is a one-liner to generate macros for 300+ syscalls
Code:
grep __NR_ unistd_32.h |awk '
{print "#define " substr($2,6,length($2)) "(...) syscall(" $2 ", __VA_ARGS__ )"}
' | sort > syscalls.h

and now you have a 0kb c "library"

Note: zero-arg functions like fork() may need to be modified on older compilers and all of the types (structs, etc...) will need to be defined and there is absolutely no type checking. If you would like some semblance of type checking, you can later modify the defines to be static inline functions like:

Code:
static inline long mycall(int a, char *b, struct somestruct c){
return syscall(__NR_mycall, a, b, c);
}


There are quite a few other functions in the kernel that would be useful if you wanted to wrap them in a syscall for userspace (all of the crypto stuff and filesystem detection for instance)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Sun 17 Feb 2013, 23:20    Post subject:  

still working on syscalls. Here is a wacked out macro that redefines syscall() based on the number of args.
Code:
#define NARGS(...) NARGS_(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)(__VA_ARGS__)
#define NARGS_(dummy, n1, n2, n3, n4, n5, n6, n, ...) syscall##n
#define syscall(...) NARGS(__VA_ARGS__)

thus syscall(__NR_mycall) becomes syscall0(mycall)
while syscall(__NR_mycall,a,b,c,d,e,f) becomes syscall6(mycall,a,b,c,d,e,f)

... and you thought you needed c++ for this Smile

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Mon 04 Mar 2013, 01:27    Post subject:  

here is a good link to some try-catch macros
http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Sat 27 Jul 2013, 13:14    Post subject:  

I now have the beginnings of my nano-libc that can be used as a single header file and have added many mime types to the "file --mime-type alternative"

The libc should work with any gcc toolchain and handles all linux syscalls (stat is the only one with the structs predefined though) as well as a couple of string functions and a basic {f}printf - I recommend using musl libc to add any extra functions you may need.

A statically compiled program that uses printf is just over 1kb (other libc implementations are at least 7kb) and just a basic write(1,"hello world\n",12); is ~600b (without using sstrip, - that brings it to under half a kb)

ftype.c has also been tested with it and runs 60-120 times faster than `file --mime-type`
ftype.c.gz
Description 
gz

 Download 
Filename  ftype.c.gz 
Filesize  10.13 KB 
Downloaded  119 Time(s) 
libc.h.gz
Description 
gz

 Download 
Filename  libc.h.gz 
Filesize  5.14 KB 
Downloaded  123 Time(s) 

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
goingnuts

Joined: 07 Dec 2008
Posts: 777

PostPosted: Mon 29 Jul 2013, 00:18    Post subject:    

Cool!
Is it possible to use the libc.h directly as #include "libc.h" or is it better to copy the different functions into the main code?
I tried to substitute the codeblock in ftype.c within #ifdef STANDALONE with #include "libc.h" but got some errors compiling:
Code:
libc.h:554: error: `__NR_write' undeclared
ftype.c:910: error: `__NR_stat' undeclared (first use in this function)

Compiling ftype.c unmodified
Code:
 [diet]  gcc -nostdlib -nostdinc -fno-builtin -Os -fno-asynchronous-unwind-tables -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections,-s ftype.c -o ftype

with diet libc I get errors:
Code:
ftype.c:(.text+0x0): multiple definition of `_start'
/usr/dietlibc/lib-i386/start.o:(.text+0x0): first defined here
/usr/dietlibc/lib-i386/libc.a(atexit.o): In function `exit':
atexit.c:(.text+0x24): multiple definition of `exit'
but with uclibc & glibc no problems.
Back to top
View user's profile Send private message Visit poster's website 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 30 Jul 2013, 00:35    Post subject:  

I leave off nostdinc so that it picks up the NR* syscall defines from the linux headers ... See unistd.h (the only include in libc.h) they could be included directly, but I am leaving platform specific stuff out as much as possible for future arm stuff. My latest version of ftype only uses libc.h instead of builtin standalone code. I think Its currently a good starting point though to bootstrap whatever additional functions you may need for a single purpose app. At least printf doesnt keep your app from running on the stack like most implementations do... at least for now since only the most common bits are implemented (va_* was the hard part, but it can be used for other fxns that need variable# of args)

When you build with the parameters in the libc.h header it should work the same for any toolchain ... Otherwise it is sucking in includes from other than just the linux headers, but I havent found a good way to get 1 without the other.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 06 Aug 2013, 21:54    Post subject:  

Its a bit disorganized but I added a ton of stuff to test
Edit: ok, really disorganized, I will do a bunch of cleanup and testing before the next version and try to complete the string functions and will be in a new thread.

The primary purpose is to allow for smaller overheads in static binaries so that multicall binaries are not needed so much. To finish this out, I need to make a tool chain that symlinks all of the stdinc files to libc.h and wraps cc with nostdlib nostdinc and fnobuiltin and some optimizations.

If anyone uses it and runs into missing structs or defined constants, please post them, (figuring out the basic types used in structs can take a lot of time) which also reminds me, I should define these to the basic types as I find them (rather than typedeffing them) ... Of course that means you should do development against a "real" c library first for sanity checks (any noted differences will help). Currently only targeting x86, but will consider basic arm support (64 bit versions wouldnt make sense, but feel free to fork )

note, linux 3.10 is lts so it will be the basis, thus sycalls may not be available in older kernels... most static binaries will work unless you try to use a newer syscall like finit_module on an older kernel
libc.h-0.1.gz
Description 
gz

 Download 
Filename  libc.h-0.1.gz 
Filesize  12.4 KB 
Downloaded  104 Time(s) 

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 2 [26 Posts]   Goto page: Previous 1, 2
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0731s ][ Queries: 13 (0.0056s) ][ GZIP on ]