|
Packit Service |
f88c7f |
/*
|
|
Packit Service |
f88c7f |
* Copyright (C) 2010 Red Hat, Inc.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* Author: Angus Salkeld <asalkeld@redhat.com>
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* This file is part of libqb.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* libqb is free software: you can redistribute it and/or modify
|
|
Packit Service |
f88c7f |
* it under the terms of the GNU Lesser General Public License as published by
|
|
Packit Service |
f88c7f |
* the Free Software Foundation, either version 2.1 of the License, or
|
|
Packit Service |
f88c7f |
* (at your option) any later version.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* libqb is distributed in the hope that it will be useful,
|
|
Packit Service |
f88c7f |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
f88c7f |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
f88c7f |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
f88c7f |
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
#ifndef QB_ARRAY_H_DEFINED
|
|
Packit Service |
f88c7f |
#define QB_ARRAY_H_DEFINED
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
#include <stdint.h>
|
|
Packit Service |
f88c7f |
#ifndef S_SPLINT_S
|
|
Packit Service |
f88c7f |
#include <unistd.h>
|
|
Packit Service |
f88c7f |
#endif /* S_SPLINT_S */
|
|
Packit Service |
f88c7f |
#include <qb/qbdefs.h>
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/* *INDENT-OFF* */
|
|
Packit Service |
f88c7f |
#ifdef __cplusplus
|
|
Packit Service |
f88c7f |
extern "C" {
|
|
Packit Service |
f88c7f |
#endif
|
|
Packit Service |
f88c7f |
/* *INDENT-ON* */
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* @file qbarray.h
|
|
Packit Service |
f88c7f |
* This is a dynamic array (it can grow, but without moving memory).
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* @code
|
|
Packit Service |
f88c7f |
* arr = qb_array_create_2(64, sizeof(struct my_struct), 256);
|
|
Packit Service |
f88c7f |
* ...
|
|
Packit Service |
f88c7f |
* res = qb_array_index(arr, idx, (void**)&my_ptr);
|
|
Packit Service |
f88c7f |
* if (res < 0) {
|
|
Packit Service |
f88c7f |
* return res;
|
|
Packit Service |
f88c7f |
* }
|
|
Packit Service |
f88c7f |
* // use my_ptr, now even if there is a grow, this pointer will be valid.
|
|
Packit Service |
f88c7f |
* @endcode
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* Currently, this dynamic array abstract data type can accommodate only
|
|
Packit Service |
f88c7f |
* 2^(@c * QB_ARRAY_MAX_INDEX_BITS) elements, and with standard zero-based
|
|
Packit Service |
f88c7f |
* indexing, this gives a valid index range [0, @c QB_ARRAY_MAX_ELEMENTS),
|
|
Packit Service |
f88c7f |
* where the notation denotes the beginning of the interval is included and
|
|
Packit Service |
f88c7f |
* the end is excluded. In other words, client space shall avoid a pitfall
|
|
Packit Service |
f88c7f |
* of relying solely on the type of @max_elements parameter to
|
|
Packit Service |
f88c7f |
* @ref qb_array_create and/or of @idx parameter to @ref qb_array_index
|
|
Packit Service |
f88c7f |
* (these types conflict, anyway).
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
#define QB_ARRAY_MAX_INDEX_BITS 16
|
|
Packit Service |
f88c7f |
#define QB_ARRAY_MAX_ELEMENTS (1 << QB_ARRAY_MAX_INDEX_BITS)
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
struct qb_array;
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* This is an opaque data type representing an instance of an array.
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
typedef struct qb_array qb_array_t;
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Create an array with fixed sized elements.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* @param max_elements initial max elements
|
|
Packit Service |
f88c7f |
* @param element_size size of each element
|
|
Packit Service |
f88c7f |
* @return array instance
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
qb_array_t* qb_array_create(size_t max_elements, size_t element_size);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Create an array with fixed sized elements.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* @param max_elements initial max elements
|
|
Packit Service |
f88c7f |
* @param element_size size of each element
|
|
Packit Service |
f88c7f |
* @param autogrow_elements the number of elements to grow automatically by.
|
|
Packit Service |
f88c7f |
* @return array instance
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
qb_array_t* qb_array_create_2(size_t max_elements, size_t element_size,
|
|
Packit Service |
f88c7f |
size_t autogrow_elements);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Get an element at a particular index.
|
|
Packit Service |
f88c7f |
* @param a array instance
|
|
Packit Service |
f88c7f |
* @param idx the index, valid in [0, @c QB_ARRAY_MAX_ELEMENTS) range
|
|
Packit Service |
f88c7f |
* @param element_out the pointer to the element data
|
|
Packit Service |
f88c7f |
* @return (0 == success, else -errno)
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
int32_t qb_array_index(qb_array_t* a, int32_t idx, void** element_out);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Grow the array.
|
|
Packit Service |
f88c7f |
*
|
|
Packit Service |
f88c7f |
* @param a array instance
|
|
Packit Service |
f88c7f |
* @param max_elements the new maximum size of the array
|
|
Packit Service |
f88c7f |
* @return (0 == success, else -errno)
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
int32_t qb_array_grow(qb_array_t* a, size_t max_elements);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Get the number of bins used by the array.
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
size_t qb_array_num_bins_get(qb_array_t* a);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Get the number of elements per bin.
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
size_t qb_array_elems_per_bin_get(qb_array_t* a);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
typedef void (*qb_array_new_bin_cb_fn)(qb_array_t * a, uint32_t bin);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Get a callback when a new bin is allocated.
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
int32_t qb_array_new_bin_cb_set(qb_array_t * a, qb_array_new_bin_cb_fn fn);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/**
|
|
Packit Service |
f88c7f |
* Free all the memory used by the array.
|
|
Packit Service |
f88c7f |
* @param a array instance
|
|
Packit Service |
f88c7f |
*/
|
|
Packit Service |
f88c7f |
void qb_array_free(qb_array_t * a);
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
/* *INDENT-OFF* */
|
|
Packit Service |
f88c7f |
#ifdef __cplusplus
|
|
Packit Service |
f88c7f |
}
|
|
Packit Service |
f88c7f |
#endif
|
|
Packit Service |
f88c7f |
/* *INDENT-ON* */
|
|
Packit Service |
f88c7f |
|
|
Packit Service |
f88c7f |
#endif /* QB_ARRAY_H_DEFINED */
|