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

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2008-2010 Daisuke Aoyama <aoyama@peach.ne.jp>.
        !             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;
        !            49:        return 0;
        !            50: }
        !            51: 
        !            52: void
        !            53: istgt_queue_destroy(ISTGT_QUEUE_Ptr head)
        !            54: {
        !            55:        ISTGT_QUEUE_Ptr qp;
        !            56:        ISTGT_QUEUE_Ptr next;
        !            57: 
        !            58:        if (head == NULL)
        !            59:                return;
        !            60:        for (qp = head->next; qp != NULL && qp != head; qp = next) {
        !            61:                next = qp->next;
        !            62:                free(qp);
        !            63:        }
        !            64:        head->next = NULL;
        !            65:        head->prev = NULL;
        !            66: }
        !            67: 
        !            68: int
        !            69: istgt_queue_count(ISTGT_QUEUE_Ptr head)
        !            70: {
        !            71:        ISTGT_QUEUE_Ptr qp;
        !            72:        int num;
        !            73: 
        !            74:        if (head == NULL)
        !            75:                return 0;
        !            76:        num = 0;
        !            77:        for (qp = head->next; qp != NULL && qp != head; qp = qp->next) {
        !            78:                num++;
        !            79:        }
        !            80:        return num;
        !            81: }
        !            82: 
        !            83: int
        !            84: istgt_queue_enqueue(ISTGT_QUEUE_Ptr head, void *elem)
        !            85: {
        !            86:        ISTGT_QUEUE_Ptr qp;
        !            87:        ISTGT_QUEUE_Ptr tail;
        !            88: 
        !            89:        if (head == NULL)
        !            90:                return -1;
        !            91:        qp = xmalloc(sizeof *qp);
        !            92:        memset(qp, 0, sizeof *qp);
        !            93: 
        !            94:        qp->elem = elem;
        !            95: 
        !            96:        tail = head->prev;
        !            97:        if (tail == NULL) {
        !            98:                head->next = qp;
        !            99:                head->prev = qp;
        !           100:                qp->next = head;
        !           101:                qp->prev = head;
        !           102:        } else {
        !           103:                tail->next = qp;
        !           104:                head->prev = qp;
        !           105:                qp->next = head;
        !           106:                qp->prev = tail;
        !           107:        }
        !           108:        return 0;
        !           109: }
        !           110: 
        !           111: void *
        !           112: istgt_queue_dequeue(ISTGT_QUEUE_Ptr head)
        !           113: {
        !           114:        ISTGT_QUEUE_Ptr first;
        !           115:        ISTGT_QUEUE_Ptr next;
        !           116:        void *elem;
        !           117: 
        !           118:        if (head == NULL)
        !           119:                return NULL;
        !           120:        first = head->next;
        !           121:        if (first == NULL || first == head) {
        !           122:                return NULL;
        !           123:        } else {
        !           124:                elem = first->elem;
        !           125:                next = first->next;
        !           126:                xfree(first);
        !           127:                if (next == NULL) {
        !           128:                        head->next = NULL;
        !           129:                        head->prev = NULL;
        !           130:                } else {
        !           131:                        head->next = next;
        !           132:                        next->prev = head;
        !           133:                }
        !           134:        }
        !           135:        return elem;
        !           136: }
        !           137: 
        !           138: int
        !           139: istgt_queue_enqueue_first(ISTGT_QUEUE_Ptr head, void *elem)
        !           140: {
        !           141:        ISTGT_QUEUE_Ptr qp;
        !           142:        ISTGT_QUEUE_Ptr first;
        !           143: 
        !           144:        if (head == NULL)
        !           145:                return -1;
        !           146:        qp = xmalloc(sizeof *qp);
        !           147:        memset(qp, 0, sizeof *qp);
        !           148: 
        !           149:        qp->elem = elem;
        !           150: 
        !           151:        first = head->next;
        !           152:        if (first == NULL || first == head) {
        !           153:                head->next = qp;
        !           154:                head->prev = qp;
        !           155:                qp->next = head;
        !           156:                qp->prev = head;
        !           157:        } else {
        !           158:                head->next = qp;
        !           159:                first->prev = qp;
        !           160:                qp->next = first;
        !           161:                qp->prev = head;
        !           162:        }
        !           163:        return 0;
        !           164: }

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