mm.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. *
  3. * MODULE: iostream
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. *
  8. * Iostream is a library that implements streams, external memory
  9. * sorting on streams, and an external memory priority queue on
  10. * streams. These are the fundamental components used in external
  11. * memory algorithms.
  12. * Credits: The library was developed by Laura Toma. The kernel of
  13. * class STREAM is based on the similar class existent in the GPL TPIE
  14. * project developed at Duke University. The sorting and priority
  15. * queue have been developed by Laura Toma based on communications
  16. * with Rajiv Wickremesinghe. The library was developed as part of
  17. * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
  18. * Rajiv Wickremesinghe as part of the Terracost project.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details. *
  29. * **************************************************************************/
  30. #ifndef _MM_H
  31. #define _MM_H
  32. #include <sys/types.h>
  33. // GCC with C++98 and -fexceptions requires exception
  34. // specifiers, however with C++11 and newer, using them causes an error.
  35. #if __cplusplus < 201103L
  36. #define GRASS_MM_USE_EXCEPTION_SPECIFIER
  37. #endif /* __cplusplus < 201103L */
  38. #define MM_REGISTER_VERSION 2
  39. // The default amount of memory we will allow to be allocated (40MB).
  40. #define MM_DEFAULT_MM_SIZE (40<<20)
  41. // MM accounting modes
  42. typedef enum {
  43. MM_IGNORE_MEMORY_EXCEEDED=0,
  44. MM_ABORT_ON_MEMORY_EXCEEDED,
  45. MM_WARN_ON_MEMORY_EXCEEDED
  46. } MM_mode;
  47. // MM Error codes
  48. enum MM_err {
  49. MM_ERROR_NO_ERROR = 0,
  50. MM_ERROR_INSUFFICIENT_SPACE,
  51. MM_ERROR_UNDERFLOW,
  52. MM_ERROR_EXCESSIVE_ALLOCATION
  53. };
  54. // types of memory usage queries we can make on streams
  55. enum MM_stream_usage {
  56. // Overhead of the object without the buffer
  57. MM_STREAM_USAGE_OVERHEAD = 1,
  58. // amount used by a buffer
  59. MM_STREAM_USAGE_BUFFER,
  60. // Amount currently in use.
  61. MM_STREAM_USAGE_CURRENT,
  62. // Maximum amount possibly in use.
  63. MM_STREAM_USAGE_MAXIMUM
  64. };
  65. // Declarations of a very simple memory manager designed to work with
  66. // BTEs that rely on the underlying OS to manage physical memory.
  67. class MM_register {
  68. private:
  69. // The number of instances of this class and descendents that exist.
  70. static int instances;
  71. // The amount of space remaining to be allocated.
  72. size_t remaining;
  73. // The user-specified limit on memory.
  74. size_t user_limit;
  75. // the amount that has been allocated.
  76. size_t used;
  77. // flag indicates how we are keeping track of memory
  78. static MM_mode register_new;
  79. //protected:
  80. // // private methods, only called by operators new and delete.
  81. public: // Need to be accessible from pqueue constructor
  82. MM_err register_allocation (size_t sz);
  83. MM_err register_deallocation(size_t sz);
  84. public:
  85. MM_register();
  86. ~MM_register(void);
  87. MM_err set_memory_limit(size_t sz);
  88. void enforce_memory_limit ();
  89. void ignore_memory_limit ();
  90. void warn_memory_limit ();
  91. MM_mode get_limit_mode();
  92. void print_limit_mode();
  93. size_t memory_available ();
  94. size_t memory_used ();
  95. size_t memory_limit ();
  96. int space_overhead ();
  97. void print();
  98. // make these members of MM_register
  99. #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
  100. void * operator new(size_t) throw (std::bad_alloc);
  101. void * operator new[] (size_t) throw (std::bad_alloc);
  102. void operator delete(void *) throw();
  103. void operator delete[](void *) throw();
  104. #else
  105. void * operator new(size_t);
  106. void * operator new[] (size_t);
  107. void operator delete(void *) noexcept;
  108. void operator delete[](void *) noexcept;
  109. #endif /* GRASS_MM_USE_EXCEPTION_SPECIFIER */
  110. friend class mm_register_init;
  111. };
  112. // A class to make sure that MM_manager gets set up properly (only one
  113. // instance) .
  114. class mm_register_init {
  115. private:
  116. // The number of mm_register_init objects that exist.
  117. static unsigned int count;
  118. public:
  119. mm_register_init(void);
  120. ~mm_register_init(void);
  121. };
  122. static mm_register_init source_file_mm_register_init;
  123. // Here is the single memory management object (defined in mm.C).
  124. extern MM_register MM_manager;
  125. #endif // _MM_H