1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2004 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
34 * Kristian Høgsberg <krh@redhat.com>
35 * Carl Worth <cworth@cworth.org>
43 * Initialize a new #cairo_array_t object to store objects each of size
46 * The #cairo_array_t object provides grow-by-doubling storage. It
47 * never interprets the data passed to it, nor does it provide any
48 * sort of callback mechanism for freeing resources held onto by
51 * When finished using the array, _cairo_array_fini() should be
52 * called to free resources allocated during use of the array.
55 _cairo_array_init (cairo_array_t
*array
, int element_size
)
58 array
->num_elements
= 0;
59 array
->element_size
= element_size
;
60 array
->elements
= NULL
;
62 array
->is_snapshot
= FALSE
;
66 * _cairo_array_init_snapshot:
67 * @array: A #cairo_array_t to be initialized as a snapshot
68 * @other: The #cairo_array_t from which to create the snapshot
70 * Initialize @array as an immutable copy of @other. It is an error to
71 * call an array-modifying function (other than _cairo_array_fini) on
72 * @array after calling this function.
75 _cairo_array_init_snapshot (cairo_array_t
*array
,
76 const cairo_array_t
*other
)
78 array
->size
= other
->size
;
79 array
->num_elements
= other
->num_elements
;
80 array
->element_size
= other
->element_size
;
81 array
->elements
= other
->elements
;
83 array
->is_snapshot
= TRUE
;
88 * @array: A #cairo_array_t
90 * Free all resources associated with @array. After this call, @array
91 * should not be used again without a subsequent call to
92 * _cairo_array_init() again first.
95 _cairo_array_fini (cairo_array_t
*array
)
97 if (array
->is_snapshot
)
100 if (array
->elements
) {
101 free (* array
->elements
);
102 free (array
->elements
);
107 * _cairo_array_grow_by:
108 * @array: a #cairo_array_t
110 * Increase the size of @array (if needed) so that there are at least
111 * @additional free spaces in the array. The actual size of the array
112 * is always increased by doubling as many times as necessary.
115 _cairo_array_grow_by (cairo_array_t
*array
, unsigned int additional
)
118 unsigned int old_size
= array
->size
;
119 unsigned int required_size
= array
->num_elements
+ additional
;
120 unsigned int new_size
;
122 assert (! array
->is_snapshot
);
124 /* check for integer overflow */
125 if (required_size
> INT_MAX
|| required_size
< array
->num_elements
)
126 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
128 if (CAIRO_INJECT_FAULT ())
129 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
131 if (required_size
<= old_size
)
132 return CAIRO_STATUS_SUCCESS
;
137 new_size
= old_size
* 2;
139 while (new_size
< required_size
)
140 new_size
= new_size
* 2;
142 if (array
->elements
== NULL
) {
143 array
->elements
= malloc (sizeof (char *));
144 if (unlikely (array
->elements
== NULL
))
145 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
147 *array
->elements
= NULL
;
150 array
->size
= new_size
;
151 new_elements
= _cairo_realloc_ab (*array
->elements
,
152 array
->size
, array
->element_size
);
154 if (unlikely (new_elements
== NULL
)) {
155 array
->size
= old_size
;
156 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
159 *array
->elements
= new_elements
;
161 return CAIRO_STATUS_SUCCESS
;
165 * _cairo_array_truncate:
166 * @array: a #cairo_array_t
168 * Truncate size of the array to @num_elements if less than the
169 * current size. No memory is actually freed. The stored objects
170 * beyond @num_elements are simply "forgotten".
173 _cairo_array_truncate (cairo_array_t
*array
, unsigned int num_elements
)
175 assert (! array
->is_snapshot
);
177 if (num_elements
< array
->num_elements
)
178 array
->num_elements
= num_elements
;
182 * _cairo_array_index:
183 * @array: a #cairo_array_t
184 * Returns: A pointer to the object stored at @index.
186 * If the resulting value is assigned to a pointer to an object of the same
187 * element_size as initially passed to _cairo_array_init() then that
188 * pointer may be used for further direct indexing with []. For
191 * <informalexample><programlisting>
192 * cairo_array_t array;
195 * _cairo_array_init (&array, sizeof(double));
196 * ... calls to _cairo_array_append() here ...
198 * values = _cairo_array_index (&array, 0);
199 * for (i = 0; i < _cairo_array_num_elements (&array); i++)
200 * ... use values[i] here ...
201 * </programlisting></informalexample>
204 _cairo_array_index (cairo_array_t
*array
, unsigned int index
)
206 /* We allow an index of 0 for the no-elements case.
207 * This makes for cleaner calling code which will often look like:
209 * elements = _cairo_array_index (array, num_elements);
210 * for (i=0; i < num_elements; i++) {
211 * ... use elements[i] here ...
214 * which in the num_elements==0 case gets the NULL pointer here,
215 * but never dereferences it.
217 if (index
== 0 && array
->num_elements
== 0)
220 assert (index
< array
->num_elements
);
222 return (void *) &(*array
->elements
)[index
* array
->element_size
];
226 * _cairo_array_copy_element:
227 * @array: a #cairo_array_t
229 * Copy a single element out of the array from index @index into the
230 * location pointed to by @dst.
233 _cairo_array_copy_element (cairo_array_t
*array
, int index
, void *dst
)
235 memcpy (dst
, _cairo_array_index (array
, index
), array
->element_size
);
239 * _cairo_array_append:
240 * @array: a #cairo_array_t
242 * Append a single item onto the array by growing the array by at
243 * least one element, then copying element_size bytes from @element
244 * into the array. The address of the resulting object within the
245 * array can be determined with:
247 * _cairo_array_index (array, _cairo_array_num_elements (array) - 1);
249 * Return value: %CAIRO_STATUS_SUCCESS if successful or
250 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available for the
254 _cairo_array_append (cairo_array_t
*array
,
257 assert (! array
->is_snapshot
);
259 return _cairo_array_append_multiple (array
, element
, 1);
263 * _cairo_array_append:
264 * @array: a #cairo_array_t
266 * Append one or more items onto the array by growing the array by
267 * @num_elements, then copying @num_elements * element_size bytes from
268 * @elements into the array.
270 * Return value: %CAIRO_STATUS_SUCCESS if successful or
271 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available for the
275 _cairo_array_append_multiple (cairo_array_t
*array
,
276 const void *elements
,
279 cairo_status_t status
;
282 assert (! array
->is_snapshot
);
284 status
= _cairo_array_allocate (array
, num_elements
, &dest
);
285 if (unlikely (status
))
288 memcpy (dest
, elements
, num_elements
* array
->element_size
);
290 return CAIRO_STATUS_SUCCESS
;
294 * _cairo_array_allocate:
295 * @array: a #cairo_array_t
297 * Allocate space at the end of the array for @num_elements additional
298 * elements, providing the address of the new memory chunk in
299 * @elements. This memory will be unitialized, but will be accounted
300 * for in the return value of _cairo_array_num_elements().
302 * Return value: %CAIRO_STATUS_SUCCESS if successful or
303 * %CAIRO_STATUS_NO_MEMORY if insufficient memory is available for the
307 _cairo_array_allocate (cairo_array_t
*array
,
308 unsigned int num_elements
,
311 cairo_status_t status
;
313 assert (! array
->is_snapshot
);
315 status
= _cairo_array_grow_by (array
, num_elements
);
316 if (unlikely (status
))
319 assert (array
->num_elements
+ num_elements
<= array
->size
);
321 *elements
= &(*array
->elements
)[array
->num_elements
* array
->element_size
];
323 array
->num_elements
+= num_elements
;
325 return CAIRO_STATUS_SUCCESS
;
329 * _cairo_array_num_elements:
330 * @array: a #cairo_array_t
331 * Returns: The number of elements stored in @array.
333 * This space was left intentionally blank, but gtk-doc filled it.
336 _cairo_array_num_elements (cairo_array_t
*array
)
338 return array
->num_elements
;
343 * @array: a #cairo_array_t
344 * Returns: The number of elements for which there is currently space
345 * allocated in @array.
347 * This space was left intentionally blank, but gtk-doc filled it.
350 _cairo_array_size (cairo_array_t
*array
)
356 * _cairo_user_data_array_init:
357 * @array: a #cairo_user_data_array_t
359 * Initializes a #cairo_user_data_array_t structure for future
360 * use. After initialization, the array has no keys. Call
361 * _cairo_user_data_array_fini() to free any allocated memory
362 * when done using the array.
365 _cairo_user_data_array_init (cairo_user_data_array_t
*array
)
367 _cairo_array_init (array
, sizeof (cairo_user_data_slot_t
));
371 * _cairo_user_data_array_fini:
372 * @array: a #cairo_user_data_array_t
374 * Destroys all current keys in the user data array and deallocates
375 * any memory allocated for the array itself.
378 _cairo_user_data_array_fini (cairo_user_data_array_t
*array
)
380 unsigned int num_slots
;
382 num_slots
= array
->num_elements
;
384 cairo_user_data_slot_t
*slots
;
386 slots
= _cairo_array_index (array
, 0);
388 if (slots
->user_data
!= NULL
&& slots
->destroy
!= NULL
)
389 slots
->destroy (slots
->user_data
);
391 } while (--num_slots
);
394 _cairo_array_fini (array
);
398 * _cairo_user_data_array_get_data:
399 * @array: a #cairo_user_data_array_t
400 * @key: the address of the #cairo_user_data_key_t the user data was
403 * Returns user data previously attached using the specified
404 * key. If no user data has been attached with the given key this
405 * function returns %NULL.
407 * Return value: the user data previously attached or %NULL.
410 _cairo_user_data_array_get_data (cairo_user_data_array_t
*array
,
411 const cairo_user_data_key_t
*key
)
414 cairo_user_data_slot_t
*slots
;
416 /* We allow this to support degenerate objects such as cairo_surface_nil. */
420 num_slots
= array
->num_elements
;
421 slots
= _cairo_array_index (array
, 0);
422 for (i
= 0; i
< num_slots
; i
++) {
423 if (slots
[i
].key
== key
)
424 return slots
[i
].user_data
;
431 * _cairo_user_data_array_set_data:
432 * @array: a #cairo_user_data_array_t
433 * @key: the address of a #cairo_user_data_key_t to attach the user data to
434 * @user_data: the user data to attach
435 * @destroy: a #cairo_destroy_func_t which will be called when the
436 * user data array is destroyed or when new user data is attached using the
439 * Attaches user data to a user data array. To remove user data,
440 * call this function with the key that was used to set it and %NULL
443 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
444 * slot could not be allocated for the user data.
447 _cairo_user_data_array_set_data (cairo_user_data_array_t
*array
,
448 const cairo_user_data_key_t
*key
,
450 cairo_destroy_func_t destroy
)
452 cairo_status_t status
;
454 cairo_user_data_slot_t
*slots
, *slot
, new_slot
;
458 new_slot
.user_data
= user_data
;
459 new_slot
.destroy
= destroy
;
462 new_slot
.user_data
= NULL
;
463 new_slot
.destroy
= NULL
;
467 num_slots
= array
->num_elements
;
468 slots
= _cairo_array_index (array
, 0);
469 for (i
= 0; i
< num_slots
; i
++) {
470 if (slots
[i
].key
== key
) {
472 if (slot
->destroy
&& slot
->user_data
)
473 slot
->destroy (slot
->user_data
);
476 if (user_data
&& slots
[i
].user_data
== NULL
) {
477 slot
= &slots
[i
]; /* Have to keep searching for an exact match */
483 return CAIRO_STATUS_SUCCESS
;
486 status
= _cairo_array_append (array
, &new_slot
);
487 if (unlikely (status
))
490 return CAIRO_STATUS_SUCCESS
;
494 _cairo_user_data_array_copy (cairo_user_data_array_t
*dst
,
495 cairo_user_data_array_t
*src
)
497 /* discard any existing user-data */
498 if (dst
->num_elements
!= 0) {
499 _cairo_user_data_array_fini (dst
);
500 _cairo_user_data_array_init (dst
);
503 if (src
->num_elements
== 0)
504 return CAIRO_STATUS_SUCCESS
;
506 return _cairo_array_append_multiple (dst
,
507 _cairo_array_index (src
, 0),
512 _cairo_user_data_array_foreach (cairo_user_data_array_t
*array
,
513 void (*func
) (const void *key
,
518 cairo_user_data_slot_t
*slots
;
521 num_slots
= array
->num_elements
;
522 slots
= _cairo_array_index (array
, 0);
523 for (i
= 0; i
< num_slots
; i
++) {
524 if (slots
[i
].user_data
!= NULL
)
525 func (slots
[i
].key
, slots
[i
].user_data
, closure
);