dovecot: Renamed queue to aqueue ("array queue") because struct ...

dovecot at dovecot.org dovecot at dovecot.org
Tue Jan 1 18:48:13 EET 2008


details:   http://hg.dovecot.org/dovecot/rev/d45c3058b91a
changeset: 7079:d45c3058b91a
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Jan 01 18:48:09 2008 +0200
description:
Renamed queue to aqueue ("array queue") because struct queue already exists
in Solaris.

diffstat:

8 files changed, 217 insertions(+), 215 deletions(-)
src/auth/db-ldap.c   |   38 +++++++--------
src/auth/db-ldap.h   |    2 
src/lib/Makefile.am  |    4 -
src/lib/aqueue.c     |  124 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/lib/aqueue.h     |   41 ++++++++++++++++
src/lib/queue.c      |  123 -------------------------------------------------
src/lib/queue.h      |   40 ----------------
src/tests/test-lib.c |   60 ++++++++++++------------

diffs (truncated from 666 to 300 lines):

diff -r 4e3c7037c2ca -r d45c3058b91a src/auth/db-ldap.c
--- a/src/auth/db-ldap.c	Sun Dec 30 17:08:29 2007 +0200
+++ b/src/auth/db-ldap.c	Tue Jan 01 18:48:09 2008 +0200
@@ -8,7 +8,7 @@
 #include "ioloop.h"
 #include "array.h"
 #include "hash.h"
-#include "queue.h"
+#include "aqueue.h"
 #include "str.h"
 #include "var-expand.h"
 #include "settings.h"
@@ -273,7 +273,7 @@ static bool db_ldap_request_queue_next(s
 static bool db_ldap_request_queue_next(struct ldap_connection *conn)
 {
 	struct ldap_request *const *requestp, *request;
-	unsigned int queue_size = queue_count(conn->request_queue);
+	unsigned int queue_size = aqueue_count(conn->request_queue);
 	int ret = -1;
 
 	if (conn->pending_count == queue_size) {
@@ -289,8 +289,8 @@ static bool db_ldap_request_queue_next(s
 		return FALSE;
 
 	requestp = array_idx(&conn->request_array,
-			     queue_idx(conn->request_queue,
-				       conn->pending_count));
+			     aqueue_idx(conn->request_queue,
+					conn->pending_count));
 	request = *requestp;
 
 	if (conn->pending_count > 0 &&
@@ -336,7 +336,7 @@ static bool db_ldap_request_queue_next(s
 		return FALSE;
 	} else {
 		/* broken request, remove from queue */
-		queue_delete_tail(conn->request_queue);
+		aqueue_delete_tail(conn->request_queue);
 		request->callback(conn, request, NULL);
 		return TRUE;
 	}
@@ -349,7 +349,7 @@ void db_ldap_request(struct ldap_connect
 	request->create_time = ioloop_time;
 
 	if (conn->request_queue->full &&
-	    queue_count(conn->request_queue) >= DB_LDAP_MAX_QUEUE_SIZE) {
+	    aqueue_count(conn->request_queue) >= DB_LDAP_MAX_QUEUE_SIZE) {
 		/* Queue is full already, fail this request */
 		auth_request_log_error(request->auth_request, "ldap",
 				       "Request queue is full");
@@ -357,7 +357,7 @@ void db_ldap_request(struct ldap_connect
 		return;
 	}
 
-	queue_append(conn->request_queue, &request);
+	aqueue_append(conn->request_queue, &request);
 	(void)db_ldap_request_queue_next(conn);
 }
 
@@ -405,9 +405,9 @@ static void db_ldap_abort_requests(struc
 	struct ldap_request *const *requestp, *request;
 	time_t diff;
 
-	while (queue_count(conn->request_queue) > 0 && max_count > 0) {
+	while (aqueue_count(conn->request_queue) > 0 && max_count > 0) {
 		requestp = array_idx(&conn->request_array,
-				     queue_idx(conn->request_queue, 0));
+				     aqueue_idx(conn->request_queue, 0));
 		request = *requestp;
 
 		diff = ioloop_time - request->create_time;
@@ -415,7 +415,7 @@ static void db_ldap_abort_requests(struc
 			break;
 
 		/* timed out, abort */
-		queue_delete_tail(conn->request_queue);
+		aqueue_delete_tail(conn->request_queue);
 
 		if (request->msgid != -1) {
 			i_assert(conn->pending_count > 0);
@@ -446,10 +446,10 @@ db_ldap_handle_result(struct ldap_connec
 		return;
 	}
 
-	count = queue_count(conn->request_queue);
+	count = aqueue_count(conn->request_queue);
 	requests = count == 0 ? NULL : array_idx(&conn->request_array, 0);
 	for (i = 0; i < count; i++) {
-		request = requests[queue_idx(conn->request_queue, i)];
+		request = requests[aqueue_idx(conn->request_queue, i)];
 		if (request->msgid == msgid)
 			break;
 		if (request->msgid == -1) {
@@ -469,7 +469,7 @@ db_ldap_handle_result(struct ldap_connec
 	}
 	i_assert(conn->pending_count > 0);
 	conn->pending_count--;
-	queue_delete(conn->request_queue, i);
+	aqueue_delete(conn->request_queue, i);
 
 	ret = ldap_result2error(conn->ld, res, 0);
 	if (ret != LDAP_SUCCESS && request->type == LDAP_REQUEST_TYPE_SEARCH) {
@@ -529,7 +529,7 @@ static void ldap_input(struct ldap_conne
 	} else if (ldap_get_errno(conn) != LDAP_SERVER_DOWN) {
 		i_error("LDAP: ldap_result() failed: %s", ldap_get_error(conn));
 		ldap_conn_reconnect(conn);
-	} else if (queue_count(conn->request_queue) > 0 ||
+	} else if (aqueue_count(conn->request_queue) > 0 ||
 		   ioloop_time - conn->last_reply_stamp <
 		   				DB_LDAP_IDLE_RECONNECT_SECS) {
 		i_error("LDAP: Connection lost to LDAP server, reconnecting");
@@ -718,7 +718,7 @@ static void db_ldap_disconnect_timeout(s
 			       DB_LDAP_REQUEST_DISCONNECT_TIMEOUT_SECS,
 			       FALSE, "LDAP server not connected");
 
-	if (queue_count(conn->request_queue) == 0) {
+	if (aqueue_count(conn->request_queue) == 0) {
 		/* no requests left, remove this timeout handler */
 		timeout_remove(&conn->to);
 	}
@@ -735,7 +735,7 @@ static void db_ldap_conn_close(struct ld
 	if (conn->pending_count != 0) {
 		requests = array_idx(&conn->request_array, 0);
 		for (i = 0; i < conn->pending_count; i++) {
-			request = requests[queue_idx(conn->request_queue, i)];
+			request = requests[aqueue_idx(conn->request_queue, i)];
 
 			i_assert(request->msgid != -1);
 			request->msgid = -1;
@@ -752,7 +752,7 @@ static void db_ldap_conn_close(struct ld
 	}
 	conn->fd = -1;
 
-	if (queue_count(conn->request_queue) == 0) {
+	if (aqueue_count(conn->request_queue) == 0) {
 		if (conn->to != NULL)
 			timeout_remove(&conn->to);
 	} else if (conn->to == NULL) {
@@ -1084,7 +1084,7 @@ struct ldap_connection *db_ldap_init(con
 	conn->set.ldap_scope = scope2str(conn->set.scope);
 
 	i_array_init(&conn->request_array, DB_LDAP_MAX_QUEUE_SIZE);
-	conn->request_queue = queue_init(&conn->request_array.arr);
+	conn->request_queue = aqueue_init(&conn->request_array.arr);
 
 	conn->next = ldap_connections;
         ldap_connections = conn;
@@ -1114,7 +1114,7 @@ void db_ldap_unref(struct ldap_connectio
 	i_assert(conn->to == NULL);
 
 	array_free(&conn->request_array);
-	queue_deinit(&conn->request_queue);
+	aqueue_deinit(&conn->request_queue);
 
 	if (conn->pass_attr_map != NULL)
 		hash_destroy(&conn->pass_attr_map);
diff -r 4e3c7037c2ca -r d45c3058b91a src/auth/db-ldap.h
--- a/src/auth/db-ldap.h	Sun Dec 30 17:08:29 2007 +0200
+++ b/src/auth/db-ldap.h	Tue Jan 01 18:48:09 2008 +0200
@@ -121,7 +121,7 @@ struct ldap_connection {
 
 	/* Request queue contains sent requests at tail (msgid != -1) and
 	   queued requests at head (msgid == -1). */
-	struct queue *request_queue;
+	struct aqueue *request_queue;
 	ARRAY_DEFINE(request_array, struct ldap_request *);
 	/* Number of messages in queue with msgid != -1 */
 	unsigned int pending_count;
diff -r 4e3c7037c2ca -r d45c3058b91a src/lib/Makefile.am
--- a/src/lib/Makefile.am	Sun Dec 30 17:08:29 2007 +0200
+++ b/src/lib/Makefile.am	Tue Jan 01 18:48:09 2008 +0200
@@ -9,6 +9,7 @@ EXTRA_DIST = unicodemap.c unicodemap.pl
 	perl $(srcdir)/unicodemap.pl < UnicodeData.txt > $@
 
 liblib_a_SOURCES = \
+	aqueue.c \
 	backtrace-string.c \
 	base64.c \
 	bsearch-insert-pos.c \
@@ -77,7 +78,6 @@ liblib_a_SOURCES = \
 	primes.c \
 	printf-format-fix.c \
 	process-title.c \
-	queue.c \
 	randgen.c \
 	read-full.c \
 	restrict-access.c \
@@ -104,6 +104,7 @@ liblib_a_SOURCES = \
 	write-full.c
 
 headers = \
+	aqueue.h \
 	array.h \
 	array-decl.h \
 	backtrace-string.h \
@@ -160,7 +161,6 @@ headers = \
 	primes.h \
 	printf-format-fix.h \
 	process-title.h \
-	queue.h \
 	randgen.h \
 	read-full.h \
 	restrict-access.h \
diff -r 4e3c7037c2ca -r d45c3058b91a src/lib/aqueue.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/aqueue.c	Tue Jan 01 18:48:09 2008 +0200
@@ -0,0 +1,124 @@
+/* Copyright (c) 2003-2007 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "aqueue.h"
+
+struct aqueue *aqueue_init(struct array *array)
+{
+	struct aqueue *aqueue;
+
+	aqueue = i_new(struct aqueue, 1);
+	aqueue->arr = array;
+	aqueue->area_size = buffer_get_size(aqueue->arr->buffer) /
+		aqueue->arr->element_size;
+	i_assert(aqueue->area_size > 0);
+	return aqueue;
+}
+
+void aqueue_deinit(struct aqueue **_aqueue)
+{
+	struct aqueue *aqueue = *_aqueue;
+
+	*_aqueue = NULL;
+	i_free(aqueue);
+}
+
+static void aqueue_grow(struct aqueue *aqueue)
+{
+	unsigned int orig_area_size, count;
+
+	i_assert(aqueue->full && aqueue->head == aqueue->tail);
+
+	orig_area_size = aqueue->area_size;
+	(void)array_append_space_i(aqueue->arr);
+	aqueue->area_size = buffer_get_size(aqueue->arr->buffer) /
+		aqueue->arr->element_size;
+	i_assert(orig_area_size < aqueue->area_size);
+
+	count = I_MIN(aqueue->area_size - orig_area_size, aqueue->head);
+	array_copy(aqueue->arr, orig_area_size, aqueue->arr, 0, count);
+	if (count < aqueue->area_size - orig_area_size)
+		aqueue->head = orig_area_size + count;
+	else {
+		array_copy(aqueue->arr, 0, aqueue->arr, count,
+			   aqueue->head - count);
+		aqueue->head -= count;
+	}
+
+	i_assert(aqueue->head != aqueue->tail);
+	aqueue->full = FALSE;
+}
+
+void aqueue_append(struct aqueue *aqueue, const void *data)
+{
+	if (aqueue->full) {
+		aqueue_grow(aqueue);
+		i_assert(!aqueue->full);
+	}
+
+	array_idx_set_i(aqueue->arr, aqueue->head, data);
+	aqueue->head = (aqueue->head + 1) % aqueue->area_size;
+	aqueue->full = aqueue->head == aqueue->tail;
+}
+
+void aqueue_delete(struct aqueue *aqueue, unsigned int n)
+{
+	unsigned int idx, count = aqueue_count(aqueue);
+
+	i_assert(n < count);
+
+	aqueue->full = FALSE;
+	if (n == 0) {
+		/* optimized deletion from tail */
+		aqueue->tail = (aqueue->tail + 1) % aqueue->area_size;
+		return;
+	}
+	if (n == count-1) {
+		/* optimized deletion from head */
+		aqueue->head = (aqueue->head + aqueue->area_size - 1) %
+			aqueue->area_size;
+		return;
+	}
+
+	idx = aqueue_idx(aqueue, n);
+	if ((n < count/2 || idx > aqueue->head) && idx > aqueue->tail) {
+		/* move tail forward.
+		   ..tail##idx##head.. or ##head..tail##idx## */
+		array_copy(aqueue->arr, aqueue->tail + 1,
+			   aqueue->arr, aqueue->tail,
+			   idx - aqueue->tail);
+		aqueue->tail++;
+		i_assert(aqueue->tail < aqueue->area_size);
+	} else {


More information about the dovecot-cvs mailing list