This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Very, Very strange problem while calling a function "normally" VERSUS "inlining"

Hi All.

I had been making some changes to the tiva-blinky example, on the Tiva-Launchpad board.

The code is as follows ::

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "src/common/include/globals.h"

#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/can.h"

#define LED_RED GPIO_PIN_1
#define LED_BLUE GPIO_PIN_2
#define LED_GREEN GPIO_PIN_3



int main()
{
    char m[100];
    SG_MEMSET(m, 0, 100)

    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN);

   for (;;) {
    // set the red LED pin high, others low
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_RED|LED_GREEN|LED_BLUE, LED_BLUE);
    ROM_SysCtlDelay(5000000);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, LED_RED|LED_GREEN|LED_BLUE, 0);
    ROM_SysCtlDelay(5000000);
    }

}

Now, two cases arise ::


a)

SG_MEMSET is

#define SG_MEMSET(dest, byte, size)                             \
    {                                                           \
        char temp[MAX_BUFFER_SIZE] = {byte};                    \
        memcpy(dest, temp, size);                               \
    }

In this "inlining" case, the BLUE light blinks fine as expected.

b)

SG_MEMSET is

void DO_SG_MEMSET(char *dest, char byte, unsigned int size)
{
    char temp[MAX_BUFFER_SIZE] = {byte};
    memcpy(dest, temp, size);
}

#define SG_MEMSET(dest, byte, size)     DO_SG_MEMSET(dest, byte, size);

In this "normal-function-calling" case, no light blinks :(

I am at a complete-loss why this could be happening.

Are multiple-frames/function-calling disabled on Tiva-Launchpad?

For brevity, here is the linker-script being used ::

MEMORY
{
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000
    SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0X00008000
}

SECTIONS
{
    /* code */
    .text :
    {
        _text = .;
        /* ensure ISR vectors are not removed by linker */
        KEEP(*(.isr_vector))
        *(.text*)
        *(.rodata*)
        _etext = .;
    } > FLASH

    /* static data */
    .data : AT(ADDR(.text) + SIZEOF(.text))
    {
        _data = .;
        *(vtable)
        *(.data*)
        _edata = .;
    } > SRAM

    /* static uninitialized data */
    .bss :
    {
        _bss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    } > SRAM

}


Will be grateful for any pointers. I cannot move forward before resolving this very basic issue :-\

Thanks and Regards,

Ajay

  • I expect you could implement memset in more obscure ways but that is certainly an odd way to do it.  Slow and opaque.

    In the macro case (not inline), the compiler could legitimately completely remove the macro code and replace it with

    char m[100] = {0};

    It is possible that the stack size is much larger in the called function version, cannot tell by how much since MAX_BUFFER_SIZE is not defined visibly.

    This

    Ajay Garg said:
    SG_MEMSET(m, 0, 100)

    while technically correct is just wrong. Making the ; sometimes optional is a maintenance issue.

    There's a lot of extra included in this (for instance CAN support) You probably should eliminate that and trim your problem down to a minimum.

    Finally why not just use memset?

    Robert

  • Hi Robert.

    Thanks for the reply.
    I was actually using "memset" the normal way itself, but using it was causing a problem.

    I observed similar problems when using "strlen" function.

    Anyhow, I changed my compilation process from "compilation and THEN linking in two steps" to "compilation and linking in one step", and surprisingly, that solved the issues. I could now use the simple vanilla "memset".


    Thanks and Regards,
    Ajay
  • That suggests a link problem. I wouldn't ignore it, you are going to need to solve it when you move to decent sized projects.

    Robert
  • Ajay Garg said:
    I cannot move forward before resolving this very basic issue

    ++;  for originality.   Our super decoder ring (however) yields, "It's Urgent!"