Prevent crash due to buffer overflow detection (and on some platforms probably actual...
[rogue-pphs.git] / list.c
1 /*
2 * Functions for dealing with linked lists of goodies
3 *
4 * @(#)list.c 3.3 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include <stdlib.h>
15 #include <string.h>
16 #include "rogue.h"
17
18 /*
19 * detach:
20 * Takes an item out of whatever linked list it might be in
21 */
22
23 _detach(list, item)
24 register struct linked_list **list, *item;
25 {
26 if (*list == item)
27 *list = next(item);
28 if (prev(item) != NULL) item->l_prev->l_next = next(item);
29 if (next(item) != NULL) item->l_next->l_prev = prev(item);
30 item->l_next = NULL;
31 item->l_prev = NULL;
32 }
33
34 /*
35 * _attach:
36 * add an item to the head of a list
37 */
38
39 _attach(list, item)
40 register struct linked_list **list, *item;
41 {
42 if (*list != NULL)
43 {
44 item->l_next = *list;
45 (*list)->l_prev = item;
46 item->l_prev = NULL;
47 }
48 else
49 {
50 item->l_next = NULL;
51 item->l_prev = NULL;
52 }
53
54 *list = item;
55 }
56
57 /*
58 * _free_list:
59 * Throw the whole blamed thing away
60 */
61
62 _free_list(ptr)
63 register struct linked_list **ptr;
64 {
65 register struct linked_list *item;
66
67 while (*ptr != NULL)
68 {
69 item = *ptr;
70 *ptr = next(item);
71 discard(item);
72 }
73 }
74
75 /*
76 * discard:
77 * free up an item
78 */
79
80 discard(item)
81 register struct linked_list *item;
82 {
83 total -= 2;
84 FREE(item->l_data);
85 FREE(item);
86 }
87
88 /*
89 * new_item
90 * get a new item with a specified size
91 */
92
93 struct linked_list *
94 new_item(size)
95 int size;
96 {
97 register struct linked_list *item;
98
99 if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
100 msg("Ran out of memory for header after %d items", total);
101 if ((item->l_data = new(size)) == NULL)
102 msg("Ran out of memory for data after %d items", total);
103 item->l_next = item->l_prev = NULL;
104 memset(item->l_data,0,size);
105 return item;
106 }
107
108 char *
109 new(size)
110 int size;
111 {
112 register char *space = ALLOC(size);
113
114 if (space == NULL)
115 {
116 sprintf(prbuf, "Rogue ran out of memory (%d). Fatal error!", md_memused());
117 fatal(prbuf);
118 }
119 total++;
120 return space;
121 }