Added spdylay_queue
This commit is contained in:
parent
a03afa6a82
commit
1000998228
|
@ -31,9 +31,9 @@ DISTCLEANFILES = $(pkgconfig_DATA)
|
|||
|
||||
lib_LTLIBRARIES = libspdylay.la
|
||||
|
||||
OBJECTS = spdylay_pq.c spdylay_map.c
|
||||
OBJECTS = spdylay_pq.c spdylay_map.c spdylay_queue.c
|
||||
|
||||
HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h
|
||||
HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h spdylay_queue.h
|
||||
|
||||
libspdylay_la_SOURCES = $(HFILES) $(OBJECTS)
|
||||
libspdylay_la_LDFLAGS = -no-undefined \
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Spdylay - SPDY Library
|
||||
*
|
||||
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "spdylay_queue.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
void spdylay_queue_init(spdylay_queue *queue)
|
||||
{
|
||||
queue->front = queue->back = NULL;
|
||||
}
|
||||
|
||||
void spdylay_queue_free(spdylay_queue *queue)
|
||||
{
|
||||
if(!queue) {
|
||||
return;
|
||||
}
|
||||
spdylay_queue_cell *p = queue->front;
|
||||
while(p) {
|
||||
spdylay_queue_cell *next = p->next;
|
||||
free(p);
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
|
||||
int spdylay_queue_push(spdylay_queue *queue, void *data)
|
||||
{
|
||||
spdylay_queue_cell *new_cell = (spdylay_queue_cell*)malloc
|
||||
(sizeof(spdylay_queue_cell));
|
||||
if(!new_cell) {
|
||||
return SPDYLAY_ERR_NOMEM;
|
||||
}
|
||||
new_cell->data = data;
|
||||
new_cell->next = NULL;
|
||||
if(queue->back) {
|
||||
queue->back->next = new_cell;
|
||||
queue->back = new_cell;
|
||||
|
||||
} else {
|
||||
queue->front = queue->back = new_cell;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void spdylay_queue_pop(spdylay_queue *queue)
|
||||
{
|
||||
spdylay_queue_cell *front = queue->front;
|
||||
assert(front);
|
||||
queue->front = front->next;
|
||||
if(front == queue->back) {
|
||||
queue->back = NULL;
|
||||
}
|
||||
free(front);
|
||||
}
|
||||
|
||||
void* spdylay_queue_front(spdylay_queue *queue)
|
||||
{
|
||||
assert(queue->front);
|
||||
return queue->front->data;
|
||||
}
|
||||
|
||||
void* spdylay_queue_back(spdylay_queue *queue)
|
||||
{
|
||||
assert(queue->back);
|
||||
return queue->back->data;
|
||||
}
|
||||
|
||||
int spdylay_queue_empty(spdylay_queue *queue)
|
||||
{
|
||||
return queue->front == NULL;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Spdylay - SPDY Library
|
||||
*
|
||||
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef SPDYLAY_QUEUE_H
|
||||
#define SPDYLAY_QUEUE_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
|
||||
#include <spdylay/spdylay.h>
|
||||
|
||||
typedef struct spdylay_queue_cell {
|
||||
void *data;
|
||||
struct spdylay_queue_cell *next;
|
||||
} spdylay_queue_cell;
|
||||
|
||||
typedef struct {
|
||||
spdylay_queue_cell *front, *back;
|
||||
} spdylay_queue;
|
||||
|
||||
void spdylay_queue_init(spdylay_queue *queue);
|
||||
void spdylay_queue_free(spdylay_queue *queue);
|
||||
int spdylay_queue_push(spdylay_queue *queue, void *data);
|
||||
void spdylay_queue_pop(spdylay_queue *queue);
|
||||
void* spdylay_queue_front(spdylay_queue *queue);
|
||||
void* spdylay_queue_back(spdylay_queue *queue);
|
||||
int spdylay_queue_empty(spdylay_queue *queue);
|
||||
|
||||
#endif /* SPDYLAY_QUEUE_H */
|
|
@ -25,9 +25,9 @@ if HAVE_CUNIT
|
|||
|
||||
check_PROGRAMS = main
|
||||
|
||||
OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c
|
||||
OBJECTS = main.c spdylay_pq_test.c spdylay_map_test.c spdylay_queue_test.c
|
||||
|
||||
HFILES = spdylay_pq_test.h spdylay_map_test.h
|
||||
HFILES = spdylay_pq_test.h spdylay_map_test.h spdylay_queue_test.h
|
||||
|
||||
main_SOURCES = $(HFILES) $(OBJECTS)
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
/* include test cases' include files here */
|
||||
#include "spdylay_pq_test.h"
|
||||
#include "spdylay_map_test.h"
|
||||
#include "spdylay_queue_test.h"
|
||||
|
||||
int init_suite1(void)
|
||||
{
|
||||
|
@ -57,7 +58,8 @@ int main()
|
|||
|
||||
/* add the tests to the suite */
|
||||
if(!CU_add_test(pSuite, "pq", test_spdylay_pq) ||
|
||||
!CU_add_test(pSuite, "map", test_spdylay_map)) {
|
||||
!CU_add_test(pSuite, "map", test_spdylay_map) ||
|
||||
!CU_add_test(pSuite, "queue", test_spdylay_queue)) {
|
||||
CU_cleanup_registry();
|
||||
return CU_get_error();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Spdylay - SPDY Library
|
||||
*
|
||||
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "spdylay_queue_test.h"
|
||||
|
||||
#include <CUnit/CUnit.h>
|
||||
|
||||
#include "spdylay_queue.h"
|
||||
|
||||
void test_spdylay_queue()
|
||||
{
|
||||
int ints[] = { 1, 2, 3, 4, 5 };
|
||||
int i;
|
||||
spdylay_queue queue;
|
||||
spdylay_queue_init(&queue);
|
||||
CU_ASSERT(spdylay_queue_empty(&queue));
|
||||
for(i = 0; i < 5; ++i) {
|
||||
spdylay_queue_push(&queue, &ints[i]);
|
||||
CU_ASSERT_EQUAL(ints[0], *(int*)(spdylay_queue_front(&queue)));
|
||||
CU_ASSERT(!spdylay_queue_empty(&queue));
|
||||
}
|
||||
for(i = 0; i < 5; ++i) {
|
||||
CU_ASSERT_EQUAL(ints[i], *(int*)(spdylay_queue_front(&queue)));
|
||||
spdylay_queue_pop(&queue);
|
||||
}
|
||||
CU_ASSERT(spdylay_queue_empty(&queue));
|
||||
spdylay_queue_free(&queue);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Spdylay - SPDY Library
|
||||
*
|
||||
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef SPDYLAY_QUEUE_TEST_H
|
||||
#define SPDYLAY_QUEUE_TEST_H
|
||||
|
||||
void test_spdylay_queue();
|
||||
|
||||
#endif // SPDYLAY_QUEUE_TEST_H
|
Loading…
Reference in New Issue