Browse Source
added thought about memory mapped asset handling, as well as a small mmap file test program.
release0.1.5
added thought about memory mapped asset handling, as well as a small mmap file test program.
release0.1.5
2 changed files with 81 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||
The Problem |
|||
=========== |
|||
|
|||
- One asset might be accessed on various positions by multiple workers. |
|||
- The asset might be large. |
|||
- The amount of usable file handles is limited. |
|||
|
|||
Idea |
|||
==== |
|||
|
|||
Create an asset class, wich holds the file handle as well as a memory |
|||
mapping to it, as well as a reference count of interested workers. |
|||
An asset object is created the first time the asset is requested. Then |
|||
the file is opened and memory mapped and the refcount is set to 1. |
|||
The asset is than stored into a hash indexed by the filename. |
|||
The next time the asset is requested it is found in the hash and the |
|||
refcount is increased. |
|||
The asset object might be an observer of each worker it uses, so the |
|||
worker can inform the asset object when they are done, so that the |
|||
reference count can be decreased. |
|||
If the reference count goes to zero, the asset object is removed from the |
|||
hash and freed. |
|||
Each worker in turn has to know how much of the asset is already processed, |
|||
so that it can ask for the position it wants the next data from. |
|||
As each request is assigned one worker and one request can only access one |
|||
asset the worker has to know only one position. |
|||
@ -0,0 +1,55 @@ |
|||
// for mmap |
|||
#include <sys/mman.h> |
|||
|
|||
// for random |
|||
#include <stdlib.h> |
|||
|
|||
// for open and fstat |
|||
#include <sys/types.h> |
|||
#include <sys/stat.h> |
|||
#include <fcntl.h> |
|||
|
|||
// for puts |
|||
#include <stdio.h> |
|||
|
|||
// for time |
|||
#include <time.h> |
|||
|
|||
int |
|||
main(int argc, char * argv[]) |
|||
{ |
|||
struct stat st; |
|||
char * map; |
|||
size_t position; |
|||
char print_buf[101]; |
|||
int i; |
|||
|
|||
print_buf[100] = '\0'; |
|||
|
|||
int fd = open("./mmapfiletest.c", O_RDONLY); |
|||
|
|||
fstat(fd, &st); |
|||
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
|||
|
|||
srandom(time(NULL)); |
|||
position = random() % (st.st_size - 100); |
|||
|
|||
for (i=0; i<100; i+=10) { |
|||
print_buf[i+0] = map[position + i + 0]; |
|||
print_buf[i+1] = map[position + i + 1]; |
|||
print_buf[i+2] = map[position + i + 2]; |
|||
print_buf[i+3] = map[position + i + 3]; |
|||
print_buf[i+4] = map[position + i + 4]; |
|||
print_buf[i+5] = map[position + i + 5]; |
|||
print_buf[i+6] = map[position + i + 6]; |
|||
print_buf[i+7] = map[position + i + 7]; |
|||
print_buf[i+8] = map[position + i + 8]; |
|||
print_buf[i+9] = map[position + i + 9]; |
|||
} |
|||
|
|||
puts(print_buf); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
// vim: set et ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue