include/mempool.h
branchpyrit
changeset 94 4c8abb8977dc
parent 93 96d65f841791
child 100 c005054bf4c1
equal deleted inserted replaced
93:96d65f841791 94:4c8abb8977dc
     1 /*
     1 /**
     2  * mempool.h: memory pool
     2  * @file  mempool.h
       
     3  * @brief Memory pool for aligned allocation
     3  *
     4  *
     4  * This file is part of Pyrit Ray Tracer.
     5  * This file is part of Pyrit Ray Tracer.
     5  *
     6  *
     6  * Copyright 2008  Radek Brich
     7  * Copyright 2008  Radek Brich
     7  *
     8  *
    27 #ifndef MEMPOOL_H
    28 #ifndef MEMPOOL_H
    28 #define MEMPOOL_H
    29 #define MEMPOOL_H
    29 
    30 
    30 #include "common.h"
    31 #include "common.h"
    31 
    32 
       
    33 /**
       
    34  * Memory pool template
       
    35  */
    32 template <typename Type>
    36 template <typename Type>
    33 class MemoryPool
    37 class MemoryPool
    34 {
    38 {
    35 	void *mem;
    39 	void *mem;
    36 	size_t cur, size, typesize, align;
    40 	size_t cur, size, typesize, align;
    43 #else
    47 #else
    44 		mem = (Type *)malloc(size * typesize);
    48 		mem = (Type *)malloc(size * typesize);
    45 #endif
    49 #endif
    46 	};
    50 	};
    47 public:
    51 public:
       
    52 	/**
       
    53 	 * Construct memory pool with default alignment (16)
       
    54 	 * @see MemoryPool(size_t, size_t)
       
    55 	 */
    48 	MemoryPool(const size_t inisize):
    56 	MemoryPool(const size_t inisize):
    49 		cur(0), size(inisize), align(16) { init(); };
    57 		cur(0), size(inisize), align(16) { init(); };
       
    58 
       
    59 	/**
       
    60 	 * Construct memory pool
       
    61 	 * @param[in] inisize  Initial capacity in Type count
       
    62 	 * @param[in] inialign Set memory alignement of items
       
    63 	 */
    50 	MemoryPool(const size_t inisize, const size_t inialign):
    64 	MemoryPool(const size_t inisize, const size_t inialign):
    51 		cur(0), size(inisize), align(inialign) { init(); };
    65 		cur(0), size(inisize), align(inialign) { init(); };
    52 	~MemoryPool()
    66 	~MemoryPool()
    53 	{
    67 	{
    54 #ifndef NO_SIMD
    68 #ifndef NO_SIMD
    56 #else
    70 #else
    57 		free(mem);
    71 		free(mem);
    58 #endif
    72 #endif
    59 	};
    73 	};
    60 
    74 
       
    75 	/**
       
    76 	 * Allocate memory for one item of type Type
       
    77 	 * @return Pointer to allocated memory
       
    78 	 */
    61 	void *alloc()
    79 	void *alloc()
    62 	{
    80 	{
    63 		if (cur == size)
    81 		if (cur == size)
    64 		{
    82 		{
    65 #ifndef NO_SIMD
    83 #ifndef NO_SIMD