blob: 23f7f82c5909c22ade9a439c45652fc22d4a3554 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001#include "plat.h"
2#include <stdlib.h>
3#include <string.h>
4
5void *malloc_aligned (size_t size)
6{
7 void *mem = malloc (size + 8 + sizeof (void *) * 2);
8 void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7);
9 aligned_ptr[ -1 ] = mem;
10 aligned_ptr[ -2 ] = (void *) size;
11 return aligned_ptr;
12}
13
14void *realloc_aligned (void *addr, size_t size)
15{
16 void *mem = malloc (size + 8 + sizeof (void *) * 2);
17 void **aligned_ptr = (void **) ((uintptr_t) (mem + 8 + sizeof (void *)) & ~7);
18 aligned_ptr[ -1 ] = mem;
19
20 memcpy (aligned_ptr, addr, (uintptr_t) aligned_ptr[ -2 ]);
21
22 return aligned_ptr;
23}
24
25void free_aligned (void *addr)
26{
27 void **ptr = (void **)addr;
28 free (ptr[ -1 ]);
29}