Imported Upstream version 3.2.2
[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 #include <strings.h>
11
12 void *malloc16Align(int size){
13   void *p;
14   void **p1;
15
16   if((p = malloc(size+31)) == NULL)
17     return NULL;
18
19   /* Round up to next 16-byte boundary */
20   p1 = (void **)(((long)p + 31) & (~15));
21
22   /* Stash actual start of block just before ptr we return */
23   p1[-1] = p;
24
25   /* Return 16-byte aligned address */
26   return (void *)p1;
27 }
28
29 void *calloc16Align(size_t nmemb,size_t size){
30   int nbytes;
31   void *p;
32
33   nbytes = nmemb*size;
34   if((p = malloc16Align(nbytes)) == NULL)
35     return NULL;
36
37   memset(p,0,nbytes);
38   return p;
39 }
40
41 void free16Align(void *p){
42
43   if(p != NULL){
44     /* Retrieve pointer to actual start of block and free it */
45     free(((void **)p)[-1]);
46   }
47 }