diff --git a/lib/clog2.c b/lib/clog2.c new file mode 100644 index 0000000000..0198dd610a --- /dev/null +++ b/lib/clog2.c @@ -0,0 +1,39 @@ +/* + * clog2 -- get a log2 in C. + * + * + * Author(s) unknown, but this classic code was found by + * Ronald G. Minnich somewhere. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + * + */ + +/* Assume 8 bits per byte */ +#define CHAR_BIT 8 + +unsigned long log2(unsigned long x) +{ + unsigned long i = 1ULL << (sizeof(x)* CHAR_BIT - 1ULL); + unsigned long pow = sizeof(x) * CHAR_BIT - 1ULL; + + if (! x) { + return -1; + } + for(; i > x; i >>= 1, pow--) + ; + + return pow; +} diff --git a/lib/lar.c b/lib/lar.c index 6159310c29..5d77bb9583 100644 --- a/lib/lar.c +++ b/lib/lar.c @@ -44,7 +44,7 @@ printk(BIOS_INFO, "start 0x%x len 0x%x\n", archive->start, archive->len); for (walk = archive->start; walk < (char *)archive->start + archive->len; walk+=16) { printk(BIOS_INFO, "in for loop *start is %x\n", *(unsigned long *) archive->start); -printk(BIOS_INFO, "walk is %s\n", walk); +printk(BIOS_INFO, "walk is %s(%p)\n", walk, walk); if(strcmp(walk, MAGIC)!=0) continue; @@ -60,8 +60,11 @@ printk(BIOS_INFO, "fullname is %s\n", fullname); } // skip file +/* FIXME: This is doing the wrong thing. its skipping too much. walk += ( ntohl(header->offset) + ntohl(header->len) + 15 ) & 0xfffffff0; + */ + walk += 16; } printk(BIOS_INFO, "return 1! walk %p archive->start %p start _+ len %p\n", walk, archive->start, (char *)archive->start + archive->len); diff --git a/lib/malloc.c b/lib/malloc.c new file mode 100644 index 0000000000..bb861afc5a --- /dev/null +++ b/lib/malloc.c @@ -0,0 +1,81 @@ +/* + * malloc -- simple non-freeing malloc. + * + * + * Author unkown. I guess we need a 'tomb of the unknown coder'. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + * + */ +#include +#include + +#if 0 +#define MALLOCDBG(x) +#else +#define MALLOCDBG(x) printk x +#endif + +/* instead of ldscript magic, just declare an array. The array + * will consume no bytes in the image. And it won't run into trouble + * the way the V2 allocator could. + */ + +#define HEAPSIZE (256*1024) +static unsigned char heap[HEAPSIZE]; +static size_t free_mem_ptr = (size_t)&heap; /* Start of heap */ +static size_t free_mem_end_ptr = (size_t)&heap[HEAPSIZE]; +/* to keep gcc etc. happy ... */ +typedef size_t malloc_mark_t; + +void malloc_mark(malloc_mark_t *place) +{ + *place = free_mem_ptr; + printk(BIOS_SPEW, "malloc_mark 0x%08lx\n", (unsigned long)free_mem_ptr); +} + +void malloc_release(malloc_mark_t *ptr) +{ + free_mem_ptr = *ptr; + printk(BIOS_SPEW, "malloc_release 0x%08lx\n", (unsigned long)free_mem_ptr); +} + +void *malloc(size_t size) +{ + void *p; + + MALLOCDBG(("%s Enter, size %d, free_mem_ptr %p\n", __FUNCTION__, size, free_mem_ptr)); + if (size < 0) + die("Error! malloc: Size < 0"); + if (free_mem_ptr <= 0) + die("Error! malloc: Free_mem_ptr <= 0"); + + free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */ + + p = (void *) free_mem_ptr; + free_mem_ptr += size; + + if (free_mem_ptr >= free_mem_end_ptr) + die("Error! malloc: free_mem_ptr >= free_mem_end_ptr"); + + MALLOCDBG(("malloc 0x%08lx\n", (unsigned long)p)); + + return p; +} + +void free(void *where) +{ + /* Don't care */ +} diff --git a/lib/mem.c b/lib/mem.c new file mode 100644 index 0000000000..9bf4d90286 --- /dev/null +++ b/lib/mem.c @@ -0,0 +1,38 @@ +/* + * mem -- simple routines that have no optimizations for anything + * + * + * Copright (C) 2007 Ronald G. Minnich + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + * + */ + +/* this one is pretty stupid. Won't handle overlaps, it's not efficient, etc. */ +void memcpy(void *dest, const void *src, int len) +{ + unsigned char *d = dest; + const unsigned char *s = src; + while (len--) + *d++ = *s++; +} + +void memset(void *v, unsigned char a, int len) +{ + unsigned char *cp = v; + while(len--) + *cp++ = a; +} +