Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement DefaultAllocator for POSIX, minor compilation fixes #816

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is only for consistency.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without std sqrt is a C function which takes and returns double. Math::maximum takes arguments of equal types.
Besides I think sqrt should compute in floats since all computations here do that.

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