source: mainline/uspace/lib/c/generic/adt/dyn_array.c@ dd5c623

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

dyn_array: Non-allocating initialization API

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[62d3d87]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 * Implementation of dynamic array that grows or shrinks based upon no. of
35 * items it contains. Non-negligible part of implementation is in @ref
36 * dyn_array.h because of type genericity.
37 */
38
39#include <assert.h>
40#include <errno.h>
41#include <adt/dyn_array.h>
42#include <macros.h>
43#include <mem.h>
44#include <stdlib.h>
45
46
[4b1c6a4b]47
[62d3d87]48
49static int dyn_array_realloc(dyn_array_t *da, size_t capacity)
50{
51 if (capacity == da->capacity) {
52 return EOK;
53 }
54
55 void *new_data = realloc(da->_data, da->_item_size * capacity);
56 if (new_data) {
57 da->_data = new_data;
58 da->capacity = capacity;
59 }
60 return (new_data == NULL) ? ENOMEM : EOK;
61}
62
63void dyn_array_destroy(dyn_array_t *da)
64{
65 dyn_array_clear(da);
66 free(da->_data);
67 da->capacity = 0;
68}
69
70/** Remove item at give position, shift rest of array */
71void dyn_array_remove(dyn_array_t *da, size_t index)
72{
73 assert(index < da->size);
74 _dyn_array_unshift(da, index, 1);
[dd5c623]75 int rc = dyn_array_reserve(da, da->size);
[62d3d87]76 assert(rc == EOK);
77}
78
[4b1c6a4b]79/** Clear dynamic array (empty) */
80void dyn_array_clear(dyn_array_t *da)
81{
82 da->size = 0;
83}
84
[62d3d87]85/** Grows/shrinks array so that it effeciently stores desired capacity
86 *
87 * @param da
[dd5c623]88 * @param[in] desired capacity of array (items)
[62d3d87]89 *
90 * @return EOK
91 * @return ENOMEM
92 */
[dd5c623]93int dyn_array_reserve(dyn_array_t *da, size_t capacity)
[62d3d87]94{
95 const size_t factor = 2;
96 size_t new_capacity;
97 if (capacity > da->capacity) {
98 new_capacity = max(da->capacity * factor, capacity);
99 } else if (capacity < da->capacity / factor) {
100 new_capacity = min(da->capacity / factor, capacity);
101 } else {
102 new_capacity = capacity;
103 }
104
105 return dyn_array_realloc(da, new_capacity);
[dd5c623]106}
107
108void _dyn_array_initialize(dyn_array_t *da, size_t item_size)
109{
110 da->_item_size = item_size;
111 da->_data = NULL;
112
113 da->capacity = 0;
114 da->size = 0;
[62d3d87]115}
116
117/** Shift block of array
118 *
119 * Extends size of dynamic array, assumes sufficient capacity.
120 *
121 * @param da
122 * @param[in] index first item shifted
123 * @param[in] offset shift in no. of items
124 */
125void _dyn_array_shift(dyn_array_t *da, size_t index, size_t offset)
126{
127 assert(da->capacity >= da->size + offset);
128
129 void *src = da->_data + index * da->_item_size;
130 void *dst = da->_data + (index + offset) * da->_item_size;
131 size_t size = (da->size - index) * da->_item_size;
132 memmove(dst, src, size);
133 da->size += offset;
134}
135
136/** Unshift block of array
137 *
138 * Reduce size of dynamic array
139 *
140 * @param da
141 * @param[in] index first item unshifted (removed)
142 * @param[in] offset shift in no. of items
143 */
144void _dyn_array_unshift(dyn_array_t *da, size_t index, size_t offset)
145{
146 void *src = da->_data + (index + offset) * da->_item_size;
147 void *dst = da->_data + index * da->_item_size;
148 size_t size = (da->size - index - offset) * da->_item_size;
149 memmove(dst, src, size);
150 da->size -= offset;
151}
152
153
154/** @}
155 */
Note: See TracBrowser for help on using the repository browser.