iMan Biglari
iMan Biglari

Reputation: 4776

Linked list issue with ARM processor

I'm trying to create a linked list in C to run on an ARM processor (not sure about exact processor specs, but -mcpu=arm7tdmi is passed to compiler) using GCC. Here's the code:

#include <posapi.h>
#include <posapi_all.h>

const APPINFO AppInfo={
    "POS-Simple example",
    "APP-TEST",
    "1.0",
    "pcteam",
    "demo program",
    "",
    0,
    0,
    0,
    ""
};

typedef struct st_dllNode {
    struct st_dllNode * next;
    struct st_dllNode * prev;
    void * data;
} dllNode;

typedef struct {
    dllNode* first;
    dllNode* cur;
    uchar size;
} ListContainer;

typedef ListContainer* List;

List createList(void)
{
    List listContainer;
    listContainer = (List) malloc(sizeof(ListContainer));
    listContainer->first = NULL;
    listContainer->cur = NULL;
    listContainer->size = 0; // exception occurs here
    return listContainer;
}

int event_main(ST_EVENT_MSG *msg)
{
    SystemInit();
    return 0;
}


int main(void)
{
    List list;
    SystemInit();
    while (1)
    {
        list = createList();
        free(list);
        Beep();
    }
}

For unknown reasons, execution of this code stops on the line marked, and the device I'm using begins dumping an exception message:

PrefetchAbortHandler:2007FFC4,AA...... (more addresses follows)
PrefetchAbort Addr: (another addr); Status:02020a01

I have no idea why this code runs perfectly fine in Windows, but when compiled for ARM, gives such an error. Any ideas?

Upvotes: 2

Views: 267

Answers (1)

Zhifei
Zhifei

Reputation: 124

Solution: Try to add one barrier() or similar macro function in your code.

Details:

List createList(void)
{
    List listContainer;
    listContainer = (List) malloc(sizeof(ListContainer));
    smb();      // this is Linux memory barrier API. Replace it with your OS similar API
    listContainer->first = NULL;
    listContainer->cur = NULL;
    listContainer->size = 0; // exception occurs here
    return listContainer;
}

Reason: The target ARM processor may execute code out of order. listContainer may not be alloced successfully, but "listContainer->size = 0" is executed first of all.

Upvotes: 1

Related Questions