source: mainline/uspace/lib/c/include/adt/dyn_array.h@ 9559cf8

Last change on this file since 9559cf8 was 25697163, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Replacing int with errno_t

The merged code from system-daemon still used the type int
for indicating an error instead of using errno_t. This
commit corrects this mistake

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2015 Michal Koutny
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#ifndef LIBC_DYN_ARRAY_H_
36#define LIBC_DYN_ARRAY_H_
37
38#include <errno.h>
39#include <stdbool.h>
40#include <stddef.h>
41
42typedef struct {
43 /** Data buffer of array */
44 void *_data;
45 /** Size on bytes of a single item */
46 size_t _item_size;
47
48 /** Allocated space (in items) of the array */
49 size_t capacity;
50 /** No. of items in the array */
51 size_t size;
52} dyn_array_t;
53
54/** Initialize dynamic array
55 *
56 * @param[in] capacity initial capacity of array
57 *
58 * @return void
59 */
60#define dyn_array_initialize(dyn_array, type) \
61 _dyn_array_initialize((dyn_array), sizeof(type))
62
63/** Dynamic array accessor
64 *
65 * @return lvalue for the given item
66 */
67#define dyn_array_at(dyn_array, type, index) \
68 (*((type *) (dyn_array)->_data + index))
69
70/** Access last element
71 *
72 * @return lvalue for the last item
73 */
74#define dyn_array_last(dyn_array, type) \
75 (*((type *) (dyn_array)->_data + ((dyn_array)->size - 1)))
76
77/** Insert item at given position, shift rest of array
78 *
79 * @return EOK on success
80 * @return ENOMEM on failure
81 */
82#define dyn_array_insert(dyn_array, type, index, value) \
83({ \
84 size_t _index = (index); \
85 dyn_array_t *_da = (dyn_array); \
86 errno_t rc = dyn_array_reserve(_da, _da->size + 1); \
87 if (!rc) { \
88 _dyn_array_shift(_da, _index, 1); \
89 dyn_array_at(_da, type, _index) = (value); \
90 } \
91 rc; \
92})
93
94/** Insert item at the end of array
95 *
96 * @return EOK on success
97 * @return ENOMEM on failure
98 */
99#define dyn_array_append(dyn_array, type, value) \
100 dyn_array_insert(dyn_array, type, (dyn_array)->size, (value))
101
102/** Dynamic array iteration
103 *
104 * @param[in] dyn_array dyn_array_t (not pointer)
105 * @param[in] it name of variable used as iterator, it's pointer
106 * to @p type
107 */
108#define dyn_array_foreach(dyn_array, type, it) \
109 for (type *it = NULL; it == NULL; it = (type *)1) \
110 for (type *_it = (type *)(dyn_array)._data; \
111 it = _it, _it != ((type *)(dyn_array)._data + (dyn_array).size); \
112 ++_it)
113
114/** Find first occurence of value
115 *
116 * @param[in] dyn_array dyn_array_t *
117 * @param[in] value value to search for
118 *
119 * @return index of found value or size of array when no found
120 */
121#define dyn_array_find(dyn_array, type, value) \
122({ \
123 size_t _result = (dyn_array)->size; \
124 dyn_array_foreach(*(dyn_array), type, _it) { \
125 if (*_it == value) { \
126 _result = _it - (type *)(dyn_array)->_data; \
127 break; \
128 } \
129 } \
130 _result; \
131})
132
133extern void dyn_array_destroy(dyn_array_t *);
134extern void dyn_array_remove(dyn_array_t *, size_t);
135void dyn_array_clear(dyn_array_t *);
136void dyn_array_clear_range(dyn_array_t *, size_t, size_t);
137extern errno_t dyn_array_concat(dyn_array_t *, dyn_array_t *);
138extern errno_t dyn_array_reserve(dyn_array_t *, size_t);
139
140extern void _dyn_array_initialize(dyn_array_t *, size_t);
141extern void _dyn_array_shift(dyn_array_t *, size_t, size_t);
142extern void _dyn_array_unshift(dyn_array_t *, size_t, size_t);
143
144#endif
145
146/** @}
147 */
Note: See TracBrowser for help on using the repository browser.