Debian Codegraph @

terps/tads/tads3/vmpool.h

raw symbols SCIP
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.h - VM constant pool manager
10 Function
11
12 Notes
13
14 Modified
15 10/20/98 MJRoberts - Creation
16 */
17
18 #ifndef VMPOOL_H
19 #define VMPOOL_H
20
21 #include <stdlib.h>
22 #include <memory.h>
23
24 #include "vmtype.h"
25
26 /* include the pool selection mechanism */
27 #include "vmpoolsl.h"
28
29 /* ------------------------------------------------------------------------ */
30 /*
31 * Constant pool page information. This structure tracks memory for one
32 * page.
33 */
34 struct CVmPool_pg
35 {
36 /* memory containing the data in this page */
37 const char *mem;
38
39 /* actual size of the page data */
40 size_t siz;
41 };
42
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Constant Pool Backing Store Interface. This is an abstract interface
47 * that pool clients must implement to provide the pool with the means
48 * of loading pages.
49 *
50 * Note that the backing store, like the pool itself, is considered
51 * read-only by the pool manager. The pool manager never needs to write
52 * data to the backing store, and expects the backing store to remain
53 * constant throughout the existence of the pool (hence the pool never
54 * needs to reload any data from the backing store that it already has
55 * in cache).
56 */
57 class CVmPoolBackingStore
58 {
59 public:
60 /*
61 * since this class is abstract, make sure all subclasses have virtual
62 * destructors
63 */
64 virtual ~CVmPoolBackingStore() { }
65
66 /*
67 * Determine the total number of pages that are available to be
68 * loaded. Implementations of the pool manager that pre-load all
69 * pages use this function to determine how many pages are available
70 * for loading.
71 */
72 virtual size_t vmpbs_get_page_count() = 0;
73
74 /*
75 * Get the common page size in the underying store. Individual
76 * pages may not use the entire page size, but no page may be larger
77 * than the common size.
78 */
79 virtual size_t vmpbs_get_common_page_size() = 0;
80
81 /*
82 * Given a starting offset and a page size, calculate how much space is
83 * actually needed for the page at the offset. This is provided to
84 * allow for partial pages, which don't need the full page size
85 * allocated. Simple implementations can simply always return the full
86 * page size.
87 */
88 virtual size_t vmpbs_get_page_size(pool_ofs_t ofs, size_t page_size) = 0;
89
90 /*
91 * Given a starting offset, allocate space for the given page and
92 * load it into memory. page_size is the normal page size in bytes,
93 * and load_size is the actual number of bytes to be allocated and
94 * loaded (this will be the value previously returned by
95 * vmpbs_get_page_size for the page).
96 *
97 * This should throw an exception if an error occurs.
98 */
99 virtual const char
100 *vmpbs_alloc_and_load_page(pool_ofs_t ofs, size_t page_size,
101 size_t load_size) = 0;
102
103 /*
104 * Delete memory allocated by vmpbs_alloc_and_load_page(). The pool
105 * will call this for each page previously allocated. 'page_size'
106 * is the normal page size in bytes for the entire pool.
107 */
108 virtual void vmpbs_free_page(const char *mem, pool_ofs_t ofs,
109 size_t page_size) = 0;
110
111 /*
112 * Given a starting offset, load the page into the given memory,
113 * which is allocated and managed by the caller. page_size is the
114 * normal page size in bytes, and load_size is the actual number of
115 * bytes to be loaded (this will be the value previously returned by
116 * vmpbs_get_page_size for the page).
117 *
118 * This should throw an exception if an error occurs.
119 */
120 virtual void vmpbs_load_page(pool_ofs_t ofs, size_t page_size,
121 size_t load_size, char *mem) = 0;
122
123 /*
124 * Determine if my pages are writable. Returns true if so, false if
125 * not. If the pool pages are directly mapped to the underlying
126 * data file, this should return false. For example, an
127 * implementation for a palm-top computer without an external
128 * storage device might simply store the image file directly in
129 * memory, and the backing store would map directly onto this memory
130 * so that the original copy of the image file in memory can be used
131 * as the loaded version as well. In such cases, the backing store
132 * should certainly not be writable. For an implementation that
133 * copies data from an external storage device (typically a hard
134 * disk), writing to the backing store copy would cause no change to
135 * the original image file data, hence this can return true in such
136 * cases.
137 */
138 virtual int vmpbs_is_writable() { return FALSE; }
139 };
140
141
142 /* ------------------------------------------------------------------------ */
143 /*
144 * Base constant memory pool class
145 */
146 class CVmPool
147 {
148 public:
149 CVmPool() { }
150 virtual ~CVmPool() { }
151
152 /*
153 * Attach to the given backing store to provide the the page data.
154 */
155 virtual void
156 attach_backing_store(class CVmPoolBackingStore *backing_store) = 0;
157
158 /*
159 * Detach from backing store - this must be called before the backing
160 * store object can be deleted.
161 */
162 virtual void detach_backing_store() { backing_store_ = 0; }
163
164 /*
165 * Translate an address from a pool offset to a physical location.
166 * Note that translating an address may invalidate a previously
167 * translated address in a swapping implementation of the pool manager,
168 * so callers should take care to assume only one translated address in
169 * a given pool is valid at a time.
170 *
171 * Because this routine is called extremely frequently, we don't make
172 * it a virtual. Instead, we depend upon the final subclass to define
173 * the method as a non-virtual, so that it can be in-lined. This means
174 * that pool object references must all be declared with the final
175 * subclass.
176 */
177 /* virtual const char *get_ptr(pool_ofs_t ofs) = 0; */
178
179 /*
180 * Given a pointer into physical memory, get the pool offset. Returns
181 * true if the pointer is a valid pointer into the pool, false if not.
182 */
183 /* virtual int get_ofs(const char *p, pool_ofs_t *ofs) = 0; */
184
185 /*
186 * Get the page size. This reflects the size of the pages in the
187 * backing store (usually the image file); this doesn't necessarily
188 * indicate anything about the way we manage the pool memory.
189 */
190 size_t get_page_size() const { return page_size_; }
191
192 /* get the number of pages in the pool */
193 size_t get_page_count() const;
194
195 /* validate an offset */
196 /* virtual int validate_ofs(pool_ofs_t ofs) = 0; */
197
198 protected:
199 /*
200 * page size in bytes - this is simply the number of bytes on each page
201 * (each page in the pool has the same number of bytes)
202 */
203 size_t page_size_;
204
205 /* backing store */
206 class CVmPoolBackingStore *backing_store_;
207 };
208
209 /* ------------------------------------------------------------------------ */
210 /*
211 * "Flat" memory pool. This type of pool loads all pages into a single
212 * contiguous chunk of memory. This is suitable for platforms with large
213 * linear memory spaces, such as 32-bit platforms.
214 */
215 class CVmPoolFlat: public CVmPool
216 {
217 public:
218 CVmPoolFlat()
219 {
220 /* we don't have our memory chunk yet */
221 mem_ = 0;
222 }
223 ~CVmPoolFlat();
224
225 /* terminate - we don't need to do anything here */
226 void terminate() { }
227
228 /* attach to the backing store - loads all pages */
229 void attach_backing_store(class CVmPoolBackingStore *backing_store);
230
231 /* detach from the backing store */
232 void detach_backing_store();
233
234 /*
235 * Translate an address. Since all of our memory is in one large
236 * contiguous chunk, this is extremely simple: just return the base of
237 * our memory block, offset by the pool offset.
238 */
239 inline const char *get_ptr(pool_ofs_t ofs) { return mem_ + ofs; }
240
241 /* validate an address */
242 inline int validate_ofs(pool_ofs_t ofs)
243 { return (ofs > 0 && ofs < (pool_ofs_t)siz_); }
244
245 /* get the pool offset given a pointer */
246 inline int get_ofs(const char *p, pool_ofs_t *ofs)
247 {
248 /* if it's in our memory block, it's a valid pointer */
249 if (p >= mem_ && p < mem_ + siz_)
250 {
251 *ofs = p - mem_;
252 return TRUE;
253 }
254 else
255 return FALSE;
256 }
257
258 /* our single contiguous allocation block */
259 char *mem_;
260
261 /* total size of the memory block */
262 long siz_;
263 };
264
265 /* ------------------------------------------------------------------------ */
266 /*
267 * Paged constant pool.
268 *
269 * This type of pool is divided into pages. A given object must be
270 * entirely contained in a single page.
271 *
272 * Each object is referenced by its address in the constant pool. An
273 * object address is simply an offset into the pool.
274 */
275 class CVmPoolPaged: public CVmPool
276 {
277 public:
278 /* create the pool */
279 CVmPoolPaged()
280 {
281 /* no page slots allocated yet */
282 pages_ = 0;
283 page_slots_ = 0;
284 page_slots_max_ = 0;
285
286 /* we don't have a backing store yet */
287 backing_store_ = 0;
288
289 /* we don't know the page size yet */
290 page_size_ = 0;
291 log2_page_size_ = 0;
292 }
293
294 /*
295 * Delete the pool. Call our non-virtual termination routine, as we
296 * can't use virtuals in destructors (not in the normal fashion,
297 * anyway).
298 */
299 virtual ~CVmPoolPaged() { terminate_nv(); }
300
301 /* delete everything in the pool using our base terminator routine */
302 virtual void terminate() { terminate_nv(); }
303
304 /*
305 * Attach to the given backing store to provide the the page data.
306 */
307 virtual void
308 attach_backing_store(class CVmPoolBackingStore *backing_store);
309
310 protected:
311 /* non-virtual termination routine */
312 void terminate_nv()
313 {
314 /* free our page memory */
315 delete_page_list();
316 }
317
318 /* delete our page list, if any */
319 void delete_page_list();
320
321 /* allocate or expand the page slot list */
322 void alloc_page_slots(size_t slots);
323
324 /*
325 * Calculate which page we need, and the offset within the page, for
326 * a given pool offset. The page is the offset divided by the page
327 * size; since the page size is a power of two, this is simply a bit
328 * shift by log2(page_size). The page offset is the remainder after
329 * dividing the offset by the page size; again because the page size
330 * is a power of two, we can calculate this remainder simply by
331 * AND'ing the offset with the page size minus one. (Using these
332 * shift and mask operations may seem a little obscure, but it's so
333 * much faster on most machines than integer division that we're
334 * willing to be a little obscure in exchange for the performance
335 * gain.)
336 */
337 inline size_t get_page_for_ofs(pool_ofs_t ofs) const
338 {
339 return (size_t)(ofs >> log2_page_size_);
340 }
341
342 inline size_t get_ofs_for_ofs(pool_ofs_t ofs) const
343 {
344 return (size_t)(ofs & (page_size_ - 1));
345 }
346
347 /* get the starting offset on the given page */
348 pool_ofs_t get_page_start_ofs(size_t pg) const
349 {
350 return ((pool_ofs_t)pg) << log2_page_size_;
351 }
352
353 /*
354 * The page list. This is an array of CVmPool_pg structures; each
355 * structure keeps track of one page in the pool.
356 *
357 * The page identified by the first page information structure contains
358 * pool offsets 0 through (page_size - 1); the next contains offsets
359 * page_size through (2*page_size - 1); and so on.
360 */
361 CVmPool_pg *pages_;
362
363 /*
364 * The number of page slots in the page list. This starts at the
365 * initial page size and can grow dynamically as more pages are added.
366 */
367 size_t page_slots_;
368
369 /*
370 * The maximum of allocated pages_ array entries. This might be larger
371 * than page_slots_, because we sometimes allocate more slots than we
372 * currently need to avoid having to allocate on every new page
373 * addition.
374 */
375 size_t page_slots_max_;
376
377 /* log2 of the page size */
378 int log2_page_size_;
379 };
380
381
382 /* ------------------------------------------------------------------------ */
383 /*
384 * Two-tiered paged pool. This is similar to the paged pool
385 * implementation, but uses a two-level page table: the first-level page
386 * table containers pointers to the second-level tables, and the
387 * second-level tables contain the pointers to the actual pages.
388 *
389 * This class is not currently used, because the two-level scheme isn't
390 * required in practice for modern machines and is less efficient than the
391 * single-level page table implemented in CVmPoolPaged. We retain this
392 * two-level code in case it's ever needed, though, because the two-level
393 * scheme might be useful for 16-bit segmented architectures.
394 *
395 * The advantage of the two-level scheme is that it allows very large
396 * memory spaces to be addressable without any single large allocations;
397 * the single-tier paged pool requires a single allocation equal to the
398 * total aggregate memory size divided by the page size times the size of a
399 * page pointer, which could be a fairly large single allocation for an
400 * extremely large aggregate pool size. However, it doesn't currently
401 * appear that the single-tier paging scheme will impose any limits that
402 * will be encountered in actual practice.
403 */
404 #if 0
405
406 /* number of page information structures in each subarray */
407 const size_t VMPOOL_SUBARRAY_SIZE = 4096;
408
409 class CVmPoolPaged2
410 {
411 public:
412 /* create the pool */
413 CVmPoolPaged2()
414 {
415 /* no page slots allocated yet */
416 pages_ = 0;
417 page_slots_ = 0;
418
419 /* we don't have a backing store yet */
420 backing_store_ = 0;
421
422 /* we don't know the page size yet */
423 page_size_ = 0;
424 log2_page_size_ = 0;
425 }
426
427 /* delete the pool */
428 virtual ~CVmPoolPaged2();
429
430 /*
431 * Attach to the given backing store to provide the the page data.
432 */
433 virtual void
434 attach_backing_store(class CVmPoolBackingStore *backing_store);
435
436 protected:
437 /* delete our page list, if any */
438 void delete_page_list();
439
440 /* allocate or expand the page slot list */
441 void alloc_page_slots(size_t slots);
442
443 /*
444 * Calculate which page we need, and the offset within the page, for a
445 * given pool offset. The page is the offset divided by the page size;
446 * since the page size is a power of two, this is simply a bit shift by
447 * log2(page_size). The page offset is the remainder after dividing
448 * the offset by the page size; again because the page size is a power
449 * of two, we can calculate this remainder simply by AND'ing the offset
450 * with the page size minus one. (Using these shift and mask
451 * operations may seem a little obscure, but it's so much faster on
452 * most machines than integer division that we're willing to be a
453 * little obscure in exchange for the performance gain.)
454 */
455 inline size_t get_page_for_ofs(pool_ofs_t ofs) const
456 {
457 return (size_t)(ofs >> log2_page_size_);
458 }
459
460 inline size_t get_ofs_for_ofs(pool_ofs_t ofs) const
461 {
462 return (size_t)(ofs & (page_size_ - 1));
463 }
464
465 /* get the starting offset on the given page */
466 pool_ofs_t get_page_start_ofs(size_t pg) const
467 {
468 return ((pool_ofs_t)pg) << log2_page_size_;
469 }
470
471 /* get the number of subarrays */
472 size_t get_subarray_count() const
473 { return ((page_slots_ + VMPOOL_SUBARRAY_SIZE - 1)
474 / VMPOOL_SUBARRAY_SIZE); }
475
476 /* get the page information structure for a given index */
477 inline CVmPool_pg *get_page_info(size_t idx) const
478 { return &(pages_[idx / VMPOOL_SUBARRAY_SIZE]
479 [idx % VMPOOL_SUBARRAY_SIZE]); }
480
481 /*
482 * The page list. Each entry in this array is a subarray containing
483 * VMPOOL_SUBARRAY_SIZE page information structures, each of contains
484 * information on a page. Conceptually, the two-tiered array forms a
485 * single array; we keep two levels of arrays to accommodate 16-bit
486 * machines where a single large could be too large for a single 64k
487 * segment.
488 *
489 * The page identified by the first page information structure contains
490 * pool offsets 0 through (page_size - 1); the next contains offsets
491 * page_size through (2*page_size - 1); and so on.
492 */
493 CVmPool_pg **pages_;
494
495 /*
496 * The number of slots allocated in the page list. This starts at
497 * the initial page size and can grow dynamically as more pages are
498 * added.
499 */
500 size_t page_slots_;
501
502 /* log2 of the page size */
503 int log2_page_size_;
504 };
505 #endif /* 0 */
506
507 /* ------------------------------------------------------------------------ */
508 /*
509 * In-memory pool implementation. This pool implementation pre-loads
510 * all available pages in the pool and keeps the complete pool in memory
511 * at all times.
512 */
513 class CVmPoolInMem: public CVmPoolPaged
514 {
515 public:
516 CVmPoolInMem() { }
517
518 /*
519 * delete - call our non-virtual terminator (use the non-virtual
520 * version, as this will just do our local termination; since we'll
521 * implicitly inherit the base class destructor, we don't want to
522 * explicitly inherit its termination as well)
523 */
524 ~CVmPoolInMem() { terminate_nv(); }
525
526 /* terminate */
527 void terminate()
528 {
529 /* call our own non-virtual termination routine */
530 terminate_nv();
531
532 /* inherit our base class handling */
533 CVmPoolPaged::terminate();
534 }
535
536 /* attach to the backing store - loads all pages */
537 void attach_backing_store(class CVmPoolBackingStore *backing_store);
538
539 /* detach from the backing store */
540 void detach_backing_store();
541
542 /*
543 * translate an address - since the pool is always in memory, we can
544 * translate an address simply by doing the arithmetic and finding
545 * the needed page, which is always loaded
546 */
547 inline const char *get_ptr(pool_ofs_t ofs)
548 {
549 /* translate the address through our page array */
550 return (pages_[get_page_for_ofs(ofs)].mem + get_ofs_for_ofs(ofs));
551 }
552
553 /* validate an offset value */
554 inline int validate_ofs(pool_ofs_t ofs)
555 {
556 /* get the page and the offset in the page */
557 size_t pg = get_page_for_ofs(ofs);
558 size_t pgofs = get_ofs_for_ofs(ofs);
559
560 /*
561 * to be valid, it must be within the range of valid pages, the
562 * page must be allocated, and the offset in the page must be
563 * within the page's actual allocated size
564 */
565 return (pg < page_slots_
566 && pages_[pg].mem != 0
567 && pgofs < pages_[pg].siz);
568 }
569
570 /* get the pool offset given a pointer */
571 inline int get_ofs(const char *p, pool_ofs_t *ofs)
572 {
573 /* check each page */
574 pool_ofs_t page_ofs = 0;
575 for (size_t i = 0 ; i < page_slots_ ; ++i, page_ofs += page_size_)
576 {
577 /* if it's in this page, it's a valid address */
578 const char *mem = pages_[i].mem;
579 if (p >= mem && p < mem + pages_[i].siz)
580 {
581 *ofs = (p - mem) + page_ofs;
582 return TRUE;
583 }
584 }
585
586 /* didn't find it */
587 return FALSE;
588 }
589
590 private:
591 /* non-virtual termination */
592 void terminate_nv();
593
594 /* free any pages we allocated from the backing store */
595 void free_backing_pages();
596 };
597
598 #endif /* VMPOOL_H */
599