comparison rogue5/list.c @ 33:f502bf60e6e4

Import Rogue 5.4 from the Roguelike Restoration Project (r1490)
author elwin
date Mon, 24 May 2010 20:10:59 +0000
parents
children
comparison
equal deleted inserted replaced
32:2dcd75e6a736 33:f502bf60e6e4
1 /*
2 * Functions for dealing with linked lists of goodies
3 *
4 * @(#)list.c 4.12 (Berkeley) 02/05/99
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980-1983, 1985, 1999 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 <stdlib.h>
14 #include <curses.h>
15 #include "rogue.h"
16
17 #ifdef MASTER
18 int total = 0; /* total dynamic memory bytes */
19 #endif
20
21 /*
22 * detach:
23 * takes an item out of whatever linked list it might be in
24 */
25
26 void
27 _detach(THING **list, THING *item)
28 {
29 if (*list == item)
30 *list = next(item);
31 if (prev(item) != NULL)
32 item->l_prev->l_next = next(item);
33 if (next(item) != NULL)
34 item->l_next->l_prev = prev(item);
35 item->l_next = NULL;
36 item->l_prev = NULL;
37 }
38
39 /*
40 * _attach:
41 * add an item to the head of a list
42 */
43
44 void
45 _attach(THING **list, THING *item)
46 {
47 if (*list != NULL)
48 {
49 item->l_next = *list;
50 (*list)->l_prev = item;
51 item->l_prev = NULL;
52 }
53 else
54 {
55 item->l_next = NULL;
56 item->l_prev = NULL;
57 }
58 *list = item;
59 }
60
61 /*
62 * _free_list:
63 * Throw the whole blamed thing away
64 */
65
66 void
67 _free_list(THING **ptr)
68 {
69 THING *item;
70
71 while (*ptr != NULL)
72 {
73 item = *ptr;
74 *ptr = next(item);
75 discard(item);
76 }
77 }
78
79 /*
80 * discard:
81 * Free up an item
82 */
83
84 void
85 discard(THING *item)
86 {
87 #ifdef MASTER
88 total--;
89 #endif
90 free(item);
91 }
92
93 /*
94 * new_item
95 * Get a new item with a specified size
96 */
97 THING *
98 new_item(void)
99 {
100 THING *item;
101
102 if ((item = calloc(1, sizeof *item)) == NULL) {
103 #ifdef MASTER
104 msg("ran out of memory after %d items", total);
105 #endif
106 return NULL;
107 }
108
109 #ifdef MASTER
110 total++;
111 #endif
112
113 item->l_next = NULL;
114 item->l_prev = NULL;
115 return item;
116 }