_mem_copy is backwards

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

_mem_copy is backwards

783 Views
matthewkendall
Contributor V

This is both a short rant a warning for others. In mqx.h (line 1247) we find:

#define _mem_copy(s,d,l) memcpy(d,s,l)

Note the subtle reversing of the order of the arguments. And the same thing applies to all the higher level abstractions built on top of this (e.g. USB_mem_copy). They are all backwards. For the love of God why would you do that? Are you trying to trip people up? I mean, why follow a convention that has only been fixed since the late 70's.

Seriously, is there a good reason for this?

0 Kudos
2 Replies

436 Views
keno
Contributor II

I stumbled upon this same problem today.  I was looking at the Vybrid bootloader example (/mqx/examples/bootloader_vybrid) and noticed the source and destination arguments were backwards and thought it was a bug in the bootloader code.  That was until I found the _mem_copy() implementation in MQX in module mem_copy.c

void _mem_copy

  (

      /* [IN] address to copy data from */

      void    *from_ptr,

      /* [IN] address to copy data to */

      void    *to_ptr,

      /* [IN] number of bytes to copy */

      register _mem_size number_of_bytes

  )

The source and destination arguments are backwards from the standard c library implementation which has been in existence since forever.

I second Matthew's rant.  Why would you do this?

0 Kudos

436 Views
susansu
NXP Employee
NXP Employee

I watched the code and found that when the Macro MQXCFG_MEM_COPY defined as 1 or MQXCFG_MEM_COPY_NEON and PSP_HAS_FPU all defined as 1, the code will not use the memcpy function from C lib, instead will use _mem_copy function in MQX code.

#if MQXCFG_MEM_COPY || (MQXCFG_MEM_COPY_NEON && PSP_HAS_FPU)

extern void             _mem_copy(pointer, pointer, _mem_size);

#else

# include <string.h>

#define _mem_copy(s,d,l) memcpy(d,s,l)

#endif

With regards to the parameter reverse problem, in _mem_copy function, the first parameter is the source address of the data to copy and the second is the destination address to place at; but in memcpy function that defined in C lib, the first parameter is the destination address to place at and second is the source address of the data to copy.

0 Kudos