diff --git a/source/lib/sysdep/rtl/gcc/gcc.cpp b/source/lib/sysdep/rtl/gcc/gcc.cpp index c01d3188df..8bca002088 100644 --- a/source/lib/sysdep/rtl/gcc/gcc.cpp +++ b/source/lib/sysdep/rtl/gcc/gcc.cpp @@ -28,7 +28,7 @@ void* rtl_AllocateAligned(size_t size, size_t alignment) { void *ptr; - int ret = posix_memalign(&ptr, size, alignment); + int ret = posix_memalign(&ptr, alignment, size); if (ret) { // TODO: report error? return NULL; @@ -72,7 +72,8 @@ void* rtl_AllocateAligned(size_t size, size_t align) void rtl_FreeAligned(void* alignedPointer) { - free(((void**)alignedPointer)[-1]); + if (alignedPointer) + free(((void**)alignedPointer)[-1]); } #endif diff --git a/source/lib/sysdep/tests/test_rtl.h b/source/lib/sysdep/tests/test_rtl.h new file mode 100644 index 0000000000..f52f83a19c --- /dev/null +++ b/source/lib/sysdep/tests/test_rtl.h @@ -0,0 +1,49 @@ +/* Copyright (C) 2009 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "lib/self_test.h" + +#include "lib/sysdep/rtl.h" + +class Test_rtl : public CxxTest::TestSuite +{ + void _test_AllocateAligned_helper(size_t size, size_t align) + { + void* p = rtl_AllocateAligned(size, align); + TS_ASSERT(p != NULL); + TS_ASSERT_EQUALS((intptr_t)p % align, 0u); + memset(p, 0x42, size); + rtl_FreeAligned(p); + } +public: + void test_AllocateAligned() + { + for (size_t s = 0; s < 64; ++s) + { + _test_AllocateAligned_helper(s, 8); + _test_AllocateAligned_helper(s, 16); + _test_AllocateAligned_helper(s, 64); + _test_AllocateAligned_helper(s, 1024); + _test_AllocateAligned_helper(s, 65536); + } + } + + void test_FreeAligned_null() + { + rtl_FreeAligned(NULL); + } +};