CXXR (C++ R) API
MemoryBank.hpp
Go to the documentation of this file.
1 /*CXXR $Id: MemoryBank.hpp 1348 2013-02-25 17:49:03Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
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.1 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
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef MEMORYBANK_HPP
41 #define MEMORYBANK_HPP
42 
43 #include <cstring>
44 #include "config.h"
45 #include "CXXR/CellPool.hpp"
46 #include "CXXR/SchwarzCounter.hpp"
47 
48 namespace CXXR {
62  class MemoryBank {
63  public:
72 #ifdef __GNUC__
73  __attribute__((hot,fastcall))
74 #endif
75  static void* allocate(size_t bytes) throw (std::bad_alloc);
76 
81  static size_t blocksAllocated() {return s_blocks_allocated;}
82 
93  static size_t bytesAllocated() {return s_bytes_allocated;}
94 
100  static void check();
101 
112  static void deallocate(void* p, size_t bytes)
113  {
114  if (!p) return;
115 #ifdef FILL55
116  // This helps to diagnose premature GC:
117  memset(p, 0x55, bytes);
118 #endif
119  // Assumes sizeof(double) == 8:
120  if (bytes > s_max_cell_size)
121  ::operator delete(p);
122  else s_pools[s_pooltab[(bytes + 7) >> 3]].deallocate(p);
123  --s_blocks_allocated;
124  s_bytes_allocated -= bytes;
125  }
126 
133  static void defragment();
134 
135 #ifdef R_MEMORY_PROFILING
136 
153  static void setMonitor(void (*monitor)(size_t) = 0,
154  size_t threshold = 0);
155 #endif
156  private:
157  typedef CellPool Pool;
158  static const size_t s_num_pools = 10;
159  static const size_t s_max_cell_size;
160  static size_t s_blocks_allocated;
161  static size_t s_bytes_allocated;
162  static Pool* s_pools;
163  static const unsigned char s_pooltab[];
164 #ifdef R_MEMORY_PROFILING
165  static void (*s_monitor)(size_t);
166  static size_t s_monitor_threshold;
167 #endif
168 
169  // Not implemented. Declared to stop the compiler generating
170  // a constructor.
171  MemoryBank();
172 
173  // Free memory used by the static data members:
174  static void cleanup() {}
175 
176  // Initialize the static data members:
177  static void initialize();
178 
179  friend class SchwarzCounter<MemoryBank>;
180  };
181 }
182 
183 namespace {
184  CXXR::SchwarzCounter<CXXR::MemoryBank> memorybank_schwarz_ctr;
185 }
186 
187 #endif /* MEMORYBANK_HPP */