Annotation of embedaddon/istgt/src/istgt_queue.c, revision 1.1.1.2

1.1       misho       1: /*
1.1.1.2 ! misho       2:  * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
1.1       misho       3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     15:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     17:  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
                     18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     19:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     20:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     21:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     22:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     23:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     24:  * SUCH DAMAGE.
                     25:  *
                     26:  */
                     27: 
                     28: #ifdef HAVE_CONFIG_H
                     29: #include "config.h"
                     30: #endif
                     31: 
                     32: #include <inttypes.h>
                     33: #include <stdint.h>
                     34: 
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: 
                     38: #include "istgt_misc.h"
                     39: #include "istgt_queue.h"
                     40: 
                     41: int
                     42: istgt_queue_init(ISTGT_QUEUE_Ptr head)
                     43: {
                     44:        if (head == NULL)
                     45:                return -1;
                     46:        head->prev = NULL;
                     47:        head->next = NULL;
                     48:        head->elem = NULL;
1.1.1.2 ! misho      49:        head->num = 0;
1.1       misho      50:        return 0;
                     51: }
                     52: 
                     53: void
                     54: istgt_queue_destroy(ISTGT_QUEUE_Ptr head)
                     55: {
                     56:        ISTGT_QUEUE_Ptr qp;
                     57:        ISTGT_QUEUE_Ptr next;
                     58: 
                     59:        if (head == NULL)
                     60:                return;
                     61:        for (qp = head->next; qp != NULL && qp != head; qp = next) {
                     62:                next = qp->next;
                     63:                free(qp);
                     64:        }
                     65:        head->next = NULL;
                     66:        head->prev = NULL;
                     67: }
                     68: 
                     69: int
                     70: istgt_queue_count(ISTGT_QUEUE_Ptr head)
                     71: {
1.1.1.2 ! misho      72: #if 0
1.1       misho      73:        ISTGT_QUEUE_Ptr qp;
                     74:        int num;
                     75: 
                     76:        if (head == NULL)
                     77:                return 0;
                     78:        num = 0;
                     79:        for (qp = head->next; qp != NULL && qp != head; qp = qp->next) {
                     80:                num++;
                     81:        }
                     82:        return num;
1.1.1.2 ! misho      83: #else
        !            84:        if (head == NULL)
        !            85:                return 0;
        !            86:        return head->num;
        !            87: #endif
1.1       misho      88: }
                     89: 
                     90: int
                     91: istgt_queue_enqueue(ISTGT_QUEUE_Ptr head, void *elem)
                     92: {
                     93:        ISTGT_QUEUE_Ptr qp;
                     94:        ISTGT_QUEUE_Ptr tail;
                     95: 
                     96:        if (head == NULL)
                     97:                return -1;
                     98:        qp = xmalloc(sizeof *qp);
                     99:        memset(qp, 0, sizeof *qp);
                    100: 
                    101:        qp->elem = elem;
                    102: 
                    103:        tail = head->prev;
                    104:        if (tail == NULL) {
                    105:                head->next = qp;
                    106:                head->prev = qp;
                    107:                qp->next = head;
                    108:                qp->prev = head;
                    109:        } else {
                    110:                tail->next = qp;
                    111:                head->prev = qp;
                    112:                qp->next = head;
                    113:                qp->prev = tail;
                    114:        }
1.1.1.2 ! misho     115:        head->num++;
1.1       misho     116:        return 0;
                    117: }
                    118: 
                    119: void *
                    120: istgt_queue_dequeue(ISTGT_QUEUE_Ptr head)
                    121: {
                    122:        ISTGT_QUEUE_Ptr first;
                    123:        ISTGT_QUEUE_Ptr next;
                    124:        void *elem;
                    125: 
                    126:        if (head == NULL)
                    127:                return NULL;
                    128:        first = head->next;
                    129:        if (first == NULL || first == head) {
                    130:                return NULL;
                    131:        } else {
                    132:                elem = first->elem;
                    133:                next = first->next;
                    134:                xfree(first);
                    135:                if (next == NULL) {
                    136:                        head->next = NULL;
                    137:                        head->prev = NULL;
                    138:                } else {
                    139:                        head->next = next;
                    140:                        next->prev = head;
                    141:                }
                    142:        }
1.1.1.2 ! misho     143:        head->num--;
1.1       misho     144:        return elem;
                    145: }
                    146: 
                    147: int
                    148: istgt_queue_enqueue_first(ISTGT_QUEUE_Ptr head, void *elem)
                    149: {
                    150:        ISTGT_QUEUE_Ptr qp;
                    151:        ISTGT_QUEUE_Ptr first;
                    152: 
                    153:        if (head == NULL)
                    154:                return -1;
                    155:        qp = xmalloc(sizeof *qp);
                    156:        memset(qp, 0, sizeof *qp);
                    157: 
                    158:        qp->elem = elem;
                    159: 
                    160:        first = head->next;
                    161:        if (first == NULL || first == head) {
                    162:                head->next = qp;
                    163:                head->prev = qp;
                    164:                qp->next = head;
                    165:                qp->prev = head;
                    166:        } else {
                    167:                head->next = qp;
                    168:                first->prev = qp;
                    169:                qp->next = first;
                    170:                qp->prev = head;
                    171:        }
1.1.1.2 ! misho     172:        head->num++;
1.1       misho     173:        return 0;
                    174: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>