Beispiel #1
0
static void
buildmap(Biobuf *fp)	/* map goes from char name to value to print via *string() */
{
    uchar *p, *line, ch[100];
    int val;
    Rune r;

    curmap++;
    if(curmap >= NMAP) {
        fprint(2, "proof: out of char maps; recompile\n");
        exits("charmap");
    }
    while ((line = Brdline(fp, '\n'))!= 0) {
        if (line[0] == '\n')
            return;
        line[Blinelen(fp)-1] = 0;
        scanstr((char *) line, (char *) ch, (char **) &p);
        if (ch[0] == '\0') {
            fprint(2, "bad map file line '%s'\n", (char*)line);
            continue;
        }
        val = strtol((char *) p, 0, 10);
        dprint(2, "buildmap %s (%x %x) %s %d\n", (char*)ch, ch[0], ch[1], (char*)p, val);
        chartorune(&r, (char*)ch);
        if(utflen((char*)ch)==1 && r<QUICK)
            charmap[curmap].quick[r] = val;
        else
            addmap(curmap, strdup((char *) ch), val);	/* put somewhere else */
    }
}
Beispiel #2
0
void *malloc(int64_t size)
{
    void *retval = NULL;
    
    if(size <= 64)
    {
        if(pool_64b_firstfree == NULL)
        {
            if(addmap(phymem_get_page(), POOL_64B + pool_64b_top , MM_READWRITE) == 0)
            {
                return NULL;
            }
            for(int i = (4096/64)-1; i > 0; i--)
            {
                *(uint64_t*)(POOL_64B + pool_64b_top + (i * 64)) = pool_64b_firstfree;
                pool_64b_firstfree = POOL_64B + pool_64b_top + (i * 64);
            }
            retval = (void*)(POOL_64B + pool_64b_top);
            pool_64b_top += 4096;
        }
        else
        {
            retval = (void*)pool_64b_firstfree;
            // The first free should now be the next element in the linked list
            pool_64b_firstfree = *(uint64_t*)pool_64b_firstfree;
        }
    }
    else if(size <= 512)
    {
        if(pool_512b_firstfree == NULL)
        {
            
            if(addmap(phymem_get_page(), POOL_512B + pool_512b_top , MM_READWRITE) == 0)
            {
                return NULL;
            }
            
            for(int i = (4096/512)-1; i > 0; i--)
            {
                *(uint64_t*)(POOL_512B + pool_512b_top + (i * 512)) = pool_512b_firstfree;
                pool_512b_firstfree = POOL_512B + pool_512b_top + (i * 512);
            }
            
            retval = (void*)(POOL_512B + pool_512b_top);
            
            pool_512b_top += 4096;
        }
        else
        {
            retval = (void*)pool_512b_firstfree;
            // The first free should now be the next element in the linked list
            pool_512b_firstfree = *(uint64_t*)pool_512b_firstfree;
        }
    }
    // If we're bigger than 512b well allocate a full 4k page we 
    // don't check a linked list we just allocate the space and 
    // adjust the top address
    else if(size <= 4 * 1024)
    {
        if(addmap(phymem_get_page(), POOL_4K + pool_4k_top , MM_READWRITE) == 0)
        {
            return NULL;
        }
        retval = (void*)(POOL_4K + pool_4k_top);
        pool_4k_top += 4 * 1024;
        
    }
    else if(size <= 64 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < 64/4; i++)
        {
            uint64_t pagetemp = phymem_get_page();
            if(addmap(pagetemp, POOL_64K + pool_64k_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_64K + pool_64k_top + (i * 4096) ));
                    removemap( POOL_64K + pool_64k_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_64K + pool_64k_top);
        pool_64k_top += 64 * 1024;
    }
    else if(size <= 1 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (1*1024)/4; i++)
        {
            if(addmap(phymem_get_page(), POOL_1M + pool_1M_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_1M + pool_1M_top + (i * 4096) ));
                    removemap( POOL_1M + pool_1M_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_1M + pool_1M_top);
        pool_1M_top += (1*1024) * 1024;
    }
    else if(size <= 16 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (16*1024)/4; i++)
        {
            uint64_t temppage = phymem_get_page();
            //dprintf("%x\n",temppage);
            if(addmap(temppage, POOL_16M + pool_16M_top + (i * 4096), MM_READWRITE) == 0)
            {
                
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_16M + pool_16M_top + (i * 4096) ));
                    removemap( POOL_16M + pool_16M_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_16M + pool_16M_top);
        pool_16M_top += (16*1024) * 1024;
    }
    else if(size <= 64 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (64*1024)/4; i++)
        {
            if(addmap(phymem_get_page(), POOL_64M + pool_64M_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_64M + pool_64M_top + (i * 4096) ));
                    removemap( POOL_64M + pool_64M_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_64M + pool_64M_top);
        pool_64M_top += (64*1024) * 1024;
    }
    else if(size <= 128 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (128*1024)/4; i++)
        {
            if(addmap(phymem_get_page(), POOL_128M + pool_128M_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_128M + pool_128M_top + (i * 4096) ));
                    removemap( POOL_128M + pool_128M_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_128M + pool_128M_top);
        pool_128M_top += (128*1024) * 1024;
    }
    else if(size <= 512 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (512*1024)/4; i++)
        {
            if(addmap(phymem_get_page(), POOL_512M + pool_512M_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_512M + pool_512M_top + (i * 4096) ));
                    removemap( POOL_512M + pool_512M_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_512M + pool_512M_top);
        pool_512M_top += (512*1024) * 1024;
    }
    else if(size <= 1 * 1024 * 1024 * 1024)
    {
        // Map enough pages for the region
        for(int i = 0; i < (1024*1024)/4; i++)
        {
            if(addmap(phymem_get_page(), POOL_1G + pool_1G_top + (i * 4096), MM_READWRITE) == 0)
            {
                // If we can't map then free up the pages we succedded in mapping
                i--;
                for(;i > 0;i--)
                {
                    phymem_mark_free( getmap( POOL_1G + pool_1G_top + (i * 4096) ));
                    removemap( POOL_1G + pool_1G_top + (i * 4096) );
                }
                //  Then return a null pointer
                return NULL;
            }
        }
        retval = (void*)(POOL_1G + pool_1G_top);
        pool_1G_top += (1024*1024) * 1024;
    }
    return retval;
}