Panda3D
vertexDataBook.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file vertexDataBook.cxx
10  * @author drose
11  * @date 2007-05-16
12  */
13 
14 #include "vertexDataBook.h"
15 #include "mutexHolder.h"
16 
17 /**
18  *
19  */
20 VertexDataBook::
21 VertexDataBook(size_t block_size) {
22  // Make sure the block_size is an integer multiple of the system's page
23  // size.
24  _block_size = memory_hook->round_up_to_page_size(block_size);
25 }
26 
27 /**
28  *
29  */
30 VertexDataBook::
31 ~VertexDataBook() {
32 }
33 
34 /**
35  * Returns the total size of all bytes owned by all pages owned by this book.
36  */
37 size_t VertexDataBook::
39  MutexHolder holder(_lock);
40 
41  size_t total = 0;
42  Pages::const_iterator pi;
43  for (pi = _pages.begin(); pi != _pages.end(); ++pi) {
44  total += (*pi)->get_max_size();
45  }
46  return total;
47 }
48 
49 /**
50  * Returns the total size of all bytes owned by all pages owned by this book
51  * that have the indicated ram class.
52  */
53 size_t VertexDataBook::
54 count_total_page_size(VertexDataPage::RamClass ram_class) const {
55  MutexHolder holder(_lock);
56 
57  size_t total = 0;
58  Pages::const_iterator pi;
59  for (pi = _pages.begin(); pi != _pages.end(); ++pi) {
60  if ((*pi)->get_ram_class() == ram_class) {
61  total += (*pi)->get_max_size();
62  }
63  }
64  return total;
65 }
66 
67 /**
68  * Returns the total size of all bytes allocated within pages owned by this
69  * book.
70  */
71 size_t VertexDataBook::
73  MutexHolder holder(_lock);
74 
75  size_t total = 0;
76  Pages::const_iterator pi;
77  for (pi = _pages.begin(); pi != _pages.end(); ++pi) {
78  total += (*pi)->get_total_size();
79  }
80  return total;
81 }
82 
83 /**
84  * Returns the total size of all bytes allocated within pages owned by this
85  * book that have the indicated ram class.
86  */
87 size_t VertexDataBook::
88 count_allocated_size(VertexDataPage::RamClass ram_class) const {
89  MutexHolder holder(_lock);
90 
91  size_t total = 0;
92  Pages::const_iterator pi;
93  for (pi = _pages.begin(); pi != _pages.end(); ++pi) {
94  if ((*pi)->get_ram_class() == ram_class) {
95  total += (*pi)->get_total_size();
96  }
97  }
98  return total;
99 }
100 
101 /**
102  * Writes all pages to disk immediately, just in case they get evicted later.
103  * It makes sense to make this call just before taking down a loading screen,
104  * to minimize chugs from saving pages inadvertently later.
105  */
106 void VertexDataBook::
108  MutexHolder holder(_lock);
109 
110  Pages::iterator pi;
111  for (pi = _pages.begin(); pi != _pages.end(); ++pi) {
112  (*pi)->save_to_disk();
113  }
114 }
115 
116 
117 /**
118  * Allocates and returns a new VertexDataBuffer of the requested size.
119  *
120  * Assumes the lock is already held.
121  */
122 VertexDataBlock *VertexDataBook::
123 do_alloc(size_t size) {
124  // Look for an empty page of the appropriate size. The _pages set is sorted
125  // so that the pages with the smallest available blocks are at the front.
126 
127  // Create a dummy page to use to search the set.
128  VertexDataPage size_page(size);
129  Pages::iterator pi = _pages.lower_bound(&size_page);
130 
131  // Now we can start from the first element of the set that is possibly large
132  // enough to contain this block, and work up from there.
133  while (pi != _pages.end()) {
134  Pages::iterator pnext = pi;
135  ++pnext;
136 
137  VertexDataPage *page = (*pi);
138 
139  // Allocating a block may change the page's available contiguous size, and
140  // thereby change its position in the set, invalidating the iterator pi.
141  // This is why we've already computed pnext.
142  VertexDataBlock *block = page->do_alloc(size);
143 
144  if (block != nullptr) {
145  // This page worked.
146  return block;
147  }
148 
149  // Try the next page.
150  pi = pnext;
151  }
152 
153  // No page was good enough. Create a new page. Make it at least large
154  // enough to hold this requested block.
155  VertexDataPage *page = create_new_page(size);
156  _pages.insert(page);
157 
158  VertexDataBlock *block = page->do_alloc(size);
159  return block;
160 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A block of bytes that holds one or more VertexDataBlocks.
size_t count_allocated_size() const
Returns the total size of all bytes allocated within pages owned by this book.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition: mutexHolder.h:25
void save_to_disk()
Writes all pages to disk immediately, just in case they get evicted later.
A block of bytes that stores the actual raw vertex data referenced by a GeomVertexArrayData object.
size_t count_total_page_size() const
Returns the total size of all bytes owned by all pages owned by this book.
size_t round_up_to_page_size(size_t size) const
Rounds the indicated size request up to the next larger multiple of page_size, to qualify it for a ca...
Definition: memoryHook.I:51
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.