gmm_malloc.h revision 169689
1209513Simp/* Copyright (C) 2004 Free Software Foundation, Inc.
2209513Simp
3209552Simp   This file is part of GCC.
4209513Simp
5209513Simp   GCC is free software; you can redistribute it and/or modify
6209513Simp   it under the terms of the GNU General Public License as published by
7209513Simp   the Free Software Foundation; either version 2, or (at your option)
8209513Simp   any later version.
9209513Simp
10209513Simp   GCC is distributed in the hope that it will be useful,
11209513Simp   but WITHOUT ANY WARRANTY; without even the implied warranty of
12209513Simp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13209513Simp   GNU General Public License for more details.
14209513Simp
15209513Simp   You should have received a copy of the GNU General Public License
16209513Simp   along with GCC; see the file COPYING.  If not, write to
17209513Simp   the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18209513Simp   Boston, MA 02110-1301, USA.  */
19209513Simp
20209513Simp/* As a special exception, if you include this header file into source
21209513Simp   files compiled by GCC, this header file does not by itself cause
22209513Simp   the resulting executable to be covered by the GNU General Public
23209513Simp   License.  This exception does not however invalidate any other
24209513Simp   reasons why the executable file might be covered by the GNU General
25209513Simp   Public License.  */
26209513Simp
27209513Simp#ifndef _MM_MALLOC_H_INCLUDED
28209513Simp#define _MM_MALLOC_H_INCLUDED
29209513Simp
30209513Simp#include <stdlib.h>
31211730Simp#include <errno.h>
32211730Simp
33211730Simpstatic __inline__ void*
34211730Simp_mm_malloc (size_t size, size_t align)
35211730Simp{
36211730Simp  void * malloc_ptr;
37211730Simp  void * aligned_ptr;
38211730Simp
39209513Simp  /* Error if align is not a power of two.  */
40209513Simp  if (align & (align - 1))
41209513Simp    {
42209513Simp      errno = EINVAL;
43209513Simp      return ((void*) 0);
44209513Simp    }
45220909Sjpaetzel
46209513Simp  if (size == 0)
47209513Simp    return ((void *) 0);
48209513Simp
49209513Simp /* Assume malloc'd pointer is aligned at least to sizeof (void*).
50209513Simp    If necessary, add another sizeof (void*) to store the value
51209513Simp    returned by malloc. Effectively this enforces a minimum alignment
52209513Simp    of sizeof double. */
53220909Sjpaetzel    if (align < 2 * sizeof (void *))
54209513Simp      align = 2 * sizeof (void *);
55209513Simp
56209513Simp  malloc_ptr = malloc (size + align);
57209513Simp  if (!malloc_ptr)
58209513Simp    return ((void *) 0);
59209513Simp
60209513Simp  /* Align  We have at least sizeof (void *) space below malloc'd ptr. */
61209513Simp  aligned_ptr = (void *) (((size_t) malloc_ptr + align)
62209513Simp			  & ~((size_t) (align) - 1));
63211730Simp
64211730Simp  /* Store the original pointer just before p.  */
65211730Simp  ((void **) aligned_ptr) [-1] = malloc_ptr;
66209513Simp
67211730Simp  return aligned_ptr;
68211730Simp}
69209513Simp
70211730Simpstatic __inline__ void
71211730Simp_mm_free (void * aligned_ptr)
72211730Simp{
73211730Simp  if (aligned_ptr)
74220909Sjpaetzel    free (((void **) aligned_ptr) [-1]);
75211730Simp}
76211730Simp
77211730Simp#endif /* _MM_MALLOC_H_INCLUDED */
78211730Simp