[BACK]Return to list.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / rogue3

Annotation of early-roguelike/rogue3/list.c, Revision 1.1.1.1

1.1       rubenllo    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 "machdep.h"
                     17: #include "rogue.h"
                     18:
                     19: /*
                     20:  * detach:
                     21:  *     Takes an item out of whatever linked list it might be in
                     22:  */
                     23:
                     24: void
                     25: _detach(struct linked_list **list, struct linked_list *item)
                     26: {
                     27:     if (*list == item)
                     28:        *list = next(item);
                     29:     if (prev(item) != NULL) item->l_prev->l_next = next(item);
                     30:     if (next(item) != NULL) item->l_next->l_prev = prev(item);
                     31:     item->l_next = NULL;
                     32:     item->l_prev = NULL;
                     33: }
                     34:
                     35: /*
                     36:  * _attach:
                     37:  *     add an item to the head of a list
                     38:  */
                     39:
                     40: void
                     41: _attach(struct linked_list **list, struct linked_list *item)
                     42: {
                     43:     if (*list != NULL)
                     44:     {
                     45:        item->l_next = *list;
                     46:        (*list)->l_prev = item;
                     47:        item->l_prev = NULL;
                     48:     }
                     49:     else
                     50:     {
                     51:        item->l_next = NULL;
                     52:        item->l_prev = NULL;
                     53:     }
                     54:
                     55:     *list = item;
                     56: }
                     57:
                     58: /*
                     59:  * _free_list:
                     60:  *     Throw the whole blamed thing away
                     61:  */
                     62:
                     63: void
                     64: _free_list(struct linked_list **ptr)
                     65: {
                     66:     struct linked_list *item;
                     67:
                     68:     while (*ptr != NULL)
                     69:     {
                     70:        item = *ptr;
                     71:        *ptr = next(item);
                     72:        discard(item);
                     73:     }
                     74: }
                     75:
                     76: /*
                     77:  * discard:
                     78:  *     free up an item
                     79:  */
                     80:
                     81: void
                     82: discard(struct linked_list *item)
                     83: {
                     84:     total -= 2;
                     85:     FREE(item->l_data);
                     86:     FREE(item);
                     87: }
                     88:
                     89: /*
                     90:  * new_item
                     91:  *     get a new item with a specified size
                     92:  */
                     93:
                     94: struct linked_list *
                     95: new_item(int size)
                     96: {
                     97:     struct linked_list *item;
                     98:
                     99:     if ((item = (struct linked_list *) _new(sizeof *item)) == NULL)
                    100:     {
                    101:        msg("Ran out of memory for header after %d items", total);
                    102:        return NULL;
                    103:     }
                    104:
                    105:     if ((item->l_data = _new(size)) == NULL)
                    106:     {
                    107:        msg("Ran out of memory for data after %d items", total);
                    108:        free(item);
                    109:        return NULL;
                    110:     }
                    111:
                    112:     item->l_next = item->l_prev = NULL;
                    113:     memset(item->l_data,0,size);
                    114:     return item;
                    115: }
                    116:
                    117: char *
                    118: _new(size_t size)
                    119: {
                    120:     char *space = ALLOC(size);
                    121:
                    122:     if (space == NULL)
                    123:     {
                    124:        sprintf(prbuf, "Rogue ran out of memory (%ld).  Fatal error!", md_memused());
                    125:         fatal(prbuf);
                    126:     }
                    127:     total++;
                    128:     return space;
                    129: }

CVSweb