0

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)

Elliot
  • 483
  • 2
  • 6
  • 17
  • Unrelated, casting `malloc` to `void *` is senseless. Related, your alignment calculation is incorrect. To round to a nearest boundary you add the rounding factor less-one, then do your modulo and readd work. And you seem to be leaving no path back to the original `malloc` result to later free when finished. `free()`-ing `p2` is obviously not going to work. – WhozCraig Jun 07 '15 at 17:39
  • "Write an aligned malloc & free function" are you telling us to do this? Or are you saying that someone - perhaps a teacher - asked you to do this? – Drew Dormann Jun 07 '15 at 17:42
  • 1
    When you used the debugger, which lines are causing the issue? You did use a debugger before posting? – Thomas Matthews Jun 07 '15 at 17:43
  • I was reading a book that asked me to do this. I tried doing it on my own which I have already written in the question. All I am asking from you is to check if its correct or not? This is not a homework question. If it would have been, I wouldn't have posted it here. I would have probably gone and consulted my teacher in the first place. – Elliot Jun 07 '15 at 17:44
  • You may also find [this question](https://stackoverflow.com/questions/12504776/aligned-malloc-in-c) interesting, especially as the purpose of the question is to idenitfy why, and how, it works. – WhozCraig Jun 07 '15 at 17:45
  • @WhozCraig, that's correct. So, what should I do in the alloc function so that I can trace back to free. Moreso, why is it that casting malloc to void* is senseless? – Elliot Jun 07 '15 at 17:46
  • 1
    This is a Q&A, not a helpdesk. "Questions" on issues without prior debugging are almost always off-topic, by virtue of how they are constructed. – Lightness Races in Orbit Jun 07 '15 at 17:46

2 Answers2

1

Your align malloc is erroneous, you can do something like this to align a pointer.

uint8_t* ptr = (uint8_t*)malloc(N + alignment);
....
uint8_t offset = alignment - ((uintptr_t)ptr % alignment);
ptr += offset;

Now you have a pointer that has offset bytes of free space to the front, you store the offset there.

*((uint8_t*)ptr - 1) = offset;

To free the ptr you decrement offset to get to the beginning

uint8_t offset = *((uint8_t*)ptr - 1);
free((uint8_t*)ptr - offset);
yngccc
  • 5,594
  • 2
  • 23
  • 33
  • That is making an assumption about integer data type sizes and also the amount of alignment he had to add. For example what if it turns out that offset is actually 0? – Puppy Jun 07 '15 at 18:16
  • I am making assumption that alignment is small, offset cannot be zero assuming valid value for ptr and alignment, I omitted those error checks. – yngccc Jun 07 '15 at 18:22
  • The offset can be zero if malloc's result happens to already be aligned to alignment. Although I did observe that you specifically typed offset as uint8_t which does eliminate one of my complaints about integer data type sizes. – Puppy Jun 07 '15 at 18:26
  • In that case wouldn't offset be alignment? – yngccc Jun 07 '15 at 18:26
  • Yes, yes it would be. That's my mistake. That does raise the related issue of aligning pointers that are already aligned :P – Puppy Jun 07 '15 at 18:27
  • Sorry this is a very basic question, but is there a specific reason why you used `uint8_t` and not `size_t`? – Elliot Jun 08 '15 at 05:42
  • @Elliot I assume small alignment, you rarely need more than 128 bytes aligned pointer, if you need it then you need to allocate more memory and the offset calculation would be different, something like [this](http://stackoverflow.com/questions/1919183/how-to-allocate-and-free-aligned-memory-in-c). – yngccc Jun 09 '15 at 02:03
0

This is not exactly a dup question, but see my answer here for help

What is the recommended way to align memory in C++11

The methodology is to be able to find the beginning of the block from the returned memory by referencing the memory before the returned memory

Community
  • 1
  • 1
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80