-2

I am working on a micro controller, so, no malloc. Actually, I want to create a memory manager, so I am kinda implementing the malloc function for later use and using the BLOCK strategy to get it, like FreeRTOS does.

typedef struct BLOCK {
    unsigned char used;     // If 1 block is used, if 0 it's free to use
    unsigned long offset;   // Offset of the starting block
    unsigned long size;     // Size of this block
    struct BLOCK * next;    // Pointer to the next block
} BLOCK_t;

#define MAX_PROGRAMS    3
#define BLOCKS_NEEDED   (MAX_PROGRAMS * 2) + 1
BLOCK_t blocks[BLOCKS_NEEDED]; // Not allocating 7 * sizeof(BLOCK)

This BLOCK is a linked list and I want to create (allocate) a fixed amount of them and set the first blocks[0] only. The next ones will be created in execution time, when memory is allocated.

Thanks in advance.

EDIT: In case the title is not clear enough, I want to compiler to assign some memory space to my array (fixed location and size) but I don't want to initialize it with data because I will get the data in run-time, so I want an array of 7 BLOCKs with empty data. The code above shows my attempt to do it, I declared the array, but I assume that declaring an array doesn't give you the space needed. How can I achieve this ? How can I get the compiler to give me that space ?

EDIT 2: This would be tha Java code to do it.

private static int MAX_PROGRAMS = 3;
private static int BLOCKS_NEEDED = (MAX_PROGRAMS * 2) + 1:
Block myBlockList[] = new Block[BLOCKS_NEEDED];

This get the space for myBlockList even though the list is empty and each item is uninitialized, but I have the space already.

UDKOX
  • 738
  • 3
  • 15
  • 2
    and the question is...?? – LPs Feb 16 '16 at 16:22
  • FreeRTOS provides at least four different allocators. In general, you should not implement dynamic memory management if you not really need it. FreeRTOS allows e.g. to statically allocate memory for tasks (there is no more than one program on an MCU normally. Not sure what you mean with "program" here). – too honest for this site Feb 16 '16 at 16:49
  • @LPs @EugeneSh. Question updated. @Olaf In my case I will have several programs running, so I need to create a `malloc` function to load them in the RAM. I need the dynamic memory. – UDKOX Feb 17 '16 at 12:13
  • 2
    Using [tag:c] writing `BLOCK_t blocks[BLOCKS_NEEDED];` you are declaring the array and `sizeof(BLOCK_t)*BLOCKS_NEEDED` bytes are occupied by the array. – LPs Feb 17 '16 at 12:59
  • @LPs *facepalm* True. If you write it as an answer I will mark it as the correct one. If not, I will post it myself in some hours. Thanks. – UDKOX Feb 17 '16 at 13:14

2 Answers2

0

All you want to do is allocate memory automatically on the stack.

#include <stdio.h>

#define blockcontent_size 1024
#define blockcount 3

typedef struct
{
    unsigned char used;   
    unsigned long offset;
    unsigned long size; 
    unsigned data[blockcontent_size];
} BLOCK;

BLOCK blocks[blockcount];

int main()
{
    printf("memory available in one block %u\n", sizeof(blocks[0].data));
    printf("memory used for one block %u\n", sizeof(BLOCK));
    printf("memory used for all blocks %u\n", sizeof(blocks));

    return 0;
}

You actually do not need a linked list, you can just use the index. Is this close to what you are asking?

Johannes
  • 6,490
  • 10
  • 59
  • 108
  • I don't understand how this will solve my problem. I updated the question, maybe you could check it and add a little bit of explanation. Thanks. – UDKOX Feb 17 '16 at 12:17
0

@LPs quote:

Using c writing BLOCK_t blocks[BLOCKS_NEEDED]; you are declaring the array and sizeof(BLOCK_t)*BLOCKS_NEEDED bytes are occupied by the array.

So my statement :

BLOCK_t blocks[BLOCKS_NEEDED]; // Not allocating 7 * sizeof(BLOCK)

was false, it actually does allocate the space.

UDKOX
  • 738
  • 3
  • 15