allocator.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef OPENCV_FLANN_ALLOCATOR_H_
  31. #define OPENCV_FLANN_ALLOCATOR_H_
  32. //! @cond IGNORED
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. namespace cvflann
  36. {
  37. /**
  38. * Allocates (using C's malloc) a generic type T.
  39. *
  40. * Params:
  41. * count = number of instances to allocate.
  42. * Returns: pointer (of type T*) to memory buffer
  43. */
  44. template <typename T>
  45. T* allocate(size_t count = 1)
  46. {
  47. T* mem = (T*) ::malloc(sizeof(T)*count);
  48. return mem;
  49. }
  50. /**
  51. * Pooled storage allocator
  52. *
  53. * The following routines allow for the efficient allocation of storage in
  54. * small chunks from a specified pool. Rather than allowing each structure
  55. * to be freed individually, an entire pool of storage is freed at once.
  56. * This method has two advantages over just using malloc() and free(). First,
  57. * it is far more efficient for allocating small objects, as there is
  58. * no overhead for remembering all the information needed to free each
  59. * object or consolidating fragmented memory. Second, the decision about
  60. * how long to keep an object is made at the time of allocation, and there
  61. * is no need to track down all the objects to free them.
  62. *
  63. */
  64. const size_t WORDSIZE=16;
  65. const size_t BLOCKSIZE=8192;
  66. class PooledAllocator
  67. {
  68. /* We maintain memory alignment to word boundaries by requiring that all
  69. allocations be in multiples of the machine wordsize. */
  70. /* Size of machine word in bytes. Must be power of 2. */
  71. /* Minimum number of bytes requested at a time from the system. Must be multiple of WORDSIZE. */
  72. int remaining; /* Number of bytes left in current block of storage. */
  73. void* base; /* Pointer to base of current block of storage. */
  74. void* loc; /* Current location in block to next allocate memory. */
  75. int blocksize;
  76. public:
  77. int usedMemory;
  78. int wastedMemory;
  79. /**
  80. Default constructor. Initializes a new pool.
  81. */
  82. PooledAllocator(int blockSize = BLOCKSIZE)
  83. {
  84. blocksize = blockSize;
  85. remaining = 0;
  86. base = NULL;
  87. loc = NULL;
  88. usedMemory = 0;
  89. wastedMemory = 0;
  90. }
  91. /**
  92. * Destructor. Frees all the memory allocated in this pool.
  93. */
  94. ~PooledAllocator()
  95. {
  96. void* prev;
  97. while (base != NULL) {
  98. prev = *((void**) base); /* Get pointer to prev block. */
  99. ::free(base);
  100. base = prev;
  101. }
  102. }
  103. /**
  104. * Returns a pointer to a piece of new memory of the given size in bytes
  105. * allocated from the pool.
  106. */
  107. void* allocateMemory(int size)
  108. {
  109. int blockSize;
  110. /* Round size up to a multiple of wordsize. The following expression
  111. only works for WORDSIZE that is a power of 2, by masking last bits of
  112. incremented size to zero.
  113. */
  114. size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
  115. /* Check whether a new block must be allocated. Note that the first word
  116. of a block is reserved for a pointer to the previous block.
  117. */
  118. if (size > remaining) {
  119. wastedMemory += remaining;
  120. /* Allocate new storage. */
  121. blockSize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
  122. size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
  123. // use the standard C malloc to allocate memory
  124. void* m = ::malloc(blockSize);
  125. if (!m) {
  126. fprintf(stderr,"Failed to allocate memory.\n");
  127. return NULL;
  128. }
  129. /* Fill first word of new block with pointer to previous block. */
  130. ((void**) m)[0] = base;
  131. base = m;
  132. int shift = 0;
  133. //int shift = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
  134. remaining = blockSize - sizeof(void*) - shift;
  135. loc = ((char*)m + sizeof(void*) + shift);
  136. }
  137. void* rloc = loc;
  138. loc = (char*)loc + size;
  139. remaining -= size;
  140. usedMemory += size;
  141. return rloc;
  142. }
  143. /**
  144. * Allocates (using this pool) a generic type T.
  145. *
  146. * Params:
  147. * count = number of instances to allocate.
  148. * Returns: pointer (of type T*) to memory buffer
  149. */
  150. template <typename T>
  151. T* allocate(size_t count = 1)
  152. {
  153. T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
  154. return mem;
  155. }
  156. private:
  157. PooledAllocator(const PooledAllocator &); // copy disabled
  158. PooledAllocator& operator=(const PooledAllocator &); // assign disabled
  159. };
  160. }
  161. //! @endcond
  162. #endif //OPENCV_FLANN_ALLOCATOR_H_