dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cbe / lib / malloc_align.h
1 /* --------------------------------------------------------------  */
2 /* (C)Copyright 2001,2007,                                         */
3 /* International Business Machines Corporation,                    */
4 /* Sony Computer Entertainment, Incorporated,                      */
5 /* Toshiba Corporation,                                            */
6 /*                                                                 */
7 /* All Rights Reserved.                                            */
8 /*                                                                 */
9 /* Redistribution and use in source and binary forms, with or      */
10 /* without modification, are permitted provided that the           */
11 /* following conditions are met:                                   */
12 /*                                                                 */
13 /* - Redistributions of source code must retain the above copyright*/
14 /*   notice, this list of conditions and the following disclaimer. */
15 /*                                                                 */
16 /* - Redistributions in binary form must reproduce the above       */
17 /*   copyright notice, this list of conditions and the following   */
18 /*   disclaimer in the documentation and/or other materials        */
19 /*   provided with the distribution.                               */
20 /*                                                                 */
21 /* - Neither the name of IBM Corporation nor the names of its      */
22 /*   contributors may be used to endorse or promote products       */
23 /*   derived from this software without specific prior written     */
24 /*   permission.                                                   */
25 /*                                                                 */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
27 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
28 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
29 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
30 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
31 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
33 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
34 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
35 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
36 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
37 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
38 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
39 /* --------------------------------------------------------------  */
40 /* PROLOG END TAG zYx                                              */
41
42 #ifndef _MALLOC_ALIGN_H_
43 #define _MALLOC_ALIGN_H_        1
44
45 #include <stdlib.h>
46
47 /* Function
48  *
49  *      void * malloc_align(size_t size, unsigned int log2_align)
50  *
51  * Description
52  *      The malloc_align routine allocates a memory buffer of <size>
53  *      bytes aligned to the power of 2 alignment specified by <log2_align>.
54  *      For example, malloc_align(4096, 7) will allocate a memory heap 
55  *      buffer of 4096 bytes aligned on a 128 byte boundary.
56  *
57  *      The aligned malloc routine allocates an enlarged buffer
58  *      from the standard memory heap. Space for the real allocated memory
59  *      pointer is reserved on the front of the memory buffer.
60  *
61  *            ----------------- <--- start of allocated memory
62  *           |    pad 0 to     |
63  *           |(1<<log2_align)-1|
64  *           |     bytes       |
65  *           |-----------------|
66  *           | allocation size |
67  *           |-----------------|
68  *           | real buffer ptr |
69  *           |-----------------|<---- returned aligned memory pointer
70  *           |                 |
71  *           |    requested    |
72  *           |     memory      |
73  *           |     buffer      |
74  *           |      size       |
75  *           |      bytes      |
76  *           |_________________|
77  *
78  *      Memory allocated by this routine must be freed using the free_align
79  *      routine.
80  *
81  *      The size of the allocation is saved for special cases where the 
82  *      data must be mored during a realloc_align.
83  */
84
85 static __inline void * _malloc_align(size_t size, unsigned int log2_align)
86 {
87   void *ret;
88   char *real;
89   unsigned long offset;
90   unsigned long align;
91   
92   align = 1 << log2_align;
93   real = (char *)malloc(size + 2*sizeof(void *) + (align-1));
94   if (real) {
95     offset = (align - (unsigned long)(real + 2*sizeof(void *))) & (align-1);
96     ret = (void *)((real + 2*sizeof(void *)) + offset);
97     *((size_t *)(ret)-2) = size;
98     *((void **)(ret)-1) = (void *)(real);
99   } else {
100     ret = (void *)(real);
101   }
102   return (ret);
103 }
104
105 #endif /* _MALLOC_ALIGN_H_ */