Hi,

Falcon has it own basic memory manager functions.
In these functions, during memory allocation,
the size of allocated memory is expanded by sizeof(size_t)
[4 bytes, on 32-bit MIPS],
which is used for saving an amount of allocated memory.

The first 4 bytes are used to store the amount.
Because of that,
the pointer is offset by these 4 bytes and
address of the actual data is not aligned by 8.

Bus Error happens for data with an amount of 8 bytes
or larger, due to usage of 32-bit MIPS
instructions ldc1 and sdc1,
for double type access on  address not aligned by 8.


To avoid this behavior I add one more
block of 4 bytes (sizeof(size_t)) during allocation,
to realign the data address.


With these changes I was able to built the falconpl successfully
on my local MIPS board.

Besides that,
I run tests from tests/core/testsuite/.
There were no fails with a Bus Error.


The patch that contains these changes is attached.

Could you consider including this patch?


Thanks,
Dejan
--- a/engine/memory.cpp
+++ b/engine/memory.cpp
@@ -70,7 +70,11 @@
 
 void * DflAccountMemAlloc( size_t amount )
 {
+#if defined (__mips__ ) && !defined(__mips64)
+   size_t *ret =  (size_t*) malloc( amount + 2 * sizeof(size_t) );
+#else
    size_t *ret =  (size_t*) malloc( amount + sizeof(size_t) );
+#endif
    if ( ret == 0 ) {
       printf( "Falcon: fatal allocation error when allocating %d bytes\n", (int) amount );
       exit(1);
@@ -78,7 +82,11 @@
 
    gcMemAccount( amount );
    *ret = amount;
+#if defined (__mips__ ) && !defined(__mips64)
+   return ret+2;
+#else
    return ret+1;
+#endif
 }
 
 
@@ -87,8 +95,13 @@
    if ( mem != 0 )
    {
       size_t *smem = (size_t*) mem;
+#if defined (__mips__ ) && !defined(__mips64)
+      gcMemUnaccount( smem[-2] );
+      free( smem-2 );
+#else
       gcMemUnaccount( smem[-1] );
       free( smem-1 );
+#endif
    }
 }
 
@@ -105,10 +118,18 @@
 
 
    size_t *smem = (size_t*) mem;
+#if defined (__mips__ ) && !defined(__mips64)
+   smem-=2;
+#else
    smem--;
+#endif
    size_t oldalloc = *smem;
 
+#if defined (__mips__ ) && !defined(__mips64)
+   size_t *nsmem = (size_t*) realloc( smem, amount + 2 * sizeof( size_t ) );
+#else
    size_t *nsmem = (size_t*) realloc( smem, amount + sizeof( size_t ) );
+#endif
 
    if ( nsmem == 0 ) {
       printf( "Falcon: fatal reallocation error when allocating %d bytes\n", (int) amount );
@@ -121,7 +142,11 @@
    else
       gcMemUnaccount( oldalloc - amount );
 
+#if defined (__mips__ ) && !defined(__mips64)
+   return nsmem+2;
+#else
    return nsmem+1;
+#endif
 }
 
 

Reply via email to