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;
41 cq
= kmalloc(alloc_size
, GFP_KERNEL
|__GFP_NORETRY
);
44 cq
->alloc_size
= alloc_size
;
48 spin_lock_init(&cq
->lock
);
51 EXPORT_SYMBOL_GPL(visor_charqueue_create
);
53 void visor_charqueue_enqueue(struct charqueue
*charqueue
, unsigned char c
)
55 int alloc_slots
= charqueue
->nslots
+1; /* 1 slot is always empty */
57 spin_lock(&charqueue
->lock
);
58 charqueue
->head
= (charqueue
->head
+1) % alloc_slots
;
59 if (charqueue
->head
== charqueue
->tail
)
60 /* overflow; overwrite the oldest entry */
61 charqueue
->tail
= (charqueue
->tail
+1) % alloc_slots
;
62 charqueue
->buf
[charqueue
->head
] = c
;
63 spin_unlock(&charqueue
->lock
);
65 EXPORT_SYMBOL_GPL(visor_charqueue_enqueue
);
67 BOOL
visor_charqueue_is_empty(struct charqueue
*charqueue
)
71 spin_lock(&charqueue
->lock
);
72 b
= IS_EMPTY(charqueue
);
73 spin_unlock(&charqueue
->lock
);
76 EXPORT_SYMBOL_GPL(visor_charqueue_is_empty
);
78 static int charqueue_dequeue_1(struct charqueue
*charqueue
)
80 int alloc_slots
= charqueue
->nslots
+ 1; /* 1 slot is always empty */
82 if (IS_EMPTY(charqueue
))
84 charqueue
->tail
= (charqueue
->tail
+1) % alloc_slots
;
85 return charqueue
->buf
[charqueue
->tail
];
88 int charqueue_dequeue(struct charqueue
*charqueue
)
92 spin_lock(&charqueue
->lock
);
93 rc
= charqueue_dequeue_1(charqueue
);
94 spin_unlock(&charqueue
->lock
);
98 int visor_charqueue_dequeue_n(struct charqueue
*charqueue
, unsigned char *buf
,
101 int rc
, counter
= 0, c
;
103 spin_lock(&charqueue
->lock
);
106 break; /* no more buffer space */
107 c
= charqueue_dequeue_1(charqueue
);
109 break; /* no more input */
110 *buf
= (unsigned char)(c
);
116 spin_unlock(&charqueue
->lock
);
119 EXPORT_SYMBOL_GPL(visor_charqueue_dequeue_n
);
121 void visor_charqueue_destroy(struct charqueue
*charqueue
)
123 if (charqueue
== NULL
)
127 EXPORT_SYMBOL_GPL(visor_charqueue_destroy
);