Ardour  9.0-pre0-582-g084a23a80d
reallocpool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef _reallocpool_h_
19 #define _reallocpool_h_
20 
21 #ifndef NDEBUG
22 //#define RAP_WITH_CALL_STATS // collect statistics on calls counts (light)
23 //#define RAP_WITH_HISTOGRAM 513 // collect statistic about allocation size (not bad)
24 //#define RAP_WITH_SEGMENT_STATS // collect statistics (expensive)
25 #endif
26 
27 #ifndef RAP_BLOCKSIZE
28 #define RAP_BLOCKSIZE 7 // [bytes] power-of-two minus one (optional)
29 #endif
30 
31 #ifdef RAP_WITH_SEGMENT_STATS
32 #define RAP_WITH_CALL_STATS
33 #endif
34 
35 #include <string>
36 
37 #ifndef LIBPBD_API
38 #include "pbd/libpbd_visibility.h"
39 #endif
40 
41 namespace PBD {
42 
44 {
45 public:
46  ReallocPool (std::string name, size_t bytes);
48 
49  void set_name (const std::string& n) { _name = n; }
50 
51  static void * lalloc (void* pool, void *ptr, size_t oldsize, size_t newsize) {
52  return static_cast<ReallocPool*>(pool)->_realloc (ptr, oldsize, newsize);
53  }
54 
55  void * malloc (size_t size) {
56  return _realloc (NULL, 0, size);
57  }
58 
59  void free (void *ptr) {
60  if (ptr) _realloc (ptr, 0, 0);
61  }
62 
63  void * realloc (void *ptr, size_t newsize) {
64  return _realloc (ptr, _asize(ptr), newsize);
65  }
66 
67  void printstats ();
68  void dumpsegments ();
69 
70 #ifdef RAP_WITH_CALL_STATS
71  size_t mem_used () const { return _cur_used; }
72 #endif
73 
74 private:
75  std::string _name;
76  size_t _poolsize;
77  char *_pool;
78  char *_mru;
79 
80 #ifdef RAP_WITH_SEGMENT_STATS
81  size_t _cur_avail;
82  size_t _cur_allocated;
83  size_t _max_allocated;
84  size_t _seg_cur_count;
85  size_t _seg_max_count;
86  size_t _seg_max_used;
87  size_t _seg_max_avail;
88  void collect_segment_stats ();
89 #endif
90 #ifdef RAP_WITH_CALL_STATS
91  size_t _n_alloc;
92  size_t _n_grow;
93  size_t _n_shrink;
94  size_t _n_free;
95  size_t _n_noop;
96  size_t _n_oom;
97  size_t _cur_used; // cheaper _cur_allocated
98  size_t _max_used; // cheaper _max_allocated
99 #endif
100 #ifdef RAP_WITH_HISTOGRAM
101  size_t _hist_alloc [RAP_WITH_HISTOGRAM];
102  size_t _hist_free [RAP_WITH_HISTOGRAM];
103  size_t _hist_grow [RAP_WITH_HISTOGRAM];
104  size_t _hist_shrink [RAP_WITH_HISTOGRAM];
105 
106  unsigned int hist_bin (size_t s) const;
107  void print_histogram (size_t const * const histogram) const;
108 #endif
109 
110  void *_realloc (void *ptr, size_t oldsize, size_t newsize);
111  void *_malloc (size_t);
112  void _free (void *ptr);
113  void _shrink (void *, size_t);
114  size_t _asize (void *);
115  void consolidate_ptr (char *);
116 };
117 
118 } /* namespace */
119 #endif // _reallocpool_h_
ReallocPool(std::string name, size_t bytes)
size_t _asize(void *)
void set_name(const std::string &n)
Definition: reallocpool.h:49
void * _malloc(size_t)
void _shrink(void *, size_t)
void * _realloc(void *ptr, size_t oldsize, size_t newsize)
std::string _name
Definition: reallocpool.h:75
void consolidate_ptr(char *)
void free(void *ptr)
Definition: reallocpool.h:59
void * realloc(void *ptr, size_t newsize)
Definition: reallocpool.h:63
void * malloc(size_t size)
Definition: reallocpool.h:55
void _free(void *ptr)
static void * lalloc(void *pool, void *ptr, size_t oldsize, size_t newsize)
Definition: reallocpool.h:51
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
Definition: axis_view.h:42
minimum disk read bytes