src/ucx/cx/array_list.h

Sun, 19 Mar 2023 14:02:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Mar 2023 14:02:39 +0100
changeset 483
f69d88f86b9c
parent 438
22eca559aded
child 490
d218607f5a7e
permissions
-rw-r--r--

add error handling to webdav_xattr_serialze_map()

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * \file array_list.h
 * \brief Array list implementation.
 * \details Also provides several low-level functions for custom array list implementations.
 * \author Mike Becker
 * \author Olaf Wintermann
 * \version 3.0
 * \copyright 2-Clause BSD License
 */


#ifndef UCX_ARRAY_LIST_H
#define UCX_ARRAY_LIST_H

#include "list.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Defines a reallocation mechanism for arrays.
 */
struct cx_array_reallocator_s {
    /**
     * Re-allocates space for the given array.
     *
     * Implementations are not required to free the original array.
     * This allows re-allocation of static memory by allocating heap memory
     * and copying the array contents. The information in \p data can keep
     * track of the state of the memory or other additional allocator info.
     *
     * @param array the array to reallocate
     * @param capacity the new capacity (number of elements)
     * @param elem_size the size of each element
     * @param alloc a reference to this allocator
     * @return a pointer to the reallocated memory or \c NULL on failure
     */
    void *(*realloc)(
            void *array,
            size_t capacity,
            size_t elem_size,
            struct cx_array_reallocator_s *alloc
    );

    /**
     * Custom data pointer.
     */
    void *ptr1;
    /**
     * Custom data pointer.
     */
    void *ptr2;
    /**
     * Custom data integer.
     */
    size_t int1;
    /**
     * Custom data integer.
     */
    size_t int2;
};

/**
 * Return codes for cx_array_copy().
 */
enum cx_array_copy_result {
    CX_ARRAY_COPY_SUCCESS,
    CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED,
    CX_ARRAY_COPY_REALLOC_FAILED,
};

/**
 * Copies elements from one array to another.
 *
 * The elements are copied to the \p target array at the specified \p index,
 * overwriting possible elements. The index must be in range of the current
 * array \p size. If the number of elements added would extend the array's size,
 * and \p capacity is not \c NULL, the remaining capacity is used.
 *
 * If the capacity is insufficient to hold the new data, a reallocation
 * attempt is made, unless the allocator is set to \c NULL, in which case
 * this function ultimately returns a failure.
 *
 * @param target the target array
 * @param size a pointer to the size of the target array
 * @param capacity a pointer to the target array's capacity -
 * \c NULL if only the size shall be used to bound the array
 * @param index the index where the copied elements shall be placed
 * @param src the source array
 * @param elem_size the size of one element
 * @param elem_count the number of elements to copy
 * @param reallocator the array re-allocator to use, or \c NULL
 * if re-allocation shall not happen
 * @return zero on success, non-zero error code on failure
 */
enum cx_array_copy_result cx_array_copy(
        void **target,
        size_t *size,
        size_t *capacity,
        size_t index,
        void const *src,
        size_t elem_size,
        size_t elem_count,
        struct cx_array_reallocator_s *reallocator
) __attribute__((__nonnull__(1, 2, 5)));


/**
 * Swaps two array elements.
 *
 * @param arr the array
 * @param elem_size the element size
 * @param idx1 index of first element
 * @param idx2 index of second element
 */
void cx_array_swap(
        void *arr,
        size_t elem_size,
        size_t idx1,
        size_t idx2
) __attribute__((__nonnull__));

/**
 * Allocates an array list for storing elements with \p item_size bytes each.
 *
 * @param allocator the allocator for allocating the list memory
 * @param comparator the comparator for the elements
 * @param item_size the size of each element in bytes
 * @param initial_capacity the initial number of elements the array can store
 * @return the created list
 */
CxList *cxArrayListCreate(
        CxAllocator const *allocator,
        CxListComparator comparator,
        size_t item_size,
        size_t initial_capacity
) __attribute__((__nonnull__));


#ifdef __cplusplus
} // extern "C"
#endif

#endif // UCX_ARRAY_LIST_H

mercurial