| 1 | /* |
| 2 | * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | vmpoolfl.cpp - "flat" memory pool |
| 10 | Function |
| 11 | Implements the flat memory pool, which allocates the entire pool's |
| 12 | memory in a single contiguous chunk. |
| 13 | Notes |
| 14 | |
| 15 | Modified |
| 16 | 09/18/02 MJRoberts - Creation |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <memory.h> |
| 21 | |
| 22 | #include "t3std.h" |
| 23 | #include "vmpool.h" |
| 24 | |
| 25 | /* |
| 26 | * delete |
| 27 | */ |
| 28 | CVmPoolFlat::~CVmPoolFlat() |
| 29 | { |
| 30 | /* delete our memory block */ |
| 31 | if (mem_ != 0) |
| 32 | t3free(mem_); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Attach to the backing store |
| 37 | */ |
| 38 | void CVmPoolFlat::attach_backing_store(CVmPoolBackingStore *backing_store) |
| 39 | { |
| 40 | size_t i; |
| 41 | char *dst; |
| 42 | size_t pg_cnt; |
| 43 | pool_ofs_t ofs; |
| 44 | |
| 45 | /* inherit the base implementation */ |
| 46 | CVmPool::attach_backing_store(backing_store); |
| 47 | |
| 48 | /* delete any existing memory */ |
| 49 | if (mem_ != 0) |
| 50 | t3free(mem_); |
| 51 | |
| 52 | /* |
| 53 | * Allocate our memory. We need to allocate a single chunk large |
| 54 | * enough for all of the pages. |
| 55 | */ |
| 56 | pg_cnt = backing_store_->vmpbs_get_page_count(); |
| 57 | siz_ = page_size_ * pg_cnt; |
| 58 | mem_ = (char *)t3malloc(siz_); |
| 59 | if (mem_ == 0) |
| 60 | err_throw(VMERR_OUT_OF_MEMORY); |
| 61 | |
| 62 | /* load all of the pages */ |
| 63 | for (i = 0, ofs = 0, dst = mem_ ; i < pg_cnt ; |
| 64 | ++i, dst += page_size_, ofs += page_size_) |
| 65 | { |
| 66 | size_t cur_pg_siz; |
| 67 | |
| 68 | /* get the load size for this page */ |
| 69 | cur_pg_siz = backing_store_->vmpbs_get_page_size(ofs, page_size_); |
| 70 | |
| 71 | /* load this page from the backing store directly into our memory */ |
| 72 | backing_store_->vmpbs_load_page(ofs, page_size_, cur_pg_siz, dst); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * detach from the backing store |
| 78 | */ |
| 79 | void CVmPoolFlat::detach_backing_store() |
| 80 | { |
| 81 | /* free our memory */ |
| 82 | if (mem_ != 0) |
| 83 | { |
| 84 | t3free(mem_); |
| 85 | mem_ = 0; |
| 86 | } |
| 87 | |
| 88 | /* inherit default */ |
| 89 | CVmPool::detach_backing_store(); |
| 90 | } |
| 91 |