Subversion Repositories Kolibri OS

Rev

Rev 1892 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1892 serge 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 
35
 */
36
 
37
#ifndef CAIRO_MALLOC_PRIVATE_H
38
#define CAIRO_MALLOC_PRIVATE_H
39
 
40
#include "cairo-wideint-private.h"
3959 Serge 41
#include 
1892 serge 42
 
43
#if HAVE_MEMFAULT
44
#include 
45
#define CAIRO_INJECT_FAULT() MEMFAULT_INJECT_FAULT()
46
#else
47
#define CAIRO_INJECT_FAULT() 0
48
#endif
49
 
50
/**
51
 * _cairo_malloc:
52
 * @size: size in bytes
53
 *
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.
57
 *
58
 * Return value: A pointer to the newly allocated memory, or %NULL in
59
 * case of malloc() failure or size is 0.
3959 Serge 60
 **/
1892 serge 61
 
62
#define _cairo_malloc(size) \
63
   ((size) ? malloc((unsigned) (size)) : NULL)
64
 
65
/**
66
 * _cairo_malloc_ab:
3959 Serge 67
 * @a: number of elements to allocate
1892 serge 68
 * @size: size of each element
69
 *
3959 Serge 70
 * Allocates @a*@size memory using _cairo_malloc(), taking care to not
1892 serge 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().
74
 *
75
 * @size should be a constant so that the compiler can optimize
76
 * out a constant division.
77
 *
78
 * Return value: A pointer to the newly allocated memory, or %NULL in
79
 * case of malloc() failure or overflow.
3959 Serge 80
 **/
1892 serge 81
 
82
#define _cairo_malloc_ab(a, size) \
83
  ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
84
   _cairo_malloc((unsigned) (a) * (unsigned) (size)))
85
 
86
/**
87
 * _cairo_realloc_ab:
88
 * @ptr: original pointer to block of memory to be resized
3959 Serge 89
 * @a: number of elements to allocate
1892 serge 90
 * @size: size of each element
91
 *
3959 Serge 92
 * Reallocates @ptr a block of @a*@size memory using realloc(), taking
1892 serge 93
 * care to not overflow when doing the multiplication.  The memory
94
 * should be freed using free().
95
 *
96
 * @size should be a constant so that the compiler can optimize
97
 * out a constant division.
98
 *
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).
3959 Serge 102
 **/
1892 serge 103
 
104
#define _cairo_realloc_ab(ptr, a, size) \
105
  ((size) && (unsigned) (a) >= INT32_MAX / (unsigned) (size) ? NULL : \
106
   realloc(ptr, (unsigned) (a) * (unsigned) (size)))
107
 
108
/**
109
 * _cairo_malloc_abc:
3959 Serge 110
 * @a: first factor of number of elements to allocate
1892 serge 111
 * @b: second factor of number of elements to allocate
112
 * @size: size of each element
113
 *
3959 Serge 114
 * Allocates @a*@b*@size memory using _cairo_malloc(), taking care to not
1892 serge 115
 * overflow when doing the multiplication.  Behaves like
116
 * _cairo_malloc_ab().  The memory should be freed using free().
117
 *
118
 * @size should be a constant so that the compiler can optimize
119
 * out a constant division.
120
 *
121
 * Return value: A pointer to the newly allocated memory, or %NULL in
122
 * case of malloc() failure or overflow.
3959 Serge 123
 **/
1892 serge 124
 
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)))
129
 
130
/**
131
 * _cairo_malloc_ab_plus_c:
3959 Serge 132
 * @a: number of elements to allocate
1892 serge 133
 * @size: size of each element
3959 Serge 134
 * @c: additional size to allocate
1892 serge 135
 *
3959 Serge 136
 * Allocates @a*@size+@c memory using _cairo_malloc(), taking care to not
137
 * overflow when doing the arithmetic.  Behaves similar to
1892 serge 138
 * _cairo_malloc_ab().  The memory should be freed using free().
139
 *
140
 * Return value: A pointer to the newly allocated memory, or %NULL in
141
 * case of malloc() failure or overflow.
3959 Serge 142
 **/
1892 serge 143
 
3959 Serge 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)))
1892 serge 148
 
149
#endif /* CAIRO_MALLOC_PRIVATE_H */