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

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

libc: Add more dyn_array functions
Conflicts:

boot/Makefile.common

  • Property mode set to 100644
File size: 4.3 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);
75 int rc = _dyn_array_reserve(da, da->size);
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]85int _dyn_array_initialize(dyn_array_t *da, size_t item_size, size_t capacity)
86{
87 da->_item_size = item_size;
88 da->_data = NULL;
[4b1c6a4b]89
[62d3d87]90 da->capacity = 0;
91 da->size = 0;
92
93 return _dyn_array_reserve(da, capacity);
94}
95
96/** Grows/shrinks array so that it effeciently stores desired capacity
97 *
98 * @param da
99 * @param[in] desired capacity of array
100 *
101 * @return EOK
102 * @return ENOMEM
103 */
104int _dyn_array_reserve(dyn_array_t *da, size_t capacity)
105{
106 const size_t factor = 2;
107 size_t new_capacity;
108 if (capacity > da->capacity) {
109 new_capacity = max(da->capacity * factor, capacity);
110 } else if (capacity < da->capacity / factor) {
111 new_capacity = min(da->capacity / factor, capacity);
112 } else {
113 new_capacity = capacity;
114 }
115
116 return dyn_array_realloc(da, new_capacity);
117}
118
119/** Shift block of array
120 *
121 * Extends size of dynamic array, assumes sufficient capacity.
122 *
123 * @param da
124 * @param[in] index first item shifted
125 * @param[in] offset shift in no. of items
126 */
127void _dyn_array_shift(dyn_array_t *da, size_t index, size_t offset)
128{
129 assert(da->capacity >= da->size + offset);
130
131 void *src = da->_data + index * da->_item_size;
132 void *dst = da->_data + (index + offset) * da->_item_size;
133 size_t size = (da->size - index) * da->_item_size;
134 memmove(dst, src, size);
135 da->size += offset;
136}
137
138/** Unshift block of array
139 *
140 * Reduce size of dynamic array
141 *
142 * @param da
143 * @param[in] index first item unshifted (removed)
144 * @param[in] offset shift in no. of items
145 */
146void _dyn_array_unshift(dyn_array_t *da, size_t index, size_t offset)
147{
148 void *src = da->_data + (index + offset) * da->_item_size;
149 void *dst = da->_data + index * da->_item_size;
150 size_t size = (da->size - index - offset) * da->_item_size;
151 memmove(dst, src, size);
152 da->size -= offset;
153}
154
155
156/** @}
157 */
Note: See TracBrowser for help on using the repository browser.