long allocate(short type) throws IOException {
        
        if (type == Magic.FREE_PAGE)
            throw new Error("allocate of free page?");
        
        // do we have something on the free list?
        long retval = header.getFirstOf(Magic.FREE_PAGE);
        boolean isNew = false;
        if (retval != 0) {
            // yes. Point to it and make the next of that page the
            // new first free page.
            header.setFirstOf(Magic.FREE_PAGE, getNext(retval));
        }
        else {
            // nope. make a new record
            retval = header.getLastOf(Magic.FREE_PAGE);
            if (retval == 0)
                // very new file - allocate record #1
                retval = 1;
            header.setLastOf(Magic.FREE_PAGE, retval + 1);
            isNew = true;
        }
        
        // Cool. We have a record, add it to the correct list
        BlockIo buf = file.get(retval);
        PageHeader pageHdr = isNew ? new PageHeader(buf, type) 
            : PageHeader.getView(buf);
        long oldLast = header.getLastOf(type);
        
        // Clean data.
        System.arraycopy(RecordFile.cleanData, 0, 
                         buf.getData(), 0, 
                         RecordFile.BLOCK_SIZE);
        pageHdr.setType(type);
        pageHdr.setPrev(oldLast);
        pageHdr.setNext(0);
        
        
        if (oldLast == 0)
            // This was the first one of this type
            header.setFirstOf(type, retval);
        header.setLastOf(type, retval);
        file.release(retval, true);
        
        // If there's a previous, fix up its pointer
        if (oldLast != 0) {
            buf = file.get(oldLast);
            pageHdr = PageHeader.getView(buf);
            pageHdr.setNext(retval);
            file.release(oldLast, true);
        }
        
        // remove the view, we have modified the type.
        buf.setView(null);
        
        return retval;
    }