openCV library for Renesas RZ/A

Dependents:   RZ_A2M_Mbed_samples

Committer:
RyoheiHagimoto
Date:
Fri Jan 29 04:53:38 2021 +0000
Revision:
0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:0e0631af0305 1 /***********************************************************************
RyoheiHagimoto 0:0e0631af0305 2 * Software License Agreement (BSD License)
RyoheiHagimoto 0:0e0631af0305 3 *
RyoheiHagimoto 0:0e0631af0305 4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
RyoheiHagimoto 0:0e0631af0305 5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
RyoheiHagimoto 0:0e0631af0305 6 *
RyoheiHagimoto 0:0e0631af0305 7 * THE BSD LICENSE
RyoheiHagimoto 0:0e0631af0305 8 *
RyoheiHagimoto 0:0e0631af0305 9 * Redistribution and use in source and binary forms, with or without
RyoheiHagimoto 0:0e0631af0305 10 * modification, are permitted provided that the following conditions
RyoheiHagimoto 0:0e0631af0305 11 * are met:
RyoheiHagimoto 0:0e0631af0305 12 *
RyoheiHagimoto 0:0e0631af0305 13 * 1. Redistributions of source code must retain the above copyright
RyoheiHagimoto 0:0e0631af0305 14 * notice, this list of conditions and the following disclaimer.
RyoheiHagimoto 0:0e0631af0305 15 * 2. Redistributions in binary form must reproduce the above copyright
RyoheiHagimoto 0:0e0631af0305 16 * notice, this list of conditions and the following disclaimer in the
RyoheiHagimoto 0:0e0631af0305 17 * documentation and/or other materials provided with the distribution.
RyoheiHagimoto 0:0e0631af0305 18 *
RyoheiHagimoto 0:0e0631af0305 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
RyoheiHagimoto 0:0e0631af0305 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
RyoheiHagimoto 0:0e0631af0305 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
RyoheiHagimoto 0:0e0631af0305 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
RyoheiHagimoto 0:0e0631af0305 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
RyoheiHagimoto 0:0e0631af0305 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
RyoheiHagimoto 0:0e0631af0305 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
RyoheiHagimoto 0:0e0631af0305 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
RyoheiHagimoto 0:0e0631af0305 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
RyoheiHagimoto 0:0e0631af0305 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
RyoheiHagimoto 0:0e0631af0305 29 *************************************************************************/
RyoheiHagimoto 0:0e0631af0305 30
RyoheiHagimoto 0:0e0631af0305 31 #ifndef OPENCV_FLANN_ALLOCATOR_H_
RyoheiHagimoto 0:0e0631af0305 32 #define OPENCV_FLANN_ALLOCATOR_H_
RyoheiHagimoto 0:0e0631af0305 33
RyoheiHagimoto 0:0e0631af0305 34 #include <stdlib.h>
RyoheiHagimoto 0:0e0631af0305 35 #include <stdio.h>
RyoheiHagimoto 0:0e0631af0305 36
RyoheiHagimoto 0:0e0631af0305 37
RyoheiHagimoto 0:0e0631af0305 38 namespace cvflann
RyoheiHagimoto 0:0e0631af0305 39 {
RyoheiHagimoto 0:0e0631af0305 40
RyoheiHagimoto 0:0e0631af0305 41 /**
RyoheiHagimoto 0:0e0631af0305 42 * Allocates (using C's malloc) a generic type T.
RyoheiHagimoto 0:0e0631af0305 43 *
RyoheiHagimoto 0:0e0631af0305 44 * Params:
RyoheiHagimoto 0:0e0631af0305 45 * count = number of instances to allocate.
RyoheiHagimoto 0:0e0631af0305 46 * Returns: pointer (of type T*) to memory buffer
RyoheiHagimoto 0:0e0631af0305 47 */
RyoheiHagimoto 0:0e0631af0305 48 template <typename T>
RyoheiHagimoto 0:0e0631af0305 49 T* allocate(size_t count = 1)
RyoheiHagimoto 0:0e0631af0305 50 {
RyoheiHagimoto 0:0e0631af0305 51 T* mem = (T*) ::malloc(sizeof(T)*count);
RyoheiHagimoto 0:0e0631af0305 52 return mem;
RyoheiHagimoto 0:0e0631af0305 53 }
RyoheiHagimoto 0:0e0631af0305 54
RyoheiHagimoto 0:0e0631af0305 55
RyoheiHagimoto 0:0e0631af0305 56 /**
RyoheiHagimoto 0:0e0631af0305 57 * Pooled storage allocator
RyoheiHagimoto 0:0e0631af0305 58 *
RyoheiHagimoto 0:0e0631af0305 59 * The following routines allow for the efficient allocation of storage in
RyoheiHagimoto 0:0e0631af0305 60 * small chunks from a specified pool. Rather than allowing each structure
RyoheiHagimoto 0:0e0631af0305 61 * to be freed individually, an entire pool of storage is freed at once.
RyoheiHagimoto 0:0e0631af0305 62 * This method has two advantages over just using malloc() and free(). First,
RyoheiHagimoto 0:0e0631af0305 63 * it is far more efficient for allocating small objects, as there is
RyoheiHagimoto 0:0e0631af0305 64 * no overhead for remembering all the information needed to free each
RyoheiHagimoto 0:0e0631af0305 65 * object or consolidating fragmented memory. Second, the decision about
RyoheiHagimoto 0:0e0631af0305 66 * how long to keep an object is made at the time of allocation, and there
RyoheiHagimoto 0:0e0631af0305 67 * is no need to track down all the objects to free them.
RyoheiHagimoto 0:0e0631af0305 68 *
RyoheiHagimoto 0:0e0631af0305 69 */
RyoheiHagimoto 0:0e0631af0305 70
RyoheiHagimoto 0:0e0631af0305 71 const size_t WORDSIZE=16;
RyoheiHagimoto 0:0e0631af0305 72 const size_t BLOCKSIZE=8192;
RyoheiHagimoto 0:0e0631af0305 73
RyoheiHagimoto 0:0e0631af0305 74 class PooledAllocator
RyoheiHagimoto 0:0e0631af0305 75 {
RyoheiHagimoto 0:0e0631af0305 76 /* We maintain memory alignment to word boundaries by requiring that all
RyoheiHagimoto 0:0e0631af0305 77 allocations be in multiples of the machine wordsize. */
RyoheiHagimoto 0:0e0631af0305 78 /* Size of machine word in bytes. Must be power of 2. */
RyoheiHagimoto 0:0e0631af0305 79 /* Minimum number of bytes requested at a time from the system. Must be multiple of WORDSIZE. */
RyoheiHagimoto 0:0e0631af0305 80
RyoheiHagimoto 0:0e0631af0305 81
RyoheiHagimoto 0:0e0631af0305 82 int remaining; /* Number of bytes left in current block of storage. */
RyoheiHagimoto 0:0e0631af0305 83 void* base; /* Pointer to base of current block of storage. */
RyoheiHagimoto 0:0e0631af0305 84 void* loc; /* Current location in block to next allocate memory. */
RyoheiHagimoto 0:0e0631af0305 85 int blocksize;
RyoheiHagimoto 0:0e0631af0305 86
RyoheiHagimoto 0:0e0631af0305 87
RyoheiHagimoto 0:0e0631af0305 88 public:
RyoheiHagimoto 0:0e0631af0305 89 int usedMemory;
RyoheiHagimoto 0:0e0631af0305 90 int wastedMemory;
RyoheiHagimoto 0:0e0631af0305 91
RyoheiHagimoto 0:0e0631af0305 92 /**
RyoheiHagimoto 0:0e0631af0305 93 Default constructor. Initializes a new pool.
RyoheiHagimoto 0:0e0631af0305 94 */
RyoheiHagimoto 0:0e0631af0305 95 PooledAllocator(int blockSize = BLOCKSIZE)
RyoheiHagimoto 0:0e0631af0305 96 {
RyoheiHagimoto 0:0e0631af0305 97 blocksize = blockSize;
RyoheiHagimoto 0:0e0631af0305 98 remaining = 0;
RyoheiHagimoto 0:0e0631af0305 99 base = NULL;
RyoheiHagimoto 0:0e0631af0305 100
RyoheiHagimoto 0:0e0631af0305 101 usedMemory = 0;
RyoheiHagimoto 0:0e0631af0305 102 wastedMemory = 0;
RyoheiHagimoto 0:0e0631af0305 103 }
RyoheiHagimoto 0:0e0631af0305 104
RyoheiHagimoto 0:0e0631af0305 105 /**
RyoheiHagimoto 0:0e0631af0305 106 * Destructor. Frees all the memory allocated in this pool.
RyoheiHagimoto 0:0e0631af0305 107 */
RyoheiHagimoto 0:0e0631af0305 108 ~PooledAllocator()
RyoheiHagimoto 0:0e0631af0305 109 {
RyoheiHagimoto 0:0e0631af0305 110 void* prev;
RyoheiHagimoto 0:0e0631af0305 111
RyoheiHagimoto 0:0e0631af0305 112 while (base != NULL) {
RyoheiHagimoto 0:0e0631af0305 113 prev = *((void**) base); /* Get pointer to prev block. */
RyoheiHagimoto 0:0e0631af0305 114 ::free(base);
RyoheiHagimoto 0:0e0631af0305 115 base = prev;
RyoheiHagimoto 0:0e0631af0305 116 }
RyoheiHagimoto 0:0e0631af0305 117 }
RyoheiHagimoto 0:0e0631af0305 118
RyoheiHagimoto 0:0e0631af0305 119 /**
RyoheiHagimoto 0:0e0631af0305 120 * Returns a pointer to a piece of new memory of the given size in bytes
RyoheiHagimoto 0:0e0631af0305 121 * allocated from the pool.
RyoheiHagimoto 0:0e0631af0305 122 */
RyoheiHagimoto 0:0e0631af0305 123 void* allocateMemory(int size)
RyoheiHagimoto 0:0e0631af0305 124 {
RyoheiHagimoto 0:0e0631af0305 125 int blockSize;
RyoheiHagimoto 0:0e0631af0305 126
RyoheiHagimoto 0:0e0631af0305 127 /* Round size up to a multiple of wordsize. The following expression
RyoheiHagimoto 0:0e0631af0305 128 only works for WORDSIZE that is a power of 2, by masking last bits of
RyoheiHagimoto 0:0e0631af0305 129 incremented size to zero.
RyoheiHagimoto 0:0e0631af0305 130 */
RyoheiHagimoto 0:0e0631af0305 131 size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
RyoheiHagimoto 0:0e0631af0305 132
RyoheiHagimoto 0:0e0631af0305 133 /* Check whether a new block must be allocated. Note that the first word
RyoheiHagimoto 0:0e0631af0305 134 of a block is reserved for a pointer to the previous block.
RyoheiHagimoto 0:0e0631af0305 135 */
RyoheiHagimoto 0:0e0631af0305 136 if (size > remaining) {
RyoheiHagimoto 0:0e0631af0305 137
RyoheiHagimoto 0:0e0631af0305 138 wastedMemory += remaining;
RyoheiHagimoto 0:0e0631af0305 139
RyoheiHagimoto 0:0e0631af0305 140 /* Allocate new storage. */
RyoheiHagimoto 0:0e0631af0305 141 blockSize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
RyoheiHagimoto 0:0e0631af0305 142 size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
RyoheiHagimoto 0:0e0631af0305 143
RyoheiHagimoto 0:0e0631af0305 144 // use the standard C malloc to allocate memory
RyoheiHagimoto 0:0e0631af0305 145 void* m = ::malloc(blockSize);
RyoheiHagimoto 0:0e0631af0305 146 if (!m) {
RyoheiHagimoto 0:0e0631af0305 147 fprintf(stderr,"Failed to allocate memory.\n");
RyoheiHagimoto 0:0e0631af0305 148 return NULL;
RyoheiHagimoto 0:0e0631af0305 149 }
RyoheiHagimoto 0:0e0631af0305 150
RyoheiHagimoto 0:0e0631af0305 151 /* Fill first word of new block with pointer to previous block. */
RyoheiHagimoto 0:0e0631af0305 152 ((void**) m)[0] = base;
RyoheiHagimoto 0:0e0631af0305 153 base = m;
RyoheiHagimoto 0:0e0631af0305 154
RyoheiHagimoto 0:0e0631af0305 155 int shift = 0;
RyoheiHagimoto 0:0e0631af0305 156 //int shift = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
RyoheiHagimoto 0:0e0631af0305 157
RyoheiHagimoto 0:0e0631af0305 158 remaining = blockSize - sizeof(void*) - shift;
RyoheiHagimoto 0:0e0631af0305 159 loc = ((char*)m + sizeof(void*) + shift);
RyoheiHagimoto 0:0e0631af0305 160 }
RyoheiHagimoto 0:0e0631af0305 161 void* rloc = loc;
RyoheiHagimoto 0:0e0631af0305 162 loc = (char*)loc + size;
RyoheiHagimoto 0:0e0631af0305 163 remaining -= size;
RyoheiHagimoto 0:0e0631af0305 164
RyoheiHagimoto 0:0e0631af0305 165 usedMemory += size;
RyoheiHagimoto 0:0e0631af0305 166
RyoheiHagimoto 0:0e0631af0305 167 return rloc;
RyoheiHagimoto 0:0e0631af0305 168 }
RyoheiHagimoto 0:0e0631af0305 169
RyoheiHagimoto 0:0e0631af0305 170 /**
RyoheiHagimoto 0:0e0631af0305 171 * Allocates (using this pool) a generic type T.
RyoheiHagimoto 0:0e0631af0305 172 *
RyoheiHagimoto 0:0e0631af0305 173 * Params:
RyoheiHagimoto 0:0e0631af0305 174 * count = number of instances to allocate.
RyoheiHagimoto 0:0e0631af0305 175 * Returns: pointer (of type T*) to memory buffer
RyoheiHagimoto 0:0e0631af0305 176 */
RyoheiHagimoto 0:0e0631af0305 177 template <typename T>
RyoheiHagimoto 0:0e0631af0305 178 T* allocate(size_t count = 1)
RyoheiHagimoto 0:0e0631af0305 179 {
RyoheiHagimoto 0:0e0631af0305 180 T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
RyoheiHagimoto 0:0e0631af0305 181 return mem;
RyoheiHagimoto 0:0e0631af0305 182 }
RyoheiHagimoto 0:0e0631af0305 183
RyoheiHagimoto 0:0e0631af0305 184 };
RyoheiHagimoto 0:0e0631af0305 185
RyoheiHagimoto 0:0e0631af0305 186 }
RyoheiHagimoto 0:0e0631af0305 187
RyoheiHagimoto 0:0e0631af0305 188 #endif //OPENCV_FLANN_ALLOCATOR_H_