rogue

Ken Arnold's Rogue
git clone https://git.woozle.org/neale/rogue.git

Neale Pickett  ·  2011-10-13

error.c

  1/*
  2 * Functions for dealing with linked lists of goodies
  3 *
  4 * @(#)error.c	4.12 (Berkeley) 02/05/99
  5 */
  6
  7#include <curses.h>
  8#include "netprot.h"
  9
 10#ifdef MASTER
 11static int Total = 0;			/* Total dynamic memory bytes */
 12#endif
 13
 14/*
 15 * detach:
 16 *	Takes an item out of whatever linked list it might be in
 17 */
 18void
 19_detach(THING **list, THING *item)
 20{
 21    if (*list == item)
 22	*list = next(item);
 23    if (prev(item) != NULL)
 24	item->l_prev->l_next = next(item);
 25    if (next(item) != NULL)
 26	item->l_next->l_prev = prev(item);
 27    item->l_next = NULL;
 28    item->l_prev = NULL;
 29}
 30
 31/*
 32 * _attach:
 33 *	add an item to the head of a list
 34 */
 35void
 36_attach(THING **list, THING *item)
 37{
 38    if (*list != NULL)
 39    {
 40	item->l_next = *list;
 41	(*list)->l_prev = item;
 42	item->l_prev = NULL;
 43    }
 44    else
 45    {
 46	item->l_next = NULL;
 47	item->l_prev = NULL;
 48    }
 49    *list = item;
 50}
 51
 52/*
 53 * _free_list:
 54 *	Throw the whole blamed thing away
 55 */
 56void
 57_free_list(THING **ptr)
 58{
 59    THING *item;
 60
 61    while (*ptr != NULL)
 62    {
 63	item = *ptr;
 64	*ptr = next(item);
 65	discard(item);
 66    }
 67}
 68
 69/*
 70 * discard:
 71 *	Free up an item
 72 */
 73void
 74discard(THING *item)
 75{
 76#ifdef MASTER
 77    Total--;
 78#endif
 79    free((char *) item);
 80}
 81
 82/*
 83 * new_item
 84 *	Get a new item with a specified size
 85 */
 86THING *
 87new_item(void)
 88{
 89    THING *item;
 90
 91#ifdef MASTER
 92    if ((item = calloc(1, sizeof *item)) == NULL)
 93	msg("ran out of memory after %d items", Total);
 94    else
 95	Total++;
 96#else
 97    item = calloc(1, sizeof *item);
 98#endif
 99    item->l_next = NULL;
100    item->l_prev = NULL;
101    return item;
102}