graphene-alloc.c 4.46 KB
Newer Older
Iñigo Martínez's avatar
Iñigo Martínez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/* graphene-alloc.c: aligned allocator
 *
 * Copyright 2014  Emmanuele Bassi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "graphene-private.h"
#include "graphene-alloc-private.h"

#if defined(HAVE_POSIX_MEMALIGN) && !defined(_XOPEN_SOURCE)
# define _XOPEN_SOURCE 600
#endif

#if defined(HAVE_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
/* Required for _aligned_malloc() and _aligned_free() on Windows */
#include <malloc.h>
#endif

#ifdef HAVE__ALIGNED_MALLOC
/* _aligned_malloc() takes parameters of aligned_malloc() in reverse order */
# define aligned_alloc(alignment, size) _aligned_malloc (size, alignment)

/* _aligned_malloc()'ed memory must be freed by _align_free() on MSVC */
# define aligned_free(x) _aligned_free (x)
#else
# define aligned_free(x) free (x)
#endif

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

/*< private >
 * graphene_aligned_alloc:
 * @size: the size of the memory to allocate
 * @number: the multiples of @size to allocate
 * @alignment: the alignment to be enforced, as a power of 2
 *
 * Allocates @number times @size memory, with the given @alignment.
 *
 * If the total requested memory overflows %G_MAXSIZE, this function
 * will abort.
 *
 * If allocation fails, this function will abort, in line with
 * the behaviour of GLib.
 *
 * Returns: (transfer full): the allocated memory
 */
void *
graphene_aligned_alloc (size_t size,
                        size_t number,
                        size_t alignment)
{
  void *res = NULL;
  size_t max_size = (size_t) -1;
  size_t real_size;

  if (size == 0 || number == 0)
    return NULL;

  if (size > 0 && number > max_size / size)
    {
#ifndef G_DISABLE_ASSERT
      fprintf (stderr,
               "Overflow in the allocation of (%lu x %lu) bytes\n",
               (unsigned long) size,
               (unsigned long) number);
      abort ();
#else
      return NULL;
#endif
    }

  real_size = size * number;

#ifndef G_DISABLE_ASSERT
  errno = 0;
#endif

#if defined(HAVE_POSIX_MEMALIGN)
  errno = posix_memalign (&res, alignment, real_size);
#elif defined(HAVE_ALIGNED_ALLOC) || defined(HAVE__ALIGNED_MALLOC)
  /* real_size must be a multiple of alignment */
  if (real_size % alignment != 0)
    {
      size_t offset = real_size % alignment;
      real_size += (alignment - offset);
    }

  res = aligned_alloc (alignment, real_size);
#elif defined(HAVE_MEMALIGN)
  res = memalign (alignment, real_size);
#else
  res = malloc (real_size);
#endif

#ifndef G_DISABLE_ASSERT
  if (errno != 0 || res == NULL)
    {
      fprintf (stderr, "Allocation error: %s\n", strerror (errno));
      abort ();
    }
#endif

  return res;
}

/*< private >
 * graphene_aligned_alloc:
 * @size: the size of the memory to allocate
 * @number: the multiples of @size to allocate
 * @alignment: the alignment to be enforced, as a power of 2
 *
 * Allocates @number times @size memory, with the given @alignment,
 * like graphene_aligned_alloc(), but it also clears the memory.
 *
 * Returns: (transfer full): the allocated, cleared memory
 */
void *
graphene_aligned_alloc0 (size_t size,
                         size_t number,
                         size_t alignment)
{
  void *res = graphene_aligned_alloc (size, number, alignment);

  if (res != NULL)
    memset (res, 0, size * number);

  return res;
}

/*< private >
 * graphene_aligned_free:
 * @mem: the memory to deallocate
 *
 * Frees the memory allocated by graphene_aligned_alloc().
 */
void
graphene_aligned_free (void *mem)
{
  aligned_free (mem);
}