Commit 8b6600be309b30fa7e7a49736121ab70c1cfe6c9

Authored by Georg Hopp
1 parent f41259db

Change queue to list and make a new queue class based on it.

@@ -72,6 +72,7 @@ AC_CONFIG_FILES([Makefile @@ -72,6 +72,7 @@ AC_CONFIG_FILES([Makefile
72 src/Makefile 72 src/Makefile
73 src/cbuf/Makefile 73 src/cbuf/Makefile
74 src/hash/Makefile 74 src/hash/Makefile
  75 + src/list/Makefile
75 src/queue/Makefile 76 src/queue/Makefile
76 src/tree/Makefile 77 src/tree/Makefile
77 src/dynarray/Makefile 78 src/dynarray/Makefile
@@ -2,6 +2,7 @@ nobase_include_HEADERS = trdata.h \ @@ -2,6 +2,7 @@ nobase_include_HEADERS = trdata.h \
2 tr/cbuf.h \ 2 tr/cbuf.h \
3 tr/hash.h \ 3 tr/hash.h \
4 tr/hash_value.h \ 4 tr/hash_value.h \
  5 + tr/list.h \
5 tr/queue.h \ 6 tr/queue.h \
6 tr/tree.h \ 7 tr/tree.h \
7 tr/dynarray.h \ 8 tr/dynarray.h \
  1 +/**
  2 + * \file
  3 + * Holds requests ready for processing.
  4 + *
  5 + * \todo change this to a real queue.
  6 + *
  7 + * \author Georg Hopp
  8 + *
  9 + * \copyright
  10 + * Copyright © 2014 Georg Hopp
  11 + *
  12 + * This program is free software: you can redistribute it and/or modify
  13 + * it under the terms of the GNU General Public License as published by
  14 + * the Free Software Foundation, either version 3 of the License, or
  15 + * (at your option) any later version.
  16 + *
  17 + * This program is distributed in the hope that it will be useful,
  18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20 + * GNU General Public License for more details.
  21 + *
  22 + * You should have received a copy of the GNU General Public License
  23 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24 + */
  25 +
  26 +#ifndef __TR_LIST_H__
  27 +#define __TR_LIST_H__
  28 +
  29 +#include <sys/types.h>
  30 +
  31 +#include "trbase.h"
  32 +
  33 +
  34 +TR_CLASS(TR_List) {
  35 + size_t size;
  36 + size_t start;
  37 + size_t end;
  38 + void ** data;
  39 + int free_msgs;
  40 +};
  41 +TR_INSTANCE_INIT(TR_List);
  42 +TR_CLASSVARS_DECL(TR_List) {};
  43 +
  44 +void TR_listPut(TR_List, void *);
  45 +void TR_listPutFirst(TR_List, void *);
  46 +void * TR_listGet(TR_List);
  47 +void * TR_listGetFirst(TR_List);
  48 +
  49 +#define TR_listEmpty(this) (this->start == this->end)
  50 +#define TR_listFirst(this) ((this)->start)
  51 +#define TR_listLast(this) ((this)->end - 1)
  52 +#define TR_listSize(this) ((this)->size)
  53 +
  54 +#endif // __TR_LIST_H__
  55 +
  56 +// vim: set ts=4 sw=4:
@@ -29,26 +29,22 @@ @@ -29,26 +29,22 @@
29 #include <sys/types.h> 29 #include <sys/types.h>
30 30
31 #include "trbase.h" 31 #include "trbase.h"
  32 +#include "tr/list.h"
32 33
33 34
34 TR_CLASS(TR_Queue) { 35 TR_CLASS(TR_Queue) {
35 - size_t size;  
36 - size_t start;  
37 - size_t end;  
38 - void ** data;  
39 - int free_msgs; 36 + TR_EXTENDS(TR_List);
40 }; 37 };
41 TR_INSTANCE_INIT(TR_Queue); 38 TR_INSTANCE_INIT(TR_Queue);
42 TR_CLASSVARS_DECL(TR_Queue) {}; 39 TR_CLASSVARS_DECL(TR_Queue) {};
43 40
44 -void TR_queuePut(TR_Queue, void *);  
45 -void TR_queuePutFirst(TR_Queue, void *);  
46 -void * TR_queueGet(TR_Queue);  
47 41
48 -#define TR_queueEmpty(this) (this->start == this->end)  
49 -#define TR_queueFirst(this) ((this)->start)  
50 -#define TR_queueLast(this) ((this)->end - 1)  
51 -#define TR_queueSize(this) ((this)->size) 42 +#define TR_queueEmpty(this) (TR_listEmpty(((TR_List)(this))))
  43 +#define TR_queueSize(this) (TR_listSize(((TR_List)(this))))
  44 +#define TR_queuePut(this, data) (TR_listPut(((TR_List)(this)), (data)))
  45 +#define TR_queuePutFirst(this, data) \
  46 + (TR_listPutFirst(((TR_List)(this)), (data)))
  47 +#define TR_queueGet(this) (TR_listGetFirst(((TR_List)(this))))
52 48
53 #endif // __TR_QUEUE_H__ 49 #endif // __TR_QUEUE_H__
54 50
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 #include "tr/cbuf.h" 4 #include "tr/cbuf.h"
5 #include "tr/hash.h" 5 #include "tr/hash.h"
6 #include "tr/hash_value.h" 6 #include "tr/hash_value.h"
  7 +#include "tr/list.h"
7 #include "tr/queue.h" 8 #include "tr/queue.h"
8 #include "tr/tree.h" 9 #include "tr/tree.h"
9 #include "tr/dynarray.h" 10 #include "tr/dynarray.h"
@@ -6,6 +6,7 @@ AM_LDFLAGS += @@ -6,6 +6,7 @@ AM_LDFLAGS +=
6 6
7 TRDATALIBS = cbuf/libcbuf.la \ 7 TRDATALIBS = cbuf/libcbuf.la \
8 hash/libhash.la \ 8 hash/libhash.la \
  9 + list/liblist.la \
9 queue/libqueue.la \ 10 queue/libqueue.la \
10 tree/libtree.la \ 11 tree/libtree.la \
11 dynarray/libdynarray.la \ 12 dynarray/libdynarray.la \
@@ -18,4 +19,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS) @@ -18,4 +19,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS)
18 libtrdata_la_LIBADD = $(TRDATALIBS) 19 libtrdata_la_LIBADD = $(TRDATALIBS)
19 libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS) 20 libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS)
20 21
21 -SUBDIRS = cbuf hash queue tree dynarray heap 22 +SUBDIRS = cbuf hash list queue tree dynarray heap
  1 +ACLOCAL_AMFLAGS = -I m4
  2 +AUTOMAKE_OPTIONS = subdir-objects
  3 +
  4 +AM_CFLAGS += -I../../include/ -std=c99
  5 +AM_LDFLAGS +=
  6 +
  7 +LIST = list.c \
  8 + get.c \
  9 + get_first.c \
  10 + put.c \
  11 + put_first.c
  12 +
  13 +noinst_LTLIBRARIES = liblist.la
  14 +
  15 +liblist_la_SOURCES = $(LIST)
  16 +liblist_la_CFLAGS = $(AM_CFLAGS)
  17 +liblist_la_LIBADD = $(AM_LDFLAGS)
@@ -19,14 +19,15 @@ @@ -19,14 +19,15 @@
19 * You should have received a copy of the GNU General Public License 19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */ 21 */
  22 +#ifndef __TR_LIST_RESIZE_H__
  23 +#define __TR_LIST_RESIZE_H__
22 24
23 #include "trbase.h" 25 #include "trbase.h"
24 -#include "tr/queue.h" 26 +#include "tr/list.h"
25 27
26 -static  
27 inline 28 inline
28 void 29 void
29 -_TR_queueResize(TR_Queue this, size_t split) 30 +_TR_listResize(TR_List this, size_t split)
30 { 31 {
31 #define VPSIZE(elem) ((elem) * sizeof(void*)) 32 #define VPSIZE(elem) ((elem) * sizeof(void*))
32 33
@@ -46,28 +47,6 @@ _TR_queueResize(TR_Queue this, size_t split) @@ -46,28 +47,6 @@ _TR_queueResize(TR_Queue this, size_t split)
46 this->data = new; 47 this->data = new;
47 } 48 }
48 49
49 -void  
50 -TR_queuePutFirst(TR_Queue this, void * msg)  
51 -{  
52 - size_t next = this->start == 0 ? this->size - 1: this->start - 1;  
53 -  
54 - this->data[next] = msg;  
55 - this->start = next;  
56 -  
57 - if (next == this->end) {  
58 - _TR_queueResize(this, this->end);  
59 - }  
60 -}  
61 -  
62 -void  
63 -TR_queuePut(TR_Queue this, void * msg)  
64 -{  
65 - this->data[this->end] = msg;  
66 - this->end = this->end + 1 == this->size ? 0 : this->end + 1;  
67 -  
68 - if (this->end == this->start) {  
69 - _TR_queueResize(this, this->end);  
70 - }  
71 -} 50 +#endif // __TR_LIST_RESIZE_H__
72 51
73 // vim: set ts=4 sw=4: 52 // vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include "trbase.h"
  24 +#include "tr/list.h"
  25 +
  26 +void *
  27 +TR_listGet(TR_List this)
  28 +{
  29 + void * retval;
  30 +
  31 + if (TR_listEmpty(this)) {
  32 + return NULL;
  33 + }
  34 +
  35 + retval = this->data[this->end];
  36 + this->end = this->end == 0 ? this->size - 1 : this->end - 1;
  37 +
  38 + return retval;
  39 +}
  40 +
  41 +// vim: set ts=4 sw=4:
@@ -21,14 +21,14 @@ @@ -21,14 +21,14 @@
21 */ 21 */
22 22
23 #include "trbase.h" 23 #include "trbase.h"
24 -#include "tr/queue.h" 24 +#include "tr/list.h"
25 25
26 void * 26 void *
27 -TR_queueGet(TR_Queue this) 27 +TR_listGetFirst(TR_List this)
28 { 28 {
29 void * retval; 29 void * retval;
30 30
31 - if (TR_queueEmpty(this)) { 31 + if (TR_listEmpty(this)) {
32 return NULL; 32 return NULL;
33 } 33 }
34 34
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "tr/list.h"
  27 +
  28 +static
  29 +int
  30 +listCtor(void * _this, va_list * params)
  31 +{
  32 + TR_List this = _this;
  33 +
  34 + this->data = (void **)TR_malloc(32 * sizeof(void *));
  35 + this->size = TR_getUsableSize(this->data) / sizeof(void *);
  36 + this->start = this->end = 0;
  37 + this->free_msgs = 1;
  38 +
  39 + return 0;
  40 +}
  41 +
  42 +static
  43 +void
  44 +listDtor(void * _this)
  45 +{
  46 + TR_List this = _this;
  47 + size_t i;
  48 +
  49 + if (this->free_msgs) {
  50 + for (
  51 + i = this->start;
  52 + i != this->end;
  53 + i = i + 1 == this->size ? 0 : i + 1) {
  54 + if (this->data[i]) {
  55 + TR_delete(this->data[i]);
  56 + }
  57 + }
  58 + }
  59 +
  60 + TR_MEM_FREE(this->data);
  61 +}
  62 +
  63 +TR_INIT_IFACE(TR_Class, listCtor, listDtor, NULL);
  64 +TR_CREATE_CLASS(TR_List, NULL, NULL, TR_IF(TR_Class));
  65 +
  66 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include "trbase.h"
  24 +#include "tr/list.h"
  25 +
  26 +#include "_resize.h"
  27 +
  28 +extern inline void _TR_listResize(TR_List, size_t);
  29 +
  30 +void
  31 +TR_listPut(TR_List this, void * msg)
  32 +{
  33 + this->data[this->end] = msg;
  34 + this->end = this->end + 1 == this->size ? 0 : this->end + 1;
  35 +
  36 + if (this->end == this->start) {
  37 + _TR_listResize(this, this->end);
  38 + }
  39 +}
  40 +
  41 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include "trbase.h"
  24 +#include "tr/list.h"
  25 +
  26 +#include "_resize.h"
  27 +
  28 +void
  29 +TR_listPutFirst(TR_List this, void * msg)
  30 +{
  31 + size_t next = this->start == 0 ? this->size - 1: this->start - 1;
  32 +
  33 + this->data[next] = msg;
  34 + this->start = next;
  35 +
  36 + if (next == this->end) {
  37 + _TR_listResize(this, this->end);
  38 + }
  39 +}
  40 +
  41 +// vim: set ts=4 sw=4:
@@ -4,9 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects @@ -4,9 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects
4 AM_CFLAGS += -I../../include/ -std=c99 4 AM_CFLAGS += -I../../include/ -std=c99
5 AM_LDFLAGS += 5 AM_LDFLAGS +=
6 6
7 -QUEUE = queue.c \  
8 - get.c \  
9 - put.c 7 +QUEUE = queue.c
10 8
11 noinst_LTLIBRARIES = libqueue.la 9 noinst_LTLIBRARIES = libqueue.la
12 10
@@ -24,18 +24,13 @@ @@ -24,18 +24,13 @@
24 24
25 #include "trbase.h" 25 #include "trbase.h"
26 #include "tr/queue.h" 26 #include "tr/queue.h"
  27 +#include "tr/list.h"
27 28
28 static 29 static
29 int 30 int
30 queueCtor(void * _this, va_list * params) 31 queueCtor(void * _this, va_list * params)
31 { 32 {
32 - TR_Queue this = _this;  
33 -  
34 - this->data = (void **)TR_malloc(32 * sizeof(void *));  
35 - this->size = TR_getUsableSize(this->data) / sizeof(void *);  
36 - this->start = this->end = 0;  
37 - this->free_msgs = 1;  
38 - 33 + TR_PARENTCALL(TR_Queue, _this, TR_Class, ctor, params);
39 return 0; 34 return 0;
40 } 35 }
41 36
@@ -43,24 +38,10 @@ static @@ -43,24 +38,10 @@ static
43 void 38 void
44 queueDtor(void * _this) 39 queueDtor(void * _this)
45 { 40 {
46 - TR_Queue this = _this;  
47 - size_t i;  
48 -  
49 - if (this->free_msgs) {  
50 - for (  
51 - i = this->start;  
52 - i != this->end;  
53 - i = i + 1 == this->size ? 0 : i + 1) {  
54 - if (this->data[i]) {  
55 - TR_delete(this->data[i]);  
56 - }  
57 - }  
58 - }  
59 -  
60 - TR_MEM_FREE(this->data); 41 + TR_PARENTCALL(TR_Queue, _this, TR_Class, dtor);
61 } 42 }
62 43
63 TR_INIT_IFACE(TR_Class, queueCtor, queueDtor, NULL); 44 TR_INIT_IFACE(TR_Class, queueCtor, queueDtor, NULL);
64 -TR_CREATE_CLASS(TR_Queue, NULL, NULL, TR_IF(TR_Class)); 45 +TR_CREATE_CLASS(TR_Queue, TR_List, NULL, TR_IF(TR_Class));
65 46
66 // vim: set ts=4 sw=4: 47 // vim: set ts=4 sw=4:
Please register or login to post a comment