| 1 | /* |
| 2 | * test the pool |
| 3 | */ |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | #include "vmpool.h" |
| 9 | #include "t3test.h" |
| 10 | |
| 11 | static void test_pool(CVmPoolInMem *pool) |
| 12 | { |
| 13 | /* translate some addresses */ |
| 14 | pool->get_ptr(0); |
| 15 | pool->get_ptr(100); |
| 16 | pool->get_ptr(1024); |
| 17 | pool->get_ptr(4096); |
| 18 | pool->get_ptr(4097); |
| 19 | pool->get_ptr(1024); |
| 20 | pool->get_ptr(0); |
| 21 | pool->get_ptr(10000); |
| 22 | pool->get_ptr(1); |
| 23 | pool->get_ptr(10001); |
| 24 | pool->get_ptr(2); |
| 25 | pool->get_ptr(10002); |
| 26 | pool->get_ptr(4096); |
| 27 | pool->get_ptr(2048); |
| 28 | pool->get_ptr(4097); |
| 29 | pool->get_ptr(3); |
| 30 | |
| 31 | /* done with the pool - delete it */ |
| 32 | delete pool; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * backing store - simple implementation that just loads from a file |
| 37 | */ |
| 38 | class CMyBS: public CVmPoolBackingStore |
| 39 | { |
| 40 | public: |
| 41 | CMyBS(FILE *fp) { fp_ = fp; } |
| 42 | ~CMyBS() { fclose(fp_); } |
| 43 | |
| 44 | size_t vmpbs_get_page_count() { return 50; } |
| 45 | size_t vmpbs_get_common_page_size() { return 8192; } |
| 46 | size_t vmpbs_get_page_size(pool_ofs_t, size_t page_size) |
| 47 | { |
| 48 | return page_size; |
| 49 | } |
| 50 | void vmpbs_load_page(pool_ofs_t ofs, size_t page_size, size_t load_size, |
| 51 | char *mem) |
| 52 | { |
| 53 | fseek(fp_, ofs, SEEK_SET); |
| 54 | fread(mem, page_size, 1, fp_); |
| 55 | } |
| 56 | const char *vmpbs_alloc_and_load_page(pool_ofs_t ofs, size_t page_size, |
| 57 | size_t load_size) |
| 58 | { |
| 59 | char *mem; |
| 60 | |
| 61 | mem = (char *)t3malloc(load_size); |
| 62 | vmpbs_load_page(ofs, page_size, load_size, mem); |
| 63 | return mem; |
| 64 | } |
| 65 | void vmpbs_free_page(const char *mem, pool_ofs_t, size_t) |
| 66 | { |
| 67 | t3free((void *)mem); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | FILE *fp_; |
| 72 | }; |
| 73 | |
| 74 | int main() |
| 75 | { |
| 76 | CVmPoolInMem *pool; |
| 77 | CMyBS *bs; |
| 78 | |
| 79 | /* initialize for testing */ |
| 80 | test_init(); |
| 81 | |
| 82 | /* |
| 83 | * create a backing store object - use an arbitrary (but fairly large) |
| 84 | * file as the data source |
| 85 | */ |
| 86 | bs = new CMyBS(fopen("vmregex.cpp", "r")); |
| 87 | |
| 88 | /* create an in-memory pool */ |
| 89 | pool = new CVmPoolInMem(); |
| 90 | |
| 91 | /* initialize it with the backing store */ |
| 92 | pool->attach_backing_store(bs); |
| 93 | |
| 94 | /* run the tests */ |
| 95 | test_pool(pool); |
| 96 | |
| 97 | #if 0 // the swapping pool has been deprecated |
| 98 | /* create a swapping pool */ |
| 99 | pool = new CVmPoolSwap(2); |
| 100 | |
| 101 | /* initialize it */ |
| 102 | pool->attach_backing_store(bs); |
| 103 | |
| 104 | /* test it */ |
| 105 | test_pool(pool); |
| 106 | #endif |
| 107 | |
| 108 | /* done */ |
| 109 | return 0; |
| 110 | } |
| 111 |