author | Radek Brich <radek.brich@devl.cz> |
Sun, 31 May 2009 16:53:05 +0200 | |
branch | pyrit |
changeset 99 | f3abdaa2e8fb |
parent 94 | 4c8abb8977dc |
child 100 | c005054bf4c1 |
permissions | -rw-r--r-- |
94 | 1 |
/** |
2 |
* @file mempool.h |
|
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 |
|
94 | 33 |
/** |
34 |
* Memory pool template |
|
35 |
*/ |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
template <typename Type> |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
class MemoryPool |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
{ |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
void *mem; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
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
|
41 |
|
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
void init() |
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 |
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
|
45 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
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
|
47 |
#else |
93 | 48 |
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
|
49 |
#endif |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
}; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
public: |
94 | 52 |
/** |
53 |
* Construct memory pool with default alignment (16) |
|
54 |
* @see MemoryPool(size_t, size_t) |
|
55 |
*/ |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
MemoryPool(const size_t inisize): |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
cur(0), size(inisize), align(16) { init(); }; |
94 | 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 |
*/ |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
~MemoryPool() |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
{ |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
_mm_free(mem); |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
#else |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
free(mem); |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
#endif |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
}; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
|
94 | 75 |
/** |
76 |
* Allocate memory for one item of type Type |
|
77 |
* @return Pointer to allocated memory |
|
78 |
*/ |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
void *alloc() |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
{ |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
if (cur == size) |
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 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
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
|
85 |
#else |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
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
|
87 |
#endif |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
memcpy(newmem, mem, size * typesize); |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
mem = newmem; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
size *= 2; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
} |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
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
|
93 |
}; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
}; |
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 |
#endif |