Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
  2. /* Cairo - a vector graphics library with display and print output
  3.  *
  4.  * Copyright © 2007 Mozilla Corporation
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it either under the terms of the GNU Lesser General Public
  8.  * License version 2.1 as published by the Free Software Foundation
  9.  * (the "LGPL") or, at your option, under the terms of the Mozilla
  10.  * Public License Version 1.1 (the "MPL"). If you do not alter this
  11.  * notice, a recipient may use your version of this file under either
  12.  * the MPL or the LGPL.
  13.  *
  14.  * You should have received a copy of the LGPL along with this library
  15.  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  17.  * You should have received a copy of the MPL along with this library
  18.  * in the file COPYING-MPL-1.1
  19.  *
  20.  * The contents of this file are subject to the Mozilla Public License
  21.  * Version 1.1 (the "License"); you may not use this file except in
  22.  * compliance with the License. You may obtain a copy of the License at
  23.  * http://www.mozilla.org/MPL/
  24.  *
  25.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  26.  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  27.  * the specific language governing rights and limitations.
  28.  *
  29.  * The Original Code is the cairo graphics library.
  30.  *
  31.  * The Initial Developer of the Original Code is Mozilla Foundation
  32.  *
  33.  * Contributor(s):
  34.  *      Vladimir Vukicevic <vladimir@pobox.com>
  35.  */
  36.  
  37. #ifndef CAIRO_MALLOC_PRIVATE_H
  38. #define CAIRO_MALLOC_PRIVATE_H
  39.  
  40. #include "cairo-wideint-private.h"
  41.  
  42. #if HAVE_MEMFAULT
  43. #include <memfault.h>
  44. #define CAIRO_INJECT_FAULT() MEMFAULT_INJECT_FAULT()
  45. #else
  46. #define CAIRO_INJECT_FAULT() 0
  47. #endif
  48.  
  49. /**
  50.  * _cairo_malloc:
  51.  * @size: size in bytes
  52.  *
  53.  * Allocate @size memory using malloc().
  54.  * The memory should be freed using free().
  55.  * malloc is skipped, if 0 bytes are requested, and %NULL will be returned.
  56.  *
  57.  * Return value: A pointer to the newly allocated memory, or %NULL in
  58.  * case of malloc() failure or size is 0.
  59.  */
  60.  
  61. #define _cairo_malloc(size) \
  62.    ((size) ? malloc((unsigned) (size)) : NULL)
  63.  
  64. /**
  65.  * _cairo_malloc_ab:
  66.  * @n: number of elements to allocate
  67.  * @size: size of each element
  68.  *
  69.  * Allocates @n*@size memory using _cairo_malloc(), taking care to not
  70.  * overflow when doing the multiplication.  Behaves much like
  71.  * calloc(), except that the returned memory is not set to zero.
  72.  * The memory should be freed using free().
  73.  *
  74.  * @size should be a constant so that the compiler can optimize
  75.  * out a constant division.
  76.  *
  77.  * Return value: A pointer to the newly allocated memory, or %NULL in
  78.  * case of malloc() failure or overflow.
  79.  */
  80.  
  81. #define _cairo_malloc_ab(a, size) \
  82.   ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
  83.    _cairo_malloc((unsigned) (a) * (unsigned) (size)))
  84.  
  85. /**
  86.  * _cairo_realloc_ab:
  87.  * @ptr: original pointer to block of memory to be resized
  88.  * @n: number of elements to allocate
  89.  * @size: size of each element
  90.  *
  91.  * Reallocates @ptr a block of @n*@size memory using realloc(), taking
  92.  * care to not overflow when doing the multiplication.  The memory
  93.  * should be freed using free().
  94.  *
  95.  * @size should be a constant so that the compiler can optimize
  96.  * out a constant division.
  97.  *
  98.  * Return value: A pointer to the newly allocated memory, or %NULL in
  99.  * case of realloc() failure or overflow (whereupon the original block
  100.  * of memory * is left untouched).
  101.  */
  102.  
  103. #define _cairo_realloc_ab(ptr, a, size) \
  104.   ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
  105.    realloc(ptr, (unsigned) (a) * (unsigned) (size)))
  106.  
  107. /**
  108.  * _cairo_malloc_abc:
  109.  * @n: first factor of number of elements to allocate
  110.  * @b: second factor of number of elements to allocate
  111.  * @size: size of each element
  112.  *
  113.  * Allocates @n*@b*@size memory using _cairo_malloc(), taking care to not
  114.  * overflow when doing the multiplication.  Behaves like
  115.  * _cairo_malloc_ab().  The memory should be freed using free().
  116.  *
  117.  * @size should be a constant so that the compiler can optimize
  118.  * out a constant division.
  119.  *
  120.  * Return value: A pointer to the newly allocated memory, or %NULL in
  121.  * case of malloc() failure or overflow.
  122.  */
  123.  
  124. #define _cairo_malloc_abc(a, b, size) \
  125.   ((b) && (unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
  126.    (size) && (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
  127.    _cairo_malloc((unsigned) (a) * (unsigned) (b) * (unsigned) (size)))
  128.  
  129. /**
  130.  * _cairo_malloc_ab_plus_c:
  131.  * @n: number of elements to allocate
  132.  * @size: size of each element
  133.  * @k: additional size to allocate
  134.  *
  135.  * Allocates @n*@ksize+@k memory using _cairo_malloc(), taking care to not
  136.  * overflow when doing the arithmetic.  Behaves like
  137.  * _cairo_malloc_ab().  The memory should be freed using free().
  138.  *
  139.  * Return value: A pointer to the newly allocated memory, or %NULL in
  140.  * case of malloc() failure or overflow.
  141.  */
  142.  
  143. #define _cairo_malloc_ab_plus_c(n, size, k) \
  144.   ((size) && (unsigned) (n) >= INT32_MAX / (unsigned) (size) ? NULL : \
  145.    (unsigned) (k) >= INT32_MAX - (unsigned) (n) * (unsigned) (size) ? NULL : \
  146.    _cairo_malloc((unsigned) (n) * (unsigned) (size) + (unsigned) (k)))
  147.  
  148. #endif /* CAIRO_MALLOC_PRIVATE_H */
  149.