memset on movable pointers

Technical questions regarding the XTC tools and programming with XMOS.
DemoniacMilk
XCore Addict
Posts: 191
Joined: Tue Jul 05, 2016 2:19 pm

memset on movable pointers

Post by DemoniacMilk »

I tried to use memset on a movable pointer, but that seems to not be working as intended when trying to move the movable pointer afterwards (error: trying to move aliased pointer)

Code: Select all

    char * movable txbuf;
    char * movable rxbuf;
    
    // malloc, check for NULL
    
    memset(txbuf, 0, len);
    memset(rxbuf, 0, len);

    spi_if.begin_transaction(SPI_TO_DSP_SS, 5000, SPI_MODE_3); 

    // "trying to move aliased pointer 'rxbuf' "
    // "trying to move aliased pointer 'txbuf' "
    spi_if.init_transfer_array_8(move(rxbuf), move(txbuf), 2);
The error disappears on removing memset(). The type of the arguments first parameter in the functions prototype in string.h is char*alias, so I guess that is why the pointers are treated as aliased, although they are declared as movable?


robertxmos
XCore Addict
Posts: 169
Joined: Fri Oct 23, 2015 10:23 am

Post by robertxmos »

Hi

Yes, the tools are getting it wrong.
The include/xc/safe/string.h header files has two ways to call memset from XC.

Code: Select all

#define memset(s, c, n) _safe_memset((char *)s, c, n)
inline char * alias _safe_memset(char * alias s, int c, unsigned n) {
  if (__builtin_array_bound(s) < n)
    __builtin_trap();
  memset(s, c, n);
  return s;
}
or

Code: Select all

void safememset(unsigned char dst[length], int value, unsigned length);
If you call safememset() things work as expected:

Code: Select all

void bar(char * movable p, unsigned len);
void foo (char * movable p, unsigned len) {
    memset(p, 0, len); // this causes alias error.
    safememset(p, 0, len); // this does not.
    bar(move(p), len);
}
The bug will be dealt with.
cheers