3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
19 * Simple character queue implementation for Linux kernel mode.
22 #include "charqueue.h"
24 #define MYDRVNAME "charqueue"
26 #define IS_EMPTY(charqueue) (charqueue->head == charqueue->tail)
31 spinlock_t lock; /* read/write lock for this structure */
36 struct charqueue *visor_charqueue_create(ulong nslots)
38 int alloc_size = sizeof(struct charqueue) + nslots + 1;
39 struct charqueue *cq = kmalloc(alloc_size, GFP_KERNEL|__GFP_NORETRY);
42 ERRDRV("visor_charqueue_create allocation failed (alloc_size=%d)",
46 cq->alloc_size = alloc_size;
50 spin_lock_init(&cq->lock);
53 EXPORT_SYMBOL_GPL(visor_charqueue_create);
55 void visor_charqueue_enqueue(struct charqueue *charqueue, unsigned char c)
57 int alloc_slots = charqueue->nslots+1; /* 1 slot is always empty */
59 spin_lock(&charqueue->lock);
60 charqueue->head = (charqueue->head+1) % alloc_slots;
61 if (charqueue->head == charqueue->tail)
62 /* overflow; overwrite the oldest entry */
63 charqueue->tail = (charqueue->tail+1) % alloc_slots;
64 charqueue->buf[charqueue->head] = c;
65 spin_unlock(&charqueue->lock);
67 EXPORT_SYMBOL_GPL(visor_charqueue_enqueue);
69 BOOL visor_charqueue_is_empty(struct charqueue *charqueue)
73 spin_lock(&charqueue->lock);
74 b = IS_EMPTY(charqueue);
75 spin_unlock(&charqueue->lock);
78 EXPORT_SYMBOL_GPL(visor_charqueue_is_empty);
80 static int charqueue_dequeue_1(struct charqueue *charqueue)
82 int alloc_slots = charqueue->nslots + 1; /* 1 slot is always empty */
84 if (IS_EMPTY(charqueue))
86 charqueue->tail = (charqueue->tail+1) % alloc_slots;
87 return charqueue->buf[charqueue->tail];
90 int charqueue_dequeue(struct charqueue *charqueue)
94 spin_lock(&charqueue->lock);
95 rc = charqueue_dequeue_1(charqueue);
96 spin_unlock(&charqueue->lock);
100 int visor_charqueue_dequeue_n(struct charqueue *charqueue, unsigned char *buf,
103 int rc, counter = 0, c;
105 spin_lock(&charqueue->lock);
108 break; /* no more buffer space */
109 c = charqueue_dequeue_1(charqueue);
111 break; /* no more input */
112 *buf = (unsigned char)(c);
118 spin_unlock(&charqueue->lock);
121 EXPORT_SYMBOL_GPL(visor_charqueue_dequeue_n);
123 void visor_charqueue_destroy(struct charqueue *charqueue)
125 if (charqueue == NULL)
129 EXPORT_SYMBOL_GPL(visor_charqueue_destroy);