Write an aligned malloc & free function which takes number of bytes and aligned byte (which is always power of 2) and returns the memory address divisible by the number alligned byte.
Ex. align_malloc (1000,128);
it will return memory address multiple of 128 of the size 1000.
aligned_free();
it will free memory allocated by align_malloc.
For allocating function, I wrote the following code:
void * allocatemyfunc (size_t bytestobeallocated, size_t allignment)
{
void * p1;
void * p2;
p1 = (void*)malloc(bytestobeallocated+allignment);
if ( p1 == NULL )
return 'error';
else
{
size_t addr = bytestobeallocated + allignment;
p2 = (void*)addr-(addr%allignment);
return p2;
}
}
This seems to be the appropriate solution for alligned allocation. (I might be wrong, correct me if I am).
How do I write the alligned free function? (this will basically free all the memory allocated)