1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#define CS_SYSDEF_PROVIDE_HARDWARE_MMIO 1
#include "cssysdef.h"
#include "csutil/mmapio.h"
csMemoryMappedIO::csMemoryMappedIO(unsigned _block_size, char *filename):
block_size(_block_size)
{
valid_mmio_object = MemoryMapFile(&platform, filename);
}
csMemoryMappedIO::~csMemoryMappedIO()
{
UnMemoryMapFile(&platform);
}
/////////////////////////////////////////// Software Support for Memory Mapping ///////////////////////////////////
#ifndef CS_HAS_MEMORY_MAPPED_IO
bool
csMemoryMappedIO::MemoryMapFile(mmioInfo *_platform, char *filename)
{
// Clear the page map
page_map = NULL;
// Initialize cache management variables
cache_block_size = csmmioDefaultCacheBlockSize;
cache_max_size = csmmioDefaultCacheSize;
cache_block_count=0;
// Initialize the cache so that all buckets have at least one block
unsigned i;
for(i=0; i<csmmioDefaultHashSize; ++i)
{
CacheBlock *cp=new CacheBlock;
++cache_block_count;
// Initialize it
cp->data = new unsigned char[block_size * cache_block_size];
// Set young so that it can be used again.
cp->age=0;
// This is the least likely page value.
cp->page=0xffffffff;
// Nothing next
cp->next=0;
cache[i] = cp;
}
#ifdef CS_DEBUG
hits=0;
misses=0;
#endif
if ((_platform->hMappedFile=fopen(filename, "rb")) == NULL)
{
return false;
}
else
{
// Create a page map with one bit per cache block.
page_map = new csBitArray(_platform->file_size/cache_block_size);
// Clear all of it's bits
page_map->Clear();
// Set that we're good to go.
return true;
}
}
void
csMemoryMappedIO::UnMemoryMapFile(mmioInfo *_platform)
{
if (_platform->hMappedFile)
fclose(_platform->hMappedFile);
if (page_map)
delete page_map;
unsigned int i;
CacheBlock *cp, *np;
// Free all allocated memory.
for(i=0; i<csmmioDefaultHashSize; ++i)
{
cp=cache[i];
while(cp)
{
np=cp->next;
delete [] cp->data;
delete cp;
cp=np;
}
}
}
void
csMemoryMappedIO::CachePage(unsigned int page)
{
unsigned int bucket = page % csmmioDefaultHashSize;
CacheBlock *cp;
if (cache_block_count < cache_max_size)
{
cp = new CacheBlock;
++cache_block_count;
// Insert it into the bucket.
cp->next=cache[bucket];
cache[bucket]=cp;
// Initialize it
assert((cp->data = new unsigned char[block_size * cache_block_size])!=NULL);
}
else
{
CacheBlock *block;
// Find the least used block in this bucket.
cp=cache[bucket];
block=cp->next;
while(block)
{
if (block->age < cp->age)
cp=block;
block=block->next;
}
// Unmark this page as allocated
if (cp->page!=0xffffffff) page_map->ClearBit(cp->page);
}
// Get the data for it.
cp->offset=page*cache_block_size;
cp->page=page;
cp->age=0;
// Mark this page as allocated
page_map->SetBit(page);
// Read the page from the file
fseek(platform.hMappedFile, page*cache_block_size*block_size, SEEK_SET);
fread(cp->data, block_size, cache_block_size, platform.hMappedFile);
}
#endif
|