Panda3D
 All Classes Functions Variables Enumerations
neverFreeMemory.h
1 // Filename: neverFreeMemory.h
2 // Created by: drose (14Jun07)
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 #ifndef NEVERFREEMEMORY_H
16 #define NEVERFREEMEMORY_H
17 
18 #include "dtoolbase.h"
19 
20 #include "mutexImpl.h"
21 #include <set>
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : NeverFreeMemory
25 // Description : This class is used to allocate bytes of memory from a
26 // pool that is never intended to be freed. It is
27 // particularly useful to support DeletedChain, which
28 // allocates memory in just such a fashion.
29 //
30 // When it is known that memory will not be freed, it is
31 // preferable to use this instead of the standard
32 // malloc() (or global_operator_new()) call, since this
33 // will help reduce fragmentation problems in the
34 // dynamic heap. Also, memory allocated from here will
35 // exhibit less wasted space.
36 ////////////////////////////////////////////////////////////////////
37 class EXPCL_DTOOL NeverFreeMemory {
38 private:
40 
41 public:
42  INLINE static void *alloc(size_t size);
43 
44 PUBLISHED:
45  INLINE static size_t get_total_alloc();
46  INLINE static size_t get_total_used();
47  INLINE static size_t get_total_unused();
48 
49 private:
50  void *ns_alloc(size_t size);
51  INLINE static NeverFreeMemory *get_global_ptr();
52  static void make_global_ptr();
53 
54 private:
55  class Page {
56  public:
57  INLINE Page(void *start, size_t size);
58  INLINE bool operator < (const Page &other) const;
59  INLINE void *alloc(size_t size);
60 
61  unsigned char *_next;
62  size_t _remaining;
63  };
64 
65  typedef set<Page> Pages;
66  Pages _pages;
67 
68  size_t _total_alloc;
69  size_t _total_used;
70  MutexImpl _lock;
71  static NeverFreeMemory * TVOLATILE _global_ptr;
72 };
73 
74 #include "neverFreeMemory.I"
75 
76 #endif
This class is used to allocate bytes of memory from a pool that is never intended to be freed...
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...