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 */
~CVmPoolFlat()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 */
attach_backing_store(CVmPoolBackingStore * backing_store)38 void CVmPoolFlat::attach_backing_store(CVmPoolBackingStore *backing_store)
39 {
40 size_t i;
41 char *dst;
42 size_t pg_cnt;
43 size_t siz;
44 pool_ofs_t ofs;
45
46 /* inherit the base implementation */
47 CVmPool::attach_backing_store(backing_store);
48
49 /* delete any existing memory */
50 if (mem_ != 0)
51 t3free(mem_);
52
53 /*
54 * Allocate our memory. We need to allocate a single chunk large
55 * enough for all of the pages.
56 */
57 pg_cnt = backing_store_->vmpbs_get_page_count();
58 siz = page_size_ * pg_cnt;
59 mem_ = (char *)t3malloc(siz);
60 if (mem_ == 0)
61 err_throw(VMERR_OUT_OF_MEMORY);
62
63 /* load all of the pages */
64 for (i = 0, ofs = 0, dst = mem_ ; i < pg_cnt ;
65 ++i, dst += page_size_, ofs += page_size_)
66 {
67 size_t cur_pg_siz;
68
69 /* get the load size for this page */
70 cur_pg_siz = backing_store_->vmpbs_get_page_size(ofs, page_size_);
71
72 /* load this page from the backing store directly into our memory */
73 backing_store_->vmpbs_load_page(ofs, page_size_, cur_pg_siz, dst);
74 }
75 }
76
77 /*
78 * detach from the backing store
79 */
detach_backing_store()80 void CVmPoolFlat::detach_backing_store()
81 {
82 /* free our memory */
83 if (mem_ != 0)
84 {
85 t3free(mem_);
86 mem_ = 0;
87 }
88
89 /* inherit default */
90 CVmPool::detach_backing_store();
91 }
92
93