blob: 32cd45b0b017539e1269c728afd677877486ef3d [file] [log] [blame]
Simon Glass75581e42024-07-30 08:39:37 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Handles a contiguous list of pointers which be allocated and freed
4 *
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <alist.h>
10#include <display_options.h>
11#include <malloc.h>
12#include <stdio.h>
13#include <string.h>
14
15enum {
16 ALIST_INITIAL_SIZE = 4, /* default size of unsized list */
17};
18
19bool alist_init(struct alist *lst, uint obj_size, uint start_size)
20{
21 /* Avoid realloc for the initial size to help malloc_simple */
22 memset(lst, '\0', sizeof(struct alist));
23 if (start_size) {
24 lst->data = calloc(obj_size, start_size);
25 if (!lst->data) {
26 lst->flags = ALISTF_FAIL;
27 return false;
28 }
29 lst->alloc = start_size;
30 }
31 lst->obj_size = obj_size;
32
33 return true;
34}
35
36void alist_uninit(struct alist *lst)
37{
38 free(lst->data);
39
40 /* Clear fields to avoid any confusion */
41 memset(lst, '\0', sizeof(struct alist));
42}
43
Simon Glass5bd4ead2024-10-19 09:21:46 -060044void alist_empty(struct alist *lst)
45{
46 lst->count = 0;
47}
48
Simon Glass75581e42024-07-30 08:39:37 -060049/**
50 * alist_expand_to() - Expand a list to the given size
51 *
52 * @lst: List to modify
53 * @inc_by: Amount to expand to
54 * Return: true if OK, false if out of memory
55 */
56static bool alist_expand_to(struct alist *lst, uint new_alloc)
57{
58 void *new_data;
59
60 if (lst->flags & ALISTF_FAIL)
61 return false;
62
63 /* avoid using realloc() since it increases code size */
64 new_data = malloc(lst->obj_size * new_alloc);
65 if (!new_data) {
66 lst->flags |= ALISTF_FAIL;
67 return false;
68 }
69
70 memcpy(new_data, lst->data, lst->obj_size * lst->alloc);
71 free(lst->data);
72
73 memset(new_data + lst->obj_size * lst->alloc, '\0',
74 lst->obj_size * (new_alloc - lst->alloc));
75 lst->alloc = new_alloc;
76 lst->data = new_data;
77
78 return true;
79}
80
81bool alist_expand_by(struct alist *lst, uint inc_by)
82{
83 return alist_expand_to(lst, lst->alloc + inc_by);
84}
85
86/**
87 * alist_expand_min() - Expand to at least the provided size
88 *
89 * Expands to the lowest power of two which can incorporate the new size
90 *
91 * @lst: alist to expand
92 * @min_alloc: Minimum new allocated size; if 0 then ALIST_INITIAL_SIZE is used
93 * Return: true if OK, false if out of memory
94 */
95static bool alist_expand_min(struct alist *lst, uint min_alloc)
96{
97 uint new_alloc;
98
99 for (new_alloc = lst->alloc ?: ALIST_INITIAL_SIZE;
100 new_alloc < min_alloc;)
101 new_alloc *= 2;
102
103 return alist_expand_to(lst, new_alloc);
104}
105
106const void *alist_get_ptr(const struct alist *lst, uint index)
107{
108 if (index >= lst->count)
109 return NULL;
110
111 return lst->data + index * lst->obj_size;
112}
113
Simon Glass1d49f782024-10-19 09:21:44 -0600114int alist_calc_index(const struct alist *lst, const void *ptr)
115{
116 uint index;
117
118 if (!lst->count || ptr < lst->data)
119 return -1;
120
121 index = (ptr - lst->data) / lst->obj_size;
122
123 return index;
124}
125
Simon Glassd785a772024-10-19 09:21:45 -0600126bool alist_chk_ptr(const struct alist *lst, const void *ptr)
127{
128 int index = alist_calc_index(lst, ptr);
129
130 return index >= 0 && index < lst->count;
131}
132
Simon Glass1d49f782024-10-19 09:21:44 -0600133const void *alist_next_ptrd(const struct alist *lst, const void *ptr)
134{
135 int index = alist_calc_index(lst, ptr);
136
137 assert(index != -1);
138
139 return alist_get_ptr(lst, index + 1);
140}
141
Simon Glass75581e42024-07-30 08:39:37 -0600142void *alist_ensure_ptr(struct alist *lst, uint index)
143{
144 uint minsize = index + 1;
145 void *ptr;
146
147 if (index >= lst->alloc && !alist_expand_min(lst, minsize))
148 return NULL;
149
150 ptr = lst->data + index * lst->obj_size;
151 if (minsize >= lst->count)
152 lst->count = minsize;
153
154 return ptr;
155}
156
157void *alist_add_placeholder(struct alist *lst)
158{
159 return alist_ensure_ptr(lst, lst->count);
160}
161
162void *alist_add_ptr(struct alist *lst, void *obj)
163{
164 void *ptr;
165
166 ptr = alist_add_placeholder(lst);
167 if (!ptr)
168 return NULL;
169 memcpy(ptr, obj, lst->obj_size);
170
171 return ptr;
172}
173
174void *alist_uninit_move_ptr(struct alist *alist, size_t *countp)
175{
176 void *ptr;
177
178 if (countp)
179 *countp = alist->count;
180 if (!alist->count) {
181 alist_uninit(alist);
182 return NULL;
183 }
184
185 ptr = alist->data;
186
187 /* Clear everything out so there is no record of the data */
188 alist_init(alist, alist->obj_size, 0);
189
190 return ptr;
191}