LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Arena.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
29 #ifndef LSST_AP_UTILS_ARENA_H
30 #define LSST_AP_UTILS_ARENA_H
31 
32 #include <cassert>
33 #include <cstdlib>
34 #include <algorithm>
35 #include <stdexcept>
36 #include <vector>
37 
38 #include "boost/static_assert.hpp"
39 
40 #include "lsst/pex/exceptions.h"
41 
42 // Forward declarations
43 namespace lsst { namespace ap { namespace utils {
44  template <typename T> class Arena;
45 }}}
46 
47 template <typename T> void * operator new(size_t, lsst::ap::utils::Arena<T> &);
48 template <typename T> void operator delete(void *, lsst::ap::utils::Arena<T> &);
49 
50 
51 namespace lsst { namespace ap { namespace utils {
52 
65 template <typename T>
66 class Arena {
67 public:
68  Arena(size_t blockCapacity=262144/sizeof(T));
69  ~Arena();
70 
71  inline void destroy(T *ptr);
72 
73  inline size_t capacity() const;
74  inline size_t getNumBytes() const;
75  inline size_t getBlockCapacity() const;
76 
77 private:
78  // T might be or contain a vector type (e.g. an SSEx type).
79  // These must be 16 byte aligned on some platforms.
80  static const size_t ALIGN = 16;
81  static const size_t SIZE = (sizeof(T) + ALIGN - 1) & ~(ALIGN - 1);
82 
83  BOOST_STATIC_ASSERT((ALIGN & (ALIGN - 1)) == 0);
84 
85  // disable copy construction/assignment
86  Arena(Arena const &);
87  Arena & operator=(Arena const &);
88 
89  inline void *_alloc();
90  inline void _dealloc(void *ptr);
91  void _grow();
92 
93  static inline unsigned char * _align(unsigned char *p);
94 
95  std::vector<unsigned char *> _blocks;
96  std::vector<std::vector<bool> > _masks;
97  size_t const _blockCapacity;
101  size_t _nFree;
102  unsigned char *_free;
103 
104  friend void * (::operator new<>)(size_t, lsst::ap::utils::Arena<T> &);
105  friend void (::operator delete<>)(void *, lsst::ap::utils::Arena<T> &);
106 };
107 
108 }}} // namespace lsst::ap::utils
109 
110 #include "Arena.cc"
111 
112 
113 template <typename T>
114 inline void * operator new(size_t, lsst::ap::utils::Arena<T> &arena) {
115  return arena._alloc();
116 }
117 
118 template <typename T>
119 inline void operator delete(void *ptr, lsst::ap::utils::Arena<T> &arena) {
120  arena._dealloc(ptr);
121 }
122 
123 #endif // LSST_AP_UTILS_ARENA_H
124 
unsigned char * _free
Head of linked free-list, 0 if arena is full.
Definition: Arena.h:102
static unsigned char * _align(unsigned char *p)
Definition: Arena.cc:221
size_t getNumBytes() const
Definition: Arena.cc:154
Single threaded arena (pool) memory allocator.
size_t _nFree
Number of free elements.
Definition: Arena.h:101
size_t getBlockCapacity() const
Definition: Arena.cc:161
friend void(::operator delete<>)(void *
Arena & operator=(Arena const &)
size_t const _blockCapacity
Capacity of a memory block.
Definition: Arena.h:100
void _dealloc(void *ptr)
Definition: Arena.cc:129
BOOST_STATIC_ASSERT((ALIGN &(ALIGN-1))==0)
size_t capacity() const
Definition: Arena.cc:147
std::vector< std::vector< bool > > _masks
Definition: Arena.h:96
std::vector< unsigned char * > _blocks
List of memory blocks.
Definition: Arena.h:95
static const size_t SIZE
Definition: Arena.h:81
static const size_t ALIGN
Definition: Arena.h:80
void destroy(T *ptr)
Definition: Arena.cc:139
Arena(size_t blockCapacity=262144/sizeof(T))
Definition: Arena.cc:41
Include files required for standard LSST Exception handling.