Adding upstream version 3.70-pre9+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / syslinux / zonelist.c
blob62b1cf3e49324e7128c5d5f8a64ba60b228b09c8
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * zonelist.c
31 * Deal with syslinux_memmap's, which are data structures designed to
32 * hold memory maps. A zonelist is a sorted linked list of memory
33 * ranges, with the guarantee that no two adjacent blocks have the
34 * same range type. Additionally, all unspecified memory have a range
35 * type of zero.
38 #include <stdlib.h>
39 #include <syslinux/movebits.h>
42 * Create an empty syslinux_memmap list.
44 struct syslinux_memmap *syslinux_init_memmap(void)
46 struct syslinux_memmap *sp, *ep;
48 sp = malloc(sizeof(*sp));
49 if (!sp)
50 return NULL;
52 ep = malloc(sizeof(*ep));
53 if (!ep) {
54 free(sp);
55 return NULL;
58 sp->start = 0;
59 sp->type = SMT_UNDEFINED;
60 sp->next = ep;
62 ep->start = 0; /* Wrap around... */
63 ep->type = SMT_END; /* End of chain */
64 ep->next = NULL;
66 return sp;
70 * Add an item to a syslinux_memmap list, potentially overwriting
71 * what is already there.
73 int syslinux_add_memmap(struct syslinux_memmap **list,
74 addr_t start, addr_t len,
75 enum syslinux_memmap_types type)
77 addr_t last;
78 struct syslinux_memmap *mp, **mpp;
79 struct syslinux_memmap *mpi;
80 struct syslinux_memmap *range;
81 enum syslinux_memmap_types oldtype;
83 /* Remove this to make len == 0 mean all of memory */
84 if (len == 0)
85 return 0;
87 /* Last byte -- to avoid rollover */
88 last = start+len-1;
90 mpp = list;
91 oldtype = SMT_ERROR;
92 while (mp = *mpp, start > mp->start && mp->type != SMT_END) {
93 oldtype = mp->type;
94 mpp = &mp->next;
97 if (start < mp->start || mp->type == SMT_END) {
98 range = malloc(sizeof(*range));
99 if (!range)
100 return -1;
102 range->start = start;
103 range->type = type;
104 *mpp = range;
105 range->next = mp;
106 mpp = &range->next;
109 while (mp = *mpp, last > mp->start-1) {
110 oldtype = mp->type;
111 mp->type = type;
112 mpp = &mp->next;
115 if (last < mp->start-1) {
116 range = malloc(sizeof(*range));
117 if (!range)
118 return -1;
120 range->start = last+1;
121 range->type = oldtype;
122 *mpp = range;
123 range->next = mp;
126 /* Now the map is correct, but quite possibly not optimal. Scan the
127 map for ranges which are redundant and remove them. This is
128 technically excessive, since we scan the list to the end even
129 though only part of it could have changed. In particular, the first
130 entry that could change is one earlier than the first one changed
131 above, and once we stop changing things, there shouldn't be any
132 more changes. */
133 mpi = *list;
134 while (mpi->type != SMT_END) {
135 mp = mpi->next;
136 if (mpi->type == mp->type) {
137 mpi->next = mp->next;
138 free(mp);
139 } else {
140 /* Go on to the next one */
141 mpi = mp;
145 return 0;
149 * Verify what type a certain memory region is. This function returns
150 * SMT_ERROR if the memory region has multiple types.
152 enum syslinux_memmap_types syslinux_memmap_type(struct syslinux_memmap *list,
153 addr_t start, addr_t len)
155 addr_t last, llast;
157 last = start+len-1;
159 while (list->type != SMT_END) {
160 llast = list->next->start-1;
161 if (list->start <= start) {
162 if (llast >= last)
163 return list->type; /* Region has a well-defined type */
164 else if (llast >= start)
165 return SMT_ERROR; /* Crosses region boundary */
167 list = list->next;
170 return SMT_ERROR; /* Internal error? */
174 * Find the largest zone of a specific type. Returns -1 on failure.
176 int syslinux_memmap_largest(struct syslinux_memmap *list,
177 enum syslinux_memmap_types type,
178 addr_t *start, addr_t *len)
180 addr_t size, best_size = 0;
181 struct syslinux_memmap *best = NULL;
183 while (list->type != SMT_END) {
184 size = list->next->start - list->start;
186 if (list->type == type && size > best_size) {
187 best = list;
188 best_size = size;
191 list = list->next;
194 if (!best)
195 return -1;
197 *start = best->start;
198 *len = best_size;
200 return 0;
204 * Free a zonelist.
206 void syslinux_free_memmap(struct syslinux_memmap *list)
208 struct syslinux_memmap *ml;
210 while (list) {
211 ml = list;
212 list = list->next;
213 free(ml);
218 * Duplicate a zonelist. Returns NULL on failure.
220 struct syslinux_memmap *syslinux_dup_memmap(struct syslinux_memmap *list)
222 struct syslinux_memmap *newlist = NULL, **nlp = &newlist;
223 struct syslinux_memmap *ml;
225 while (list) {
226 ml = malloc(sizeof(*ml));
227 if (!ml) {
228 syslinux_free_memmap(newlist);
229 return NULL;
231 ml->start = list->start;
232 ml->type = list->type;
233 ml->next = NULL;
234 *nlp = ml;
235 nlp = &ml->next;
237 list = list->next;
240 return newlist;