OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
nanoflann::PooledAllocator Class Reference

#include <nanoflann.hpp>

+ Collaboration diagram for nanoflann::PooledAllocator:

Public Member Functions

 PooledAllocator (const size_t blocksize_=BLOCKSIZE)
 Default constructor.
 
 ~PooledAllocator ()
 Destructor.
 
void free_all ()
 Frees all allocated memory chunks.
 
void * malloc (const size_t req_size)
 Returns a pointer to a piece of new memory of the given size in bytes allocated from the pool.
 
template<typename T >
T * allocate (const size_t count=1)
 Allocates (using this pool) a generic type T.
 

Public Attributes

size_t usedMemory
 
size_t wastedMemory
 

Detailed Description

Definition at line 532 of file nanoflann.hpp.

Constructor & Destructor Documentation

◆ PooledAllocator()

nanoflann::PooledAllocator::PooledAllocator ( const size_t blocksize_ = BLOCKSIZE)
inline

Default constructor.

Initializes a new pool.

Definition at line 557 of file nanoflann.hpp.

558 : blocksize(blocksize_) {
559 internal_init();
560 }

◆ ~PooledAllocator()

nanoflann::PooledAllocator::~PooledAllocator ( )
inline

Destructor.

Frees all the memory allocated in this pool.

Definition at line 565 of file nanoflann.hpp.

565 {
566 free_all();
567 }
void free_all()
Frees all allocated memory chunks.

Member Function Documentation

◆ allocate()

template<typename T >
T * nanoflann::PooledAllocator::allocate ( const size_t count = 1)
inline

Allocates (using this pool) a generic type T.

Params: count = number of instances to allocate. Returns: pointer (of type T*) to memory buffer

Definition at line 636 of file nanoflann.hpp.

636 {
637 T* mem = (T*) this->malloc(sizeof(T) * count);
638 return mem;
639 }
void * malloc(const size_t req_size)
Returns a pointer to a piece of new memory of the given size in bytes allocated from the pool.

◆ free_all()

void nanoflann::PooledAllocator::free_all ( )
inline

Frees all allocated memory chunks.

Definition at line 570 of file nanoflann.hpp.

570 {
571 while (base != NULL) {
572 void *prev = *((void**) base); /* Get pointer to prev block. */
573 ::free(base);
574 base = prev;
575 }
576 internal_init();
577 }

◆ malloc()

void * nanoflann::PooledAllocator::malloc ( const size_t req_size)
inline

Returns a pointer to a piece of new memory of the given size in bytes allocated from the pool.

Definition at line 583 of file nanoflann.hpp.

583 {
584 /* Round size up to a multiple of wordsize. The following expression
585 only works for WORDSIZE that is a power of 2, by masking last bits of
586 incremented size to zero.
587 */
588 const size_t size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
589
590 /* Check whether a new block must be allocated. Note that the first word
591 of a block is reserved for a pointer to the previous block.
592 */
593 if (size > remaining) {
594
595 wastedMemory += remaining;
596
597 /* Allocate new storage. */
598 const size_t blocksize =
599 (size + sizeof(void*) + (WORDSIZE - 1) > BLOCKSIZE) ?
600 size + sizeof(void*) + (WORDSIZE - 1) : BLOCKSIZE;
601
602 // use the standard C malloc to allocate memory
603 void* m = ::malloc(blocksize);
604 if (!m) {
605 fprintf(stderr, "Failed to allocate memory.\n");
606 return NULL;
607 }
608
609 /* Fill first word of new block with pointer to previous block. */
610 ((void**) m)[0] = base;
611 base = m;
612
613 size_t shift = 0;
614 //int size_t = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
615
616 remaining = blocksize - sizeof(void*) - shift;
617 loc = ((char*) m + sizeof(void*) + shift);
618 }
619 void* rloc = loc;
620 loc = (char*) loc + size;
621 remaining -= size;
622
623 usedMemory += size;
624
625 return rloc;
626 }
const size_t WORDSIZE
Pooled storage allocator.
const size_t BLOCKSIZE
constexpr T m(unsigned iPop, unsigned jPop, tag::MRT)

References nanoflann::BLOCKSIZE, and nanoflann::WORDSIZE.

Member Data Documentation

◆ usedMemory

size_t nanoflann::PooledAllocator::usedMemory

Definition at line 551 of file nanoflann.hpp.

◆ wastedMemory

size_t nanoflann::PooledAllocator::wastedMemory

Definition at line 552 of file nanoflann.hpp.


The documentation for this class was generated from the following file: