GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-565e82de51
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 #ifndef _MM_H
37 #define _MM_H
38 
39 #include <sys/types.h>
40 
41 // GCC with C++98 and -fexceptions requires exception
42 // specifiers, however with C++11 and newer, using them causes an error.
43 #if __cplusplus < 201103L
44 #define GRASS_MM_USE_EXCEPTION_SPECIFIER
45 #endif /* __cplusplus < 201103L */
46 
47 #define MM_REGISTER_VERSION 2
48 
49 // The default amount of memory we will allow to be allocated (40MB).
50 #define MM_DEFAULT_MM_SIZE (40 << 20)
51 
52 // MM accounting modes
53 typedef enum {
58 
59 // MM Error codes
60 enum MM_err {
65 };
66 
67 // types of memory usage queries we can make on streams
69  // Overhead of the object without the buffer
71 
72  // amount used by a buffer
74 
75  // Amount currently in use.
77 
78  // Maximum amount possibly in use.
80 };
81 
82 #ifdef GRASS_CMAKE_BUILD
83 #include <export/grass_iostream_export.h>
84 #else
85 #define GRASS_IOSTREAM_EXPORT
86 #endif
87 
88 // Declarations of a very simple memory manager designed to work with
89 // BTEs that rely on the underlying OS to manage physical memory.
91 private:
92  // The number of instances of this class and descendents that exist.
93  static int instances;
94 
95  // The amount of space remaining to be allocated.
96  size_t remaining;
97 
98  // The user-specified limit on memory.
99  size_t user_limit;
100 
101  // the amount that has been allocated.
102  size_t used;
103 
104  // flag indicates how we are keeping track of memory
105  static MM_mode register_new;
106 
107  // protected:
108  // // private methods, only called by operators new and delete.
109 
110 public: // Need to be accessible from pqueue constructor
111  MM_err register_allocation(size_t sz);
112  MM_err register_deallocation(size_t sz);
113 
114 public:
115  MM_register();
116  ~MM_register(void);
117 
118  MM_err set_memory_limit(size_t sz);
119  void enforce_memory_limit();
120  void ignore_memory_limit();
121  void warn_memory_limit();
122  MM_mode get_limit_mode();
123  void print_limit_mode();
124 
125  size_t memory_available();
126  size_t memory_used();
127  size_t memory_limit();
128 
129  int space_overhead();
130 
131  void print();
132 
133  // make these members of MM_register
134 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
135  void *operator new(size_t) throw(std::bad_alloc);
136  void *operator new[](size_t) throw(std::bad_alloc);
137  void operator delete(void *) throw();
138  void operator delete[](void *) throw();
139 #else
140  void *operator new(size_t);
141  void *operator new[](size_t);
142  void operator delete(void *) noexcept;
143  void operator delete[](void *) noexcept;
144 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
145 
146  friend class mm_register_init;
147 };
148 
149 // A class to make sure that MM_manager gets set up properly (only one
150 // instance) .
152 private:
153  // The number of mm_register_init objects that exist.
154  static unsigned int count;
155 
156 public:
157  mm_register_init(void);
158  ~mm_register_init(void);
159 };
160 
161 static mm_register_init source_file_mm_register_init;
162 
163 // Here is the single memory management object (defined in mm.C).
165 
166 #endif // _MM_H
Definition: mm.h:90
mm_register_init(void)
Definition: mm.cpp:488
~mm_register_init(void)
Definition: mm.cpp:495
MM_stream_usage
Definition: mm.h:68
@ MM_STREAM_USAGE_OVERHEAD
Definition: mm.h:70
@ MM_STREAM_USAGE_MAXIMUM
Definition: mm.h:79
@ MM_STREAM_USAGE_BUFFER
Definition: mm.h:73
@ MM_STREAM_USAGE_CURRENT
Definition: mm.h:76
MM_err
Definition: mm.h:60
@ MM_ERROR_NO_ERROR
Definition: mm.h:61
@ MM_ERROR_UNDERFLOW
Definition: mm.h:63
@ MM_ERROR_INSUFFICIENT_SPACE
Definition: mm.h:62
@ MM_ERROR_EXCESSIVE_ALLOCATION
Definition: mm.h:64
#define GRASS_IOSTREAM_EXPORT
Definition: mm.h:85
MM_mode
Definition: mm.h:53
@ MM_ABORT_ON_MEMORY_EXCEEDED
Definition: mm.h:55
@ MM_WARN_ON_MEMORY_EXCEEDED
Definition: mm.h:56
@ MM_IGNORE_MEMORY_EXCEEDED
Definition: mm.h:54
GRASS_IOSTREAM_EXPORT MM_register MM_manager
Definition: mm.cpp:475