GRASS Programmer's Manual
6.4.2(2012)
|
00001 /**************************************************************************** 00002 * 00003 * MODULE: iostream 00004 * 00005 * COPYRIGHT (C) 2007 Laura Toma 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 *****************************************************************************/ 00018 00019 #ifndef _MM_H 00020 #define _MM_H 00021 00022 #include <sys/types.h> 00023 00024 00025 #define MM_REGISTER_VERSION 2 00026 00027 // The default amount of memory we will allow to be allocated (40MB). 00028 #define MM_DEFAULT_MM_SIZE (40<<20) 00029 00030 00031 // MM accounting modes 00032 typedef enum { 00033 MM_IGNORE_MEMORY_EXCEEDED=0, 00034 MM_ABORT_ON_MEMORY_EXCEEDED, 00035 MM_WARN_ON_MEMORY_EXCEEDED 00036 } MM_mode; 00037 00038 00039 // MM Error codes 00040 enum MM_err { 00041 MM_ERROR_NO_ERROR = 0, 00042 MM_ERROR_INSUFFICIENT_SPACE, 00043 MM_ERROR_UNDERFLOW, 00044 MM_ERROR_EXCESSIVE_ALLOCATION 00045 }; 00046 00047 00048 // types of memory usage queries we can make on streams 00049 enum MM_stream_usage { 00050 // Overhead of the object without the buffer 00051 MM_STREAM_USAGE_OVERHEAD = 1, 00052 00053 // amount used by a buffer 00054 MM_STREAM_USAGE_BUFFER, 00055 00056 // Amount currently in use. 00057 MM_STREAM_USAGE_CURRENT, 00058 00059 // Maximum amount possibly in use. 00060 MM_STREAM_USAGE_MAXIMUM 00061 }; 00062 00063 00064 00065 00066 // Declarations of a very simple memory manager desgined to work with 00067 // BTEs that rely on the underlying OS to manage physical memory. 00068 class MM_register { 00069 private: 00070 // The number of instances of this class and descendents that exist. 00071 static int instances; 00072 00073 // The amount of space remaining to be allocated. 00074 size_t remaining; 00075 00076 // The user-specified limit on memory. 00077 size_t user_limit; 00078 00079 // the amount that has been allocated. 00080 size_t used; 00081 00082 // flag indicates how we are keeping track of memory 00083 static MM_mode register_new; 00084 00085 //protected: 00086 // // private methods, only called by operators new and delete. 00087 00088 public: // Need to be accessible from pqueue constructor 00089 MM_err register_allocation (size_t sz); 00090 MM_err register_deallocation(size_t sz); 00091 00092 00093 public: 00094 MM_register(); 00095 ~MM_register(void); 00096 00097 MM_err set_memory_limit(size_t sz); 00098 void enforce_memory_limit (); 00099 void ignore_memory_limit (); 00100 void warn_memory_limit (); 00101 MM_mode get_limit_mode(); 00102 void print_limit_mode(); 00103 00104 size_t memory_available (); 00105 size_t memory_used (); 00106 size_t memory_limit (); 00107 00108 int space_overhead (); 00109 00110 void print(); 00111 00112 friend class mm_register_init; 00113 friend void * operator new(size_t); 00114 friend void * operator new[](size_t); 00115 friend void operator delete(void *); 00116 friend void operator delete[](void *); 00117 }; 00118 00119 00120 00121 00122 // A class to make sure that MM_manager gets set up properly (only one 00123 // instance) . 00124 class mm_register_init { 00125 private: 00126 // The number of mm_register_init objects that exist. 00127 static unsigned int count; 00128 00129 public: 00130 mm_register_init(void); 00131 ~mm_register_init(void); 00132 }; 00133 00134 static mm_register_init source_file_mm_register_init; 00135 00136 00137 00138 00139 00140 // Here is the single memory management object (defined in mm.C). 00141 extern MM_register MM_manager; 00142 00143 00144 00145 #endif // _MM_H