=================================================================== RCS file: /cvs/funnyos/kern/kern_mem.c,v retrieving revision 1.1 retrieving revision 1.7 diff -u -r1.1 -r1.7 --- funnyos/kern/kern_mem.c 2007/10/16 09:41:04 1.1 +++ funnyos/kern/kern_mem.c 2007/10/26 14:15:20 1.7 @@ -1,11 +1,11 @@ /* - * $Id: kern_mem.c,v 1.1 2007/10/16 08:41:04 init Exp $ + * $Id: kern_mem.c,v 1.7 2007/10/26 13:15:20 init Exp $ */ #include #include #include -#define KMEM_DEBUG +/* #define KMEM_DEBUG */ #ifdef KMEM_DEBUG #define DPRINTF(x...) do { printf(x); } while (0) @@ -125,7 +125,7 @@ DPRINTF("kmalloc: requested %d bytes (%d pages) of %d free pages; ", nbytes, npages, freepages); /* check if we have enough free pages */ - if (npages >= freepages) { + if (npages > freepages) { DPRINTF("not enough free pages\n"); return(NULL); } @@ -154,25 +154,26 @@ /* calculate pointer to this (last) page */ lastpageptr = (uint32_t *)((allocbase + (8 * nmbmbytes * KMEM_PAGESIZE)) + (KMEM_PAGESIZE * nmbmbits)); - DPRINTF("lastpageptr=0x%x\n", lastpageptr); /* rewind pointer to the first byte */ firstpageptr = lastpageptr - ((npages - 1)); - DPRINTF("firstpageptr=0x%x\n", firstpageptr); - offbits = mask ? mask - 1 : mask; /* XXX */ /* fill this (npages pages) region with zeroes */ while(firstpageptr <= lastpageptr) { /* TODO invalidate page in membmap */ offbytes = ((uint32_t)firstpageptr - (uint32_t)allocbase) / (KMEM_PAGESIZE * 8); -// printf("offbytes=%d\n", offbytes); + offbits = ((uint32_t)firstpageptr - (uint32_t)allocbase) / (KMEM_PAGESIZE); + + if (offbits > 7) + offbits -= 8 * (offbits / 8); + // printf("membmap[%d] |= 1 << %d\n", offbytes, offbits); membmap[offbytes] |= 1 << offbits; /* zerofy page */ for (i = 0; i < KMEM_PAGESIZE; i++) { - *(uint8_t *)firstpageptr = 0xaa; + *(uint8_t *)firstpageptr = KMEM_FILLBYTE; ((uint8_t *)firstpageptr)++; } /* TODO rework offbits calculation */ @@ -186,7 +187,7 @@ firstpageptr = lastpageptr - ((npages - 1)); /* got it */ - DPRINTF("allocated, addr=0x%x\n", firstpageptr); + DPRINTF("allocated, addr=0x%x len=%d [0x%x-0x%x]\n", firstpageptr, npages * KMEM_PAGESIZE, firstpageptr, lastpageptr); /* decrement system free pages */ freepages -= npages;