Fix mysterious crashes such as #7049
[ardour.git] / libs / pbd / pbd / reallocpool.h
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 #ifndef _reallocpool_h_
20 #define _reallocpool_h_
21
22 #ifndef NDEBUG
23 //#define RAP_WITH_CALL_STATS // collect statistics on calls counts (light)
24 //#define RAP_WITH_HISTOGRAM 513 // collect statistic about allocation size (not bad)
25 //#define RAP_WITH_SEGMENT_STATS // collect statistics (expensive)
26 #endif
27
28 #ifndef RAP_BLOCKSIZE
29 #define RAP_BLOCKSIZE 7 // [bytes] power-of-two minus one (optional)
30 #endif
31
32 #ifdef RAP_WITH_SEGMENT_STATS
33 #define RAP_WITH_CALL_STATS
34 #endif
35
36 #include <string>
37
38 #ifndef LIBPBD_API
39 #include "pbd/libpbd_visibility.h"
40 #endif
41
42 namespace PBD {
43
44 class LIBPBD_API ReallocPool
45 {
46 public:
47         ReallocPool (std::string name, size_t bytes);
48         ~ReallocPool ();
49
50         void set_name (const std::string& n) { _name = n; }
51
52         static void * lalloc (void* pool, void *ptr, size_t oldsize, size_t newsize) {
53                 return static_cast<ReallocPool*>(pool)->_realloc (ptr, oldsize, newsize);
54         }
55
56         void * malloc (size_t size) {
57                 return _realloc (NULL, 0, size);
58         }
59
60         void free (void *ptr) {
61                 if (ptr) _realloc (ptr, 0, 0);
62         }
63
64         void * realloc (void *ptr, size_t newsize) {
65                 return _realloc (ptr, _asize(ptr), newsize);
66         }
67
68         void printstats ();
69         void dumpsegments ();
70
71 #ifdef RAP_WITH_CALL_STATS
72         size_t mem_used () const { return _cur_used; }
73 #endif
74
75 private:
76         std::string _name;
77         size_t _poolsize;
78         char *_pool;
79         char *_mru;
80
81 #ifdef RAP_WITH_SEGMENT_STATS
82         size_t _cur_avail;
83         size_t _cur_allocated;
84         size_t _max_allocated;
85         size_t _seg_cur_count;
86         size_t _seg_max_count;
87         size_t _seg_max_used;
88         size_t _seg_max_avail;
89         void collect_segment_stats ();
90 #endif
91 #ifdef RAP_WITH_CALL_STATS
92         size_t _n_alloc;
93         size_t _n_grow;
94         size_t _n_shrink;
95         size_t _n_free;
96         size_t _n_noop;
97         size_t _n_oom;
98         size_t _cur_used; // cheaper _cur_allocated
99         size_t _max_used; // cheaper _max_allocated
100 #endif
101 #ifdef RAP_WITH_HISTOGRAM
102         size_t _hist_alloc [RAP_WITH_HISTOGRAM];
103         size_t _hist_free [RAP_WITH_HISTOGRAM];
104         size_t _hist_grow [RAP_WITH_HISTOGRAM];
105         size_t _hist_shrink [RAP_WITH_HISTOGRAM];
106
107         unsigned int hist_bin (size_t s) const;
108         void print_histogram (size_t const * const histogram) const;
109 #endif
110
111         void *_realloc (void *ptr, size_t oldsize, size_t newsize);
112         void *_malloc (size_t);
113         void _free (void *ptr);
114         void _shrink (void *, size_t);
115         size_t _asize (void *);
116         void consolidate_ptr (char *);
117 };
118
119 } /* namespace */
120 #endif // _reallocpool_h_