First of all state machine are usually not made to be dynamic. In this library once you've started a state machine you cannot allocate any more structure in it. So if you are using the compilation option USE_PRE_ALLOCATED_BUF and if you follow carefully the advises in this section , the state machine will never have to call the free() function and will never allocate any space after having been started. Starting from this, you may think that you can use a static buffer to give memory space to the state machine. All that you need is to know the amount of memory the state machine will claim during initialisation time. So let's move on...
#define fake_malloc(X, Y, Z) malloc(X * Y)
In a separated file (outside the library):
unsigned long int type1 = 0, type2 = 0, type3 = 0, type4 = 0, type5 = 0; void* my_malloc(unsigned short int nb, unsigned long int size, unsigned char type) { switch(type) { case 1: type1 += nb; break; case 2: type2 += nb; break; case 3: type3 += nb; break; case 4: type4 += nb; break; case 5: type5 += nb; break; } return malloc(nb * size); }
/*#define fake_malloc(X, Y, Z) malloc(X * Y)*/ #define fake_malloc(X, Y, Z) my_malloc(X, Y, Z) void* my_malloc(unsigned short int nb, unsigned long int size, unsigned char type);
Note that typeX variable are stored at global scope. Now if you allocate a state machine, start it and try to display the 5 variables typeX you can know how much space the library need to work. Let's describe a bit. The first parameter 'nb' is the number of element the library want to allocate and the 'size' parameter is the size of one element. Usually you call the malloc function this way: malloc(nb*sizeof(MyStruct)). Here, it is the same, but splited into two distinct variables. The last variable is an kind of ID:
Let's imagine the result after a state machine instantiation and this line of code:
printf("Allocated %ld StateMachine, %ld State, %ld ParallelState, %ld transitions and %ld void*\n", type1, type2, type3, type4, type5);
Allocated 1 StateMachine, 6 State, 0 ParallelState, 7 transitions and 27 void*
The final step is to implement our own function:
char MY_HEAP[960]; void* ptr = MY_HEAP; void* my_malloc(unsigned long int size) { void* temp = ptr; ptr+=size; return temp; }
And you're done with your super heap. Don't try to free the memory :)
Previous: How to build the library.