GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
mm.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * MODULE: iostream
4  *
5 
6  * COPYRIGHT (C) 2007 Laura Toma
7  *
8  *
9 
10  * Iostream is a library that implements streams, external memory
11  * sorting on streams, and an external memory priority queue on
12  * streams. These are the fundamental components used in external
13  * memory algorithms.
14 
15  * Credits: The library was developed by Laura Toma. The kernel of
16  * class STREAM is based on the similar class existent in the GPL TPIE
17  * project developed at Duke University. The sorting and priority
18  * queue have been developed by Laura Toma based on communications
19  * with Rajiv Wickremesinghe. The library was developed as part of
20  * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
21  * Rajiv Wickremesinghe as part of the Terracost project.
22 
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29 
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * General Public License for more details. *
34  * **************************************************************************/
35 
36 
37 #ifndef _MM_H
38 #define _MM_H
39 
40 #include <sys/types.h>
41 
42 // GCC with C++98 and -fexceptions requires exception
43 // specifiers, however with C++11 and newer, using them causes an error.
44 #if __cplusplus < 201103L
45 #define GRASS_MM_USE_EXCEPTION_SPECIFIER
46 #endif /* __cplusplus < 201103L */
47 
48 #define MM_REGISTER_VERSION 2
49 
50 // The default amount of memory we will allow to be allocated (40MB).
51 #define MM_DEFAULT_MM_SIZE (40<<20)
52 
53 
54 // MM accounting modes
55 typedef enum {
59 } MM_mode;
60 
61 
62 // MM Error codes
63 enum MM_err {
68 };
69 
70 
71 // types of memory usage queries we can make on streams
73  // Overhead of the object without the buffer
75 
76  // amount used by a buffer
78 
79  // Amount currently in use.
81 
82  // Maximum amount possibly in use.
84 };
85 
86 
87 
88 
89 // Declarations of a very simple memory manager designed to work with
90 // BTEs that rely on the underlying OS to manage physical memory.
91 class MM_register {
92 private:
93  // The number of instances of this class and descendents that exist.
94  static int instances;
95 
96  // The amount of space remaining to be allocated.
97  size_t remaining;
98 
99  // The user-specified limit on memory.
100  size_t user_limit;
101 
102  // the amount that has been allocated.
103  size_t used;
104 
105  // flag indicates how we are keeping track of memory
106  static MM_mode register_new;
107 
108 //protected:
109 // // private methods, only called by operators new and delete.
110 
111 public: // Need to be accessible from pqueue constructor
112  MM_err register_allocation (size_t sz);
113  MM_err register_deallocation(size_t sz);
114 
115 
116 public:
117  MM_register();
118  ~MM_register(void);
119 
120  MM_err set_memory_limit(size_t sz);
121  void enforce_memory_limit ();
122  void ignore_memory_limit ();
123  void warn_memory_limit ();
125  void print_limit_mode();
126 
127  size_t memory_available ();
128  size_t memory_used ();
129  size_t memory_limit ();
130 
131  int space_overhead ();
132 
133  void print();
134 
135  // make these members of MM_register
136 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
137  void * operator new(size_t) throw (std::bad_alloc);
138  void * operator new[] (size_t) throw (std::bad_alloc);
139  void operator delete(void *) throw();
140  void operator delete[](void *) throw();
141 #else
142  void * operator new(size_t);
143  void * operator new[] (size_t);
144  void operator delete(void *) noexcept;
145  void operator delete[](void *) noexcept;
146 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
147 
148  friend class mm_register_init;
149 };
150 
151 
152 
153 
154 // A class to make sure that MM_manager gets set up properly (only one
155 // instance) .
157 private:
158  // The number of mm_register_init objects that exist.
159  static unsigned int count;
160 
161 public:
162  mm_register_init(void);
163  ~mm_register_init(void);
164 };
165 
166 static mm_register_init source_file_mm_register_init;
167 
168 
169 
170 
171 
172 // Here is the single memory management object (defined in mm.C).
173 extern MM_register MM_manager;
174 
175 
176 
177 #endif // _MM_H
void print_limit_mode()
Definition: mm.cpp:179
MM_mode
Definition: mm.h:55
~MM_register(void)
Definition: mm.cpp:73
void print()
Definition: mm.cpp:86
MM_err register_deallocation(size_t sz)
Definition: mm.cpp:258
int count
MM_register MM_manager
Definition: mm.cpp:487
MM_register()
Definition: mm.cpp:56
MM_err register_allocation(size_t sz)
Definition: mm.cpp:238
int space_overhead()
Definition: mm.cpp:228
MM_err
Definition: mm.h:63
MM_mode get_limit_mode()
Definition: mm.cpp:173
MM_err set_memory_limit(size_t sz)
Definition: mm.cpp:103
MM_stream_usage
Definition: mm.h:72
void enforce_memory_limit()
Definition: mm.cpp:152
size_t memory_used()
Definition: mm.cpp:205
void warn_memory_limit()
Definition: mm.cpp:145
friend class mm_register_init
Definition: mm.h:148
void ignore_memory_limit()
Definition: mm.cpp:166
Definition: mm.h:91
size_t memory_limit()
Definition: mm.cpp:211
size_t memory_available()
Definition: mm.cpp:200