allocator.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  *
7  * THE BSD LICENSE
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *************************************************************************/
30 
31 #ifndef RTABMAP_FLANN_ALLOCATOR_H_
32 #define RTABMAP_FLANN_ALLOCATOR_H_
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 
37 
38 namespace rtflann
39 {
40 
48 template <typename T>
49 T* allocate(size_t count = 1)
50 {
51  T* mem = (T*) ::malloc(sizeof(T)*count);
52  return mem;
53 }
54 
55 
56 
72 const size_t WORDSIZE=16;
73 const size_t BLOCKSIZE=8192;
74 
76 {
77  /* We maintain memory alignment to word boundaries by requiring that all
78  allocations be in multiples of the machine wordsize. */
79  /* Size of machine word in bytes. Must be power of 2. */
80  /* Minimum number of bytes requested at a time from the system. Must be multiple of WORDSIZE. */
81 
82 
83  int remaining; /* Number of bytes left in current block of storage. */
84  void* base; /* Pointer to base of current block of storage. */
85  void* loc; /* Current location in block to next allocate memory. */
86  int blocksize;
87 
88 
89 public:
92 
96  PooledAllocator(int blocksize = BLOCKSIZE)
97  {
98  this->blocksize = blocksize;
99  remaining = 0;
100  base = NULL;
101 
102  usedMemory = 0;
103  wastedMemory = 0;
104  }
105 
110  {
111  free();
112  }
113 
114  void free()
115  {
116  void* prev;
117  while (base != NULL) {
118  prev = *((void**) base); /* Get pointer to prev block. */
119  ::free(base);
120  base = prev;
121  }
122  base = NULL;
123  remaining = 0;
124  usedMemory = 0;
125  wastedMemory = 0;
126  }
127 
132  void* allocateMemory(int size)
133  {
134  int blocksize;
135 
136  /* Round size up to a multiple of wordsize. The following expression
137  only works for WORDSIZE that is a power of 2, by masking last bits of
138  incremented size to zero.
139  */
140  size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
141 
142  /* Check whether a new block must be allocated. Note that the first word
143  of a block is reserved for a pointer to the previous block.
144  */
145  if (size > remaining) {
146 
147  wastedMemory += remaining;
148 
149  /* Allocate new storage. */
150  blocksize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
151  size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
152 
153  // use the standard C malloc to allocate memory
154  void* m = ::malloc(blocksize);
155  if (!m) {
156  fprintf(stderr,"Failed to allocate memory.\n");
157  return NULL;
158  }
159 
160  /* Fill first word of new block with pointer to previous block. */
161  ((void**) m)[0] = base;
162  base = m;
163 
164  int shift = 0;
165  //int shift = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
166 
167  remaining = blocksize - sizeof(void*) - shift;
168  loc = ((char*)m + sizeof(void*) + shift);
169  }
170  void* rloc = loc;
171  loc = (char*)loc + size;
172  remaining -= size;
173 
174  usedMemory += size;
175 
176  return rloc;
177  }
178 
186  template <typename T>
187  T* allocate(size_t count = 1)
188  {
189  T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
190  return mem;
191  }
192 
193 };
194 
195 }
196 
197 inline void* operator new (std::size_t size, rtflann::PooledAllocator& allocator)
198 {
199  return allocator.allocateMemory(size) ;
200 }
201 
202 #endif //FLANN_ALLOCATOR_H_
#define NULL
const size_t BLOCKSIZE
Definition: allocator.h:73
T * allocate(size_t count=1)
Definition: allocator.h:49
void * allocateMemory(int size)
Definition: allocator.h:132
const size_t WORDSIZE
Definition: allocator.h:72
T * allocate(size_t count=1)
Definition: allocator.h:187
PooledAllocator(int blocksize=BLOCKSIZE)
Definition: allocator.h:96


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:30