include/mempool.h
author Radek Brich <radek.brich@devl.cz>
Tue, 26 Jul 2016 18:19:37 +0200
branchpyrit
changeset 104 2274a07510c1
parent 100 c005054bf4c1
permissions -rw-r--r--
Cleanup, dropped Windows support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     1
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     2
 * @file  mempool.h
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     3
 * @brief Memory pool for aligned allocation
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
 * This file is part of Pyrit Ray Tracer.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
 * Copyright 2008  Radek Brich
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
 * of this software and associated documentation files (the "Software"), to deal
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
 * in the Software without restriction, including without limitation the rights
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
 * copies of the Software, and to permit persons to whom the Software is
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
 * furnished to do so, subject to the following conditions:
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
 * The above copyright notice and this permission notice shall be included in
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
 * all copies or substantial portions of the Software.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
 * THE SOFTWARE.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
 */
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#ifndef MEMPOOL_H
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
#define MEMPOOL_H
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
#include "common.h"
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
100
c005054bf4c1 new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    33
#include <string.h>
c005054bf4c1 new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    34
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    35
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    36
 * Memory pool template
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    37
 */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
template <typename Type>
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
class MemoryPool
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
	void *mem;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
	size_t cur, size, typesize, align;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	void init()
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
		typesize = ((sizeof(Type)-1)/align+1)*align;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
		mem = (Type *)_mm_malloc(size * typesize, align);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
#else
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    50
		mem = (Type *)malloc(size * typesize);
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
#endif
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	};
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
public:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    54
	/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    55
	 * Construct memory pool with default alignment (16)
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    56
	 * @see MemoryPool(size_t, size_t)
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    57
	 */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
	MemoryPool(const size_t inisize):
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		cur(0), size(inisize), align(16) { init(); };
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    60
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    61
	/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    62
	 * Construct memory pool
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    63
	 * @param[in] inisize  Initial capacity in Type count
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    64
	 * @param[in] inialign Set memory alignement of items
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    65
	 */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
	MemoryPool(const size_t inisize, const size_t inialign):
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
		cur(0), size(inisize), align(inialign) { init(); };
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
	~MemoryPool()
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
	{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
		_mm_free(mem);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
#else
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
		free(mem);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
#endif
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
	};
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    77
	/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    78
	 * Allocate memory for one item of type Type
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    79
	 * @return Pointer to allocated memory
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    80
	 */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
	void *alloc()
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
		if (cur == size)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
		{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
			void *newmem = _mm_malloc(2 * size * typesize, align);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
#else
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
			void *newmem = malloc(2 * size * typesize);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
#endif
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
			memcpy(newmem, mem, size * typesize);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
			mem = newmem;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
			size *= 2;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
		}
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
		return (void*)((size_t)mem + typesize*(cur++));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
	};
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
};
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
#endif