comparison urogue/list.c @ 256:c495a4f288c6

Import UltraRogue from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Tue, 31 Jan 2017 19:56:04 -0500
parents
children
comparison
equal deleted inserted replaced
253:d9badb9c0179 256:c495a4f288c6
1 /*
2 list.c - Functions for dealing with linked lists of goodies
3
4 UltraRogue: The Ultimate Adventure in the Dungeons of Doom
5 Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
6 All rights reserved.
7
8 Based on "Advanced Rogue"
9 Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
10 All rights reserved.
11
12 Based on "Rogue: Exploring the Dungeons of Doom"
13 Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
14 All rights reserved.
15
16 See the file LICENSE.TXT for full copyright and licensing information.
17 */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include "rogue.h"
23
24 static char errbuf[2 * LINELEN];
25
26 /*
27 ur_alloc()
28 ur_free()
29
30 These are just calls to the system alloc and free, and they also adjust
31 the totals. The buffer is cleared out because idents need to be zero
32 before going into the pack, or they will be used as indices!
33 */
34
35 void *
36 ur_alloc(size_t size)
37 {
38 char *buf_p;
39
40 total++;
41
42 buf_p = mem_malloc(size);
43
44 if (buf_p == NULL)
45 return(NULL);
46
47 memset(buf_p,0,size);
48
49 return(buf_p);
50 }
51
52 void
53 ur_free(void *buf_p)
54 {
55 mem_free(buf_p);
56 total--;
57 }
58
59 /*
60 detach()
61 Takes an item out of whatever linked list it might be in
62 .... function needs to be renamed....
63 */
64
65 void
66 _detach(struct linked_list **list, struct linked_list *item)
67 {
68 if (*list == item)
69 *list = next(item);
70
71 if (prev(item) != NULL)
72 item->l_prev->l_next = next(item);
73
74 if (next(item) != NULL)
75 item->l_next->l_prev = prev(item);
76
77 item->l_next = NULL;
78 item->l_prev = NULL;
79 }
80
81 /*
82 _attach()
83 add an item to the head of a list
84 ... this needs to be renamed as well ...
85 */
86
87 void
88 _attach(struct linked_list **list, struct linked_list *item)
89 {
90 if (*list != NULL)
91 {
92 item->l_next = *list;
93 (*list)->l_prev = item;
94 item->l_prev = NULL;
95 }
96 else
97 {
98 item->l_next = NULL;
99 item->l_prev = NULL;
100 }
101
102 *list = item;
103 }
104
105 /*
106 _attach_after()
107
108 Attaches the given item after the supplied one in the list. If the listed
109 item is NULL, the new item is attached at the head of the list.
110 */
111
112 void
113 _attach_after(linked_list **list_pp, linked_list *list_p, linked_list *new_p)
114 {
115 if (list_p == NULL)
116 {
117 _attach(list_pp, new_p); /* stuff it at the beginning */
118 return;
119 }
120
121 if (next(list_p) != NULL) /* something after this one? */
122 {
123 new_p->l_next = next(list_p);
124 list_p->l_next->l_prev = new_p;
125 }
126 else
127 new_p->l_next = NULL;
128
129 list_p->l_next = new_p;
130 new_p->l_prev = list_p;
131 }
132
133 /*
134 _free_list()
135 Throw the whole blamed thing away
136 */
137
138 void
139 _free_list(linked_list **ptr)
140 {
141 linked_list *item;
142
143 while(*ptr != NULL)
144 {
145 item = *ptr;
146 *ptr = next(item);
147 discard(item);
148 }
149 }
150
151 /*
152 discard()
153 free up an item
154 */
155
156 void
157 discard(struct linked_list *item)
158 {
159 throw_away(item->data.obj);
160 ur_free(item);
161 }
162
163 /*
164 throw_away()
165 toss out something (like discard, but without the link_list)
166 */
167
168 void
169 throw_away(struct object *ptr)
170 {
171 free_ident(ptr);
172 ur_free(ptr);
173 }
174
175 /*
176 new_item()
177 get a new item with a specified size
178 */
179
180 struct linked_list *
181 new_item(int size)
182 {
183 struct linked_list *item;
184
185 if ((item = new_list()) == NULL)
186 msg("Ran out of memory for header after %d items.", total);
187
188 if ((item->data.l_data = new_alloc(size)) == NULL)
189 msg("Ran out of memory for data after %d items.", total);
190
191 item->l_next = item->l_prev = NULL;
192
193 return(item);
194 }
195
196 void *
197 new_alloc(size_t size)
198 {
199 void *space = ur_alloc(size);
200
201 if (space == NULL)
202 {
203 sprintf(errbuf, "Rogue ran out of memory.");
204 fatal(errbuf);
205 }
206
207 return(space);
208 }
209
210 struct linked_list *
211 new_list(void)
212 {
213 union /* ugly_lint_hack */
214 {
215 struct linked_list *ll;
216 void *vptr;
217 } newp;
218
219 newp.vptr = mem_malloc(sizeof(struct linked_list));
220 memset(newp.vptr,0,sizeof(struct linked_list));
221 return(newp.ll);
222 }