neverFreeMemory.I
00001 // Filename: neverFreeMemory.I
00002 // Created by:  drose (14Jun07)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: NeverFreeMemory::alloc
00018 //       Access: Public, Static
00019 //  Description: Returns a pointer to a newly-allocated block of
00020 //               memory of the indicated size.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE void *NeverFreeMemory::
00023 alloc(size_t size) {
00024   return get_global_ptr()->ns_alloc(size);
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: NeverFreeMemory::get_total_alloc
00029 //       Access: Published, Static
00030 //  Description: Returns the total number of bytes consumed by all the
00031 //               pages allocated internally by this object.
00032 ////////////////////////////////////////////////////////////////////
00033 INLINE size_t NeverFreeMemory::
00034 get_total_alloc() {
00035   return get_global_ptr()->_total_alloc;
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: NeverFreeMemory::get_total_used
00040 //       Access: Published, Static
00041 //  Description: Returns the total number of bytes requested by the
00042 //               application in calls to NeverFreeMemory::alloc().
00043 ////////////////////////////////////////////////////////////////////
00044 INLINE size_t NeverFreeMemory::
00045 get_total_used() {
00046   return get_global_ptr()->_total_used;
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: NeverFreeMemory::get_total_unused
00051 //       Access: Published, Static
00052 //  Description: Returns the difference between get_total_alloc() and
00053 //               get_total_used().  This represents bytes in allocated
00054 //               pages that have not (yet) been used by the
00055 //               application.
00056 ////////////////////////////////////////////////////////////////////
00057 INLINE size_t NeverFreeMemory::
00058 get_total_unused() {
00059   NeverFreeMemory *global_ptr = get_global_ptr();
00060   global_ptr->_lock.acquire();
00061   size_t total_unused = global_ptr->_total_alloc - global_ptr->_total_used;
00062   global_ptr->_lock.release();
00063   return total_unused;
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function: NeverFreeMemory::get_global_ptr
00068 //       Access: Private, Static
00069 //  Description: 
00070 ////////////////////////////////////////////////////////////////////
00071 INLINE NeverFreeMemory *NeverFreeMemory::
00072 get_global_ptr() {
00073   if (_global_ptr == (NeverFreeMemory *)NULL) {
00074     make_global_ptr();
00075   }
00076   return _global_ptr;
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: NeverFreeMemory::Page::Constructor
00081 //       Access: Public
00082 //  Description: 
00083 ////////////////////////////////////////////////////////////////////
00084 INLINE NeverFreeMemory::Page::
00085 Page(void *start, size_t size) : 
00086   _next((unsigned char *)start),
00087   _remaining(size)
00088 {
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: NeverFreeMemory::Page::operator <
00093 //       Access: Public
00094 //  Description: 
00095 ////////////////////////////////////////////////////////////////////
00096 INLINE bool NeverFreeMemory::Page::
00097 operator < (const NeverFreeMemory::Page &other) const {
00098   return _remaining < other._remaining;
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: NeverFreeMemory::Page::alloc
00103 //       Access: Public
00104 //  Description: 
00105 ////////////////////////////////////////////////////////////////////
00106 INLINE void *NeverFreeMemory::Page::
00107 alloc(size_t size) {
00108   assert(size <= _remaining);
00109   void *result = _next;
00110   _next += size;
00111   _remaining -= size;
00112   return result;
00113 }