Blame src/array.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_array_h__
Packit Service 20376f
#define INCLUDE_array_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Use this to declare a typesafe resizable array of items, a la:
Packit Service 20376f
 *
Packit Service 20376f
 *     git_array_t(int) my_ints = GIT_ARRAY_INIT;
Packit Service 20376f
 *     ...
Packit Service 20376f
 *     int *i = git_array_alloc(my_ints);
Packit Service 20376f
 *     GITERR_CHECK_ALLOC(i);
Packit Service 20376f
 *     ...
Packit Service 20376f
 *     git_array_clear(my_ints);
Packit Service 20376f
 *
Packit Service 20376f
 * You may also want to do things like:
Packit Service 20376f
 *
Packit Service 20376f
 *     typedef git_array_t(my_struct) my_struct_array_t;
Packit Service 20376f
 */
Packit Service 20376f
#define git_array_t(type) struct { type *ptr; size_t size, asize; }
Packit Service 20376f
Packit Service 20376f
#define GIT_ARRAY_INIT { NULL, 0, 0 }
Packit Service 20376f
Packit Service 20376f
#define git_array_init(a) \
Packit Service 20376f
	do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
Packit Service 20376f
Packit Service 20376f
#define git_array_init_to_size(a, desired) \
Packit Service 20376f
	do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0)
Packit Service 20376f
Packit Service 20376f
#define git_array_clear(a) \
Packit Service 20376f
	do { git__free((a).ptr); git_array_init(a); } while (0)
Packit Service 20376f
Packit Service 20376f
#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
typedef git_array_t(char) git_array_generic_t;
Packit Service 20376f
Packit Service 20376f
/* use a generic array for growth so this can return the new item */
Packit Service 20376f
GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
Packit Service 20376f
{
Packit Service 20376f
	volatile git_array_generic_t *a = _a;
Packit Service 20376f
	size_t new_size;
Packit Service 20376f
	char *new_array;
Packit Service 20376f
Packit Service 20376f
	if (a->size < 8) {
Packit Service 20376f
		new_size = 8;
Packit Service 20376f
	} else {
Packit Service 20376f
		if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, a->size, 3))
Packit Service 20376f
			goto on_oom;
Packit Service 20376f
		new_size /= 2;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL)
Packit Service 20376f
		goto on_oom;
Packit Service 20376f
Packit Service 20376f
	a->ptr = new_array; a->asize = new_size; a->size++;
Packit Service 20376f
	return a->ptr + (a->size - 1) * item_size;
Packit Service 20376f
Packit Service 20376f
on_oom:
Packit Service 20376f
	git_array_clear(*a);
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define git_array_alloc(a) \
Packit Service 20376f
	(((a).size >= (a).asize) ? \
Packit Service 20376f
	git_array_grow(&(a), sizeof(*(a).ptr)) : \
Packit Service 20376f
	((a).ptr ? &(a).ptr[(a).size++] : NULL))
Packit Service 20376f
Packit Service 20376f
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
Packit Service 20376f
Packit Service 20376f
#define git_array_pop(a) ((a).size ? &(a).ptr[--(a).size] : NULL)
Packit Service 20376f
Packit Service 20376f
#define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : NULL)
Packit Service 20376f
Packit Service 20376f
#define git_array_size(a) (a).size
Packit Service 20376f
Packit Service 20376f
#define git_array_valid_index(a, i) ((i) < (a).size)
Packit Service 20376f
Packit Service 20376f
#define git_array_foreach(a, i, element) \
Packit Service 20376f
	for ((i) = 0; (i) < (a).size && ((element) = &(a).ptr[(i)]); (i)++)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_array__search(
Packit Service 20376f
	size_t *out,
Packit Service 20376f
	void *array_ptr,
Packit Service 20376f
	size_t item_size,
Packit Service 20376f
	size_t array_len,
Packit Service 20376f
	int (*compare)(const void *, const void *),
Packit Service 20376f
	const void *key)
Packit Service 20376f
{
Packit Service 20376f
	size_t lim;
Packit Service 20376f
	unsigned char *part, *array = array_ptr, *base = array_ptr;
Packit Service 20376f
	int cmp = -1;
Packit Service 20376f
Packit Service 20376f
	for (lim = array_len; lim != 0; lim >>= 1) {
Packit Service 20376f
		part = base + (lim >> 1) * item_size;
Packit Service 20376f
		cmp = (*compare)(key, part);
Packit Service 20376f
Packit Service 20376f
		if (cmp == 0) {
Packit Service 20376f
			base = part;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		if (cmp > 0) { /* key > p; take right partition */
Packit Service 20376f
			base = part + 1 * item_size;
Packit Service 20376f
			lim--;
Packit Service 20376f
		} /* else take left partition */
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (out)
Packit Service 20376f
		*out = (base - array) / item_size;
Packit Service 20376f
Packit Service 20376f
	return (cmp == 0) ? 0 : GIT_ENOTFOUND;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define git_array_search(out, a, cmp, key) \
Packit Service 20376f
	git_array__search(out, (a).ptr, sizeof(*(a).ptr), (a).size, \
Packit Service 20376f
		(cmp), (key))
Packit Service 20376f
Packit Service 20376f
#endif