Skip to content

Commit db11bee

Browse files
committedOct 14, 2022
[alloc] Fixing linking problem with embree::alignedMalloc
Replacing the use of embree::alignedMalloc with an inlined version of openpgl::alignedMalloc
1 parent a1404c5 commit db11bee

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed
 

‎openpgl/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ set(OPENPGL_SRC
2020
directional/IVolumeSamplingDistribution.cpp
2121
directional/vmm/VMMPhaseFunctions.cpp
2222
../third-party/embreeSrc/common/simd/sse.cpp
23-
../third-party/embreeSrc/common/sys/alloc.cpp
2423
)
2524

2625
if(OPENPGL_BUILD_STATIC)

‎openpgl/directional/vmm/VMMSurfaceSamplingDistribution.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace openpgl
1313
template<class TVMMDistribution, bool UseParallaxCompensation>
1414
struct __aligned(TVMMDistribution::VectorSize*4) VMMSurfaceSamplingDistribution: public ISurfaceSamplingDistribution
1515
{
16-
ALIGNED_STRUCT_(TVMMDistribution::VectorSize*4)
16+
OPENPGL_ALIGNED_STRUCT_(TVMMDistribution::VectorSize*4)
1717

1818
VMMSurfaceSamplingDistribution() {};
1919
~VMMSurfaceSamplingDistribution() = default;

‎openpgl/directional/vmm/VMMVolumeSamplingDistribution.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ template<class TVMMDistribution, bool UseParallaxCompensation>
1313
struct __aligned(TVMMDistribution::VectorSize*4) VMMVolumeSamplingDistribution: public IVolumeSamplingDistribution
1414
{
1515

16-
ALIGNED_STRUCT_(TVMMDistribution::VectorSize*4)
16+
OPENPGL_ALIGNED_STRUCT_(TVMMDistribution::VectorSize*4)
1717
VMMVolumeSamplingDistribution(): IVolumeSamplingDistribution(){};
1818
~VMMVolumeSamplingDistribution() = default;
1919

‎openpgl/openpgl_common.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ inline void sincosf(const float theta, float* sin, float* cos)
2323

2424
#endif
2525

26+
namespace openpgl
27+
{
28+
29+
inline void* alignedMalloc(size_t size, size_t align)
30+
{
31+
if (size == 0)
32+
return nullptr;
33+
34+
assert((align & (align-1)) == 0);
35+
void* ptr = _mm_malloc(size,align);
36+
37+
if (size != 0 && ptr == nullptr)
38+
throw std::bad_alloc();
39+
40+
return ptr;
41+
}
42+
43+
inline void alignedFree(void* ptr)
44+
{
45+
if (ptr)
46+
_mm_free(ptr);
47+
}
48+
}
49+
50+
#define OPENPGL_ALIGNED_STRUCT_(align) \
51+
void* operator new(size_t size) { return openpgl::alignedMalloc(size,align); } \
52+
void operator delete(void* ptr) { openpgl::alignedFree(ptr); } \
53+
void* operator new[](size_t size) { return openpgl::alignedMalloc(size,align); } \
54+
void operator delete[](void* ptr) { openpgl::alignedFree(ptr); }
2655

2756
namespace openpgl
2857
{

‎openpgl/spatial/KNN.h

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,6 @@
1818
#define NUM_KNN_NEIGHBOURS 8
1919
#define DEBUG_SAMPLE_APPROXIMATE_CLOSEST_REGION_IDX 0
2020

21-
inline void* myalignedMalloc(size_t size, size_t align)
22-
{
23-
if (size == 0)
24-
return nullptr;
25-
26-
assert((align & (align-1)) == 0);
27-
void* ptr = _mm_malloc(size,align);
28-
29-
if (size != 0 && ptr == nullptr)
30-
throw std::bad_alloc();
31-
32-
return ptr;
33-
}
34-
35-
inline void myalignedFree(void* ptr)
36-
{
37-
if (ptr)
38-
_mm_free(ptr);
39-
}
40-
41-
42-
4321
namespace openpgl
4422
{
4523

@@ -181,7 +159,7 @@ struct KNearestRegionsSearchTree
181159
{
182160
struct Point
183161
{
184-
ALIGNED_STRUCT_(16)
162+
OPENPGL_ALIGNED_STRUCT_(16)
185163
embree::Vec3fa p; //!< position
186164
};
187165

@@ -201,8 +179,8 @@ struct KNearestRegionsSearchTree
201179

202180
~KNearestRegionsSearchTree()
203181
{
204-
myalignedFree(points);
205-
myalignedFree(neighbours);
182+
alignedFree(points);
183+
alignedFree(neighbours);
206184
}
207185

208186
template<typename TRegionStorageContainer>
@@ -211,9 +189,9 @@ struct KNearestRegionsSearchTree
211189
num_points = regionStorage.size();
212190
if (points)
213191
{
214-
myalignedFree(points);
192+
alignedFree(points);
215193
}
216-
points = (Point*) myalignedMalloc(num_points*sizeof(Point), 32);
194+
points = (Point*) alignedMalloc(num_points*sizeof(Point), 32);
217195

218196
for (size_t i = 0; i < num_points; i++)
219197
{
@@ -235,9 +213,9 @@ struct KNearestRegionsSearchTree
235213

236214
if (neighbours)
237215
{
238-
myalignedFree(neighbours);
216+
alignedFree(neighbours);
239217
}
240-
neighbours = (RN*) myalignedMalloc(num_points*sizeof(RN), 32);
218+
neighbours = (RN*) alignedMalloc(num_points*sizeof(RN), 32);
241219
#if defined(OPENPGL_USE_OMP_THREADING)
242220
#pragma omp parallel for num_threads(this->m_nCores) schedule(dynamic)
243221
for (size_t n=0; n < num_points; n++)
@@ -360,14 +338,14 @@ struct KNearestRegionsSearchTree
360338
{
361339
if(points)
362340
{
363-
myalignedFree(points);
341+
alignedFree(points);
364342
points = nullptr;
365343
num_points = 0;
366344
}
367345

368346
if(neighbours)
369347
{
370-
myalignedFree(neighbours);
348+
alignedFree(neighbours);
371349
neighbours = nullptr;
372350
}
373351

@@ -381,7 +359,7 @@ struct KNearestRegionsSearchTree
381359
if(_isBuild)
382360
{
383361
stream.read(reinterpret_cast<char*>(&num_points), sizeof(uint32_t));
384-
points = (Point*) myalignedMalloc(num_points*sizeof(Point), 32);
362+
points = (Point*) alignedMalloc(num_points*sizeof(Point), 32);
385363
for (uint32_t n = 0; n < num_points; n++)
386364
{
387365
Point p;

0 commit comments

Comments
 (0)