Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / malloc16.c
1 /* Wrapper functions for malloc/free that force 16-byte alignment
2  * See http://perso.club-internet.fr/matmac/sourcesc.htm
3
4  * Copyright 2001 Phil Karn, KA9Q
5  * May be used under the terms of the GNU Public License (GPL)
6  */
7
8 #include "malloc16.h"
9 #include <string.h>
10
11 void *malloc16Align(int size){
12   void *p;
13   void **p1;
14
15   if((p = malloc(size+31)) == NULL)
16     return NULL;
17
18   /* Round up to next 16-byte boundary */
19   p1 = (void **)(((long)p + 31) & (~15));
20
21   /* Stash actual start of block just before ptr we return */
22   p1[-1] = p;
23
24   /* Return 16-byte aligned address */
25   return (void *)p1;
26 }
27
28 void *calloc16Align(size_t nmemb,size_t size){
29   int nbytes;
30   void *p;
31
32   nbytes = nmemb*size;
33   if((p = malloc16Align(nbytes)) == NULL)
34     return NULL;
35
36   memset(p,0,nbytes);
37   return p;
38 }
39
40 void free16Align(void *p){
41
42   if(p != NULL){
43     /* Retrieve pointer to actual start of block and free it */
44     free(((void **)p)[-1]);
45   }
46 }