1
0
forked from 0ad/0ad

Fix rtl_AllocateAligned on GCC. Add tests for it.

This was SVN commit r7255.
This commit is contained in:
Ykkrosh 2010-01-07 18:52:29 +00:00
parent d0e741b261
commit 8dca5f7320
2 changed files with 52 additions and 2 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
};