00001
00066 #define fake_malloc(X, Y, Z) my_malloc(X, Y, Z)
00067 void* my_malloc(unsigned short int nb, unsigned long int size, unsigned char type);
00068 \endcode
00069
00070 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
00071 you can know how much space the library need to work.
00072 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
00073 size of one element. Usually you call the malloc function this way: malloc(nb*sizeof(MyStruct)). Here, it is the same, but splited into
00074 two distinct variables. The last variable is an kind of ID:
00075 \li If the value is 1 it mean that the system try to allocate StateMachine structure.
00076 \li If the value is 2 it mean that the system try to allocate State structure.
00077 \li If the value is 3 it mean that the system try to allocate ParallelState structure.
00078 \li If the value is 4 it mean that the system try to allocate Transition structure.
00079 \li If the value is 5 it mean that the system try to allocate void* elements.
00080
00081 So now you have everything to calculate which amount of space you need. And even if you are running your programm in a computer that is
00082 not the final target of it, you can calculate the amount of space needed. You know exactly how many allocation of every kind the system
00083 need. If you know the size of the elements in the final target you're done.
00084
00085 Let's imagine the result after a state machine instantiation and this line of code:
00086 \code
00087 printf("Allocated %ld StateMachine, %ld State, %ld ParallelState, %ld transitions and %ld void*\n", type1, type2, type3, type4, type5);
00088 \endcode
00089 The output is:
00090 \verbatim
00091 Allocated 1 StateMachine, 6 State, 0 ParallelState, 7 transitions and 27 void*
00092 \endverbatim
00093 Assuming that on the target system:
00094 \li sizeof(StateMachine) = 56
00095 \li sizeof(State) = 96
00096 \li sizeof(ParallelState) = 40
00097 \li sizeof(Transition) = 16
00098 \li sizeof(void*) = 8
00099
00100 The total amount of space needed is: 1 * 56 + 6 * 96 + 0 * 40 + 7 * 16 + 27 * 8 = 960
00101
00102 The final step is to implement our own function:
00103
00104 \code
00105 char MY_HEAP[960];
00106 void* ptr = MY_HEAP;
00107 void* my_malloc(unsigned long int size)
00108 {
00109 void* temp = ptr;
00110 ptr+=size;
00111 return temp;
00112 }
00113 \endcode
00114
00115 And you're done with your super heap.
00116 Don't try to free the memory :)
00117
00118 Previous: \ref makefileguide
00119 */