Main Page | Modules | File List | File Members

sbrk.c

00001 
00013 #define ENOMEM (-1)
00014 
00015 void *sbrk(signed int increment);
00016 
00017 // start off at 4MB, for now
00018 //const long initialBrk = 0x00300000;
00019 const long initialBrk = 0x00400000;
00020 long brk = initialBrk;
00021 long availableInPage = 0x0;
00022 
00023 // TEMPORARY!!!
00024 //long availableMemory = 0xffffffff;
00025 long availableMemory  = 0x00A00000;
00026 
00027 void *sbrk(signed int increment)
00028 {
00029         void *allocBase;
00030         void *pageAddr;
00031         //              void *linearAddr = allocBase;
00032         long linearAddr, physAddr;
00033 
00034 
00035         //console() << "sbrk(" << (long)increment << ");\n";
00036 
00037         if(increment > 0)
00038         {
00039                 // allocate more memory, on top of brk...
00040                 allocBase = (void *)brk;
00041                 //console() << "inc > 0, inPage " << availableInPage << "\t";
00042 
00043                 if((unsigned)availableMemory >= (unsigned)increment)
00044                 {
00045                         // increase brk -- not yet!
00046                         brk += increment;
00047 
00048                         // map more pages, if need be
00049                         if(availableInPage > increment)
00050                         {
00051                                 //console() << "Alloc less then a page\t";
00052                                 availableInPage -= increment;
00053                         }
00054                         else
00055                         {
00056                                 /*
00057                                 void *pageAddr;
00058                                 void *linearAddr = allocBase;
00059                                 */
00060                                 linearAddr = (long)allocBase;
00061 
00062                                 //console() << "Alloc more then a page\t";
00063 
00064                                 // use up the rest of the top page
00065                                 increment -= availableInPage;
00066 
00067                                 // and figure out how much we'll have left over...
00068                                 availableInPage = PAGE_SIZE - (increment & PAGE_SIZE_MASK);
00069                                 if(availableInPage == PAGE_SIZE) availableInPage = 0;
00070 
00071                                 // finally, perform all the necessary page mappings
00072                                 while(increment > 0)
00073                                 {
00074                                         // map a new page here
00075                                         //console() << "1";
00076                                         pageAddr = pages().AllocPage();
00077                                         //console() << "2";
00078                                         pager().MapToLinear(pageAddr, (void *)linearAddr);
00079                                         //console() << "3";
00080 
00081                                         //linearAddr = (void *)( (long)linearAddr + PAGE_SIZE );
00082                                         linearAddr += PAGE_SIZE;
00083 
00084                                         increment -= PAGE_SIZE;
00085                                 }
00086                         }
00087                         return allocBase;
00088                 }
00089                 else return (void *)(-2);
00090         }
00091         else if(increment < 0)
00092         {
00093                 // lets make this more intuitive here... subtracting negatives confused
00094                 // people :)
00095                 increment = -increment;
00096 
00097                 if( (brk - increment) >= initialBrk)
00098                 {
00099                         // deallocate memory from top of heap...
00100                         //brk -= increment;
00101 
00102                         if( (PAGE_SIZE - availableInPage) > increment)
00103                         {
00104                                 availableInPage -= increment;
00105                                 brk -= increment;
00106                         }
00107                         else
00108                         {
00109                                 linearAddr = brk - (PAGE_SIZE - availableInPage);
00110                                 brk -= increment;
00111 
00112                                 while(increment >= PAGE_SIZE)
00113                                 {
00114                                         // unmap top page
00115                                         //linearAddr = brk & (!PAGE_SIZE);
00116                                         physAddr = (long)pager().LinearToPhysical((void *)linearAddr);
00117 
00118                                         pager().UnMap((void *)linearAddr);
00119                                         pages().FreePage((void *)physAddr);
00120 
00121                                         increment -= PAGE_SIZE;
00122                                         linearAddr -= PAGE_SIZE;
00123                                 }
00124 
00125                                 // adjust the ammount left in the top page
00126                                 availableInPage = PAGE_SIZE - increment;
00127                                 if(availableInPage == PAGE_SIZE) availableInPage = 0;
00128                         }
00129                 }
00130                 else return (void *)(-3);
00131         }
00132 
00133         return (void *)brk;
00134 }

Generated on Sun Nov 21 18:26:11 2004 for ndk by doxygen 1.3.2