Skip to content

Commit

Permalink
Merge pull request #816 from MAnyKey/linux-port-3
Browse files Browse the repository at this point in the history
implement DefaultAllocator for POSIX, minor compilation fixes
  • Loading branch information
nem0 committed Apr 21, 2016
2 parents 0c265e5 + c8bf9cf commit 0d20f8b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/engine/core/default_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Lumix
return realloc(ptr, size);
}

#ifdef _WIN32
void* DefaultAllocator::allocate_aligned(size_t size, size_t align)
{
return _aligned_malloc(size, align);
Expand All @@ -39,5 +40,35 @@ namespace Lumix
{
return _aligned_realloc(ptr, size, align);
}
#else
void* DefaultAllocator::allocate_aligned(size_t size, size_t align)
{
return aligned_alloc(align, size);
}


void DefaultAllocator::deallocate_aligned(void* ptr)
{
free(ptr);
}


void* DefaultAllocator::reallocate_aligned(void* ptr, size_t size, size_t align)
{
// POSIX and glibc do not provide a way to realloc with alignment preservation
if (size == 0) {
free(ptr);
return nullptr;
}
void* newptr = aligned_alloc(align, size);
if (newptr == nullptr) {
return nullptr;
}
memcpy(newptr, ptr, size);
free(ptr);
return newptr;
}
#endif


} // ~namespace Lumix
4 changes: 2 additions & 2 deletions src/engine/core/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Frustum::computeOrtho(const Vec3& position,

center = (near_center + far_center) * 0.5f;
float z_diff = far_distance - near_distance;
radius = sqrt(4 * width * width + 4 * height * height + z_diff * z_diff) * 0.5f;
radius = std::sqrt(4 * width * width + 4 * height * height + z_diff * z_diff) * 0.5f;
this->position = position;
}

Expand Down Expand Up @@ -98,7 +98,7 @@ void Frustum::computePerspective(const Vec3& position,
Vec3 corner2 = far_center + x * far_width + y * far_height;

float size = (corner1 - corner2).length();
size = Math::maximum(sqrt(far_width * far_width * 4 + far_height * far_height * 4), size);
size = Math::maximum(std::sqrt(far_width * far_width * 4 + far_height * far_height * 4), size);
this->radius = size * 0.5f;
this->position = position;
this->direction = direction;
Expand Down
2 changes: 0 additions & 2 deletions src/engine/core/path_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#include "path_utils.h"
#include "core/string.h"

Expand Down
4 changes: 4 additions & 0 deletions src/engine/core/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ int compareStringN(const char* lhs, const char* rhs, int length)

int compareIStringN(const char* lhs, const char* rhs, int length)
{
#ifdef _WIN32
return strnicmp(lhs, rhs, length);
#else
return strncasecmp(lhs, rhs, length);
#endif
}


Expand Down

0 comments on commit 0d20f8b

Please sign in to comment.