123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #ifndef OPENCV_FLANN_ALLOCATOR_H_
- #define OPENCV_FLANN_ALLOCATOR_H_
- #include <stdlib.h>
- #include <stdio.h>
- namespace cvflann
- {
- template <typename T>
- T* allocate(size_t count = 1)
- {
- T* mem = (T*) ::malloc(sizeof(T)*count);
- return mem;
- }
- const size_t WORDSIZE=16;
- const size_t BLOCKSIZE=8192;
- class PooledAllocator
- {
-
-
-
- int remaining;
- void* base;
- void* loc;
- int blocksize;
- public:
- int usedMemory;
- int wastedMemory;
-
- PooledAllocator(int blockSize = BLOCKSIZE)
- {
- blocksize = blockSize;
- remaining = 0;
- base = NULL;
- usedMemory = 0;
- wastedMemory = 0;
- }
-
- ~PooledAllocator()
- {
- void* prev;
- while (base != NULL) {
- prev = *((void**) base);
- ::free(base);
- base = prev;
- }
- }
-
- void* allocateMemory(int size)
- {
- int blockSize;
-
- size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
-
- if (size > remaining) {
- wastedMemory += remaining;
-
- blockSize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
- size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
-
- void* m = ::malloc(blockSize);
- if (!m) {
- fprintf(stderr,"Failed to allocate memory.\n");
- return NULL;
- }
-
- ((void**) m)[0] = base;
- base = m;
- int shift = 0;
-
- remaining = blockSize - sizeof(void*) - shift;
- loc = ((char*)m + sizeof(void*) + shift);
- }
- void* rloc = loc;
- loc = (char*)loc + size;
- remaining -= size;
- usedMemory += size;
- return rloc;
- }
-
- template <typename T>
- T* allocate(size_t count = 1)
- {
- T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
- return mem;
- }
- };
- }
- #endif
|