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