Panda3D
 All Classes Functions Variables Enumerations
vertexDataPage.I
1 // Filename: vertexDataPage.I
2 // Created by: drose (04Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: VertexDataPage::get_ram_class
18 // Access: Published
19 // Description: Returns the current ram class of the array. If this
20 // is other than RC_resident, the array data is not
21 // resident in memory.
22 ////////////////////////////////////////////////////////////////////
23 INLINE VertexDataPage::RamClass VertexDataPage::
24 get_ram_class() const {
25  MutexHolder holder(_lock);
26  return _ram_class;
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: VertexDataPage::get_pending_ram_class
31 // Access: Published
32 // Description: Returns the pending ram class of the array. If this
33 // is different from get_ram_class(), this page has been
34 // queued to be processed by the thread. Eventually the
35 // page will be set to this ram class.
36 ////////////////////////////////////////////////////////////////////
37 INLINE VertexDataPage::RamClass VertexDataPage::
39  MutexHolder holder(_lock);
40  return _pending_ram_class;
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: VertexDataPage::request_resident
45 // Access: Published
46 // Description: Ensures that the page will become resident soon.
47 // Future calls to get_page_data() will eventually
48 // return non-NULL.
49 ////////////////////////////////////////////////////////////////////
50 INLINE void VertexDataPage::
52  MutexHolder holder(_lock);
53  if (_ram_class != RC_resident) {
54  request_ram_class(RC_resident);
55  }
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: VertexDataPage::alloc
60 // Access: Published
61 // Description: Allocates a new block. Returns NULL if a block of the
62 // requested size cannot be allocated.
63 //
64 // To free the allocated block, call block->free(), or
65 // simply delete the block pointer.
66 ////////////////////////////////////////////////////////////////////
68 alloc(size_t size) {
69  MutexHolder holder(_lock);
70  return do_alloc(size);
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: VertexDataPage::get_first_block
75 // Access: Published
76 // Description: Returns a pointer to the first allocated block, or
77 // NULL if there are no allocated blocks.
78 ////////////////////////////////////////////////////////////////////
80 get_first_block() const {
81  MutexHolder holder(_lock);
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: VertexDataPage::get_book
87 // Access: Published
88 // Description: Returns a pointer to the book that owns this page.
89 ////////////////////////////////////////////////////////////////////
91 get_book() const {
92  return _book;
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: VertexDataPage::get_global_lru
97 // Access: Published, Static
98 // Description: Returns a pointer to the global LRU object that
99 // manages the VertexDataPage's with the indicated
100 // RamClass.
101 ////////////////////////////////////////////////////////////////////
103 get_global_lru(RamClass rclass) {
104  nassertr(rclass >= 0 && rclass < RC_end_of_list, NULL);
105  return _global_lru[rclass];
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: VertexDataPage::get_pending_lru
110 // Access: Published, Static
111 // Description: Returns a pointer to the global LRU object that
112 // manages the VertexDataPage's that are pending
113 // processing by the thread.
114 ////////////////////////////////////////////////////////////////////
117  return &_pending_lru;
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: VertexDataPage::get_save_file
122 // Access: Published, Static
123 // Description: Returns the global VertexDataSaveFile that will be
124 // used to save vertex data buffers to disk when
125 // necessary.
126 ////////////////////////////////////////////////////////////////////
129  if (_save_file == (VertexDataSaveFile *)NULL) {
130  make_save_file();
131  }
132  return _save_file;
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: VertexDataPage::save_to_disk
137 // Access: Published
138 // Description: Writes the page to disk, but does not evict it from
139 // memory or affect its LRU status. If it gets evicted
140 // later without having been modified, it will not need
141 // to write itself to disk again.
142 ////////////////////////////////////////////////////////////////////
143 INLINE bool VertexDataPage::
145  MutexHolder holder(_lock);
146  return do_save_to_disk();
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function: VertexDataPage::get_num_threads
151 // Access: Published, Static
152 // Description: Returns the number of threads that have been spawned
153 // to service vertex paging requests, or 0 if no threads
154 // have been spawned (which may mean either that all
155 // paging requests will be handled by the main thread,
156 // or simply that no paging requests have yet been
157 // issued).
158 ////////////////////////////////////////////////////////////////////
159 INLINE int VertexDataPage::
161  MutexHolder holder(_tlock);
162  if (_thread_mgr == (PageThreadManager *)NULL) {
163  return 0;
164  }
165  return _thread_mgr->get_num_threads();
166 }
167 
168 ////////////////////////////////////////////////////////////////////
169 // Function: VertexDataPage::get_num_pending_reads
170 // Access: Published, Static
171 // Description: Returns the number of read requests that are waiting
172 // to be serviced by a thread.
173 ////////////////////////////////////////////////////////////////////
174 INLINE int VertexDataPage::
176  MutexHolder holder(_tlock);
177  if (_thread_mgr == (PageThreadManager *)NULL) {
178  return 0;
179  }
180  return _thread_mgr->get_num_pending_reads();
181 }
182 
183 ////////////////////////////////////////////////////////////////////
184 // Function: VertexDataPage::get_num_pending_writes
185 // Access: Published, Static
186 // Description: Returns the number of write requests that are waiting
187 // to be serviced by a thread.
188 ////////////////////////////////////////////////////////////////////
189 INLINE int VertexDataPage::
191  MutexHolder holder(_tlock);
192  if (_thread_mgr == (PageThreadManager *)NULL) {
193  return 0;
194  }
195  return _thread_mgr->get_num_pending_writes();
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: VertexDataPage::get_page_data
200 // Access: Public
201 // Description: Returns a pointer to the page's data area, or NULL if
202 // the page is not currently resident. If the page is
203 // not currently resident, this will implicitly request
204 // it to become resident soon.
205 //
206 // If force is true, this method will never return NULL,
207 // but may block until the page is available.
208 ////////////////////////////////////////////////////////////////////
209 INLINE unsigned char *VertexDataPage::
210 get_page_data(bool force) {
211  MutexHolder holder(_lock);
212  if (_ram_class != RC_resident || _pending_ram_class != RC_resident) {
213  if (force) {
214  make_resident_now();
215  } else {
216  request_ram_class(RC_resident);
217  if (_ram_class != RC_resident) {
218  return NULL;
219  }
220  }
221  }
222 
223  mark_used_lru();
224  nassertr(_size == _uncompressed_size, _page_data);
225  return _page_data;
226 }
227 
228 ////////////////////////////////////////////////////////////////////
229 // Function: VertexDataPage::operator
230 // Access: Public
231 // Description: This comparison method is used to order pages within
232 // a book.
233 ////////////////////////////////////////////////////////////////////
234 INLINE bool VertexDataPage::
235 operator < (const VertexDataPage &other) const {
236  // We sort pages so that the pages with the smallest number of
237  // available contiguous bytes come up first. We store our best
238  // estimate of continguous bytes here.
239  if (_book_size != other._book_size) {
240  return _book_size < other._book_size;
241  }
242 
243  // For pages of equal size, we sort based on pointers, to make it
244  // easy to quickly find a specific page.
245  return this < &other;
246 }
247 
248 ////////////////////////////////////////////////////////////////////
249 // Function: VertexDataPage::set_ram_class
250 // Access: Private
251 // Description: Puts the data in a new ram class. Assumes the page
252 // lock is already held.
253 ////////////////////////////////////////////////////////////////////
254 INLINE void VertexDataPage::
255 set_ram_class(RamClass rclass) {
256  _ram_class = rclass;
257  mark_used_lru(_global_lru[rclass]);
258 
259  // Changing the ram class might make our effective available space 0
260  // and thereby change the placement within the book.
261  adjust_book_size();
262 }
263 
264 ////////////////////////////////////////////////////////////////////
265 // Function: VertexDataPage::round_up
266 // Access: Private
267 // Description: Round page_size up to the next multiple of
268 // _block_size.
269 ////////////////////////////////////////////////////////////////////
270 INLINE size_t VertexDataPage::
271 round_up(size_t page_size) const {
272  return ((page_size + _block_size - 1) / _block_size) * _block_size;
273 }
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:31
A block of bytes that holds one or more VertexDataBlocks.
A temporary file to hold the vertex data that has been evicted from memory and written to disk...
VertexDataBook * get_book() const
Returns a pointer to the book that owns this page.
RamClass get_ram_class() const
Returns the current ram class of the array.
static VertexDataSaveFile * get_save_file()
Returns the global VertexDataSaveFile that will be used to save vertex data buffers to disk when nece...
SimpleAllocatorBlock * get_first_block() const
Returns a pointer to the first allocated block, or NULL if there are no allocated blocks...
RamClass get_pending_ram_class() const
Returns the pending ram class of the array.
void mark_used_lru() const
To be called when the page is used; this will move it to the tail of the SimpleLru queue it is alread...
Definition: simpleLru.I:184
static int get_num_pending_reads()
Returns the number of read requests that are waiting to be serviced by a thread.
void request_resident()
Ensures that the page will become resident soon.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition: mutexHolder.h:29
static SimpleLru * get_global_lru(RamClass rclass)
Returns a pointer to the global LRU object that manages the VertexDataPage&#39;s with the indicated RamCl...
A block of bytes that stores the actual raw vertex data referenced by a GeomVertexArrayData object...
bool operator<(const VertexDataPage &other) const
This comparison method is used to order pages within a book.
static SimpleLru * get_pending_lru()
Returns a pointer to the global LRU object that manages the VertexDataPage&#39;s that are pending process...
bool save_to_disk()
Writes the page to disk, but does not evict it from memory or affect its LRU status.
A collection of VertexDataPages, which can be used to allocate new VertexDataBlock objects...
static int get_num_pending_writes()
Returns the number of write requests that are waiting to be serviced by a thread. ...
VertexDataBlock * alloc(size_t size)
Allocates a new block.
unsigned char * get_page_data(bool force)
Returns a pointer to the page&#39;s data area, or NULL if the page is not currently resident.
VertexDataBlock * get_first_block() const
Returns a pointer to the first allocated block, or NULL if there are no allocated blocks...
static int get_num_threads()
Returns the number of threads that have been spawned to service vertex paging requests, or 0 if no threads have been spawned (which may mean either that all paging requests will be handled by the main thread, or simply that no paging requests have yet been issued).