/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ /* cairo - a vector graphics library with display and print output * * Copyright © 2004 Red Hat, Inc * * This library is free software; you can redistribute it and/or * modify it either under the terms of the GNU Lesser General Public * License version 2.1 as published by the Free Software Foundation * (the "LGPL") or, at your option, under the terms of the Mozilla * Public License Version 1.1 (the "MPL"). If you do not alter this * notice, a recipient may use your version of this file under either * the MPL or the LGPL. * * You should have received a copy of the LGPL along with this library * in the file COPYING-LGPL-2.1; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA * You should have received a copy of the MPL along with this library * in the file COPYING-MPL-1.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY * OF ANY KIND, either express or implied. See the LGPL or the MPL for * the specific language governing rights and limitations. * * The Original Code is the cairo graphics library. * * The Initial Developer of the Original Code is University of Southern * California. * * Contributor(s): * Kristian Høgsberg * Carl Worth */ #include "cairoint.h" #include "cairo-array-private.h" #include "cairo-error-private.h" /*< private > * _cairo_array_init: * * Initialize a new #cairo_array_t object to store objects each of size * @element_size. * * The #cairo_array_t object provides grow-by-doubling storage. It * never interprets the data passed to it, nor does it provide any * sort of callback mechanism for freeing resources held onto by * stored objects. * * When finished using the array, _cairo_array_fini() should be * called to free resources allocated during use of the array. **/ void _cairo_array_init (cairo_array_t *array, unsigned int element_size) { array->size = 0; array->num_elements = 0; array->element_size = element_size; array->elements = NULL; } /*< private > * _cairo_array_fini: * @array: A #cairo_array_t * * Free all resources associated with @array. After this call, @array * should not be used again without a subsequent call to * _cairo_array_init() again first. **/ void _cairo_array_fini (cairo_array_t *array) { free (array->elements); } /*< private > * _cairo_array_grow_by: * @array: a #cairo_array_t * * Increase the size of @array (if needed) so that there are at least * @additional free spaces in the array. The actual size of the array * is always increased by doubling as many times as necessary. **/ cairo_status_t _cairo_array_grow_by (cairo_array_t *array, unsigned int additional) { char *new_elements; unsigned int old_size = array->size; unsigned int required_size = array->num_elements + additional; unsigned int new_size; /* check for integer overflow */ if (required_size > INT_MAX || required_size < array->num_elements) return _cairo_error (CAIRO_STATUS_NO_MEMORY); if (CAIRO_INJECT_FAULT ()) return _cairo_error (CAIRO_STATUS_NO_MEMORY); if (required_size <= old_size) return CAIRO_STATUS_SUCCESS; if (old_size == 0) new_size = 1; else new_size = old_size * 2; while (new_size < required_size) new_size = new_size * 2; array->size = new_size; new_elements = _cairo_realloc_ab (array->elements, array->size, array->element_size); if (unlikely (new_elements == NULL)) { array->size = old_size; return _cairo_error (CAIRO_STATUS_NO_MEMORY); } array->elements = new_elements; return CAIRO_STATUS_SUCCESS; } /*< private > * _cairo_array_truncate: * @array: a #cairo_array_t * * Truncate size of the array to @num_elements if less than the * current size. No memory is actually freed. The stored objects * beyond @num_elements are simply "forgotten". **/ void _cairo_array_truncate (cairo_array_t *array, unsigned int num_elements) { if (num_elements < array->num_elements) array->num_elements = num_elements; } /*< private > * _cairo_array_index: * @array: a #cairo_array_t * * If the resulting value is assigned to a pointer to an object of the same * element_size as initially passed to _cairo_array_init() then that * pointer may be used for further direct indexing with []. For * example: * * |[ * cairo_array_t array; * double *values; * * _cairo_array_init (&array, sizeof(double)); * ... calls to _cairo_array_append() here ... * * values = _cairo_array_index (&array, 0); * for (i = 0; i < _cairo_array_num_elements (&array); i++) * ... use values[i] here ... * ]| * * Returns: A pointer to the object stored at @index. **/ void * _cairo_array_index (cairo_array_t *array, unsigned int index) { /* We allow an index of 0 for the no-elements case. * This makes for cleaner calling code which will often look like: * * elements = _cairo_array_index (array, 0); * for (i=0; i < num_elements; i++) { * ... use elements[i] here ... * } * * which in the num_elements==0 case gets the NULL pointer here, * but never dereferences it. */ if (index == 0 && array->num_elements == 0) return NULL; assert (index < array->num_elements); return array->elements + (size_t)index * array->element_size; } /*< private > * _cairo_array_index_const: * @array: a #cairo_array_t * * If the resulting value is assigned to a pointer to an object of the same * element_size as initially passed to _cairo_array_init() then that * pointer may be used for further direct indexing with []. For * example: * * |[