00001 /**************************************************************************** 00002 * VCGLib o o * 00003 * Visual and Computer Graphics Library o o * 00004 * _ O _ * 00005 * Copyright(C) 2004 \/)\/ * 00006 * Visual Computing Lab /\/| * 00007 * ISTI - Italian National Research Council | * 00008 * \ * 00009 * All rights reserved. * 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This program is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * 00020 * for more details. * 00021 * * 00022 ****************************************************************************/ 00023 00024 #ifndef __MEMORY_INFO_H 00025 #define __MEMORY_INFO_H 00026 00027 #include <stdexcept> 00028 00029 namespace vcg 00030 { 00031 //WARNING: All the classes derived from MemoryInfo has been intended to be instantiated as a singleton in the host application 00032 //(i.e. in every application using it just an instance of a class derived from MemoryInfo should be declared). 00033 00034 class MemoryInfo 00035 { 00036 public: 00037 class MemoryInfoException : public std::exception 00038 { 00039 public: 00040 MemoryInfoException(const char* text) 00041 :std::exception(),_exctext(text){} 00042 00043 ~MemoryInfoException() throw() {} 00044 inline const char* what() const throw() {return _exctext;} 00045 private: 00046 const char* _exctext; 00047 }; 00048 00049 MemoryInfo(std::ptrdiff_t originalmem) 00050 :_originaltotalmemory(originalmem),_currentfreememory(_originaltotalmemory) 00051 { 00052 } 00053 00054 virtual ~MemoryInfo() {} 00055 virtual void acquiredMemory(std::ptrdiff_t mem) = 0; 00056 virtual std::ptrdiff_t usedMemory() const = 0; 00057 virtual std::ptrdiff_t currentFreeMemory() const = 0; 00058 virtual void releasedMemory(std::ptrdiff_t mem = 0) = 0; 00059 virtual bool isAdditionalMemoryAvailable(std::ptrdiff_t mem) = 0; 00060 00061 protected: 00062 const std::ptrdiff_t _originaltotalmemory; 00063 std::ptrdiff_t _currentfreememory; 00064 }; 00065 00066 //WARNING: this is not a thread safe class. The object derived from MemoryInfo are intended to be used inside GLMeshAttributeFeeder as static variable in order to manage the available GPUMemory. 00067 //We strongly recommend you to define in your code a thread safe version of the class, defining mutexed access member functions. 00068 //This class should be consider just as a basic example for the implementations of the required functionalities. 00069 //It is safe to use it just when the user has only one mesh to pass to the GPU. 00070 00071 class NotThreadSafeMemoryInfo : public MemoryInfo 00072 { 00073 public: 00074 NotThreadSafeMemoryInfo(std::ptrdiff_t originalmem) 00075 :MemoryInfo(originalmem) 00076 { 00077 } 00078 00079 ~NotThreadSafeMemoryInfo() {} 00080 00081 void acquiredMemory(std::ptrdiff_t mem) 00082 { 00083 if (mem > _originaltotalmemory) 00084 throw MemoryInfo::MemoryInfoException("It has been requested more memory than the total one.\\n"); 00085 else 00086 if (mem > _currentfreememory) 00087 throw MemoryInfo::MemoryInfoException("It has been requested more memory than the free available one.\\n"); 00088 else 00089 _currentfreememory -= mem; 00090 } 00091 00092 std::ptrdiff_t usedMemory() const 00093 { 00094 return _originaltotalmemory - _currentfreememory; 00095 } 00096 00097 std::ptrdiff_t currentFreeMemory() const 00098 { 00099 return _currentfreememory; 00100 } 00101 00102 void releasedMemory(std::ptrdiff_t mem = 0) 00103 { 00104 if (mem > _originaltotalmemory) 00105 throw MemoryInfo::MemoryInfoException("It has been released more memory than the total one. Something strange happened!\\n"); 00106 else 00107 _currentfreememory += mem; 00108 } 00109 00110 bool isAdditionalMemoryAvailable(std::ptrdiff_t mem) 00111 { 00112 return (_currentfreememory >= mem); 00113 } 00114 }; 00115 }; 00116 00117 #endif