| 1 | /* |
| 2 | * Copyright (c) 1998, 2002 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 | vmpool.cpp - constant pool - in-memory (non-swapping) implementation |
| 10 | Function |
| 11 | |
| 12 | Notes |
| 13 | |
| 14 | Modified |
| 15 | 10/20/98 MJRoberts - Creation |
| 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <memory.h> |
| 20 | |
| 21 | #include "t3std.h" |
| 22 | #include "vmpool.h" |
| 23 | |
| 24 | |
| 25 | /* ------------------------------------------------------------------------ */ |
| 26 | /* |
| 27 | * In-memory pool implementation. This pool implementation pre-loads |
| 28 | * all available pages in the pool and keeps the complete pool in memory |
| 29 | * at all times. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * delete the pool's resources - this is called from our destructor, and |
| 34 | * can also be called explicitly to reset the pool |
| 35 | */ |
| 36 | void CVmPoolInMem::terminate_nv() |
| 37 | { |
| 38 | /* free any pages we allocated from the backing store */ |
| 39 | free_backing_pages(); |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * free pages that we allocated from the backing store |
| 44 | */ |
| 45 | void CVmPoolInMem::free_backing_pages() |
| 46 | { |
| 47 | size_t i; |
| 48 | pool_ofs_t ofs; |
| 49 | CVmPool_pg *info; |
| 50 | |
| 51 | /* if there's no backing store, there's nothing to do */ |
| 52 | if (backing_store_ == 0) |
| 53 | return; |
| 54 | |
| 55 | /* |
| 56 | * Run through the page array and delete each allocated page. Since |
| 57 | * we allocate pages through the backing store, delete pages through |
| 58 | * the backing store. |
| 59 | */ |
| 60 | for (i = 0, info = pages_, ofs = 0 ; i < page_slots_ ; |
| 61 | ++i, ++info, ofs += page_size_) |
| 62 | { |
| 63 | /* if this slot was allocated, delete it */ |
| 64 | if (info->mem != 0) |
| 65 | { |
| 66 | /* delete the page */ |
| 67 | backing_store_->vmpbs_free_page(info->mem, ofs, page_size_); |
| 68 | |
| 69 | /* forget it */ |
| 70 | info->mem = 0; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * initialize |
| 77 | */ |
| 78 | void CVmPoolInMem::attach_backing_store(CVmPoolBackingStore *backing_store) |
| 79 | { |
| 80 | size_t ofs; |
| 81 | size_t i; |
| 82 | CVmPool_pg *info; |
| 83 | |
| 84 | /* do the normal initialization to allocate the page slots */ |
| 85 | CVmPoolPaged::attach_backing_store(backing_store); |
| 86 | |
| 87 | /* load all of the pages */ |
| 88 | for (i = 0, info = pages_, ofs = 0 ; i < page_slots_ ; |
| 89 | ++i, ++info, ofs += page_size_) |
| 90 | { |
| 91 | /* determine how much memory we really need for this page */ |
| 92 | info->siz = backing_store_->vmpbs_get_page_size(ofs, page_size_); |
| 93 | |
| 94 | /* allocate and load the page */ |
| 95 | info->mem = backing_store_->vmpbs_alloc_and_load_page( |
| 96 | ofs, page_size_, info->siz); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Detach from the backing store |
| 102 | */ |
| 103 | void CVmPoolInMem::detach_backing_store() |
| 104 | { |
| 105 | /* release the backing pages */ |
| 106 | free_backing_pages(); |
| 107 | |
| 108 | /* inherit default */ |
| 109 | CVmPoolPaged::detach_backing_store(); |
| 110 | } |