cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Visitor
Visitor
6,001 Views
Registered: ‎04-19-2016

How to use dynamic memory in Zedboard Zynq-7000

Jump to solution

Hello,

I am student and I have a theoretical problem about how can I assign dynamic memory in Zynq Standalone. I wonder if you can help me.

I am working on a program that wants apply a filter to a bmp-greyscale image (255 bits per pixel) to clean rubbish values, for example shadows. I finished the part of the program that reads the information from the original bmp image. And now, I am doing the part to clean rubbish values from pixels. I will use threshold to limit the values. For example, if one pixel have a value of 133 I will consider it rubbish and I want to set this value to 0. I had thought to use a buffer to copy the image's data but to do it I used the malloc's function of lenguage C which uses dynamic memory allocation. I understand that use malloc's function is not recommendable because it can be a problem in Zynq Standalone.

 

STRUCTURES OF BMP HEADERS

 

#pragma pack(push,1)

typedef struct {
        unsigned short type;
        unsigned int size;
        unsigned short res1;
        unsigned short res2;
        unsigned int offset;
} BMP_HEADER;

typedef struct {
        unsigned int infoSize;
        unsigned int width;
        unsigned int height;
        unsigned short planes;
        unsigned short bitsPerPixel;
        unsigned int compression;
        unsigned int imageSize;
        unsigned int yPixelParameter;
        unsigned int xPixelParameter;
        unsigned int numColorsPallatte;
        unsigned int mostImpColor;
} BMP_INFO_HEADER;

typedef struct {
        BMP_HEADER bmp_header;
        BMP_INFO_HEADER bmp_info_header;
} BITMAP_FILE;

#pragma pack(pop)

 

MAIN

 

int main (int args, char ** argv) {

        char *photo_sobel_bmp = argv[1];
        FILE *fp = fopen(photo_sobel_bmp, "rwb");
        FILE *fOutput = fopen("output.bmp", "wb");
        BITMAP_FILE bitmap_file;
        fread(&bitmap_file, sizeof(BITMAP_FILE), 1, fp);
        if (bitmap_file.bmp_header.type != (('M' << 8) | 'B'))
        {
                printf("\nERROR: not a .bmp");
                return 1;
        }
        unsigned char *pixelBuffer = (unsigned char*)malloc(bitmap_file.bmp_info_header.imageSize);

        fwrite(&bitmap_file, 1, sizeof(BITMAP_FILE), fOutput);
        //I used memset to summarize the objective
        memset(pixelBuffer, 0xff, bitmap_file.bmp_info_header.imageSize);
        fwrite(pixelBuffer, 1, bitmap_file.bmp_info_header.imageSize, fOutput);

        fclose(fOutput);
        fclose(fp);
        free(pixelBuffer);

        return 0;

}

Do you know how can I do this avoiding malloc's function?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Xilinx Employee
Xilinx Employee
10,723 Views
Registered: ‎08-02-2011

If you need dynamic memory allocation, malloc is indeed the way to do it, but that opens up a can of worms (just google malloc and embedded systems for articles like this. It's not just a zynq question...)

 

If you're okay avoiding  dynamic memory allocation all together, then just declare an array statically and it'll end up on stack and allocated at compile time. Something like:

 

unsigned char pixelBuffer[bitmap_file.bmp_info_header.imageSize];

 

www.xilinx.com

View solution in original post

0 Kudos
4 Replies
Highlighted
Xilinx Employee
Xilinx Employee
10,724 Views
Registered: ‎08-02-2011

If you need dynamic memory allocation, malloc is indeed the way to do it, but that opens up a can of worms (just google malloc and embedded systems for articles like this. It's not just a zynq question...)

 

If you're okay avoiding  dynamic memory allocation all together, then just declare an array statically and it'll end up on stack and allocated at compile time. Something like:

 

unsigned char pixelBuffer[bitmap_file.bmp_info_header.imageSize];

 

www.xilinx.com

View solution in original post

0 Kudos
Highlighted
Adventurer
Adventurer
5,974 Views
Registered: ‎05-11-2010

I'm not sure if malloc actually gives problems; I think it works fine on Zynq (although I recall seeing somewhere that free didn't work correctly, but maybe that was in older versions).

 

You could also just use variable length arrays:

unsigned char pixelBuffer[bitmap_file.bmp_info_header.imageSize];

(just make sure to have enough space in the stack in lscript.ld).

 

Alternatively, just make pixelBuffer as big as you plan to let your files be:

#define MAX_BMP_SIZE 1048576
static unsigned char pixelBuffer[MAX_BMP_SIZE];
if (bitmap_file.bmp_info_header.imageSize > MAX_BMP_SIZE) {
    printf("File too big!\n");
    exit(1);
}
...

(here, static will move the variable out of the stack, so you don't need to make the stack too large as before).

0 Kudos
Highlighted
Visitor
Visitor
5,945 Views
Registered: ‎04-19-2016
Ok bwiec, I will use static array because i know the size of my image.

Thank you.
0 Kudos
Highlighted
Visitor
Visitor
5,942 Views
Registered: ‎04-19-2016

Hello cousteau,

 

Another question that i thank when i was readind your answer. If you use static array, in real time application, and do you need to set free this array from memory. I mean, if you need check another bmp file in real time. Should I use the same pixel buffer? Because i will use this application to avoid obstacles with a vehicle in real time. So i need check if some new obstacles appears. I will compute only one time per image.

 

Thank you.

 

 

0 Kudos