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

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

dyn_array: Remove runtime pointer arithmetics

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