beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-malloc-private.h
blob1e2c67f8d9aae19c249adf3c86d51d353302ebb6
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
4 * Copyright © 2007 Mozilla Corporation
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.
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
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Mozilla Foundation
33 * Contributor(s):
34 * Vladimir Vukicevic <vladimir@pobox.com>
37 #ifndef CAIRO_MALLOC_PRIVATE_H
38 #define CAIRO_MALLOC_PRIVATE_H
40 #include "cairo-wideint-private.h"
41 #include <stdlib.h>
43 #if HAVE_MEMFAULT
44 #include <memfault.h>
45 #define CAIRO_INJECT_FAULT() MEMFAULT_INJECT_FAULT()
46 #else
47 #define CAIRO_INJECT_FAULT() 0
48 #endif
50 /**
51 * _cairo_malloc:
52 * @size: size in bytes
54 * Allocate @size memory using malloc().
55 * The memory should be freed using free().
56 * malloc is skipped, if 0 bytes are requested, and %NULL will be returned.
58 * Return value: A pointer to the newly allocated memory, or %NULL in
59 * case of malloc() failure or size is 0.
60 **/
62 #define _cairo_malloc(size) \
63 ((size) ? malloc((unsigned) (size)) : NULL)
65 /**
66 * _cairo_malloc_ab:
67 * @a: number of elements to allocate
68 * @size: size of each element
70 * Allocates @a*@size memory using _cairo_malloc(), taking care to not
71 * overflow when doing the multiplication. Behaves much like
72 * calloc(), except that the returned memory is not set to zero.
73 * The memory should be freed using free().
75 * @size should be a constant so that the compiler can optimize
76 * out a constant division.
78 * Return value: A pointer to the newly allocated memory, or %NULL in
79 * case of malloc() failure or overflow.
80 **/
82 #define _cairo_malloc_ab(a, size) \
83 ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
84 _cairo_malloc((unsigned) (a) * (unsigned) (size)))
86 /**
87 * _cairo_realloc_ab:
88 * @ptr: original pointer to block of memory to be resized
89 * @a: number of elements to allocate
90 * @size: size of each element
92 * Reallocates @ptr a block of @a*@size memory using realloc(), taking
93 * care to not overflow when doing the multiplication. The memory
94 * should be freed using free().
96 * @size should be a constant so that the compiler can optimize
97 * out a constant division.
99 * Return value: A pointer to the newly allocated memory, or %NULL in
100 * case of realloc() failure or overflow (whereupon the original block
101 * of memory * is left untouched).
104 #define _cairo_realloc_ab(ptr, a, size) \
105 ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
106 realloc(ptr, (unsigned) (a) * (unsigned) (size)))
109 * _cairo_malloc_abc:
110 * @a: first factor of number of elements to allocate
111 * @b: second factor of number of elements to allocate
112 * @size: size of each element
114 * Allocates @a*@b*@size memory using _cairo_malloc(), taking care to not
115 * overflow when doing the multiplication. Behaves like
116 * _cairo_malloc_ab(). The memory should be freed using free().
118 * @size should be a constant so that the compiler can optimize
119 * out a constant division.
121 * Return value: A pointer to the newly allocated memory, or %NULL in
122 * case of malloc() failure or overflow.
125 #define _cairo_malloc_abc(a, b, size) \
126 ((b) && (unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
127 (size) && (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
128 _cairo_malloc((unsigned) (a) * (unsigned) (b) * (unsigned) (size)))
131 * _cairo_malloc_ab_plus_c:
132 * @a: number of elements to allocate
133 * @size: size of each element
134 * @c: additional size to allocate
136 * Allocates @a*@size+@c memory using _cairo_malloc(), taking care to not
137 * overflow when doing the arithmetic. Behaves similar to
138 * _cairo_malloc_ab(). The memory should be freed using free().
140 * Return value: A pointer to the newly allocated memory, or %NULL in
141 * case of malloc() failure or overflow.
144 #define _cairo_malloc_ab_plus_c(a, size, c) \
145 ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
146 (unsigned) (c) >= INT32_MAX - (unsigned) (a) * (unsigned) (size) ? NULL : \
147 _cairo_malloc((unsigned) (a) * (unsigned) (size) + (unsigned) (c)))
149 #endif /* CAIRO_MALLOC_PRIVATE_H */