mm.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #define MM_REGISTER_VERSION 2
  34. // The default amount of memory we will allow to be allocated (40MB).
  35. #define MM_DEFAULT_MM_SIZE (40<<20)
  36. // MM accounting modes
  37. typedef enum {
  38. MM_IGNORE_MEMORY_EXCEEDED=0,
  39. MM_ABORT_ON_MEMORY_EXCEEDED,
  40. MM_WARN_ON_MEMORY_EXCEEDED
  41. } MM_mode;
  42. // MM Error codes
  43. enum MM_err {
  44. MM_ERROR_NO_ERROR = 0,
  45. MM_ERROR_INSUFFICIENT_SPACE,
  46. MM_ERROR_UNDERFLOW,
  47. MM_ERROR_EXCESSIVE_ALLOCATION
  48. };
  49. // types of memory usage queries we can make on streams
  50. enum MM_stream_usage {
  51. // Overhead of the object without the buffer
  52. MM_STREAM_USAGE_OVERHEAD = 1,
  53. // amount used by a buffer
  54. MM_STREAM_USAGE_BUFFER,
  55. // Amount currently in use.
  56. MM_STREAM_USAGE_CURRENT,
  57. // Maximum amount possibly in use.
  58. MM_STREAM_USAGE_MAXIMUM
  59. };
  60. // Declarations of a very simple memory manager desgined to work with
  61. // BTEs that rely on the underlying OS to manage physical memory.
  62. class MM_register {
  63. private:
  64. // The number of instances of this class and descendents that exist.
  65. static int instances;
  66. // The amount of space remaining to be allocated.
  67. size_t remaining;
  68. // The user-specified limit on memory.
  69. size_t user_limit;
  70. // the amount that has been allocated.
  71. size_t used;
  72. // flag indicates how we are keeping track of memory
  73. static MM_mode register_new;
  74. //protected:
  75. // // private methods, only called by operators new and delete.
  76. public: // Need to be accessible from pqueue constructor
  77. MM_err register_allocation (size_t sz);
  78. MM_err register_deallocation(size_t sz);
  79. public:
  80. MM_register();
  81. ~MM_register(void);
  82. MM_err set_memory_limit(size_t sz);
  83. void enforce_memory_limit ();
  84. void ignore_memory_limit ();
  85. void warn_memory_limit ();
  86. MM_mode get_limit_mode();
  87. void print_limit_mode();
  88. size_t memory_available ();
  89. size_t memory_used ();
  90. size_t memory_limit ();
  91. int space_overhead ();
  92. void print();
  93. friend class mm_register_init;
  94. friend void * operator new(size_t) throw(std::bad_alloc);
  95. friend void * operator new[](size_t) throw(std::bad_alloc);
  96. friend void operator delete(void *) throw();
  97. friend void operator delete[](void *) throw();
  98. };
  99. // A class to make sure that MM_manager gets set up properly (only one
  100. // instance) .
  101. class mm_register_init {
  102. private:
  103. // The number of mm_register_init objects that exist.
  104. static unsigned int count;
  105. public:
  106. mm_register_init(void);
  107. ~mm_register_init(void);
  108. };
  109. static mm_register_init source_file_mm_register_init;
  110. // Here is the single memory management object (defined in mm.C).
  111. extern MM_register MM_manager;
  112. #endif // _MM_H