GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-f63024f571
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 // Declarations of a very simple memory manager designed to work with
83 // BTEs that rely on the underlying OS to manage physical memory.
84 class MM_register {
85 private:
86  // The number of instances of this class and descendents that exist.
87  static int instances;
88 
89  // The amount of space remaining to be allocated.
90  size_t remaining;
91 
92  // The user-specified limit on memory.
93  size_t user_limit;
94 
95  // the amount that has been allocated.
96  size_t used;
97 
98  // flag indicates how we are keeping track of memory
99  static MM_mode register_new;
100 
101  // protected:
102  // // private methods, only called by operators new and delete.
103 
104 public: // Need to be accessible from pqueue constructor
105  MM_err register_allocation(size_t sz);
106  MM_err register_deallocation(size_t sz);
107 
108 public:
109  MM_register();
110  ~MM_register(void);
111 
112  MM_err set_memory_limit(size_t sz);
113  void enforce_memory_limit();
114  void ignore_memory_limit();
115  void warn_memory_limit();
117  void print_limit_mode();
118 
119  size_t memory_available();
120  size_t memory_used();
121  size_t memory_limit();
122 
123  int space_overhead();
124 
125  void print();
126 
127  // make these members of MM_register
128 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
129  void *operator new(size_t) throw(std::bad_alloc);
130  void *operator new[](size_t) throw(std::bad_alloc);
131  void operator delete(void *) throw();
132  void operator delete[](void *) throw();
133 #else
134  void *operator new(size_t);
135  void *operator new[](size_t);
136  void operator delete(void *) noexcept;
137  void operator delete[](void *) noexcept;
138 #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
139 
140  friend class mm_register_init;
141 };
142 
143 // A class to make sure that MM_manager gets set up properly (only one
144 // instance) .
146 private:
147  // The number of mm_register_init objects that exist.
148  static unsigned int count;
149 
150 public:
151  mm_register_init(void);
152  ~mm_register_init(void);
153 };
154 
155 static mm_register_init source_file_mm_register_init;
156 
157 // Here is the single memory management object (defined in mm.C).
158 extern MM_register MM_manager;
159 
160 #endif // _MM_H
Definition: mm.h:84
int space_overhead()
Definition: mm.cpp:220
MM_err register_allocation(size_t sz)
Definition: mm.cpp:228
void enforce_memory_limit()
Definition: mm.cpp:146
void warn_memory_limit()
Definition: mm.cpp:139
void ignore_memory_limit()
Definition: mm.cpp:160
MM_register()
Definition: mm.cpp:52
size_t memory_limit()
Definition: mm.cpp:206
size_t memory_available()
Definition: mm.cpp:194
void print()
Definition: mm.cpp:81
MM_err register_deallocation(size_t sz)
Definition: mm.cpp:247
MM_err set_memory_limit(size_t sz)
Definition: mm.cpp:97
MM_mode get_limit_mode()
Definition: mm.cpp:167
size_t memory_used()
Definition: mm.cpp:200
~MM_register(void)
Definition: mm.cpp:68
void print_limit_mode()
Definition: mm.cpp:174
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
MM_register MM_manager
Definition: mm.cpp:475
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