Large memcpy

We are using Keil C-51 V7.06.

There is a structure defined:

typedef struct
{
  unsigned char address_bus_h;
  unsigned char address_bus_l;
  unsigned char data_bus;
}instbus_raw_t;

Using three simple lines to copy the structure to another it enlarges code by 9 bytes, but this is not an elegant solution:

instbus_raw.address_bus_h = instbus_raw_local.address_bus_h;
instbus_raw.address_bus_l = instbus_raw_local.address_bus_l;
instbus_raw.data_bus = instbus_raw_local.data_bus;

Using the normal library function memcpy
blows up the code by 300 bytes!

memcpy(&instbus_raw,&instbus_raw_local,sizeof(instbus_raw_t));

Using an own function my_memcpy the code increases by 167 bytes:

void *(my_memcpy)(void *s1, const void *s2, size_t n)
{
  char *su1 = (char *)s1;
  const char *su2 = (const char *)s2;

  for (; 0 < n; --n)
    *su1++ = *su2++;
  return (s1);
}

my_memcpy(&instbus_raw,&instbus_raw_local,sizeof(struct instbus_raw_t));

In a project with a little chip of 2k Flash, 300 bytes for copying few bytes are considerable!

Does anyone remarking same effects with library functions wasting resources?

Regards Peter

Parents
  • Does anyone remarking same effects with library functions wasting resources?

    There's always a trade-off with vendor-provided library routines. And, no matter HOW we implemented a routine, someone would be unhappy.

    The memcpy routine in the library contains a subroutine for every memory space. When memcpy runs it first figures out the source memory type and the destination memory type and calls the appropriate routine (there is a routine for data to data, data to xdata, code to data, xdata to xdata, and so on). Each of these routines uses registers and is optimized for speed. Of course, there is a size penalty.

    The memcpy you provide executes very slowly. It uses generic pointers (3-bytes) that are stored in the default memory space (XDATA in large model). Each read and each write requires a function call into the C runtime library. However, if you only need to copy something once or if you don't need a high-speed routine, this is probably just fine.

    Jon

Reply
  • Does anyone remarking same effects with library functions wasting resources?

    There's always a trade-off with vendor-provided library routines. And, no matter HOW we implemented a routine, someone would be unhappy.

    The memcpy routine in the library contains a subroutine for every memory space. When memcpy runs it first figures out the source memory type and the destination memory type and calls the appropriate routine (there is a routine for data to data, data to xdata, code to data, xdata to xdata, and so on). Each of these routines uses registers and is optimized for speed. Of course, there is a size penalty.

    The memcpy you provide executes very slowly. It uses generic pointers (3-bytes) that are stored in the default memory space (XDATA in large model). Each read and each write requires a function call into the C runtime library. However, if you only need to copy something once or if you don't need a high-speed routine, this is probably just fine.

    Jon

Children
More questions in this forum