blob: 7730fe0d473f04a5ceab9c46786c6ceb0bf4a871 [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
44/**
45 * alist_expand_to() - Expand a list to the given size
46 *
47 * @lst: List to modify
48 * @inc_by: Amount to expand to
49 * Return: true if OK, false if out of memory
50 */
51static bool alist_expand_to(struct alist *lst, uint new_alloc)
52{
53 void *new_data;
54
55 if (lst->flags & ALISTF_FAIL)
56 return false;
57
58 /* avoid using realloc() since it increases code size */
59 new_data = malloc(lst->obj_size * new_alloc);
60 if (!new_data) {
61 lst->flags |= ALISTF_FAIL;
62 return false;
63 }
64
65 memcpy(new_data, lst->data, lst->obj_size * lst->alloc);
66 free(lst->data);
67
68 memset(new_data + lst->obj_size * lst->alloc, '\0',
69 lst->obj_size * (new_alloc - lst->alloc));
70 lst->alloc = new_alloc;
71 lst->data = new_data;
72
73 return true;
74}
75
76bool alist_expand_by(struct alist *lst, uint inc_by)
77{
78 return alist_expand_to(lst, lst->alloc + inc_by);
79}
80
81/**
82 * alist_expand_min() - Expand to at least the provided size
83 *
84 * Expands to the lowest power of two which can incorporate the new size
85 *
86 * @lst: alist to expand
87 * @min_alloc: Minimum new allocated size; if 0 then ALIST_INITIAL_SIZE is used
88 * Return: true if OK, false if out of memory
89 */
90static bool alist_expand_min(struct alist *lst, uint min_alloc)
91{
92 uint new_alloc;
93
94 for (new_alloc = lst->alloc ?: ALIST_INITIAL_SIZE;
95 new_alloc < min_alloc;)
96 new_alloc *= 2;
97
98 return alist_expand_to(lst, new_alloc);
99}
100
101const void *alist_get_ptr(const struct alist *lst, uint index)
102{
103 if (index >= lst->count)
104 return NULL;
105
106 return lst->data + index * lst->obj_size;
107}
108
Simon Glass1d49f782024-10-19 09:21:44 -0600109int alist_calc_index(const struct alist *lst, const void *ptr)
110{
111 uint index;
112
113 if (!lst->count || ptr < lst->data)
114 return -1;
115
116 index = (ptr - lst->data) / lst->obj_size;
117
118 return index;
119}
120
121const void *alist_next_ptrd(const struct alist *lst, const void *ptr)
122{
123 int index = alist_calc_index(lst, ptr);
124
125 assert(index != -1);
126
127 return alist_get_ptr(lst, index + 1);
128}
129
Simon Glass75581e42024-07-30 08:39:37 -0600130void *alist_ensure_ptr(struct alist *lst, uint index)
131{
132 uint minsize = index + 1;
133 void *ptr;
134
135 if (index >= lst->alloc && !alist_expand_min(lst, minsize))
136 return NULL;
137
138 ptr = lst->data + index * lst->obj_size;
139 if (minsize >= lst->count)
140 lst->count = minsize;
141
142 return ptr;
143}
144
145void *alist_add_placeholder(struct alist *lst)
146{
147 return alist_ensure_ptr(lst, lst->count);
148}
149
150void *alist_add_ptr(struct alist *lst, void *obj)
151{
152 void *ptr;
153
154 ptr = alist_add_placeholder(lst);
155 if (!ptr)
156 return NULL;
157 memcpy(ptr, obj, lst->obj_size);
158
159 return ptr;
160}
161
162void *alist_uninit_move_ptr(struct alist *alist, size_t *countp)
163{
164 void *ptr;
165
166 if (countp)
167 *countp = alist->count;
168 if (!alist->count) {
169 alist_uninit(alist);
170 return NULL;
171 }
172
173 ptr = alist->data;
174
175 /* Clear everything out so there is no record of the data */
176 alist_init(alist, alist->obj_size, 0);
177
178 return ptr;
179}