Home | History | Annotate | Line # | Download | only in dist
      1  1.1.1.2       wiz /* Id: dba_array.c,v 1.2 2020/06/22 19:20:40 schwarze Exp  */
      2      1.1  christos /*
      3      1.1  christos  * Copyright (c) 2016 Ingo Schwarze <schwarze (at) openbsd.org>
      4      1.1  christos  *
      5      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      6      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      7      1.1  christos  * copyright notice and this permission notice appear in all copies.
      8      1.1  christos  *
      9      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11      1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15      1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16      1.1  christos  *
     17      1.1  christos  * Allocation-based arrays for the mandoc database, for read-write access.
     18      1.1  christos  * The interface is defined in "dba_array.h".
     19      1.1  christos  */
     20  1.1.1.2       wiz #include "config.h"
     21  1.1.1.2       wiz 
     22      1.1  christos #include <assert.h>
     23      1.1  christos #include <stdint.h>
     24      1.1  christos #include <stdlib.h>
     25      1.1  christos #include <string.h>
     26      1.1  christos 
     27      1.1  christos #include "mandoc_aux.h"
     28      1.1  christos #include "dba_write.h"
     29      1.1  christos #include "dba_array.h"
     30      1.1  christos 
     31      1.1  christos struct dba_array {
     32      1.1  christos 	void	**ep;	/* Array of entries. */
     33      1.1  christos 	int32_t	 *em;	/* Array of map positions. */
     34      1.1  christos 	int	  flags;
     35      1.1  christos 	int32_t	  ea;	/* Entries allocated. */
     36      1.1  christos 	int32_t	  eu;	/* Entries used (including deleted). */
     37      1.1  christos 	int32_t	  ed;	/* Entries deleted. */
     38      1.1  christos 	int32_t	  ec;	/* Currently active entry. */
     39      1.1  christos 	int32_t	  pos;  /* Map position of this array. */
     40      1.1  christos };
     41      1.1  christos 
     42      1.1  christos 
     43      1.1  christos struct dba_array *
     44      1.1  christos dba_array_new(int32_t ea, int flags)
     45      1.1  christos {
     46      1.1  christos 	struct dba_array	*array;
     47      1.1  christos 
     48      1.1  christos 	assert(ea > 0);
     49      1.1  christos 	array = mandoc_malloc(sizeof(*array));
     50      1.1  christos 	array->ep = mandoc_reallocarray(NULL, ea, sizeof(*array->ep));
     51      1.1  christos 	array->em = mandoc_reallocarray(NULL, ea, sizeof(*array->em));
     52      1.1  christos 	array->ea = ea;
     53      1.1  christos 	array->eu = 0;
     54      1.1  christos 	array->ed = 0;
     55      1.1  christos 	array->ec = 0;
     56      1.1  christos 	array->flags = flags;
     57      1.1  christos 	array->pos = 0;
     58      1.1  christos 	return array;
     59      1.1  christos }
     60      1.1  christos 
     61      1.1  christos void
     62      1.1  christos dba_array_free(struct dba_array *array)
     63      1.1  christos {
     64      1.1  christos 	int32_t	 ie;
     65      1.1  christos 
     66      1.1  christos 	if (array == NULL)
     67      1.1  christos 		return;
     68      1.1  christos 	if (array->flags & DBA_STR)
     69      1.1  christos 		for (ie = 0; ie < array->eu; ie++)
     70      1.1  christos 			free(array->ep[ie]);
     71      1.1  christos 	free(array->ep);
     72      1.1  christos 	free(array->em);
     73      1.1  christos 	free(array);
     74      1.1  christos }
     75      1.1  christos 
     76      1.1  christos void
     77      1.1  christos dba_array_set(struct dba_array *array, int32_t ie, void *entry)
     78      1.1  christos {
     79      1.1  christos 	assert(ie >= 0);
     80      1.1  christos 	assert(ie < array->ea);
     81      1.1  christos 	assert(ie <= array->eu);
     82      1.1  christos 	if (ie == array->eu)
     83      1.1  christos 		array->eu++;
     84      1.1  christos 	if (array->flags & DBA_STR)
     85      1.1  christos 		entry = mandoc_strdup(entry);
     86      1.1  christos 	array->ep[ie] = entry;
     87      1.1  christos 	array->em[ie] = 0;
     88      1.1  christos }
     89      1.1  christos 
     90      1.1  christos void
     91      1.1  christos dba_array_add(struct dba_array *array, void *entry)
     92      1.1  christos {
     93      1.1  christos 	if (array->eu == array->ea) {
     94      1.1  christos 		assert(array->flags & DBA_GROW);
     95      1.1  christos 		array->ep = mandoc_reallocarray(array->ep,
     96      1.1  christos 		    2, sizeof(*array->ep) * array->ea);
     97      1.1  christos 		array->em = mandoc_reallocarray(array->em,
     98      1.1  christos 		    2, sizeof(*array->em) * array->ea);
     99      1.1  christos 		array->ea *= 2;
    100      1.1  christos 	}
    101      1.1  christos 	dba_array_set(array, array->eu, entry);
    102      1.1  christos }
    103      1.1  christos 
    104      1.1  christos void *
    105      1.1  christos dba_array_get(struct dba_array *array, int32_t ie)
    106      1.1  christos {
    107      1.1  christos 	if (ie < 0 || ie >= array->eu || array->em[ie] == -1)
    108      1.1  christos 		return NULL;
    109      1.1  christos 	return array->ep[ie];
    110      1.1  christos }
    111      1.1  christos 
    112      1.1  christos void
    113      1.1  christos dba_array_start(struct dba_array *array)
    114      1.1  christos {
    115      1.1  christos 	array->ec = array->eu;
    116      1.1  christos }
    117      1.1  christos 
    118      1.1  christos void *
    119      1.1  christos dba_array_next(struct dba_array *array)
    120      1.1  christos {
    121      1.1  christos 	if (array->ec < array->eu)
    122      1.1  christos 		array->ec++;
    123      1.1  christos 	else
    124      1.1  christos 		array->ec = 0;
    125      1.1  christos 	while (array->ec < array->eu && array->em[array->ec] == -1)
    126      1.1  christos 		array->ec++;
    127      1.1  christos 	return array->ec < array->eu ? array->ep[array->ec] : NULL;
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos void
    131      1.1  christos dba_array_del(struct dba_array *array)
    132      1.1  christos {
    133      1.1  christos 	if (array->ec < array->eu && array->em[array->ec] != -1) {
    134      1.1  christos 		array->em[array->ec] = -1;
    135      1.1  christos 		array->ed++;
    136      1.1  christos 	}
    137      1.1  christos }
    138      1.1  christos 
    139      1.1  christos void
    140      1.1  christos dba_array_undel(struct dba_array *array)
    141      1.1  christos {
    142      1.1  christos 	memset(array->em, 0, sizeof(*array->em) * array->eu);
    143      1.1  christos }
    144      1.1  christos 
    145      1.1  christos void
    146      1.1  christos dba_array_setpos(struct dba_array *array, int32_t ie, int32_t pos)
    147      1.1  christos {
    148      1.1  christos 	array->em[ie] = pos;
    149      1.1  christos }
    150      1.1  christos 
    151      1.1  christos int32_t
    152      1.1  christos dba_array_getpos(struct dba_array *array)
    153      1.1  christos {
    154      1.1  christos 	return array->pos;
    155      1.1  christos }
    156      1.1  christos 
    157      1.1  christos void
    158      1.1  christos dba_array_sort(struct dba_array *array, dba_compare_func func)
    159      1.1  christos {
    160      1.1  christos 	assert(array->ed == 0);
    161      1.1  christos 	qsort(array->ep, array->eu, sizeof(*array->ep), func);
    162      1.1  christos }
    163      1.1  christos 
    164      1.1  christos int32_t
    165      1.1  christos dba_array_writelen(struct dba_array *array, int32_t nmemb)
    166      1.1  christos {
    167      1.1  christos 	dba_int_write(array->eu - array->ed);
    168      1.1  christos 	return dba_skip(nmemb, array->eu - array->ed);
    169      1.1  christos }
    170      1.1  christos 
    171      1.1  christos void
    172      1.1  christos dba_array_writepos(struct dba_array *array)
    173      1.1  christos {
    174      1.1  christos 	int32_t	 ie;
    175      1.1  christos 
    176      1.1  christos 	array->pos = dba_tell();
    177      1.1  christos 	for (ie = 0; ie < array->eu; ie++)
    178      1.1  christos 		if (array->em[ie] != -1)
    179      1.1  christos 			dba_int_write(array->em[ie]);
    180      1.1  christos }
    181      1.1  christos 
    182      1.1  christos void
    183      1.1  christos dba_array_writelst(struct dba_array *array)
    184      1.1  christos {
    185      1.1  christos 	const char	*str;
    186      1.1  christos 
    187      1.1  christos 	dba_array_FOREACH(array, str)
    188      1.1  christos 		dba_str_write(str);
    189      1.1  christos 	dba_char_write('\0');
    190      1.1  christos }
    191