diff --git a/docs/idea_for_asset_access.md b/docs/idea_for_asset_access.md new file mode 100644 index 0000000..1f6f523 --- /dev/null +++ b/docs/idea_for_asset_access.md @@ -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. diff --git a/src/mmapfiletest.c b/src/mmapfiletest.c new file mode 100644 index 0000000..69e2f91 --- /dev/null +++ b/src/mmapfiletest.c @@ -0,0 +1,55 @@ +// for mmap +#include + +// for random +#include + +// for open and fstat +#include +#include +#include + +// for puts +#include + +// for time +#include + +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: