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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 Corporation
34
* Vladimir Vukicevic <vladimir@pobox.com>
37
#ifndef CAIRO_MALLOC_PRIVATE_H
38
#define CAIRO_MALLOC_PRIVATE_H
40
#include "cairo-wideint-private.h"
44
* @size: size in bytes
46
* Allocate @size memory using malloc().
47
* The memory should be freed using free().
48
* malloc is skipped, if 0 bytes are requested, and %NULL will be returned.
50
* Return value: A pointer to the newly allocated memory, or %NULL in
51
* case of malloc() failure or size is 0.
54
#define _cairo_malloc(size) \
55
((size) ? malloc((unsigned) (size)) : NULL)
59
* @n: number of elements to allocate
60
* @size: size of each element
62
* Allocates @n*@size memory using _cairo_malloc(), taking care to not
63
* overflow when doing the multiplication. Behaves much like
64
* calloc(), except that the returned memory is not set to zero.
65
* The memory should be freed using free().
67
* @size should be a constant so that the compiler can optimize
68
* out a constant division.
70
* Return value: A pointer to the newly allocated memory, or %NULL in
71
* case of malloc() failure or overflow.
74
#define _cairo_malloc_ab(a, size) \
75
((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
76
_cairo_malloc((unsigned) (a) * (unsigned) (size)))
80
* @ptr: original pointer to block of memory to be resized
81
* @n: number of elements to allocate
82
* @size: size of each element
84
* Reallocates @ptr a block of @n*@size memory using realloc(), taking
85
* care to not overflow when doing the multiplication. The memory
86
* should be freed using free().
88
* @size should be a constant so that the compiler can optimize
89
* out a constant division.
91
* Return value: A pointer to the newly allocated memory, or %NULL in
92
* case of realloc() failure or overflow (whereupon the original block
93
* of memory * is left untouched).
96
#define _cairo_realloc_ab(ptr, a, size) \
97
((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
98
realloc(ptr, (unsigned) (a) * (unsigned) (size)))
102
* @n: first factor of number of elements to allocate
103
* @b: second factor of number of elements to allocate
104
* @size: size of each element
106
* Allocates @n*@b*@size memory using _cairo_malloc(), taking care to not
107
* overflow when doing the multiplication. Behaves like
108
* _cairo_malloc_ab(). The memory should be freed using free().
110
* @size should be a constant so that the compiler can optimize
111
* out a constant division.
113
* Return value: A pointer to the newly allocated memory, or %NULL in
114
* case of malloc() failure or overflow.
117
#define _cairo_malloc_abc(a, b, size) \
118
((b) && (unsigned) (a) >= INT32_MAX / (unsigned) (b) ? NULL : \
119
(size) && (unsigned) ((a)*(b)) >= INT32_MAX / (unsigned) (size) ? NULL : \
120
_cairo_malloc((unsigned) (a) * (unsigned) (b) * (unsigned) (size)))
123
* _cairo_malloc_ab_plus_c:
124
* @n: number of elements to allocate
125
* @size: size of each element
126
* @k: additional size to allocate
128
* Allocates @n*@ksize+@k memory using _cairo_malloc(), taking care to not
129
* overflow when doing the arithmetic. Behaves like
130
* _cairo_malloc_ab(). The memory should be freed using free().
132
* Return value: A pointer to the newly allocated memory, or %NULL in
133
* case of malloc() failure or overflow.
136
#define _cairo_malloc_ab_plus_c(n, size, k) \
137
((size) && (unsigned) (n) >= INT32_MAX / (unsigned) (size) ? NULL : \
138
(unsigned) (k) >= INT32_MAX - (unsigned) (n) * (unsigned) (size) ? NULL : \
139
_cairo_malloc((unsigned) (n) * (unsigned) (size) + (unsigned) (k)))
141
#endif /* CAIRO_MALLOC_PRIVATE_H */