~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to cairo/src/cairo-malloc-private.h

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
Import upstream version 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 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
 
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 Corporation
 
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
/**
 
43
 * _cairo_malloc:
 
44
 * @size: size in bytes
 
45
 *
 
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.
 
49
 *
 
50
 * Return value: A pointer to the newly allocated memory, or %NULL in
 
51
 * case of malloc() failure or size is 0.
 
52
 */
 
53
 
 
54
#define _cairo_malloc(size) \
 
55
   ((size) ? malloc((unsigned) (size)) : NULL)
 
56
 
 
57
/**
 
58
 * _cairo_malloc_ab:
 
59
 * @n: number of elements to allocate
 
60
 * @size: size of each element
 
61
 *
 
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().
 
66
 *
 
67
 * @size should be a constant so that the compiler can optimize
 
68
 * out a constant division.
 
69
 *
 
70
 * Return value: A pointer to the newly allocated memory, or %NULL in
 
71
 * case of malloc() failure or overflow.
 
72
 */
 
73
 
 
74
#define _cairo_malloc_ab(a, size) \
 
75
  ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
 
76
   _cairo_malloc((unsigned) (a) * (unsigned) (size)))
 
77
 
 
78
/**
 
79
 * _cairo_realloc_ab:
 
80
 * @ptr: original pointer to block of memory to be resized
 
81
 * @n: number of elements to allocate
 
82
 * @size: size of each element
 
83
 *
 
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().
 
87
 *
 
88
 * @size should be a constant so that the compiler can optimize
 
89
 * out a constant division.
 
90
 *
 
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).
 
94
 */
 
95
 
 
96
#define _cairo_realloc_ab(ptr, a, size) \
 
97
  ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
 
98
   realloc(ptr, (unsigned) (a) * (unsigned) (size)))
 
99
 
 
100
/**
 
101
 * _cairo_malloc_abc:
 
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
 
105
 *
 
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().
 
109
 *
 
110
 * @size should be a constant so that the compiler can optimize
 
111
 * out a constant division.
 
112
 *
 
113
 * Return value: A pointer to the newly allocated memory, or %NULL in
 
114
 * case of malloc() failure or overflow.
 
115
 */
 
116
 
 
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)))
 
121
 
 
122
/**
 
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
 
127
 *
 
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().
 
131
 *
 
132
 * Return value: A pointer to the newly allocated memory, or %NULL in
 
133
 * case of malloc() failure or overflow.
 
134
 */
 
135
 
 
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)))
 
140
 
 
141
#endif /* CAIRO_MALLOC_PRIVATE_H */