embedded list v2
Hi,
To recapitulate why I think this sort of embedded list is worthwile:
* minimal memory overhead (16 bytes for double linked list heads/nodes on
64bit systems)
* no additional memory allocation overhead
* no additional dereference to access the contents of a list element
* most modifications are completely branchless
* the current dllist.h interface has double the memory overhead and much more
complex manipulation operators
* Multiple places in postgres have grown local single or double linked list
implementations
* I need it ;)
Attached are three patches:
1. embedded list implementation
2. make the list implementation usable without USE_INLINE
3. convert all callers to ilist.h away from dllist.h
For 1 I:
a. added more comments and some introduction, some more functions
b. moved the file from utils/ilist.h to lib/ilist.h
c. actually included the c file with the check functions
d. did *not* split it up into single/double linked list files, doesn't seem to
be worth the trouble given how small ilist.(c|h) are
e. did *not* try to get an interface similar to dllist.h. I don't think the
old one is better and it makes the breakage more obvious should somebody else
use the old implementation although I doubt it.
I can be convinced to do d. and e. but I don't think they are an improvement.
For 2 I used ugly macro hackery to avoid declaring every function twice, in a
c file and in a header.
Opinions on the state of the above patches?
I did not expect any performance difference in the current usage, but just to
be sure I ran the following tests:
connection heavy:
pgbench -n -S -p 5501 -h /tmp -U andres postgres -c 16 -j 16 -T 10 -C
master: 3109 3024 3012
ilist: 3097 3033 3024
somewhat SearchCatCache heavy:
pgbench -n -S -p 5501 -h /tmp -U andres postgres -T 100 -c 16 -j 1
master: 98979.453879 99554.485631 99393.587880
ilist: 98960.545559 99583.319870 99498.923273
As expected the differences are on the level of noise...
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
0001-Add-embedded-list-interface-header-only.patchtext/x-patch; charset=UTF-8; name=0001-Add-embedded-list-interface-header-only.patchDownload
From 2e9d955fbb625004061509a62ecca83fde68d027 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 6 May 2012 00:26:35 +0200
Subject: [PATCH 1/3] Add embedded list interface (header only)
Adds a single and a double linked list which can easily embedded into other
datastructures and can be used without any additional allocations.
Problematic: It requires USE_INLINE to be used. It could be remade to fallback
to to externally defined functions if that is not available but that hardly
seems sensibly at this day and age. Besides, the speed hit would be noticeable
and its only used in new code which could be disabled on machines - given they
still exists - without proper support for inline functions
3 files changed, 509 insertions(+), 1 deletion(-)
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 2e1061e..c94297a 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS = dllist.o stringinfo.o
+OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/ilist.c b/src/backend/lib/ilist.c
new file mode 100644
index 0000000..72de7a3
--- /dev/null
+++ b/src/backend/lib/ilist.c
@@ -0,0 +1,79 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline double and single linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ *
+ * This function only contains testing code for inline single/double linked
+ * lists.
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "lib/ilist.h"
+
+#ifdef ILIST_DEBUG
+void ilist_d_check(ilist_d_head* head)
+{
+ ilist_d_node *cur;
+
+ if(!head ||
+ !(&head->head))
+ elog(ERROR, "double linked list head is not properly initialized");
+
+ for(cur = head->head.next;
+ cur != &head->head;
+ cur = cur->next){
+ if(!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ {
+ elog(ERROR, "double linked list is corrupted");
+ }
+ }
+
+ for(cur = head->head.prev;
+ cur != &head->head;
+ cur = cur->prev){
+ if(!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ {
+ elog(ERROR, "double linked list is corrupted");
+ }
+ }
+}
+
+void ilist_s_check(ilist_s_head* head)
+{
+ ilist_s_node *cur;
+
+ if(!head ||
+ !(&head->head))
+ elog(ERROR, "single linked list head is not properly initialized");
+
+ /* there isn't much we can test in a single linked list except that its really circular */
+ for(cur = head->head.next;
+ cur != &head->head;
+ cur = cur->next){
+
+ if(!cur){
+ elog(ERROR, "single linked list is corrupted");
+ }
+ }
+}
+#endif
diff --git a/src/include/lib/ilist.h b/src/include/lib/ilist.h
new file mode 100644
index 0000000..cb1de99
--- /dev/null
+++ b/src/include/lib/ilist.h
@@ -0,0 +1,429 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline double and single linked lists without memory
+ * managment overhead
+ *
+ * This is intended to be used in the following manner:
+ *
+ * #include "lib/ilist.h"
+ *
+ * typedef struct something_in_a_list {
+ * int value;
+ * int somethingelse;
+ * ilist_d_head values;
+ * } something_in_a_list;
+ *
+ * typedef struct value_in_a_list {
+ * int data;
+ * int somethingelse;
+ * ilist_d_node list_node;
+ * } value_in_a_list;
+ *
+ * something_in_a_list *somelist = get_blarg();
+ *
+ * ilist_d_push_front(somelist, &get_value_in_a_list()->list_node);
+ * ...
+ * ilist_d_push_front(somelist, &get_value_in_a_list()->list_node);
+ * ...
+ * ...
+ * ilist_d_node *node, *next;
+ *
+ * ilist_d_foreach(node, somelist)
+ * {
+ * value_in_a_list *val = ilist_container(value_in_a_list, list_node, node);
+ * do_something_with_val(val);
+ * }
+ *
+ * ilist_d_foreach_modify(node, next, somelist)
+ * {
+ * ilist_d_remove(node);
+ * }
+ *
+ * This allows somewhat convenient list manipulation with low overhead.
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+#ifndef ILIST_H
+#define ILIST_H
+
+/*
+ * enable for extra debugging. This is rather expensive so its not enabled by
+ * default even with --enable-cassert
+*/
+/* #define ILIST_DEBUG */
+
+/*
+ * gcc warns about unused parameters if -Wunused-parameter is specified (part
+ * of -Wextra ). In the cases below its perfectly fine not to use those
+ * parameters because they are just passed to make the interface consistent and
+ * to allow debugging in case of ILIST_DEBUG.
+ *
+ */
+#ifdef __GNUC__
+#define unused_attr __attribute__((unused))
+#else
+#define unused_attr
+#endif
+
+#ifndef USE_INLINE
+#error "a compiler supporting static inlines is required"
+#endif
+
+#include <assert.h>
+
+/*
+ * struct to embed in other structs that need to be part of a double linked
+ * list.
+ */
+typedef struct ilist_d_node ilist_d_node;
+struct ilist_d_node
+{
+ ilist_d_node* prev;
+ ilist_d_node* next;
+};
+
+/*
+ * Head of a double linked list. Lists are internally *always* circularly
+ * linked, even when empty. This allows to do many many manipulations without
+ * branches which is beneficial performancewise.
+ */
+typedef struct
+{
+ ilist_d_node head;
+} ilist_d_head;
+
+/*
+ * struct to embed in other structs that need to be part of a single linked
+ * list.
+ */
+typedef struct ilist_s_node ilist_s_node;
+struct ilist_s_node
+{
+ ilist_s_node* next;
+};
+
+/*
+ * Head of a single linked list.
+ */
+typedef struct
+{
+ ilist_s_node head;
+} ilist_s_head;
+
+
+
+#ifdef ILIST_DEBUG
+
+void ilist_d_check(ilist_d_head* head);
+void ilist_s_check(ilist_s_head* head);
+
+#else
+
+static inline void ilist_d_check(unused_attr ilist_d_head* head)
+{
+}
+
+static inline void ilist_s_check(unused_attr ilist_s_head* head)
+{
+}
+
+#endif /* ILIST_DEBUG */
+
+/*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+static inline void ilist_d_init(ilist_d_head *head)
+{
+ head->head.next = head->head.prev = &head->head;
+ ilist_d_check(head);
+}
+
+/*
+ * adds a node at the beginning of the list
+ */
+static inline void ilist_d_push_front(ilist_d_head *head, ilist_d_node *node)
+{
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * adds a node at the end of the list
+ */
+static inline void ilist_d_push_back(ilist_d_head *head, ilist_d_node *node)
+{
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * adds a node after another *in the same list*
+ */
+static inline void ilist_d_add_after(unused_attr ilist_d_head *head, ilist_d_node *after, ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+ ilist_d_check(head);
+}
+
+/*
+ * adds a node after another *in the same list*
+ */
+static inline void ilist_d_add_before(unused_attr ilist_d_head *head, ilist_d_node *before, ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * removes a node from a list
+ */
+static inline void ilist_d_remove(unused_attr ilist_d_head *head, ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+ ilist_d_check(head);
+}
+
+/*
+ * removes the first node from a list or returns NULL
+ */
+static inline ilist_d_node* ilist_d_pop_front(ilist_d_head *head)
+{
+ ilist_d_node* ret;
+
+ if (&head->head == head->head.next)
+ return NULL;
+
+ ret = head->head.next;
+ ilist_d_remove(head, head->head.next);
+ return ret;
+}
+
+/*
+ * moves an element that has to be in the list to the front of the list
+ */
+static inline void ilist_d_move_front(ilist_d_head *head, ilist_d_node *node)
+{
+ /* fast path if its already at the front */
+ if(&head->head == node)
+ return;
+ ilist_d_remove(head, node);
+ ilist_d_push_front(head, node);
+ ilist_d_check(head);
+}
+
+/*
+ * Check whether the passed node is the last element in the list
+ */
+static inline bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node)
+{
+ return node->next != &head->head;
+}
+
+/*
+ * Check whether the passed node is the first element in the list
+ */
+static inline bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node)
+{
+ return node->prev != &head->head;
+}
+
+/*
+ * Check whether the list is empty.
+ */
+static inline bool ilist_d_is_empty(ilist_d_head *head)
+{
+ return head->head.next == head->head.prev;
+}
+
+/*
+ * Return the value of first element in the list if there is one, return NULL
+ * otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define ilist_d_front(type, membername, ptr) (&((ptr)->head) == (ptr)->head.next) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.next)
+
+/*
+ * Return the value of the first element. This will corrupt data if there is no
+ * element in the list.
+ */
+#define ilist_d_front_unchecked(type, membername, ptr) ilist_container(type, membername, (ptr)->head.next)
+
+/*
+ * Return the value of the last element in the list if ther eis one, return
+ * NULL otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define ilist_d_back(type, membername, ptr) (&((ptr)->head) == (ptr)->head.prev) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.prev)
+
+/*
+ * Return a pointer to the struct `type` when the passed `ptr` points to the
+ * element `membername`.
+ */
+#define ilist_container(type, membername, ptr) ((type*)((char*)(ptr) - offsetof(type, membername)))
+
+/*
+ * Iterate through the list storing the current element in the variable
+ * `name`. `ptr` will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define ilist_d_foreach(name, ptr) for(name = (ptr)->head.next; \
+ name != &(ptr)->head; \
+ name = name->next)
+
+
+/*
+ * Iterate through the list storing the current element in the variable `name`
+ * and also storing the next list element in the variable `nxt`.
+ *
+ * It is allowed to remove the current element from the list. Every other
+ * manipulation can lead to curruption.
+ */
+#define ilist_d_foreach_modify(name, nxt, ptr) for(name = (ptr)->head.next, \
+ nxt = name->next; \
+ name != &(ptr)->head \
+ ; \
+ name = nxt, nxt = name->next)
+
+/*
+ * Iterate through the list in reverse order storing the current element in the
+ * variable `name`. `ptr` will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define ilist_d_reverse_foreach(name, ptr) for(name = (ptr)->head.prev; \
+ name != &(ptr)->head; \
+ name = name->prev)
+
+
+/*
+ * Initialize a single linked list element.
+ */
+static inline void ilist_s_init(ilist_s_head *head)
+{
+ head->head.next = NULL;
+ ilist_s_check(head);
+}
+
+static inline void ilist_s_push_front(ilist_s_head *head, ilist_s_node *node)
+{
+ node->next = head->head.next;
+ head->head.next = node;
+ ilist_s_check(head);
+}
+
+/*
+ * fails if the list is empty
+ */
+static inline ilist_s_node* ilist_s_pop_front(ilist_s_head *head)
+{
+ ilist_s_node* front = head->head.next;
+ head->head.next = head->head.next->next;
+ ilist_s_check(head);
+ return front;
+}
+
+/*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ *
+ * XXX: This function possibly should be out-of-line?
+ */
+static inline void ilist_s_remove(ilist_s_head *head,
+ ilist_s_node *node)
+{
+ ilist_s_node *last = &head->head;
+ ilist_s_node *cur;
+#ifdef ILIST_DEBUG
+ bool found = false;
+#endif
+ while ((cur = last->next))
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+#ifdef ILIST_DEBUG
+ found = true;
+#endif
+ break;
+ }
+ last = cur;
+ }
+ ilist_s_check(head);
+
+#ifdef ILIST_DEBUG
+ if(!found)
+ elog(ERROR, "tried to remove nonexisting node");
+ assert(found);
+#endif
+}
+
+
+static inline void ilist_s_add_after(unused_attr ilist_s_head *head,
+ ilist_s_node *after, ilist_s_node *node)
+{
+ node->next = after->next;
+ after->next = node;
+ ilist_s_check(head);
+}
+
+
+static inline bool ilist_s_is_empty(ilist_s_head *head)
+{
+ return head->head.next == NULL;
+}
+
+static inline bool ilist_s_has_next(unused_attr ilist_s_head* head,
+ ilist_s_node *node)
+{
+ return node->next != NULL;
+}
+
+
+#define ilist_s_front(type, membername, ptr) (ilist_s_is_empty(ptr) ? \
+ ilist_container(type, membername, (ptr).next) : NULL
+
+#define ilist_s_front_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+#define ilist_s_foreach(name, ptr) for(name = (ptr)->head.next; \
+ name != NULL; \
+ name = name->next)
+
+#define ilist_s_foreach_modify(name, nxt, ptr) for(name = (ptr)->head.next, \
+ nxt = name ? name->next : NULL; \
+ name != NULL; \
+ name = nxt, nxt = name ? name->next : NULL)
+
+
+#endif /* ILIST_H */
--
1.7.10.rc3.3.g19a6c.dirty
0002-Rework-the-newly-added-ilist-interface-to-be-usable-.patchtext/x-patch; charset=UTF-8; name=0002-Rework-the-newly-added-ilist-interface-to-be-usable-.patchDownload
From 0c387b8ee04811f10eb638a40c0ca5befd3059ef Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 26 Jun 2012 20:34:02 +0200
Subject: [PATCH 2/3] Rework the newly added ilist interface to be usable on
systems without USE_INLINE
I find doing such magick is ugly but...
2 files changed, 72 insertions(+), 39 deletions(-)
diff --git a/src/backend/lib/ilist.c b/src/backend/lib/ilist.c
index 72de7a3..d1dccdb 100644
--- a/src/backend/lib/ilist.c
+++ b/src/backend/lib/ilist.c
@@ -20,7 +20,15 @@
#include "postgres.h"
+/*
+ * If inlines aren't available include the function as defined in the header,
+ * but without 'static inline' defined. That way we do not have to duplicate
+ * their functionality.
+ */
+#ifndef USE_INLINE
+#define USE_DECLARATION
#include "lib/ilist.h"
+#endif
#ifdef ILIST_DEBUG
void ilist_d_check(ilist_d_head* head)
@@ -76,4 +84,5 @@ void ilist_s_check(ilist_s_head* head)
}
}
}
-#endif
+
+#endif /* ILIST_DEBUG */
diff --git a/src/include/lib/ilist.h b/src/include/lib/ilist.h
index cb1de99..91419a4 100644
--- a/src/include/lib/ilist.h
+++ b/src/include/lib/ilist.h
@@ -56,7 +56,6 @@
* default even with --enable-cassert
*/
/* #define ILIST_DEBUG */
-
/*
* gcc warns about unused parameters if -Wunused-parameter is specified (part
* of -Wextra ). In the cases below its perfectly fine not to use those
@@ -70,12 +69,6 @@
#define unused_attr
#endif
-#ifndef USE_INLINE
-#error "a compiler supporting static inlines is required"
-#endif
-
-#include <assert.h>
-
/*
* struct to embed in other structs that need to be part of a double linked
* list.
@@ -116,29 +109,56 @@ typedef struct
} ilist_s_head;
+#ifdef USE_INLINE
+#define INLINE_IF_POSSIBLE static inline
+#define USE_DECLARATION
+#else
+#define INLINE_IF_POSSIBLE
+#endif
-#ifdef ILIST_DEBUG
+#ifndef USE_INLINE
+extern void ilist_d_check(unused_attr ilist_d_head* head);
+extern void ilist_d_init(ilist_d_head *head);
+extern void ilist_d_push_front(ilist_d_head *head, ilist_d_node *node);
+extern void ilist_d_push_back(ilist_d_head *head, ilist_d_node *node);
+extern void ilist_d_add_after(unused_attr ilist_d_head *head, ilist_d_node *after, ilist_d_node *node);
+extern void ilist_d_add_before(unused_attr ilist_d_head *head, ilist_d_node *before, ilist_d_node *node);
+extern void ilist_d_remove(unused_attr ilist_d_head *head, ilist_d_node *node);
+extern ilist_d_node* ilist_d_pop_front(ilist_d_head *head);
+extern void ilist_d_move_front(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_is_empty(ilist_d_head *head);
+extern void ilist_d_check(ilist_d_head* head);
+
+extern void ilist_s_init(ilist_s_head *head);
+extern void ilist_s_push_front(ilist_s_head *head, ilist_s_node *node);
+extern ilist_s_node* ilist_s_pop_front(ilist_s_head *head);
+extern void ilist_s_remove(ilist_s_head *head, ilist_s_node *node);
+extern void ilist_s_add_after(unused_attr ilist_s_head *head,
+ ilist_s_node *after, ilist_s_node *node);
+extern bool ilist_s_is_empty(ilist_s_head *head);
+extern bool ilist_s_has_next(unused_attr ilist_s_head* head,
+ ilist_s_node *node);
+
+#endif /* USE_INLINE */
-void ilist_d_check(ilist_d_head* head);
-void ilist_s_check(ilist_s_head* head);
+#ifdef ILIST_DEBUG
+extern void ilist_d_check(ilist_d_head* head);
+extern void ilist_s_check(ilist_s_head* head);
#else
+#define ilist_d_check(head)
+#define ilist_s_check(head)
+#endif /*ILIST_DEBUG*/
-static inline void ilist_d_check(unused_attr ilist_d_head* head)
-{
-}
-
-static inline void ilist_s_check(unused_attr ilist_s_head* head)
-{
-}
-
-#endif /* ILIST_DEBUG */
+#ifdef USE_DECLARATION
/*
* Initialize the head of a list. Previous state will be thrown away without
* any cleanup.
*/
-static inline void ilist_d_init(ilist_d_head *head)
+INLINE_IF_POSSIBLE void ilist_d_init(ilist_d_head *head)
{
head->head.next = head->head.prev = &head->head;
ilist_d_check(head);
@@ -147,7 +167,7 @@ static inline void ilist_d_init(ilist_d_head *head)
/*
* adds a node at the beginning of the list
*/
-static inline void ilist_d_push_front(ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_push_front(ilist_d_head *head, ilist_d_node *node)
{
node->next = head->head.next;
node->prev = &head->head;
@@ -160,7 +180,7 @@ static inline void ilist_d_push_front(ilist_d_head *head, ilist_d_node *node)
/*
* adds a node at the end of the list
*/
-static inline void ilist_d_push_back(ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_push_back(ilist_d_head *head, ilist_d_node *node)
{
node->next = &head->head;
node->prev = head->head.prev;
@@ -173,7 +193,7 @@ static inline void ilist_d_push_back(ilist_d_head *head, ilist_d_node *node)
/*
* adds a node after another *in the same list*
*/
-static inline void ilist_d_add_after(unused_attr ilist_d_head *head, ilist_d_node *after, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_add_after(unused_attr ilist_d_head *head, ilist_d_node *after, ilist_d_node *node)
{
ilist_d_check(head);
node->prev = after;
@@ -186,7 +206,7 @@ static inline void ilist_d_add_after(unused_attr ilist_d_head *head, ilist_d_nod
/*
* adds a node after another *in the same list*
*/
-static inline void ilist_d_add_before(unused_attr ilist_d_head *head, ilist_d_node *before, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_add_before(unused_attr ilist_d_head *head, ilist_d_node *before, ilist_d_node *node)
{
ilist_d_check(head);
node->prev = before->prev;
@@ -200,7 +220,7 @@ static inline void ilist_d_add_before(unused_attr ilist_d_head *head, ilist_d_no
/*
* removes a node from a list
*/
-static inline void ilist_d_remove(unused_attr ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_remove(unused_attr ilist_d_head *head, ilist_d_node *node)
{
ilist_d_check(head);
node->prev->next = node->next;
@@ -211,7 +231,7 @@ static inline void ilist_d_remove(unused_attr ilist_d_head *head, ilist_d_node *
/*
* removes the first node from a list or returns NULL
*/
-static inline ilist_d_node* ilist_d_pop_front(ilist_d_head *head)
+INLINE_IF_POSSIBLE ilist_d_node* ilist_d_pop_front(ilist_d_head *head)
{
ilist_d_node* ret;
@@ -226,7 +246,7 @@ static inline ilist_d_node* ilist_d_pop_front(ilist_d_head *head)
/*
* moves an element that has to be in the list to the front of the list
*/
-static inline void ilist_d_move_front(ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE void ilist_d_move_front(ilist_d_head *head, ilist_d_node *node)
{
/* fast path if its already at the front */
if(&head->head == node)
@@ -239,7 +259,7 @@ static inline void ilist_d_move_front(ilist_d_head *head, ilist_d_node *node)
/*
* Check whether the passed node is the last element in the list
*/
-static inline bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node)
{
return node->next != &head->head;
}
@@ -247,7 +267,7 @@ static inline bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node)
/*
* Check whether the passed node is the first element in the list
*/
-static inline bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node)
+INLINE_IF_POSSIBLE bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node)
{
return node->prev != &head->head;
}
@@ -255,11 +275,13 @@ static inline bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node)
/*
* Check whether the list is empty.
*/
-static inline bool ilist_d_is_empty(ilist_d_head *head)
+INLINE_IF_POSSIBLE bool ilist_d_is_empty(ilist_d_head *head)
{
return head->head.next == head->head.prev;
}
+#endif /*USE_DECLARATION*/
+
/*
* Return the value of first element in the list if there is one, return NULL
* otherwise.
@@ -325,16 +347,18 @@ static inline bool ilist_d_is_empty(ilist_d_head *head)
name = name->prev)
+#ifdef USE_DECLARATION
+
/*
* Initialize a single linked list element.
*/
-static inline void ilist_s_init(ilist_s_head *head)
+INLINE_IF_POSSIBLE void ilist_s_init(ilist_s_head *head)
{
head->head.next = NULL;
ilist_s_check(head);
}
-static inline void ilist_s_push_front(ilist_s_head *head, ilist_s_node *node)
+INLINE_IF_POSSIBLE void ilist_s_push_front(ilist_s_head *head, ilist_s_node *node)
{
node->next = head->head.next;
head->head.next = node;
@@ -344,7 +368,7 @@ static inline void ilist_s_push_front(ilist_s_head *head, ilist_s_node *node)
/*
* fails if the list is empty
*/
-static inline ilist_s_node* ilist_s_pop_front(ilist_s_head *head)
+INLINE_IF_POSSIBLE ilist_s_node* ilist_s_pop_front(ilist_s_head *head)
{
ilist_s_node* front = head->head.next;
head->head.next = head->head.next->next;
@@ -359,7 +383,7 @@ static inline ilist_s_node* ilist_s_pop_front(ilist_s_head *head)
*
* XXX: This function possibly should be out-of-line?
*/
-static inline void ilist_s_remove(ilist_s_head *head,
+INLINE_IF_POSSIBLE void ilist_s_remove(ilist_s_head *head,
ilist_s_node *node)
{
ilist_s_node *last = &head->head;
@@ -384,12 +408,11 @@ static inline void ilist_s_remove(ilist_s_head *head,
#ifdef ILIST_DEBUG
if(!found)
elog(ERROR, "tried to remove nonexisting node");
- assert(found);
#endif
}
-static inline void ilist_s_add_after(unused_attr ilist_s_head *head,
+INLINE_IF_POSSIBLE void ilist_s_add_after(unused_attr ilist_s_head *head,
ilist_s_node *after, ilist_s_node *node)
{
node->next = after->next;
@@ -398,17 +421,18 @@ static inline void ilist_s_add_after(unused_attr ilist_s_head *head,
}
-static inline bool ilist_s_is_empty(ilist_s_head *head)
+INLINE_IF_POSSIBLE bool ilist_s_is_empty(ilist_s_head *head)
{
return head->head.next == NULL;
}
-static inline bool ilist_s_has_next(unused_attr ilist_s_head* head,
+INLINE_IF_POSSIBLE bool ilist_s_has_next(unused_attr ilist_s_head* head,
ilist_s_node *node)
{
return node->next != NULL;
}
+#endif /* USE_DECLARATION */
#define ilist_s_front(type, membername, ptr) (ilist_s_is_empty(ptr) ? \
ilist_container(type, membername, (ptr).next) : NULL
--
1.7.10.rc3.3.g19a6c.dirty
0003-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchtext/x-patch; charset=UTF-8; name=0003-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchDownload
From 6123767950ea72ac11bcfd8736b4518fbe0c983b Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 26 Jun 2012 19:53:24 +0200
Subject: [PATCH 3/3] Remove usage of lib/dllist.h and replace it by the new
lib/ilist.h interface
4 files changed, 125 insertions(+), 140 deletions(-)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index dade5cc..ace27b3 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -76,6 +76,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -149,6 +150,7 @@ typedef struct avl_dbase
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ ilist_d_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
@@ -256,7 +258,7 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
/* the database list in the launcher, and the context that contains it */
-static Dllist *DatabaseList = NULL;
+static ilist_d_head DatabaseList;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
@@ -403,6 +405,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* Identify myself via ps */
init_ps_display("autovacuum launcher process", "", "", "");
+ /* initialize to be empty */
+ ilist_d_init(&DatabaseList);
+
ereport(LOG,
(errmsg("autovacuum launcher started")));
@@ -505,7 +510,7 @@ AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
- DatabaseList = NULL;
+ ilist_d_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
@@ -573,7 +578,7 @@ AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
- Dlelem *elem;
+ avl_dbase *avdb;
int rc;
/*
@@ -735,11 +740,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
- elem = DLGetTail(DatabaseList);
- if (elem != NULL)
+ avdb = ilist_d_front(avl_dbase, adl_node, &DatabaseList);
+ if (avdb != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
-
/*
* launch a worker if next_worker is right now or it is in the
* past
@@ -780,7 +783,7 @@ AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
- Dlelem *elem;
+ avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
@@ -793,9 +796,8 @@ launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
- else if ((elem = DLGetTail(DatabaseList)) != NULL)
+ else if ((avdb = ilist_d_back(avl_dbase, adl_node, &DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
@@ -864,6 +866,7 @@ rebuild_database_list(Oid newdb)
int score;
int nelems;
HTAB *dbhash;
+ ilist_d_node *elem;
/* use fresh stats */
autovac_refresh_stats();
@@ -924,36 +927,28 @@ rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
- if (DatabaseList != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- Dlelem *elem;
-
- elem = DLGetHead(DatabaseList);
- while (elem != NULL)
- {
- avl_dbase *avdb = DLE_VAL(elem);
- avl_dbase *db;
- bool found;
- PgStat_StatDBEntry *entry;
-
- elem = DLGetSucc(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
+ avl_dbase *db;
+ bool found;
+ PgStat_StatDBEntry *entry;
- /*
- * skip databases with no stat entries -- in particular, this gets
- * rid of dropped databases
- */
- entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
- if (entry == NULL)
- continue;
+ /*
+ * skip databases with no stat entries -- in particular, this gets
+ * rid of dropped databases
+ */
+ entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
+ if (entry == NULL)
+ continue;
- db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
+ db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
- if (!found)
- {
- /* hash_search already filled in the key */
- db->adl_score = score++;
- /* next_worker is filled in later */
- }
+ if (!found)
+ {
+ /* hash_search already filled in the key */
+ db->adl_score = score++;
+ /* next_worker is filled in later */
}
}
@@ -984,7 +979,7 @@ rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
- DatabaseList = DLNewList();
+ ilist_d_init(&DatabaseList);
if (nelems > 0)
{
@@ -1026,15 +1021,13 @@ rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
- DLAddHead(DatabaseList, elem);
+ ilist_d_push_front(&DatabaseList, &db->adl_node);
}
}
@@ -1144,7 +1137,7 @@ do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
- Dlelem *elem;
+ ilist_d_node *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
@@ -1176,11 +1169,10 @@ do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
- while (elem != NULL)
+ ilist_d_reverse_foreach(elem, &DatabaseList)
{
- avl_dbase *dbp = DLE_VAL(elem);
+ avl_dbase *dbp = ilist_container(avl_dbase, adl_node, elem);
if (dbp->adl_datid == tmp->adw_datid)
{
@@ -1197,7 +1189,6 @@ do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
@@ -1271,7 +1262,7 @@ static void
launch_worker(TimestampTz now)
{
Oid dbid;
- Dlelem *elem;
+ ilist_d_node *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
@@ -1280,10 +1271,9 @@ launch_worker(TimestampTz now)
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
- elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
- while (elem != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- avl_dbase *avdb = DLE_VAL(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
if (avdb->adl_datid == dbid)
{
@@ -1294,10 +1284,9 @@ launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
- DLMoveToFront(elem);
+ ilist_d_move_front(&DatabaseList, elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 913734f..f64e341 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -95,7 +95,7 @@
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
@@ -145,10 +145,10 @@ typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
- Dlelem elem; /* list link in BackendList */
+ ilist_d_node elem; /* list link in BackendList */
} Backend;
-static Dllist *BackendList;
+static ilist_d_head BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
@@ -976,7 +976,7 @@ PostmasterMain(int argc, char *argv[])
/*
* Initialize the list of active backends.
*/
- BackendList = DLNewList();
+ ilist_d_init(&BackendList);
/*
* Initialize pipe (or process handle on Windows) that allows children to
@@ -1797,7 +1797,7 @@ processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
- Dlelem *curr;
+ ilist_d_node *curr;
#else
int i;
#endif
@@ -1811,9 +1811,9 @@ processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
@@ -2591,8 +2591,7 @@ static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
- Dlelem *curr;
-
+ ilist_d_node *curr, *next;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
/*
@@ -2623,9 +2622,9 @@ CleanupBackend(int pid,
return;
}
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->pid == pid)
{
@@ -2644,7 +2643,7 @@ CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_remove(&BackendList, curr);
free(bp);
break;
}
@@ -2661,7 +2660,7 @@ CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
- Dlelem *curr,
+ ilist_d_node *curr,
*next;
Backend *bp;
@@ -2677,10 +2676,10 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
- for (curr = DLGetHead(BackendList); curr; curr = next)
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- next = DLGetSucc(curr);
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
+
if (bp->pid == pid)
{
/*
@@ -2693,7 +2692,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_remove(&BackendList, curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
@@ -3056,7 +3055,7 @@ PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
- if (DLGetHead(BackendList) == NULL &&
+ if (ilist_d_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
@@ -3182,12 +3181,12 @@ signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
bool signaled = false;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -3325,8 +3324,8 @@ BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_front(&BackendList, &bn->elem);
+
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
@@ -4422,12 +4421,12 @@ PostmasterRandom(void)
static int
CountChildren(int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
int cnt = 0;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -4606,8 +4605,7 @@ StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_front(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 0307b96..3b4e87a 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -353,6 +353,8 @@ CatCachePrintStats(int code, Datum arg)
static void
CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
{
+ Index hashIndex;
+
Assert(ct->refcount == 0);
Assert(ct->my_cache == cache);
@@ -369,7 +371,9 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
}
/* delink from linked list */
- DLRemove(&ct->cache_elem);
+ hashIndex = HASH_INDEX(ct->hash_value, cache->cc_nbuckets);
+
+ ilist_d_remove(&cache->cc_bucket[hashIndex], &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
@@ -412,7 +416,7 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
- DLRemove(&cl->cache_elem);
+ ilist_d_remove(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
@@ -452,8 +456,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
+ ilist_d_head *bucket;
if (cacheId != ccp->id)
continue;
@@ -468,11 +473,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -484,12 +487,10 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
-
- for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
+ bucket = &ccp->cc_bucket[hashIndex];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (hashValue == ct->hash_value)
{
@@ -561,13 +562,13 @@ AtEOXact_CatCache(bool isCommit)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
- Dlelem *elt;
+ ilist_d_node *elt;
int i;
/* Check CatCLists */
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
@@ -577,11 +578,10 @@ AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&ccp->cc_bucket[i]);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_head *bucket = &ccp->cc_bucket[i];
+ ilist_d_foreach(elt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
@@ -604,16 +604,14 @@ AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
- for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &cache->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -624,11 +622,10 @@ ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
+ ilist_d_head *bucket = &cache->cc_bucket[i];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
@@ -770,10 +767,8 @@ InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
- cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
+ cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(ilist_d_node));
/*
* initialize the cache's relation information for the relation
@@ -792,6 +787,12 @@ InitCatCache(int id,
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ ilist_d_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++){
+ ilist_d_init(&cp->cc_bucket[i]);
+ }
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
@@ -1060,7 +1061,8 @@ SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
- Dlelem *elt;
+ ilist_d_node *elt;
+ ilist_d_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
@@ -1094,13 +1096,11 @@ SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
- {
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket){
bool res;
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead)
continue; /* ignore dead entries */
@@ -1125,7 +1125,7 @@ SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
- DLMoveToFront(&ct->cache_elem);
+ ilist_d_move_front(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
@@ -1340,7 +1340,7 @@ SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
- Dlelem *elt;
+ ilist_d_node *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
@@ -1382,13 +1382,11 @@ SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
- for (elt = DLGetHead(&cache->cc_lists);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &cache->cc_lists)
{
bool res;
- cl = (CatCList *) DLE_VAL(elt);
+ cl = ilist_container(CatCList, cache_elem, elt);
if (cl->dead)
continue; /* ignore dead entries */
@@ -1416,7 +1414,7 @@ SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
- DLMoveToFront(&cl->cache_elem);
+ ilist_d_move_front(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
@@ -1468,7 +1466,8 @@ SearchCatCacheList(CatCache *cache,
{
uint32 hashValue;
Index hashIndex;
-
+ bool found = false;
+ ilist_d_head *bucket;
/*
* See if there's an entry for this tuple already.
*/
@@ -1476,11 +1475,10 @@ SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket)
{
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
@@ -1498,10 +1496,11 @@ SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
- if (elt == NULL)
+ if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
@@ -1564,7 +1563,6 @@ SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
@@ -1587,7 +1585,7 @@ SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
- DLAddHead(&cache->cc_lists, &cl->cache_elem);
+ ilist_d_push_front(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
@@ -1664,14 +1662,14 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
- DLInitElem(&ct->cache_elem, (void *) ct);
+
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
- DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
+ ilist_d_push_front(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index d91700a..20f2fa8 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -22,7 +22,7 @@
#include "access/htup.h"
#include "access/skey.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "utils/relcache.h"
/*
@@ -51,7 +51,7 @@ typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
- Dllist cc_lists; /* list of CatCList structs */
+ ilist_d_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
@@ -66,7 +66,7 @@ typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
- Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
+ ilist_d_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
@@ -77,11 +77,11 @@ typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
- * Each tuple in a cache is a member of a Dllist that stores the elements
- * of its hash bucket. We keep each Dllist in LRU order to speed repeated
+ * Each tuple in a cache is a member of a ilist_d that stores the elements
+ * of its hash bucket. We keep each ilist_d in LRU order to speed repeated
* lookups.
*/
- Dlelem cache_elem; /* list member of per-bucket list */
+ ilist_d_node cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
@@ -139,7 +139,7 @@ typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
- Dlelem cache_elem; /* list member of per-catcache list */
+ ilist_d_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--
1.7.10.rc3.3.g19a6c.dirty
On Tue, Jun 26, 2012 at 7:26 PM, Andres Freund <andres@2ndquadrant.com> wrote:
Attached are three patches:
1. embedded list implementation
2. make the list implementation usable without USE_INLINE
3. convert all callers to ilist.h away from dllist.h
This code doesn't follow PostgreSQL coding style guidelines; in a
function definition, the name must start on its own line. Also, stuff
like this is grossly unlike what's done elsewhere; use the same
formatting as e.g. foreach:
+#define ilist_d_reverse_foreach(name, ptr) for(name =
(ptr)->head.prev; \
+ name != &(ptr)->head; \
+ name = name->prev)
And this is definitely NOT going to survive pgindent:
+ for(cur = head->head.prev;
+ cur != &head->head;
+ cur = cur->prev){
+ if(!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ {
+ elog(ERROR, "double linked list is corrupted");
+ }
+ }
And this is another meme we don't use elsewhere; add an explicit NULL test:
+ while ((cur = last->next))
And then there's stuff like this:
+ if(!cur){
+ elog(ERROR, "single linked list is corrupted");
+ }
Aside from the formatting issues, I think it would be sensible to
preserve the terminology of talking about the "head" and "tail" of a
list that we use elsewhere, instead of calling them the "front" and
"back" as you've done here. I would suggest that instead of add_after
and add_before you use the names insert_after and insert_before, which
I think is more clear; also instead of remove, I think you should say
delete, for consistency with pg_list.h.
A number of these inlined functions could be rewritten as macros -
e.g. ilist_d_has_next, ilist_d_has_prev, ilist_d_is_empty. Since some
things are written as macros anyway maybe it's good to do all the
one-liners that way, although I guess it doesn't matter that much. I
also agree with your XXX comment that ilist_s_remove should probably
be out of line.
Apart from the above, mostly fairly superficial concerns I think this
makes sense.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
On Thursday, June 28, 2012 06:23:05 PM Robert Haas wrote:
On Tue, Jun 26, 2012 at 7:26 PM, Andres Freund <andres@2ndquadrant.com>
wrote:
Attached are three patches:
1. embedded list implementation
2. make the list implementation usable without USE_INLINE
3. convert all callers to ilist.h away from dllist.hThis code doesn't follow PostgreSQL coding style guidelines; in a
function definition, the name must start on its own line.
Hrmpf. Yes.
Also, stuff like this is grossly unlike what's done elsewhere; use the same formatting as e.g. foreach: +#define ilist_d_reverse_foreach(name, ptr) for(name = (ptr)->head.prev; \ + name != &(ptr)->head; \ + name = name->prev)
Its not only unlike the rest its also ugly... Fixed.
And this is definitely NOT going to survive pgindent:
+ for(cur = head->head.prev; + cur != &head->head; + cur = cur->prev){ + if(!(cur) || + !(cur->next) || + !(cur->prev) || + !(cur->prev->next == cur) || + !(cur->next->prev == cur)) + { + elog(ERROR, "double linked list is corrupted"); + } + }
I changed the for() contents to one line. I don't think I can write anything
that won't be changed by pgindent for the if()s contents.
And this is another meme we don't use elsewhere; add an explicit NULL test:
+ while ((cur = last->next))
Fixed.
And then there's stuff like this:
+ if(!cur){ + elog(ERROR, "single linked list is corrupted"); + }
Fixed the places that I found.
Aside from the formatting issues, I think it would be sensible to
preserve the terminology of talking about the "head" and "tail" of a
list that we use elsewhere, instead of calling them the "front" and
"back" as you've done here. I would suggest that instead of add_after
and add_before you use the names insert_after and insert_before, which
I think is more clear; also instead of remove, I think you should say
delete, for consistency with pg_list.h.
Heh. Ive been poisoned from using c++ too much (thats partially stl names).
Changed.
A number of these inlined functions could be rewritten as macros -
e.g. ilist_d_has_next, ilist_d_has_prev, ilist_d_is_empty. Since some
things are written as macros anyway maybe it's good to do all the
one-liners that way, although I guess it doesn't matter that much.
I find inline functions preferrable because of multiple evaluation stuff. The
macros are macros because of the dynamic return type and other similar issues
which cannot be done in plain C.
I also agree with your XXX comment that ilist_s_remove should probably
be out of line.
Done.
Looks good now?
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
0001-Add-embedded-list-interface.patchtext/x-patch; charset=UTF-8; name=0001-Add-embedded-list-interface.patchDownload
From c3c80925e780489351c4de210364e55d223f02a8 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 6 May 2012 00:26:35 +0200
Subject: [PATCH 1/2] Add embedded list interface
Adds a single and a double linked list which can easily embedded into other
datastructures and can be used without additional memory allocations.
---
src/backend/lib/Makefile | 2 +-
src/backend/lib/ilist.c | 123 ++++++++++++
src/include/lib/ilist.h | 468 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 592 insertions(+), 1 deletion(-)
create mode 100644 src/backend/lib/ilist.c
create mode 100644 src/include/lib/ilist.h
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 2e1061e..c94297a 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS = dllist.o stringinfo.o
+OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/ilist.c b/src/backend/lib/ilist.c
new file mode 100644
index 0000000..f78ac51
--- /dev/null
+++ b/src/backend/lib/ilist.c
@@ -0,0 +1,123 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline double and single linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ *
+ * This function only contains testing code for inline single/double linked
+ * lists.
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+/*
+ * If inlines aren't available include the function as defined in the header,
+ * but without 'static inline' defined. That way we do not have to duplicate
+ * their functionality.
+ */
+#ifndef USE_INLINE
+#define ILIST_USE_DECLARATION
+#endif
+
+#include "lib/ilist.h"
+
+#ifndef USE_INLINE
+#undef ILIST_USE_DECLARATION
+#endif
+
+/*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+void
+ilist_s_delete(ilist_s_head *head, ilist_s_node *node)
+{
+ ilist_s_node *last = &head->head;
+ ilist_s_node *cur;
+#ifdef ILIST_DEBUG
+ bool found = false;
+#endif
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+#ifdef ILIST_DEBUG
+ found = true;
+#endif
+ break;
+ }
+ last = cur;
+ }
+ ilist_s_check(head);
+
+#ifdef ILIST_DEBUG
+ if(!found)
+ elog(ERROR, "tried to delete nonexisting node");
+#endif
+}
+
+#ifdef ILIST_DEBUG
+void ilist_d_check(ilist_d_head* head)
+{
+ ilist_d_node *cur;
+
+ if(!head || !(&head->head))
+ elog(ERROR, "double linked list head is not properly initialized");
+
+ for(cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if(!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ {
+ elog(ERROR, "double linked list is corrupted");
+ }
+ }
+
+ for(cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if(!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ {
+ elog(ERROR, "double linked list is corrupted");
+ }
+ }
+}
+
+void ilist_s_check(ilist_s_head* head)
+{
+ ilist_s_node *cur;
+
+ if(!head ||
+ !(&head->head))
+ elog(ERROR, "single linked list head is not properly initialized");
+
+ /*
+ * there isn't much we can test in a single linked list except that its
+ * really circular
+ */
+ for(cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if(!cur)
+ elog(ERROR, "single linked list is corrupted");
+ }
+}
+
+#endif /* ILIST_DEBUG */
diff --git a/src/include/lib/ilist.h b/src/include/lib/ilist.h
new file mode 100644
index 0000000..ef66d39
--- /dev/null
+++ b/src/include/lib/ilist.h
@@ -0,0 +1,468 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline double and single linked lists without memory
+ * managment overhead
+ *
+ * This is intended to be used in the following manner:
+ *
+ * #include "lib/ilist.h"
+ *
+ * typedef struct something_in_a_list
+ * {
+ * int value;
+ * int somethingelse;
+ * ilist_d_head values;
+ * } something_in_a_list;
+ *
+ * typedef struct value_in_a_list
+ * {
+ * int data;
+ * int somethingelse;
+ * ilist_d_node list_node;
+ * } value_in_a_list;
+ *
+ * something_in_a_list *somelist = get_blarg();
+ *
+ * ilist_d_push_head(somelist, &get_value_in_a_list()->list_node);
+ * ...
+ * ilist_d_push_head(somelist, &get_value_in_a_list()->list_node);
+ * ...
+ * ...
+ * ilist_d_node *node, *next;
+ *
+ * ilist_d_foreach(node, somelist)
+ * {
+ * value_in_a_list *val = ilist_container(value_in_a_list, list_node, node);
+ * do_something_with_val(val);
+ * }
+ *
+ * ilist_d_foreach_modify(node, next, somelist)
+ * {
+ * ilist_d_delete(node);
+ * }
+ *
+ * This allows somewhat convenient list manipulation with low overhead.
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+#ifndef ILIST_H
+#define ILIST_H
+
+/*
+ * enable for extra debugging. This is rather expensive so its not enabled by
+ * default even with --enable-cassert
+*/
+/* #define ILIST_DEBUG */
+
+/*
+ * gcc warns about unused parameters if -Wunused-parameter is specified (part
+ * of -Wextra ). In the cases below its perfectly fine not to use those
+ * parameters because they are just passed to make the interface consistent and
+ * to allow debugging in case of ILIST_DEBUG.
+ *
+ */
+#ifdef __GNUC__
+#define unused_attr __attribute__((unused))
+#else
+#define unused_attr
+#endif
+
+/*
+ * struct to embed in other structs that need to be part of a double linked
+ * list.
+ */
+typedef struct ilist_d_node ilist_d_node;
+struct ilist_d_node
+{
+ ilist_d_node* prev;
+ ilist_d_node* next;
+};
+
+/*
+ * Head of a double linked list. Lists are internally *always* circularly
+ * linked, even when empty. This allows to do many many manipulations without
+ * branches which is beneficial performancewise.
+ */
+typedef struct
+{
+ ilist_d_node head;
+} ilist_d_head;
+
+/*
+ * struct to embed in other structs that need to be part of a single linked
+ * list.
+ */
+typedef struct ilist_s_node ilist_s_node;
+struct ilist_s_node
+{
+ ilist_s_node* next;
+};
+
+/*
+ * Head of a single linked list.
+ */
+typedef struct
+{
+ ilist_s_node head;
+} ilist_s_head;
+
+
+#ifdef USE_INLINE
+#define INLINE_IF_POSSIBLE static inline
+#define ILIST_USE_DECLARATION
+#else
+#define INLINE_IF_POSSIBLE
+#endif
+
+/* Functions we want to be inlined if possible */
+#ifndef USE_INLINE
+extern void ilist_d_check(unused_attr ilist_d_head* head);
+extern void ilist_d_init(ilist_d_head *head);
+extern void ilist_d_push_head(ilist_d_head *head, ilist_d_node *node);
+extern void ilist_d_push_tail(ilist_d_head *head, ilist_d_node *node);
+extern void ilist_d_insert_after(unused_attr ilist_d_head *head,
+ ilist_d_node *after, ilist_d_node *node);
+extern void ilist_d_insert_before(unused_attr ilist_d_head *head,
+ ilist_d_node *before, ilist_d_node *node);
+extern void ilist_d_delete(unused_attr ilist_d_head *head, ilist_d_node *node);
+extern ilist_d_node* ilist_d_pop_head(ilist_d_head *head);
+extern void ilist_d_move_head(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_has_next(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node);
+extern bool ilist_d_is_empty(ilist_d_head *head);
+extern void ilist_d_check(ilist_d_head* head);
+
+extern void ilist_s_init(ilist_s_head *head);
+extern void ilist_s_push_head(ilist_s_head *head, ilist_s_node *node);
+extern ilist_s_node* ilist_s_pop_head(ilist_s_head *head);
+extern void ilist_s_insert_after(unused_attr ilist_s_head *head,
+ ilist_s_node *after, ilist_s_node *node);
+extern bool ilist_s_is_empty(ilist_s_head *head);
+extern bool ilist_s_has_next(unused_attr ilist_s_head* head,
+ ilist_s_node *node);
+
+#endif /* USE_INLINE */
+
+/* Functions which are too big to be inline */
+
+/*
+ * deletes a node from a list
+ *
+ * Attention: O(n)
+ */
+extern void ilist_s_delete(ilist_s_head *head, ilist_s_node *node);
+
+#ifdef ILIST_DEBUG
+extern void ilist_d_check(ilist_d_head* head);
+extern void ilist_s_check(ilist_s_head* head);
+#else
+#define ilist_d_check(head)
+#define ilist_s_check(head)
+#endif /*ILIST_DEBUG*/
+
+
+/*
+ * The following function declarations are only used if inlining is supported
+ * or when included from a file that explicitly declares USE_DECLARATION
+ */
+#ifdef ILIST_USE_DECLARATION
+
+/*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_init(ilist_d_head *head)
+{
+ head->head.next = head->head.prev = &head->head;
+ ilist_d_check(head);
+}
+
+/*
+ * inserts a node at the beginning of the list
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_push_head(ilist_d_head *head, ilist_d_node *node)
+{
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * inserts a node at the end of the list
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_push_tail(ilist_d_head *head, ilist_d_node *node)
+{
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * insert a node after another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_insert_after(unused_attr ilist_d_head *head, ilist_d_node *after,
+ ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+ ilist_d_check(head);
+}
+
+/*
+ * insert a node after another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_insert_before(unused_attr ilist_d_head *head, ilist_d_node *before,
+ ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+ ilist_d_check(head);
+}
+
+
+/*
+ * deletes a node from a list
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_delete(unused_attr ilist_d_head *head, ilist_d_node *node)
+{
+ ilist_d_check(head);
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+ ilist_d_check(head);
+}
+
+/*
+ * deletes the first node from a list or returns NULL
+ */
+INLINE_IF_POSSIBLE ilist_d_node*
+ilist_d_pop_head(ilist_d_head *head)
+{
+ ilist_d_node* ret;
+
+ if (&head->head == head->head.next)
+ return NULL;
+
+ ret = head->head.next;
+ ilist_d_delete(head, head->head.next);
+ return ret;
+}
+
+/*
+ * moves an element that has to be in the list to the head of the list
+ */
+INLINE_IF_POSSIBLE void
+ilist_d_move_head(ilist_d_head *head, ilist_d_node *node)
+{
+ /* fast path if its already at the head */
+ if(&head->head == node)
+ return;
+ ilist_d_delete(head, node);
+ ilist_d_push_head(head, node);
+ ilist_d_check(head);
+}
+
+/*
+ * Check whether the passed node is the last element in the list
+ */
+INLINE_IF_POSSIBLE bool
+ilist_d_has_next(ilist_d_head *head, ilist_d_node *node)
+{
+ return node->next != &head->head;
+}
+
+/*
+ * Check whether the passed node is the first element in the list
+ */
+INLINE_IF_POSSIBLE bool
+ilist_d_has_prev(ilist_d_head *head, ilist_d_node *node)
+{
+ return node->prev != &head->head;
+}
+
+/*
+ * Check whether the list is empty.
+ */
+INLINE_IF_POSSIBLE bool
+ilist_d_is_empty(ilist_d_head *head)
+{
+ return head->head.next == head->head.prev;
+}
+
+#endif /* ILIST_USE_DECLARATION */
+
+/*
+ * Return the value of first element in the list if there is one, return NULL
+ * otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define ilist_d_head(type, membername, ptr) \
+( \
+ (&((ptr)->head) == (ptr)->head.next) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.next) \
+)
+
+/*
+ * Return the value of the first element. This will corrupt data if there is no
+ * element in the list.
+ */
+#define ilist_d_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+/*
+ * Return the value of the last element in the list if ther eis one, return
+ * NULL otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define ilist_d_tail(type, membername, ptr) \
+( \
+ (&((ptr)->head) == (ptr)->head.prev) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.prev) \
+)
+
+/*
+ * Return a pointer to the struct `type` when the passed `ptr` points to the
+ * element `membername`.
+ */
+#define ilist_container(type, membername, ptr) \
+ ((type*)((char*)(ptr) - offsetof(type, membername)))
+
+/*
+ * Iterate through the list storing the current element in the variable
+ * `name`. `ptr` will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define ilist_d_foreach(name, ptr) \
+ for(name = (ptr)->head.next; name != &(ptr)->head; name = name->next)
+
+
+/*
+ * Iterate through the list storing the current element in the variable `name`
+ * and also storing the next list element in the variable `nxt`.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to curruption.
+ */
+#define ilist_d_foreach_modify(name, nxt, ptr) \
+ for(name = (ptr)->head.next, nxt = name->next; name != &(ptr)->head; \
+ name = nxt, nxt = name->next)
+
+/*
+ * Iterate through the list in reverse order storing the current element in the
+ * variable `name`. `ptr` will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define ilist_d_reverse_foreach(name, ptr) \
+ for(name = (ptr)->head.prev; name != &(ptr)->head; name = name->prev)
+
+
+#ifdef ILIST_USE_DECLARATION
+
+/*
+ * Initialize a single linked list element.
+ */
+INLINE_IF_POSSIBLE void
+ilist_s_init(ilist_s_head *head)
+{
+ head->head.next = NULL;
+ ilist_s_check(head);
+}
+
+INLINE_IF_POSSIBLE void
+ilist_s_push_head(ilist_s_head *head, ilist_s_node *node)
+{
+ node->next = head->head.next;
+ head->head.next = node;
+ ilist_s_check(head);
+}
+
+/*
+ * fails if the list is empty
+ */
+INLINE_IF_POSSIBLE ilist_s_node*
+ilist_s_pop_head(ilist_s_head *head)
+{
+ ilist_s_node* node = head->head.next;
+ head->head.next = head->head.next->next;
+ ilist_s_check(head);
+ return node;
+}
+
+INLINE_IF_POSSIBLE void
+ilist_s_insert_after(unused_attr ilist_s_head *head, ilist_s_node *after,
+ ilist_s_node *node)
+{
+ node->next = after->next;
+ after->next = node;
+ ilist_s_check(head);
+}
+
+
+INLINE_IF_POSSIBLE
+bool ilist_s_is_empty(ilist_s_head *head)
+{
+ return head->head.next == NULL;
+}
+
+INLINE_IF_POSSIBLE bool
+ilist_s_has_next(unused_attr ilist_s_head* head,
+ ilist_s_node *node)
+{
+ return node->next != NULL;
+}
+
+#endif /* ILIST_USE_DECLARATION */
+
+#define ilist_s_head(type, membername, ptr) \
+( \
+ ilist_s_is_empty(ptr) ? \
+ ilist_container(type, membername, (ptr).next) \
+ : NULL \
+)
+
+#define ilist_s_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+#define ilist_s_foreach(name, ptr) \
+ for(name = (ptr)->head.next; name != NULL; name = name->next)
+
+#define ilist_s_foreach_modify(name, nxt, ptr) \
+ for(name = (ptr)->head.next, nxt = name ? name->next : NULL; \
+ name != NULL; \
+ name = nxt, nxt = name ? name->next : NULL)
+
+/*
+ * if we defined ILIST_USE_DECLARATION undef it again, its not interesting
+ * outside this file
+ */
+#ifdef USE_INLINE
+#undef ILIST_USE_DECLARATION
+#endif
+
+#endif /* ILIST_H */
--
1.7.10.rc3.3.g19a6c.dirty
0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchtext/x-patch; charset=UTF-8; name=0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchDownload
From 5d2e23c6dc346b9f277869f4e4f1e048faaa574d Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 26 Jun 2012 19:53:24 +0200
Subject: [PATCH 2/2] Remove usage of lib/dllist.h and replace it by the new
lib/ilist.h interface
---
src/backend/postmaster/autovacuum.c | 91 +++++++++++++-----------------
src/backend/postmaster/postmaster.c | 54 +++++++++---------
src/backend/utils/cache/catcache.c | 106 +++++++++++++++++------------------
src/include/utils/catcache.h | 14 ++---
4 files changed, 125 insertions(+), 140 deletions(-)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index dade5cc..1f0886c 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -76,6 +76,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -149,6 +150,7 @@ typedef struct avl_dbase
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ ilist_d_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
@@ -256,7 +258,7 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
/* the database list in the launcher, and the context that contains it */
-static Dllist *DatabaseList = NULL;
+static ilist_d_head DatabaseList;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
@@ -403,6 +405,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* Identify myself via ps */
init_ps_display("autovacuum launcher process", "", "", "");
+ /* initialize to be empty */
+ ilist_d_init(&DatabaseList);
+
ereport(LOG,
(errmsg("autovacuum launcher started")));
@@ -505,7 +510,7 @@ AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
- DatabaseList = NULL;
+ ilist_d_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
@@ -573,7 +578,7 @@ AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
- Dlelem *elem;
+ avl_dbase *avdb;
int rc;
/*
@@ -735,11 +740,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
- elem = DLGetTail(DatabaseList);
- if (elem != NULL)
+ avdb = ilist_d_head(avl_dbase, adl_node, &DatabaseList);
+ if (avdb != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
-
/*
* launch a worker if next_worker is right now or it is in the
* past
@@ -780,7 +783,7 @@ AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
- Dlelem *elem;
+ avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
@@ -793,9 +796,8 @@ launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
- else if ((elem = DLGetTail(DatabaseList)) != NULL)
+ else if ((avdb = ilist_d_tail(avl_dbase, adl_node, &DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
@@ -864,6 +866,7 @@ rebuild_database_list(Oid newdb)
int score;
int nelems;
HTAB *dbhash;
+ ilist_d_node *elem;
/* use fresh stats */
autovac_refresh_stats();
@@ -924,36 +927,28 @@ rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
- if (DatabaseList != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- Dlelem *elem;
-
- elem = DLGetHead(DatabaseList);
- while (elem != NULL)
- {
- avl_dbase *avdb = DLE_VAL(elem);
- avl_dbase *db;
- bool found;
- PgStat_StatDBEntry *entry;
-
- elem = DLGetSucc(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
+ avl_dbase *db;
+ bool found;
+ PgStat_StatDBEntry *entry;
- /*
- * skip databases with no stat entries -- in particular, this gets
- * rid of dropped databases
- */
- entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
- if (entry == NULL)
- continue;
+ /*
+ * skip databases with no stat entries -- in particular, this gets
+ * rid of dropped databases
+ */
+ entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
+ if (entry == NULL)
+ continue;
- db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
+ db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
- if (!found)
- {
- /* hash_search already filled in the key */
- db->adl_score = score++;
- /* next_worker is filled in later */
- }
+ if (!found)
+ {
+ /* hash_search already filled in the key */
+ db->adl_score = score++;
+ /* next_worker is filled in later */
}
}
@@ -984,7 +979,7 @@ rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
- DatabaseList = DLNewList();
+ ilist_d_init(&DatabaseList);
if (nelems > 0)
{
@@ -1026,15 +1021,13 @@ rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
- DLAddHead(DatabaseList, elem);
+ ilist_d_push_head(&DatabaseList, &db->adl_node);
}
}
@@ -1144,7 +1137,7 @@ do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
- Dlelem *elem;
+ ilist_d_node *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
@@ -1176,11 +1169,10 @@ do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
- while (elem != NULL)
+ ilist_d_reverse_foreach(elem, &DatabaseList)
{
- avl_dbase *dbp = DLE_VAL(elem);
+ avl_dbase *dbp = ilist_container(avl_dbase, adl_node, elem);
if (dbp->adl_datid == tmp->adw_datid)
{
@@ -1197,7 +1189,6 @@ do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
@@ -1271,7 +1262,7 @@ static void
launch_worker(TimestampTz now)
{
Oid dbid;
- Dlelem *elem;
+ ilist_d_node *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
@@ -1280,10 +1271,9 @@ launch_worker(TimestampTz now)
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
- elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
- while (elem != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- avl_dbase *avdb = DLE_VAL(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
if (avdb->adl_datid == dbid)
{
@@ -1294,10 +1284,9 @@ launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
- DLMoveToFront(elem);
+ ilist_d_move_head(&DatabaseList, elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 913734f..36e9b0b 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -95,7 +95,7 @@
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
@@ -145,10 +145,10 @@ typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
- Dlelem elem; /* list link in BackendList */
+ ilist_d_node elem; /* list link in BackendList */
} Backend;
-static Dllist *BackendList;
+static ilist_d_head BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
@@ -976,7 +976,7 @@ PostmasterMain(int argc, char *argv[])
/*
* Initialize the list of active backends.
*/
- BackendList = DLNewList();
+ ilist_d_init(&BackendList);
/*
* Initialize pipe (or process handle on Windows) that allows children to
@@ -1797,7 +1797,7 @@ processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
- Dlelem *curr;
+ ilist_d_node *curr;
#else
int i;
#endif
@@ -1811,9 +1811,9 @@ processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
@@ -2591,8 +2591,7 @@ static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
- Dlelem *curr;
-
+ ilist_d_node *curr, *next;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
/*
@@ -2623,9 +2622,9 @@ CleanupBackend(int pid,
return;
}
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->pid == pid)
{
@@ -2644,7 +2643,7 @@ CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_delete(&BackendList, curr);
free(bp);
break;
}
@@ -2661,7 +2660,7 @@ CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
- Dlelem *curr,
+ ilist_d_node *curr,
*next;
Backend *bp;
@@ -2677,10 +2676,10 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
- for (curr = DLGetHead(BackendList); curr; curr = next)
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- next = DLGetSucc(curr);
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
+
if (bp->pid == pid)
{
/*
@@ -2693,7 +2692,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_delete(&BackendList, curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
@@ -3056,7 +3055,7 @@ PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
- if (DLGetHead(BackendList) == NULL &&
+ if (ilist_d_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
@@ -3182,12 +3181,12 @@ signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
bool signaled = false;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -3325,8 +3324,8 @@ BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_head(&BackendList, &bn->elem);
+
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
@@ -4422,12 +4421,12 @@ PostmasterRandom(void)
static int
CountChildren(int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
int cnt = 0;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -4606,8 +4605,7 @@ StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 0307b96..efe34d9 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -353,6 +353,8 @@ CatCachePrintStats(int code, Datum arg)
static void
CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
{
+ Index hashIndex;
+
Assert(ct->refcount == 0);
Assert(ct->my_cache == cache);
@@ -369,7 +371,9 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
}
/* delink from linked list */
- DLRemove(&ct->cache_elem);
+ hashIndex = HASH_INDEX(ct->hash_value, cache->cc_nbuckets);
+
+ ilist_d_delete(&cache->cc_bucket[hashIndex], &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
@@ -412,7 +416,7 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
- DLRemove(&cl->cache_elem);
+ ilist_d_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
@@ -452,8 +456,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
+ ilist_d_head *bucket;
if (cacheId != ccp->id)
continue;
@@ -468,11 +473,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -484,12 +487,10 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
-
- for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
+ bucket = &ccp->cc_bucket[hashIndex];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (hashValue == ct->hash_value)
{
@@ -561,13 +562,13 @@ AtEOXact_CatCache(bool isCommit)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
- Dlelem *elt;
+ ilist_d_node *elt;
int i;
/* Check CatCLists */
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
@@ -577,11 +578,10 @@ AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&ccp->cc_bucket[i]);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_head *bucket = &ccp->cc_bucket[i];
+ ilist_d_foreach(elt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
@@ -604,16 +604,14 @@ AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
- for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &cache->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -624,11 +622,10 @@ ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
+ ilist_d_head *bucket = &cache->cc_bucket[i];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
@@ -770,10 +767,8 @@ InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
- cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
+ cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(ilist_d_node));
/*
* initialize the cache's relation information for the relation
@@ -792,6 +787,12 @@ InitCatCache(int id,
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ ilist_d_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++){
+ ilist_d_init(&cp->cc_bucket[i]);
+ }
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
@@ -1060,7 +1061,8 @@ SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
- Dlelem *elt;
+ ilist_d_node *elt;
+ ilist_d_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
@@ -1094,13 +1096,11 @@ SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
- {
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket){
bool res;
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead)
continue; /* ignore dead entries */
@@ -1125,7 +1125,7 @@ SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
- DLMoveToFront(&ct->cache_elem);
+ ilist_d_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
@@ -1340,7 +1340,7 @@ SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
- Dlelem *elt;
+ ilist_d_node *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
@@ -1382,13 +1382,11 @@ SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
- for (elt = DLGetHead(&cache->cc_lists);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &cache->cc_lists)
{
bool res;
- cl = (CatCList *) DLE_VAL(elt);
+ cl = ilist_container(CatCList, cache_elem, elt);
if (cl->dead)
continue; /* ignore dead entries */
@@ -1416,7 +1414,7 @@ SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
- DLMoveToFront(&cl->cache_elem);
+ ilist_d_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
@@ -1468,7 +1466,8 @@ SearchCatCacheList(CatCache *cache,
{
uint32 hashValue;
Index hashIndex;
-
+ bool found = false;
+ ilist_d_head *bucket;
/*
* See if there's an entry for this tuple already.
*/
@@ -1476,11 +1475,10 @@ SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket)
{
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
@@ -1498,10 +1496,11 @@ SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
- if (elt == NULL)
+ if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
@@ -1564,7 +1563,6 @@ SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
@@ -1587,7 +1585,7 @@ SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
- DLAddHead(&cache->cc_lists, &cl->cache_elem);
+ ilist_d_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
@@ -1664,14 +1662,14 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
- DLInitElem(&ct->cache_elem, (void *) ct);
+
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
- DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
+ ilist_d_push_head(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index d91700a..20f2fa8 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -22,7 +22,7 @@
#include "access/htup.h"
#include "access/skey.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "utils/relcache.h"
/*
@@ -51,7 +51,7 @@ typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
- Dllist cc_lists; /* list of CatCList structs */
+ ilist_d_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
@@ -66,7 +66,7 @@ typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
- Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
+ ilist_d_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
@@ -77,11 +77,11 @@ typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
- * Each tuple in a cache is a member of a Dllist that stores the elements
- * of its hash bucket. We keep each Dllist in LRU order to speed repeated
+ * Each tuple in a cache is a member of a ilist_d that stores the elements
+ * of its hash bucket. We keep each ilist_d in LRU order to speed repeated
* lookups.
*/
- Dlelem cache_elem; /* list member of per-bucket list */
+ ilist_d_node cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
@@ -139,7 +139,7 @@ typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
- Dlelem cache_elem; /* list member of per-catcache list */
+ ilist_d_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--
1.7.10.rc3.3.g19a6c.dirty
Excerpts from Andres Freund's message of jue jun 28 14:20:59 -0400 2012:
Looks good now?
The one thing I dislike about this code is the names you've chosen. I
mean, ilist_s_stuff and ilist_d_stuff. I mean, why not just Slist_foo
and Dlist_bar, say? As far as I can tell, you've chosen the "i" prefix
because it's "integrated" or "inline", but this seems to me a rather
irrelevant implementation detail that's of little use to the callers.
Also, I don't find so great an idea to have everything in a single file.
Is there anything wrong with separating singly and doubly linked lists
each to its own file? Other than you not liking it, I mean. As a
person who spends some time trying to untangle header dependencies, I
would appreciate keeping stuff as lean as possible. However, since
nobody else seems to have commented on this, maybe it's just me.
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
On Thu, Jun 28, 2012 at 3:47 PM, Alvaro Herrera
<alvherre@commandprompt.com> wrote:
Excerpts from Andres Freund's message of jue jun 28 14:20:59 -0400 2012:
Looks good now?
The one thing I dislike about this code is the names you've chosen. I
mean, ilist_s_stuff and ilist_d_stuff. I mean, why not just Slist_foo
and Dlist_bar, say? As far as I can tell, you've chosen the "i" prefix
because it's "integrated" or "inline", but this seems to me a rather
irrelevant implementation detail that's of little use to the callers.Also, I don't find so great an idea to have everything in a single file.
Is there anything wrong with separating singly and doubly linked lists
each to its own file? Other than you not liking it, I mean. As a
person who spends some time trying to untangle header dependencies, I
would appreciate keeping stuff as lean as possible. However, since
nobody else seems to have commented on this, maybe it's just me.
Well, it's not JUST you. I agree entirely with all of your points.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
On Thursday, June 28, 2012 09:47:05 PM Alvaro Herrera wrote:
Excerpts from Andres Freund's message of jue jun 28 14:20:59 -0400 2012:
Looks good now?
The one thing I dislike about this code is the names you've chosen. I
mean, ilist_s_stuff and ilist_d_stuff. I mean, why not just Slist_foo
and Dlist_bar, say? As far as I can tell, you've chosen the "i" prefix
because it's "integrated" or "inline", but this seems to me a rather
irrelevant implementation detail that's of little use to the callers.
Well, its not irrelevant because you actually need to change the contained
structs to use it. I find that a pretty relevant distinction.
Also, I don't find so great an idea to have everything in a single file.
Is there anything wrong with separating singly and doubly linked lists
each to its own file? Other than you not liking it, I mean. As a
person who spends some time trying to untangle header dependencies, I
would appreciate keeping stuff as lean as possible. However, since
nobody else seems to have commented on this, maybe it's just me.
Robert had the same comment, its not just you...
It would mean duplicating the ugliness around the conditional inlining, the
comment explaining how to use the stuff (because its basically used the same
way for single and double linked lists), you would need to #define
ilist_container twice or have a third file....
Just seems to much overhead for ~100 lines (the single linked list
implementation).
What I wonder is how hard it would be to remove catcache.h's structs into the
implementation. Thats the reason why the old and new list implementation
currently is included all over the backend...
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Thursday, June 28, 2012 10:03:26 PM Andres Freund wrote:
What I wonder is how hard it would be to remove catcache.h's structs into
the implementation. Thats the reason why the old and new list
implementation currently is included all over the backend...
Moving them into the implementation isn't possible, but catcache.h being
included just about everywhere simply isn't needed.
It being included everywhere was introduced by a series of commits from Bruce:
b85a965f5fc7243d0386085e12f7a6c836503b42
b43ebe5f83b28e06a3fd933b989aeccf0df7844a
e0522505bd13bc5aae993fc50b8f420665d78e96
and others
That looks broken. An implementation file not including its own header... A
minimal patch to fix this particular problem is attached (looks like there are
others in the series).
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
0001-Stop-including-catcache.h-from-syscache.h.patchtext/x-patch; charset=UTF-8; name=0001-Stop-including-catcache.h-from-syscache.h.patchDownload
From 45e2c358e6a21e837f13731981da2644bcb57a88 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 28 Jun 2012 23:03:44 +0200
Subject: [PATCH] Stop including catcache.h from syscache.h
syscache.h used to not rely on catcache.h and even today ships with the comment
"Users of this must import catcache.h too" for the one function requiring
catcache.h knowledge.
This was changed in a series of commits including:
b85a965f5fc7243d0386085e12f7a6c836503b42
b43ebe5f83b28e06a3fd933b989aeccf0df7844a
e0522505bd13bc5aae993fc50b8f420665d78e96
Change it back.
---
src/backend/access/transam/xact.c | 1 +
src/backend/catalog/namespace.c | 1 +
src/backend/catalog/pg_conversion.c | 1 +
src/backend/catalog/pg_enum.c | 1 +
src/backend/utils/adt/acl.c | 1 +
src/backend/utils/cache/attoptcache.c | 1 +
src/backend/utils/cache/catcache.c | 1 +
src/backend/utils/cache/inval.c | 1 +
src/backend/utils/cache/lsyscache.c | 1 +
src/backend/utils/cache/relcache.c | 1 +
src/backend/utils/cache/spccache.c | 1 +
src/backend/utils/cache/syscache.c | 1 +
src/backend/utils/cache/ts_cache.c | 1 +
src/backend/utils/cache/typcache.c | 1 +
src/backend/utils/resowner/resowner.c | 5 +++--
src/include/utils/resowner.h | 10 ++++++----
src/include/utils/snapmgr.h | 1 +
src/include/utils/syscache.h | 2 +-
18 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 4755ee6..1f743f7 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -44,6 +44,7 @@
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
+#include "utils/catcache.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index 20850ab..10ad82b 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -46,6 +46,7 @@
#include "storage/sinval.h"
#include "utils/acl.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
diff --git a/src/backend/catalog/pg_conversion.c b/src/backend/catalog/pg_conversion.c
index f86c84f..358bd39 100644
--- a/src/backend/catalog/pg_conversion.c
+++ b/src/backend/catalog/pg_conversion.c
@@ -25,6 +25,7 @@
#include "catalog/pg_proc.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/syscache.h"
diff --git a/src/backend/catalog/pg_enum.c b/src/backend/catalog/pg_enum.c
index 41665c1..20e26c4 100644
--- a/src/backend/catalog/pg_enum.c
+++ b/src/backend/catalog/pg_enum.c
@@ -23,6 +23,7 @@
#include "storage/lmgr.h"
#include "miscadmin.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c
index 77322a1..2cc87d8 100644
--- a/src/backend/utils/adt/acl.c
+++ b/src/backend/utils/adt/acl.c
@@ -29,6 +29,7 @@
#include "miscadmin.h"
#include "utils/acl.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
diff --git a/src/backend/utils/cache/attoptcache.c b/src/backend/utils/cache/attoptcache.c
index e01ae21..5d872ba 100644
--- a/src/backend/utils/cache/attoptcache.c
+++ b/src/backend/utils/cache/attoptcache.c
@@ -18,6 +18,7 @@
#include "access/reloptions.h"
#include "utils/attoptcache.h"
+#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/syscache.h"
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 0307b96..f27d90d 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -29,6 +29,7 @@
#endif
#include "storage/lmgr.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/memutils.h"
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 34802ee..15b1711 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -100,6 +100,7 @@
#include "miscadmin.h"
#include "storage/sinval.h"
#include "storage/smgr.h"
+#include "utils/catcache.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/rel.h"
diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c
index 64b413b..0e479e8 100644
--- a/src/backend/utils/cache/lsyscache.c
+++ b/src/backend/utils/cache/lsyscache.c
@@ -33,6 +33,7 @@
#include "nodes/makefuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 2e6776e..8d7e766 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -64,6 +64,7 @@
#include "storage/smgr.h"
#include "utils/array.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
diff --git a/src/backend/utils/cache/spccache.c b/src/backend/utils/cache/spccache.c
index cf18ee1..0b8cc39 100644
--- a/src/backend/utils/cache/spccache.c
+++ b/src/backend/utils/cache/spccache.c
@@ -23,6 +23,7 @@
#include "commands/tablespace.h"
#include "miscadmin.h"
#include "optimizer/cost.h"
+#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/spccache.h"
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index c365ec7..bf96678 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -54,6 +54,7 @@
#include "catalog/pg_ts_template.h"
#include "catalog/pg_type.h"
#include "catalog/pg_user_mapping.h"
+#include "utils/catcache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
diff --git a/src/backend/utils/cache/ts_cache.c b/src/backend/utils/cache/ts_cache.c
index b408de0..b369790 100644
--- a/src/backend/utils/cache/ts_cache.c
+++ b/src/backend/utils/cache/ts_cache.c
@@ -39,6 +39,7 @@
#include "commands/defrem.h"
#include "tsearch/ts_cache.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 72798ef..8726dba 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -55,6 +55,7 @@
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "utils/builtins.h"
+#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index 3ded469..f7a658f 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -23,6 +23,7 @@
#include "access/hash.h"
#include "storage/predicate.h"
#include "storage/proc.h"
+#include "utils/catcache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
@@ -787,7 +788,7 @@ ResourceOwnerEnlargeCatCacheListRefs(ResourceOwner owner)
* Caller must have previously done ResourceOwnerEnlargeCatCacheListRefs()
*/
void
-ResourceOwnerRememberCatCacheListRef(ResourceOwner owner, CatCList *list)
+ResourceOwnerRememberCatCacheListRef(ResourceOwner owner, struct catclist *list)
{
Assert(owner->ncatlistrefs < owner->maxcatlistrefs);
owner->catlistrefs[owner->ncatlistrefs] = list;
@@ -798,7 +799,7 @@ ResourceOwnerRememberCatCacheListRef(ResourceOwner owner, CatCList *list)
* Forget that a catcache-list reference is owned by a ResourceOwner
*/
void
-ResourceOwnerForgetCatCacheListRef(ResourceOwner owner, CatCList *list)
+ResourceOwnerForgetCatCacheListRef(ResourceOwner owner, struct catclist *list)
{
CatCList **catlistrefs = owner->catlistrefs;
int nc1 = owner->ncatlistrefs - 1;
diff --git a/src/include/utils/resowner.h b/src/include/utils/resowner.h
index e1c992e..30c8444 100644
--- a/src/include/utils/resowner.h
+++ b/src/include/utils/resowner.h
@@ -21,10 +21,9 @@
#include "storage/fd.h"
#include "storage/lock.h"
-#include "utils/catcache.h"
#include "utils/plancache.h"
#include "utils/snapshot.h"
-
+#include "utils/relcache.h"
/*
* ResourceOwner objects are an opaque data structure known only within
@@ -64,6 +63,8 @@ typedef void (*ResourceReleaseCallback) (ResourceReleasePhase phase,
bool isTopLevel,
void *arg);
+/* forward declare to avoid including catcache.h everywhere */
+struct catclist;
/*
* Functions in resowner.c
@@ -101,10 +102,11 @@ extern void ResourceOwnerRememberCatCacheRef(ResourceOwner owner,
extern void ResourceOwnerForgetCatCacheRef(ResourceOwner owner,
HeapTuple tuple);
extern void ResourceOwnerEnlargeCatCacheListRefs(ResourceOwner owner);
+/* Users of these must import catcache.h too */
extern void ResourceOwnerRememberCatCacheListRef(ResourceOwner owner,
- CatCList *list);
+ struct catclist *list);
extern void ResourceOwnerForgetCatCacheListRef(ResourceOwner owner,
- CatCList *list);
+ struct catclist *list);
/* support for relcache refcount management */
extern void ResourceOwnerEnlargeRelationRefs(ResourceOwner owner);
diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h
index f195981..54bf602 100644
--- a/src/include/utils/snapmgr.h
+++ b/src/include/utils/snapmgr.h
@@ -13,6 +13,7 @@
#ifndef SNAPMGR_H
#define SNAPMGR_H
+#include "fmgr.h"
#include "utils/resowner.h"
diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h
index d59dd4e..37dc9ed 100644
--- a/src/include/utils/syscache.h
+++ b/src/include/utils/syscache.h
@@ -16,7 +16,7 @@
#ifndef SYSCACHE_H
#define SYSCACHE_H
-#include "utils/catcache.h"
+#include "access/htup.h"
/*
* SysCache identifiers.
--
1.7.10.rc3.3.g19a6c.dirty
Excerpts from Andres Freund's message of jue jun 28 16:03:26 -0400 2012:
On Thursday, June 28, 2012 09:47:05 PM Alvaro Herrera wrote:
Excerpts from Andres Freund's message of jue jun 28 14:20:59 -0400 2012:
Looks good now?
The one thing I dislike about this code is the names you've chosen. I
mean, ilist_s_stuff and ilist_d_stuff. I mean, why not just Slist_foo
and Dlist_bar, say? As far as I can tell, you've chosen the "i" prefix
because it's "integrated" or "inline", but this seems to me a rather
irrelevant implementation detail that's of little use to the callers.Well, its not irrelevant because you actually need to change the contained
structs to use it. I find that a pretty relevant distinction.
Sure, at that point it is relevant. Once you're past that, there's no
point. I mean, you would look up how it's used in the header comment of
the implementation, and then just refer to the API.
Also, I don't find so great an idea to have everything in a single file.
Is there anything wrong with separating singly and doubly linked lists
each to its own file? Other than you not liking it, I mean. As a
person who spends some time trying to untangle header dependencies, I
would appreciate keeping stuff as lean as possible. However, since
nobody else seems to have commented on this, maybe it's just me.Robert had the same comment, its not just you...
It would mean duplicating the ugliness around the conditional inlining, the
comment explaining how to use the stuff (because its basically used the same
way for single and double linked lists), you would need to #define
ilist_container twice or have a third file....
Just seems to much overhead for ~100 lines (the single linked list
implementation).
Well, then don't duplicate a comment -- create a README.lists and
refer to it in the comments. Not sure about the ilist_container stuff,
but in principle I don't have a problem with having a file with a single
#define that's included by two other headers.
What I wonder is how hard it would be to remove catcache.h's structs into the
implementation. Thats the reason why the old and new list implementation
currently is included all over the backend...
Yeah, catcache.h is a pretty ugly beast. I'm sure there are ways to behead it.
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Excerpts from Andres Freund's message of jue jun 28 17:06:49 -0400 2012:
On Thursday, June 28, 2012 10:03:26 PM Andres Freund wrote:
What I wonder is how hard it would be to remove catcache.h's structs into
the implementation. Thats the reason why the old and new list
implementation currently is included all over the backend...Moving them into the implementation isn't possible, but catcache.h being
included just about everywhere simply isn't needed.It being included everywhere was introduced by a series of commits from Bruce:
b85a965f5fc7243d0386085e12f7a6c836503b42
b43ebe5f83b28e06a3fd933b989aeccf0df7844a
e0522505bd13bc5aae993fc50b8f420665d78e96
and othersThat looks broken. An implementation file not including its own header... A
minimal patch to fix this particular problem is attached (looks like there are
others in the series).
Hmm, I think this is against project policy -- we do want each header to
be compilable separately. I would go instead the way of splitting
resowner.h in two or more pieces.
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
On Thursday, June 28, 2012 11:45:08 PM Alvaro Herrera wrote:
Excerpts from Andres Freund's message of jue jun 28 17:06:49 -0400 2012:
On Thursday, June 28, 2012 10:03:26 PM Andres Freund wrote:
What I wonder is how hard it would be to remove catcache.h's structs
into the implementation. Thats the reason why the old and new list
implementation currently is included all over the backend...Moving them into the implementation isn't possible, but catcache.h being
included just about everywhere simply isn't needed.It being included everywhere was introduced by a series of commits from
Bruce: b85a965f5fc7243d0386085e12f7a6c836503b42
b43ebe5f83b28e06a3fd933b989aeccf0df7844a
e0522505bd13bc5aae993fc50b8f420665d78e96
and othersThat looks broken. An implementation file not including its own header...
A minimal patch to fix this particular problem is attached (looks like
there are others in the series).Hmm, I think this is against project policy -- we do want each header to
be compilable separately. I would go instead the way of splitting
resowner.h in two or more pieces.
It was done nearly the same way in catcache.h before Bruce changed things. You
can see still the rememnants of that in syscache.h:
/* list-search interface. Users of this must import catcache.h too */
extern struct catclist *SearchSysCacheList(int cacheId, int nkeys,
Datum key1, Datum key2, Datum key3, Datum key4);
The only difference is that gcc warns if you declare a struct in a parameter -
thats why I forward declared it explicitly...
resowner.h still compiles standalone and is still usable. You can even call
ResourceOwnerRememberCatCacheListRef if you get the list parameter from
somewhere else.
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On 28 June 2012 19:20, Andres Freund <andres@2ndquadrant.com> wrote:
<0001-Add-embedded-list-interface.patch>
Looks good now?
I have a few gripes.
+ * there isn't much we can test in a single linked list except that its
There are numerous references to "single linked lists", where, I
believe, "singly linked list" is intended (the same can be said for
"double" and "doubly" respectively).
/* Functions we want to be inlined if possible */
+ #ifndef USE_INLINE
...
+ #endif /* USE_INLINE */
A minor quibble, but that last line probably be:
#endif /* ! defined USE_INLINE */
Another minor quibble. We usually try and keep these in alphabetical order:
*** a/src/backend/lib/Makefile
--- b/src/backend/lib/Makefile
*************** subdir = src/backend/lib
*** 12,17 ****
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
--- 12,17 ----
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
New files generally don't require this:
+ * Portions Copyright (c) 1994, Regents of the University of California
This needs to be altered:
+ /*
+ * enable for extra debugging. This is rather expensive so its not enabled by
+ * default even with --enable-cassert
+ */
+ /* #define ILIST_DEBUG */
I'm not sure that this extra error-detection warrants its own macro.
syncrep.c similarly has its own rather expensive error-detection, with
entire function bodies only compiled when USE_ASSERT_CHECKING is
defined. Any of the other dedicated "debugging macros", like
LOCK_DEBUG or WAL_DEBUG seem to exist to dump LOG messages when
binaries were built with the macros defined (this can happen via
pg_config_manual.h. I note that what you have here lacks a
corresponding entry). I think that it would be more idiomatic to just
use USE_ASSERT_CHECKING, and rewrite the debugging functions such that
they are used directly within an Assert, in the style of syncrep.c .
Now, I know Tom wasn't too enthusiastic about having this sort of
thing within --enable-cassert builds, but I'm inclined to think that
there is little point in having this additional error checking if
they're not at least available when that configuration is used. Maybe
we should consider taking the sophisticated asserts out of
--enable-cassert builds, or removing them entirely, if and when they
prove to be annoying?
There is slight misalignment within the comments at the top of ilist.h.
Within ilist.c, the following block exists:
+ #ifndef USE_INLINE
+ #define ILIST_USE_DECLARATION
+ #endif
+
+ #include "lib/ilist.h"
+
+ #ifndef USE_INLINE
+ #undef ILIST_USE_DECLARATION
+ #endif
I see little reason for the latter "#undef" block within ilist.c. It
isn't that exactly the same code already exists at the end of ilist.h
(though there is that too) - it's mostly that it's unnecessary,
because there is no need or logical reason to #undef within ilist.c.
+ /*
+ * The following function declarations are only used if inlining is supported
+ * or when included from a file that explicitly declares USE_DECLARATION
+ */
+ #ifdef ILIST_USE_DECLARATION
Shouldn't that be "The following function definitions..." and
ILIST_USE_DEFINITIONS?
I think the fact that it's possible in principle for
ILIST_USE_DECLARATION to not be exactly equivalent to ! USE_INLINE is
strictly speaking dangerous, since USE_INLINE needs to be baked into
the ABI targeted by third-party module developers. What if a module
was built that called a function that didn't have an entry in the
procedure linkage table, due to ad-hoc usage of ILIST_USE_DECLARATION?
That'd result in a libdl error, if you're lucky. Now, that probably
sounds stupid - I'm pretty sure that you didn't intend that
ILIST_USE_DECLARATION be set by just any old client of this
infrastructure. Rather, you intended that ILIST_USE_DECLARATION be set
either when ilist.h was included while USE_INLINE, so that macro
expansion would make those functions inline, or within ilist.c, when
!USE_INLINE, so that the functions would not be inlined. This should
be much more explicit though. You simply describe the mechanism rather
than the intent at present.
I rather suspect that INLINE_IF_POSSIBLE should be a general purpose
utility, perhaps defined next to NON_EXEC_STATIC within c.h, with a
brief explanation there (rather than in any new header that needs to
do this). I think you'd be able to reasonably split singly and doubly
linked lists into their own headers without much repetition between
the two then. You could formalise the idea that where USE_INLINE,
another macro, defined alongside INLINE_IF_POSSIBLE (functionally
equivalent to ILIST_USE_DECLARATION in the extant code - say,
USE_INLINING_DEFINITIONS) is going to be generally defined everywhere
USE_INLINE is defined. You're then going to have to deal with the hack
whereby USE_INLINING_DEFINITIONS is set just "to suck the definitions
out of the header" to make !USE_INLINE work within .c files only (or
pretty close). That's kins of logical though - !USE_INLINE is hardly
ever used, so it deserves to get the slightly grottier code. The only
direct macro usage that your header files now need is "#ifdef
USE_INLINING_DEFINITIONS" (plus INLINE_IF_POSSIBLE rather than plain
"static inline", of course), as well as having extern declarations for
the !USE_INLINE case. The idea is that each header file has slightly
fewer eyebrow-raising macro hacks, and there is at least no obvious
redundancy between the two. In particular, this only has to happen
once, within c.h:
#ifdef USE_INLINE
#define INLINE_IF_POSSIBLE static inline
#define USE_INLINING_DEFINITIONS // actually spelt
"ILIST_USE_DECLARATION" in patch
#else
#define INLINE_IF_POSSIBLE
#endif
What's more, under this scheme, the following code does not need to
(and shouldn't) appear at the end of headers like ilist.h:
+ /*
+ * if we defined ILIST_USE_DECLARATION undef it again, its not interesting
+ * outside this file
+ */
+ #ifdef USE_INLINE
+ #undef USE_INLINING_DEFINITIONS // actually spelt
"ILIST_USE_DECLARATION" in patch
+ #endif
because only when considering the !USE_INLINE case do we have to
consider that we might need to manually set USE_INLINING_DEFINITIONS
to "suck in" definitions within C files (as I've said, unsetting it
very probably doesn't matter within a C file, so no need to do it
there either).
If that isn't quite clear, the simple version is:
We assume the USE_INLINE case until we learn otherwise, rather than
assuming neither USE_INLINE nor !USE_INLINE. It's cleaner to treat
!USE_INLINE as a sort of edge case with special handling, rather than
having ilist.h and friends worry about cleaning up after themselves
all the time, and worrying about initialising things for themselves
alone according to the present build configuration.
In any case, if we're going to manage the !USE_INLINE case like this
at all, we should probably formalise it along some lines.
I still don't think this is going to fly:
+ #ifdef __GNUC__
+ #define unused_attr __attribute__((unused))
+ #else
+ #define unused_attr
+ #endif
There is no reasonable way to prevent MSVC from giving a warning as a
result of this. The rationale for doing things this way is:
+ /*
+ * gcc warns about unused parameters if -Wunused-parameter is specified (part
+ * of -Wextra ). In the cases below its perfectly fine not to use those
+ * parameters because they are just passed to make the interface
consistent and
+ * to allow debugging in case of ILIST_DEBUG.
+ *
+ */
Seems to me that you'd be able to do a better job with a thin macro
shim on the existing (usually) inline functions. That'd also take care
of your concerns about multiple evaluation.
The comments could probably use some wordsmithing in various places.
That's no big deal though.
I agree with Alvaro on the naming of these functions. I'd prefer it if
they didn't have the "i" prefix too, though that's hardly very
important.
That's all I have for now. I'll take a look at the other patch
(0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patch)
soon.
--
Peter Geoghegan http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services
On 5 July 2012 02:49, Peter Geoghegan <peter@2ndquadrant.com> wrote:
On 28 June 2012 19:20, Andres Freund <andres@2ndquadrant.com> wrote:
<0001-Add-embedded-list-interface.patch>
Looks good now?
I have a few gripes.
We are passed the nominal deadline. Had you planned on getting back to
me this commitfest? If not, I'll shelve my review of
"0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patch"
until commitfest 2012-09, and mark this "returned with feedback".
--
Peter Geoghegan http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services
On Monday, July 23, 2012 12:55:01 PM Peter Geoghegan wrote:
On 5 July 2012 02:49, Peter Geoghegan <peter@2ndquadrant.com> wrote:
On 28 June 2012 19:20, Andres Freund <andres@2ndquadrant.com> wrote:
<0001-Add-embedded-list-interface.patch>
Looks good now?
I have a few gripes.
We are passed the nominal deadline. Had you planned on getting back to
me this commitfest? If not, I'll shelve my review of
"0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patch"
until commitfest 2012-09, and mark this "returned with feedback".
I was actually waiting for the second review ;). Sorry for the
misunderstanding.
There is no rule that review cannot happen outside the commitfest, so I would
appreciate if we could push this further...
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Excerpts from Andres Freund's message of jue jun 28 17:06:49 -0400 2012:
On Thursday, June 28, 2012 10:03:26 PM Andres Freund wrote:
What I wonder is how hard it would be to remove catcache.h's structs into
the implementation. Thats the reason why the old and new list
implementation currently is included all over the backend...Moving them into the implementation isn't possible, but catcache.h being
included just about everywhere simply isn't needed.
FWIW this got fixed during some header changes I made a couple of weeks
ago. If you have similar fixes to propose, let me know.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Here's a prettified version of this stuff. I found one bug in the macro
ilist_s_head: the test was reversed. Also, curiously, the macro had the
same name as the struct, so I renamed the macro. I take it you haven't
used this macro, so maybe it shouldn't be there at all? Or maybe I
completely misread what the macro is supposed to do.
I also renamed all the structs and functions by changing ilist_s_foo to
Slist_foo. Similarly for ilist_d_foo. This is all mechanical so any
subsequent patch should be trivial to refresh for this change.
I think README.ilist (which is what you had in the comment at the top of
ilist.h) should be heavily expanded. I don't find it at all clear.
There were other cosmetic changes, but the implementation is pretty much
the same you submitted.
I didn't look at the other patch you posted, replacing dllist.c usage;
will do that next to verify that the list implementation works.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
ilist.patchapplication/octet-stream; name=ilist.patchDownload
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 2e1061e..c94297a 100644
*** a/src/backend/lib/Makefile
--- b/src/backend/lib/Makefile
***************
*** 12,17 **** subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
--- 12,17 ----
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/REnew file mode 100644
index 0000000..3a3d8a8
*** /dev/null
--- b/src/backend/lib/README.ilist
***************
*** 0 ****
--- 1,43 ----
+ his is intended to be used in the following manner:
+
+ #include "lib/ilist.h"
+
+ typedef struct something_in_a_list
+ {
+ int value;
+ int somethingelse;
+ Dlist_head values;
+ } something_in_a_list;
+
+ typedef struct value_in_a_list
+ {
+ int data;
+ int somethingelse;
+ Dlist_node list_node;
+ } value_in_a_list;
+
+ something_in_a_list *somelist = get_blarg();
+
+ Dlist_push_head(somelist, &get_value_in_a_list()->list_node);
+ ...
+ Dlist_push_head(somelist, &get_value_in_a_list()->list_node);
+ ...
+ ...
+ Dlist_node *node,
+ *next;
+
+ Dlist_foreach(node, somelist)
+ {
+ value_in_a_list *val = ilist_container(value_in_a_list, list_node, node);
+ do_something_with_val(val);
+ }
+
+ Dlist_foreach_modify(node, next, somelist)
+ {
+ /*
+ * here we can inspect the node and maybe delete it. Any other
+ * manipulation is forbidden.
+ */
+ if (node is to be deleted)
+ Dlist_delete(node);
+ }
diff --git a/src/backend/lib/ilist.new file mode 100644
index 0000000..239c538
*** /dev/null
--- b/src/backend/lib/ilist.c
***************
*** 0 ****
--- 1,117 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+ #include "postgres.h"
+
+ /*
+ * If inlines are in use, include the header normally which will get us only
+ * the function definitions as inlines. But if inlines aren't available,
+ * include the header with ILIST_USE_DEFINITION defined; this makes it pour in
+ * regular (not inline) function declarations and their definitions.
+ */
+ #ifdef USE_INLINE
+ #include "lib/ilist.h"
+ #else /* USE_INLINE */
+ #define ILIST_USE_DEFINITION
+ #include "lib/ilist.h"
+ #undef ILIST_USE_DEFINITION
+ #endif
+
+ /*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+ void
+ Slist_delete(Slist_head *head, Slist_node *node)
+ {
+ Slist_node *last = &head->head;
+ Slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+ #ifdef USE_ASSERT_CHECKING
+ found = true;
+ #endif
+ break;
+ }
+ last = cur;
+ }
+
+ Slist_check(head);
+ Assert(found);
+ }
+
+ #ifdef ILIST_DEBUG
+ /*
+ * Verify integrity of a doubly linked list
+ */
+ void
+ Dlist_check(Dlist_head *head)
+ {
+ Dlist_node *cur;
+
+ if (!head || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+ }
+
+ /*
+ * Verify integrity of a singly linked list
+ */
+ void
+ Slist_check(Slist_head *head)
+ {
+ Slist_node *cur;
+
+ if (!head || !(&head->head))
+ elog(ERROR, "singly linked list head is not properly initialized");
+
+ /*
+ * there isn't much we can test in a singly linked list except that its
+ * really circular
+ */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ if (!cur)
+ elog(ERROR, "singly linked list is corrupted");
+ }
+
+ #endif /* ILIST_DEBUG */
diff --git a/src/include/lib/inew file mode 100644
index 0000000..27c5dba
*** /dev/null
--- b/src/include/lib/ilist.h
***************
*** 0 ****
--- 1,453 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure. Also, they are always circularly linked, even
+ * when empty; this enables many manipulations to be done without branches,
+ * which is beneficial performance-wise.
+ *
+ * See src/backend/lib/README.ilist for intended usage.
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+ #ifndef ILIST_H
+ #define ILIST_H
+
+ /*
+ * enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even with --enable-cassert.
+ */
+ #undef ILIST_DEBUG
+
+ /*
+ * gcc warns about unused parameters if -Wunused-parameter is specified (part
+ * of -Wextra). In the cases below its perfectly fine not to use those
+ * parameters because they are just passed to make the interface consistent and
+ * to allow debugging in case of ILIST_DEBUG.
+ *
+ * FIXME -- this should be part of c.h.
+ */
+ #ifdef __GNUC__
+ #define unused_attr __attribute__((unused))
+ #else
+ #define unused_attr
+ #endif
+
+ /*
+ * struct to embed in other structs that need to be part of a doubly linked
+ * list.
+ */
+ typedef struct Dlist_node Dlist_node;
+ struct Dlist_node
+ {
+ Dlist_node *prev;
+ Dlist_node *next;
+ };
+
+ /*
+ * Head of a doubly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty.
+ */
+ typedef struct Dlist_head
+ {
+ Dlist_node head;
+ } Dlist_head;
+
+ /*
+ * struct to embed in other structs that need to be part of a singly linked
+ * list.
+ */
+ typedef struct Slist_node Slist_node;
+ struct Slist_node
+ {
+ Slist_node *next;
+ };
+
+ /*
+ * Head of a singly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty.
+ */
+ typedef struct Slist_head
+ {
+ Slist_node head;
+ } Slist_head;
+
+ #ifdef USE_INLINE
+ #define INLINE_IF_POSSIBLE static inline
+ #define ILIST_USE_DEFINITION
+ #else
+ #define INLINE_IF_POSSIBLE
+
+ /* Prototypes for functions we want to be inlined if possible */
+ extern void Dlist_check(unused_attr Dlist_head *head);
+ extern void Dlist_init(Dlist_head *head);
+ extern void Dlist_push_head(Dlist_head *head, Dlist_node *node);
+ extern void Dlist_push_tail(Dlist_head *head, Dlist_node *node);
+ extern void Dlist_insert_after(unused_attr Dlist_head *head,
+ Dlist_node *after, Dlist_node *node);
+ extern void Dlist_insert_before(unused_attr Dlist_head *head,
+ Dlist_node *before, Dlist_node *node);
+ extern void Dlist_delete(unused_attr Dlist_head *head, Dlist_node *node);
+ extern Dlist_node *Dlist_pop_head(Dlist_head *head);
+ extern void Dlist_move_head(Dlist_head *head, Dlist_node *node);
+ extern bool Dlist_has_next(Dlist_head *head, Dlist_node *node);
+ extern bool Dlist_has_prev(Dlist_head *head, Dlist_node *node);
+ extern bool Dlist_is_empty(Dlist_head *head);
+ extern void Dlist_check(Dlist_head *head);
+
+ extern void Slist_init(Slist_head *head);
+ extern void Slist_push_head(Slist_head *head, Slist_node *node);
+ extern Slist_node *Slist_pop_head(Slist_head *head);
+ extern void Slist_insert_after(unused_attr Slist_head *head,
+ Slist_node *after, Slist_node *node);
+ extern bool Slist_is_empty(Slist_head *head);
+ extern bool Slist_has_next(unused_attr Slist_head *head,
+ Slist_node *node);
+ #endif /* USE_INLINE */
+
+ /* These functions are too big to be inline, so they are in the C file always */
+ extern void Slist_delete(Slist_head *head, Slist_node *node);
+
+ #ifdef ILIST_DEBUG
+ extern void Dlist_check(Dlist_head *head);
+ extern void Slist_check(Slist_head *head);
+ #else
+ #define Dlist_check(head)
+ #define Slist_check(head)
+ #endif /* ILIST_DEBUG */
+
+ /*
+ * Return a pointer to the struct 'type' when the passed 'ptr' points to the
+ * element 'membername'.
+ */
+ #define ilist_container(type, membername, ptr) \
+ ((type*)((char*)(ptr) - offsetof(type, membername)))
+
+ /*
+ * The following function definitions are only used if inlining is supported by
+ * the compiler, or when included from a file that explicitly declares
+ * ILIST_USE_DEFINITION.
+ */
+ #ifdef ILIST_USE_DEFINITION
+
+ /*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_init(Dlist_head *head)
+ {
+ head->head.next = head->head.prev = &head->head;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * inserts a node at the beginning of the list
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_push_head(Dlist_head *head, Dlist_node *node)
+ {
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * inserts a node at the end of the list
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_push_tail(Dlist_head *head, Dlist_node *node)
+ {
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * insert a node after another *in the same list*
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_insert_after(unused_attr Dlist_head *head, Dlist_node *after,
+ Dlist_node *node)
+ {
+ Dlist_check(head);
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * insert a node after another *in the same list*
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_insert_before(unused_attr Dlist_head *head, Dlist_node *before,
+ Dlist_node *node)
+ {
+ Dlist_check(head);
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * deletes a node from a list
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_delete(unused_attr Dlist_head *head, Dlist_node *node)
+ {
+ Dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ Dlist_check(head);
+ }
+
+ /*
+ * deletes the first node from a list or returns NULL
+ */
+ INLINE_IF_POSSIBLE Dlist_node *
+ Dlist_pop_head(Dlist_head *head)
+ {
+ Dlist_node *ret;
+
+ if (&head->head == head->head.next)
+ return NULL;
+
+ ret = head->head.next;
+ Dlist_delete(head, head->head.next);
+ return ret;
+ }
+
+ /*
+ * moves an element (that has to be in the list) to the head of the list
+ */
+ INLINE_IF_POSSIBLE void
+ Dlist_move_head(Dlist_head *head, Dlist_node *node)
+ {
+ /* fast path if it's already at the head */
+ if (&head->head == node)
+ return;
+
+ Dlist_delete(head, node);
+ Dlist_push_head(head, node);
+
+ Dlist_check(head);
+ }
+
+ /*
+ * Check whether the passed node is the last element in the list
+ */
+ INLINE_IF_POSSIBLE bool
+ Dlist_has_next(Dlist_head *head, Dlist_node *node)
+ {
+ return node->next != &head->head;
+ }
+
+ /*
+ * Check whether the passed node is the first element in the list
+ */
+ INLINE_IF_POSSIBLE bool
+ Dlist_has_prev(Dlist_head *head, Dlist_node *node)
+ {
+ return node->prev != &head->head;
+ }
+
+ /*
+ * Check whether the list is empty.
+ */
+ INLINE_IF_POSSIBLE bool
+ Dlist_is_empty(Dlist_head *head)
+ {
+ return head->head.next == head->head.prev;
+ }
+ #endif /* ILIST_USE_DEFINITION */
+
+ /*
+ * Return the value of first element in the list if there is one, return NULL
+ * otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+ #define Dlist_head(type, membername, ptr) \
+ ( \
+ (&((ptr)->head) == (ptr)->head.next) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.next) \
+ )
+
+ /*
+ * Return the value of the first element. This will return corrupt data if
+ * there is no element in the list.
+ */
+ #define Dlist_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+ /*
+ * Return the value of the last element in the list if there is one, return
+ * NULL otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+ #define Dlist_tail(type, membername, ptr) \
+ ( \
+ (&((ptr)->head) == (ptr)->head.prev) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.prev) \
+ )
+
+ /*
+ * Iterate through the list storing the current element in the variable
+ * 'name'. 'ptr' will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define Dlist_foreach(name, ptr) \
+ for (name = (ptr)->head.next; name != &(ptr)->head; name = name->next)
+
+ /*
+ * Iterate through the list storing the current element in the variable 'name'
+ * and also storing the next list element in the variable 'nxt'.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+ #define Dlist_foreach_modify(name, nxt, ptr) \
+ for (name = (ptr)->head.next, nxt = name->next; \
+ name != &(ptr)->head; \
+ name = nxt, nxt = name->next)
+
+ /*
+ * Iterate through the list in reverse order storing the current element in the
+ * variable 'name'. 'ptr' will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define Dlist_reverse_foreach(name, ptr) \
+ for (name = (ptr)->head.prev; name != &(ptr)->head; name = name->prev)
+
+
+ #ifdef ILIST_USE_DEFINITION
+
+ /*
+ * Initialize a singly linked list.
+ */
+ INLINE_IF_POSSIBLE void
+ Slist_init(Slist_head *head)
+ {
+ head->head.next = NULL;
+
+ Slist_check(head);
+ }
+
+ /*
+ * Install a new element as head of the list, pushing the original head to the
+ * second position.
+ */
+ INLINE_IF_POSSIBLE void
+ Slist_push_head(Slist_head *head, Slist_node *node)
+ {
+ node->next = head->head.next;
+ head->head.next = node;
+
+ Slist_check(head);
+ }
+
+ /*
+ * Returns the first element of the list, removing it.
+ * Fails if the list is empty
+ */
+ INLINE_IF_POSSIBLE Slist_node *
+ Slist_pop_head(Slist_head *head)
+ {
+ Slist_node *node;
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ Slist_check(head);
+
+ return node;
+ }
+
+ /*
+ * Insert a new element after the listed one.
+ */
+ INLINE_IF_POSSIBLE void
+ Slist_insert_after(unused_attr Slist_head *head, Slist_node *after,
+ Slist_node *node)
+ {
+ node->next = after->next;
+ after->next = node;
+
+ Slist_check(head);
+ }
+
+ /*
+ * Is the list empty?
+ */
+ INLINE_IF_POSSIBLE bool
+ Slist_is_empty(Slist_head *head)
+ {
+ return head->head.next == NULL;
+ }
+
+ /*
+ * Is there at least one more element in the list?
+ */
+ INLINE_IF_POSSIBLE bool
+ Slist_has_next(unused_attr Slist_head *head,
+ Slist_node *node)
+ {
+ return node->next != NULL;
+ }
+ #endif /* ILIST_USE_DEFINITION */
+
+ #define Slist_get_head(type, membername, ptr) \
+ ( \
+ Slist_is_empty(ptr) ? NULL : \
+ ilist_container(type, membername, (ptr).next) \
+ )
+
+ #define Slist_get_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+ #define Slist_foreach(name, ptr) \
+ for (name = (ptr)->head.next; name != NULL; name = name->next)
+
+ #define Slist_foreach_modify(name, nxt, ptr) \
+ for (name = (ptr)->head.next, nxt = name ? name->next : NULL; \
+ name != NULL; \
+ name = nxt, nxt = name ? name->next : NULL)
+
+ /*
+ * Get rid of ILIST_USE_DEFINITION; it's not interesting outside this file.
+ */
+ #ifdef USE_INLINE
+ #undef ILIST_USE_DEFINITION
+ #endif
+
+ #endif /* ILIST_H */
diff --git a/src/tools/pgindenindex a831a1e..0aa3e3c 100644
On Thu, Sep 6, 2012 at 12:09 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
Here's a prettified version of this stuff. I found one bug in the macro
ilist_s_head: the test was reversed. Also, curiously, the macro had the
same name as the struct, so I renamed the macro. I take it you haven't
used this macro, so maybe it shouldn't be there at all? Or maybe I
completely misread what the macro is supposed to do.I also renamed all the structs and functions by changing ilist_s_foo to
Slist_foo. Similarly for ilist_d_foo. This is all mechanical so any
subsequent patch should be trivial to refresh for this change.
I think this is a good direction, but why not just slist_foo and
dlist_foo? I don't see much value in capitalizing the first letter.
It's not like it's the beginning of a word or anything. Plus, that
way the new stuff will be more obviously different from Dllist, which
it will (I think) replace.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Hi Alvaro,
Thanks for the review!
On Thursday, September 06, 2012 06:09:35 PM Alvaro Herrera wrote:
Here's a prettified version of this stuff. I found one bug in the macro
ilist_s_head: the test was reversed.
Oh, good catch. I had only used the _unchecked version because my code checked
that there are elements just some lines before that...
Also, curiously, the macro had the same name as the struct, so I renamed the
macro. I take it you haven't used this macro, so maybe it shouldn't be
there at all? Or maybe I completely misread what the macro is supposed to do.
According to my patches here that got introduced by me whe renaming
_front/back to _head/tail according to Roberts wishes. Sorry for that.
I also renamed all the structs and functions by changing ilist_s_foo to
Slist_foo. Similarly for ilist_d_foo. This is all mechanical so any
subsequent patch should be trivial to refresh for this change.
Ok. I concur with robert that a lower case first letter might be better
readable but again, I don't really care that much.
I think README.ilist (which is what you had in the comment at the top of
ilist.h) should be heavily expanded. I don't find it at all clear.
Hm. I agree :(. Let me have a go when you have a state you find acceptable
otherwise...
There were other cosmetic changes, but the implementation is pretty much
the same you submitted.
Good.
I didn't look at the other patch you posted, replacing dllist.c usage;
will do that next to verify that the list implementation works.
Thanks!
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Here's an updated version of both patches, as well as a third patch that
converts the cc_node list link in catcache.c into an slist.
There are very few changes here; in ilist.h a singleton slist was being
considered empty. Andres reported this to me privately. One other
change is that in catcache.c we no longer compute a new HASH_INDEX on a
CatCTup in order to remove it from its list; instead we store a pointer
to the list in the element itself. We weren't able to measure any
difference between these two approaches to the problem, so we chose the
approach that hasn't been previously vetoed -- see
http://archives.postgresql.org/message-id/2852.1174575239%40sss.pgh.pa.us
I also addressed the unused_attr thingy by taking it out and having the
non-debug version emit a cast to void of the argument.
I think I would get this committed during CF2, and then have a look at
changing some uses of SHM_QUEUE with ilists too.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
0001-initial-ilist-stuff-from-Andres.patchapplication/octet-stream; name=0001-initial-ilist-stuff-from-Andres.patchDownload
From 5f72be6b33b43f9615700305e469e3e3698417dd Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Tue, 4 Sep 2012 19:07:38 -0300
Subject: [PATCH 1/3] initial ilist stuff from Andres
---
src/backend/lib/Makefile | 2 +-
src/backend/lib/README.ilist | 43 ++++
src/backend/lib/ilist.c | 115 +++++++++++
src/include/lib/ilist.h | 454 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 613 insertions(+), 1 deletions(-)
create mode 100644 src/backend/lib/README.ilist
create mode 100644 src/backend/lib/ilist.c
create mode 100644 src/include/lib/ilist.h
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 2e1061e..c94297a 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS = dllist.o stringinfo.o
+OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/README.ilist b/src/backend/lib/README.ilist
new file mode 100644
index 0000000..3a3d8a8
--- /dev/null
+++ b/src/backend/lib/README.ilist
@@ -0,0 +1,43 @@
+his is intended to be used in the following manner:
+
+#include "lib/ilist.h"
+
+typedef struct something_in_a_list
+{
+ int value;
+ int somethingelse;
+ Dlist_head values;
+} something_in_a_list;
+
+typedef struct value_in_a_list
+{
+ int data;
+ int somethingelse;
+ Dlist_node list_node;
+} value_in_a_list;
+
+something_in_a_list *somelist = get_blarg();
+
+Dlist_push_head(somelist, &get_value_in_a_list()->list_node);
+...
+Dlist_push_head(somelist, &get_value_in_a_list()->list_node);
+...
+...
+Dlist_node *node,
+ *next;
+
+Dlist_foreach(node, somelist)
+{
+ value_in_a_list *val = ilist_container(value_in_a_list, list_node, node);
+ do_something_with_val(val);
+}
+
+Dlist_foreach_modify(node, next, somelist)
+{
+ /*
+ * here we can inspect the node and maybe delete it. Any other
+ * manipulation is forbidden.
+ */
+ if (node is to be deleted)
+ Dlist_delete(node);
+}
diff --git a/src/backend/lib/ilist.c b/src/backend/lib/ilist.c
new file mode 100644
index 0000000..d8bd4f3
--- /dev/null
+++ b/src/backend/lib/ilist.c
@@ -0,0 +1,115 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+/*
+ * If inlines are in use, include the header normally which will get us only
+ * the function definitions as inlines. But if inlines aren't available,
+ * include the header with ILIST_USE_DEFINITION defined; this makes it pour in
+ * regular (not inline) function declarations and the corresponding (non
+ * inline) definitions.
+ */
+#ifndef USE_INLINE
+#define ILIST_USE_DEFINITION
+#endif
+#include "lib/ilist.h"
+
+/*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+void
+slist_delete(slist_head *head, slist_node *node)
+{
+ slist_node *last = &head->head;
+ slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+#ifdef USE_ASSERT_CHECKING
+ found = true;
+#endif
+ break;
+ }
+ last = cur;
+ }
+
+ slist_check(head);
+ Assert(found);
+}
+
+#ifdef ILIST_DEBUG
+/*
+ * Verify integrity of a doubly linked list
+ */
+void
+dlist_check(dlist_head *head)
+{
+ dlist_node *cur;
+
+ if (!head || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (!(cur) ||
+ !(cur->next) ||
+ !(cur->prev) ||
+ !(cur->prev->next == cur) ||
+ !(cur->next->prev == cur))
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+}
+
+/*
+ * Verify integrity of a singly linked list
+ */
+void
+slist_check(slist_head *head)
+{
+ slist_node *cur;
+
+ if (!head || !(&head->head))
+ elog(ERROR, "singly linked list head is not properly initialized");
+
+ /*
+ * there isn't much we can test in a singly linked list except that its
+ * really circular
+ */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ if (!cur)
+ elog(ERROR, "singly linked list is corrupted");
+}
+
+#endif /* ILIST_DEBUG */
diff --git a/src/include/lib/ilist.h b/src/include/lib/ilist.h
new file mode 100644
index 0000000..2c29902
--- /dev/null
+++ b/src/include/lib/ilist.h
@@ -0,0 +1,454 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure. Also, they are always circularly linked, even
+ * when empty; this enables many manipulations to be done without branches,
+ * which is beneficial performance-wise.
+ *
+ * See src/backend/lib/README.ilist for intended usage.
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+#ifndef ILIST_H
+#define ILIST_H
+
+/*
+ * enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even with --enable-cassert.
+*/
+#undef ILIST_DEBUG
+
+/*
+ * struct to embed in other structs that need to be part of a doubly linked
+ * list.
+ */
+typedef struct dlist_node dlist_node;
+struct dlist_node
+{
+ dlist_node *prev;
+ dlist_node *next;
+};
+
+/*
+ * Head of a doubly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty.
+ */
+typedef struct dlist_head
+{
+ dlist_node head;
+} dlist_head;
+
+/*
+ * struct to embed in other structs that need to be part of a singly linked
+ * list.
+ */
+typedef struct slist_node slist_node;
+struct slist_node
+{
+ slist_node *next;
+};
+
+/*
+ * Head of a singly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty.
+ */
+typedef struct slist_head
+{
+ slist_node head;
+} slist_head;
+
+#ifdef USE_INLINE
+#define INLINE_IF_POSSIBLE static inline
+#define ILIST_USE_DEFINITION
+#else
+#define INLINE_IF_POSSIBLE
+
+/* Prototypes for functions we want to be inlined if possible */
+extern void dlist_init(dlist_head *head);
+extern void dlist_push_head(dlist_head *head, dlist_node *node);
+extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+extern void dlist_insert_after(dlist_head *head,
+ dlist_node *after, dlist_node *node);
+extern void dlist_insert_before(dlist_head *head,
+ dlist_node *before, dlist_node *node);
+extern void dlist_delete(dlist_head *head, dlist_node *node);
+extern dlist_node *dlist_pop_head(dlist_head *head);
+extern void dlist_move_head(dlist_head *head, dlist_node *node);
+extern bool dlist_has_next(dlist_head *head, dlist_node *node);
+extern bool dlist_has_prev(dlist_head *head, dlist_node *node);
+extern bool dlist_is_empty(dlist_head *head);
+extern void dlist_check(dlist_head *head);
+
+extern void slist_init(slist_head *head);
+extern void slist_push_head(slist_head *head, slist_node *node);
+extern slist_node *slist_pop_head(slist_head *head);
+extern void slist_insert_after(slist_head *head,
+ slist_node *after, slist_node *node);
+extern bool slist_is_empty(slist_head *head);
+extern bool slist_has_next(slist_head *head,
+ slist_node *node);
+#endif /* USE_INLINE */
+
+/* These functions are too big to be inline, so they are in the C file always */
+extern void slist_delete(slist_head *head, slist_node *node);
+
+#ifdef ILIST_DEBUG
+extern void dlist_check(dlist_head *head);
+extern void slist_check(slist_head *head);
+#else
+/*
+ * These seemingly useless casts to void are here to keep the compiler quiet
+ * about the argument being unused in many functions in a non-debug compile,
+ * in which functions the only point of passing the list head pointer is to be
+ * able to run these checks.
+ */
+#define dlist_check(head) (void) (head)
+#define slist_check(head) (void) (head)
+#endif /* ILIST_DEBUG */
+
+/*
+ * Return a pointer to the struct 'type' when the passed 'ptr' points to the
+ * element 'membername'.
+ */
+#define ilist_container(type, membername, ptr) \
+ ((type *)((char *)(ptr) - offsetof(type, membername)))
+
+/*
+ * The following function definitions are only used if inlining is supported by
+ * the compiler, or when included from a file that explicitly declares
+ * ILIST_USE_DEFINITION.
+ */
+#ifdef ILIST_USE_DEFINITION
+
+/*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+INLINE_IF_POSSIBLE void
+dlist_init(dlist_head *head)
+{
+ head->head.next = head->head.prev = &head->head;
+
+ dlist_check(head);
+}
+
+/*
+ * inserts a node at the beginning of the list
+ */
+INLINE_IF_POSSIBLE void
+dlist_push_head(dlist_head *head, dlist_node *node)
+{
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+}
+
+/*
+ * inserts a node at the end of the list
+ */
+INLINE_IF_POSSIBLE void
+dlist_push_tail(dlist_head *head, dlist_node *node)
+{
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+}
+
+/*
+ * insert a node after another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+dlist_insert_after(dlist_head *head, dlist_node *after,
+ dlist_node *node)
+{
+ dlist_check(head);
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ dlist_check(head);
+}
+
+/*
+ * insert a node after another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+dlist_insert_before(dlist_head *head, dlist_node *before,
+ dlist_node *node)
+{
+ dlist_check(head);
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ dlist_check(head);
+}
+
+/*
+ * deletes a node from a list
+ */
+INLINE_IF_POSSIBLE void
+dlist_delete(dlist_head *head, dlist_node *node)
+{
+ dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ dlist_check(head);
+}
+
+/*
+ * deletes the first node from a list or returns NULL
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_pop_head(dlist_head *head)
+{
+ dlist_node *ret;
+
+ if (&head->head == head->head.next)
+ return NULL;
+
+ ret = head->head.next;
+ dlist_delete(head, head->head.next);
+ return ret;
+}
+
+/*
+ * moves an element (that has to be in the list) to the head of the list
+ */
+INLINE_IF_POSSIBLE void
+dlist_move_head(dlist_head *head, dlist_node *node)
+{
+ /* fast path if it's already at the head */
+ if (&head->head == node)
+ return;
+
+ dlist_delete(head, node);
+ dlist_push_head(head, node);
+
+ dlist_check(head);
+}
+
+/*
+ * Check whether the passed node is the last element in the list
+ */
+INLINE_IF_POSSIBLE bool
+dlist_has_next(dlist_head *head, dlist_node *node)
+{
+ return node->next != &head->head;
+}
+
+/*
+ * Check whether the passed node is the first element in the list
+ */
+INLINE_IF_POSSIBLE bool
+dlist_has_prev(dlist_head *head, dlist_node *node)
+{
+ return node->prev != &head->head;
+}
+
+/*
+ * Check whether the list is empty.
+ */
+INLINE_IF_POSSIBLE bool
+dlist_is_empty(dlist_head *head)
+{
+ return head->head.next == &head->head;
+}
+#endif /* ILIST_USE_DEFINITION */
+
+/*
+ * Return the value of first element in the list if there is one, return NULL
+ * otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define dlist_get_head(type, membername, ptr) \
+( \
+ (&((ptr)->head) == (ptr)->head.next) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.next) \
+)
+
+/*
+ * Return the value of the first element. This will return corrupt data if
+ * there is no element in the list.
+ */
+#define dlist_get_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+/*
+ * Return the value of the last element in the list if there is one, return
+ * NULL otherwise.
+ *
+ * ptr *will* be evaluated multiple times.
+ */
+#define dlist_get_tail(type, membername, ptr) \
+( \
+ (&((ptr)->head) == (ptr)->head.prev) ? \
+ NULL : ilist_container(type, membername, (ptr)->head.prev) \
+)
+
+/*
+ * Iterate through the list storing the current element in the variable
+ * 'name'. 'ptr' will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define dlist_foreach(name, ptr) \
+ for (name = (ptr)->head.next; name != &(ptr)->head; name = name->next)
+
+/*
+ * Iterate through the list storing the current element in the variable 'name'
+ * and also storing the next list element in the variable 'nxt'.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+#define dlist_foreach_modify(name, nxt, ptr) \
+ for (name = (ptr)->head.next, nxt = name->next; \
+ name != &(ptr)->head; \
+ name = nxt, nxt = name->next)
+
+/*
+ * Iterate through the list in reverse order storing the current element in the
+ * variable 'name'. 'ptr' will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define dlist_reverse_foreach(name, ptr) \
+ for (name = (ptr)->head.prev; name != &(ptr)->head; name = name->prev)
+
+
+#ifdef ILIST_USE_DEFINITION
+
+/*
+ * Initialize a singly linked list.
+ */
+INLINE_IF_POSSIBLE void
+slist_init(slist_head *head)
+{
+ head->head.next = NULL;
+
+ slist_check(head);
+}
+
+/*
+ * Install a new element as head of the list, pushing the original head to the
+ * second position.
+ */
+INLINE_IF_POSSIBLE void
+slist_push_head(slist_head *head, slist_node *node)
+{
+ node->next = head->head.next;
+ head->head.next = node;
+
+ slist_check(head);
+}
+
+/*
+ * Returns the first element of the list, removing it.
+ * Fails if the list is empty
+ */
+INLINE_IF_POSSIBLE slist_node *
+slist_pop_head(slist_head *head)
+{
+ slist_node *node;
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ slist_check(head);
+
+ return node;
+}
+
+/*
+ * Insert a new element after the listed one.
+ */
+INLINE_IF_POSSIBLE void
+slist_insert_after(slist_head *head, slist_node *after,
+ slist_node *node)
+{
+ node->next = after->next;
+ after->next = node;
+
+ slist_check(head);
+}
+
+/*
+ * Is the list empty?
+ */
+INLINE_IF_POSSIBLE bool
+slist_is_empty(slist_head *head)
+{
+ slist_check(head);
+
+ return head->head.next == NULL;
+}
+
+/*
+ * Is there at least one more element in the list?
+ */
+INLINE_IF_POSSIBLE bool
+slist_has_next(slist_head *head,
+ slist_node *node)
+{
+ slist_check(head);
+
+ return node->next != NULL;
+}
+#endif /* ILIST_USE_DEFINITION */
+
+#define slist_get_head(type, membername, ptr) \
+( \
+ slist_is_empty(ptr) ? NULL : \
+ ilist_container(type, membername, (ptr).next) \
+)
+
+#define slist_get_head_unchecked(type, membername, ptr) \
+ ilist_container(type, membername, (ptr)->head.next)
+
+/*
+ * Iterate through the list storing the current element in the variable
+ * 'name'. 'ptr' will be evaluated repeatedly.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define slist_foreach(name, ptr) \
+ for (name = (ptr)->head.next; name != NULL; name = name->next)
+
+/*
+ * Iterate through the list storing the current element in the variable 'name'
+ * and also storing the next list element in the variable 'nxt'.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+#define slist_foreach_modify(name, nxt, ptr) \
+ for (name = (ptr)->head.next, nxt = name ? name->next : NULL; \
+ name != NULL; \
+ name = nxt, nxt = name ? name->next : NULL)
+
+#endif /* ILIST_H */
--
1.7.2.5
0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchapplication/octet-stream; name=0002-Remove-usage-of-lib-dllist.h-and-replace-it-by-the-n.patchDownload
From 5d2e23c6dc346b9f277869f4e4f1e048faaa574d Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 26 Jun 2012 19:53:24 +0200
Subject: [PATCH 2/2] Remove usage of lib/dllist.h and replace it by the new
lib/ilist.h interface
---
src/backend/postmaster/autovacuum.c | 91 +++++++++++++-----------------
src/backend/postmaster/postmaster.c | 54 +++++++++---------
src/backend/utils/cache/catcache.c | 106 +++++++++++++++++------------------
src/include/utils/catcache.h | 14 ++---
4 files changed, 125 insertions(+), 140 deletions(-)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index dade5cc..1f0886c 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -76,6 +76,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -149,6 +150,7 @@ typedef struct avl_dbase
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ ilist_d_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
@@ -256,7 +258,7 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
/* the database list in the launcher, and the context that contains it */
-static Dllist *DatabaseList = NULL;
+static ilist_d_head DatabaseList;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
@@ -403,6 +405,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* Identify myself via ps */
init_ps_display("autovacuum launcher process", "", "", "");
+ /* initialize to be empty */
+ ilist_d_init(&DatabaseList);
+
ereport(LOG,
(errmsg("autovacuum launcher started")));
@@ -505,7 +510,7 @@ AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
- DatabaseList = NULL;
+ ilist_d_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
@@ -573,7 +578,7 @@ AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
- Dlelem *elem;
+ avl_dbase *avdb;
int rc;
/*
@@ -735,11 +740,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
- elem = DLGetTail(DatabaseList);
- if (elem != NULL)
+ avdb = ilist_d_head(avl_dbase, adl_node, &DatabaseList);
+ if (avdb != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
-
/*
* launch a worker if next_worker is right now or it is in the
* past
@@ -780,7 +783,7 @@ AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
- Dlelem *elem;
+ avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
@@ -793,9 +796,8 @@ launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
- else if ((elem = DLGetTail(DatabaseList)) != NULL)
+ else if ((avdb = ilist_d_tail(avl_dbase, adl_node, &DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
@@ -864,6 +866,7 @@ rebuild_database_list(Oid newdb)
int score;
int nelems;
HTAB *dbhash;
+ ilist_d_node *elem;
/* use fresh stats */
autovac_refresh_stats();
@@ -924,36 +927,28 @@ rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
- if (DatabaseList != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- Dlelem *elem;
-
- elem = DLGetHead(DatabaseList);
- while (elem != NULL)
- {
- avl_dbase *avdb = DLE_VAL(elem);
- avl_dbase *db;
- bool found;
- PgStat_StatDBEntry *entry;
-
- elem = DLGetSucc(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
+ avl_dbase *db;
+ bool found;
+ PgStat_StatDBEntry *entry;
- /*
- * skip databases with no stat entries -- in particular, this gets
- * rid of dropped databases
- */
- entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
- if (entry == NULL)
- continue;
+ /*
+ * skip databases with no stat entries -- in particular, this gets
+ * rid of dropped databases
+ */
+ entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
+ if (entry == NULL)
+ continue;
- db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
+ db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
- if (!found)
- {
- /* hash_search already filled in the key */
- db->adl_score = score++;
- /* next_worker is filled in later */
- }
+ if (!found)
+ {
+ /* hash_search already filled in the key */
+ db->adl_score = score++;
+ /* next_worker is filled in later */
}
}
@@ -984,7 +979,7 @@ rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
- DatabaseList = DLNewList();
+ ilist_d_init(&DatabaseList);
if (nelems > 0)
{
@@ -1026,15 +1021,13 @@ rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
- DLAddHead(DatabaseList, elem);
+ ilist_d_push_head(&DatabaseList, &db->adl_node);
}
}
@@ -1144,7 +1137,7 @@ do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
- Dlelem *elem;
+ ilist_d_node *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
@@ -1176,11 +1169,10 @@ do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
- while (elem != NULL)
+ ilist_d_reverse_foreach(elem, &DatabaseList)
{
- avl_dbase *dbp = DLE_VAL(elem);
+ avl_dbase *dbp = ilist_container(avl_dbase, adl_node, elem);
if (dbp->adl_datid == tmp->adw_datid)
{
@@ -1197,7 +1189,6 @@ do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
@@ -1271,7 +1262,7 @@ static void
launch_worker(TimestampTz now)
{
Oid dbid;
- Dlelem *elem;
+ ilist_d_node *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
@@ -1280,10 +1271,9 @@ launch_worker(TimestampTz now)
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
- elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
- while (elem != NULL)
+ ilist_d_foreach(elem, &DatabaseList)
{
- avl_dbase *avdb = DLE_VAL(elem);
+ avl_dbase *avdb = ilist_container(avl_dbase, adl_node, elem);
if (avdb->adl_datid == dbid)
{
@@ -1294,10 +1284,9 @@ launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
- DLMoveToFront(elem);
+ ilist_d_move_head(&DatabaseList, elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 913734f..36e9b0b 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -95,7 +95,7 @@
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
@@ -145,10 +145,10 @@ typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
- Dlelem elem; /* list link in BackendList */
+ ilist_d_node elem; /* list link in BackendList */
} Backend;
-static Dllist *BackendList;
+static ilist_d_head BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
@@ -976,7 +976,7 @@ PostmasterMain(int argc, char *argv[])
/*
* Initialize the list of active backends.
*/
- BackendList = DLNewList();
+ ilist_d_init(&BackendList);
/*
* Initialize pipe (or process handle on Windows) that allows children to
@@ -1797,7 +1797,7 @@ processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
- Dlelem *curr;
+ ilist_d_node *curr;
#else
int i;
#endif
@@ -1811,9 +1811,9 @@ processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
@@ -2591,8 +2591,7 @@ static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
- Dlelem *curr;
-
+ ilist_d_node *curr, *next;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
/*
@@ -2623,9 +2622,9 @@ CleanupBackend(int pid,
return;
}
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->pid == pid)
{
@@ -2644,7 +2643,7 @@ CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_delete(&BackendList, curr);
free(bp);
break;
}
@@ -2661,7 +2660,7 @@ CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
- Dlelem *curr,
+ ilist_d_node *curr,
*next;
Backend *bp;
@@ -2677,10 +2676,10 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
- for (curr = DLGetHead(BackendList); curr; curr = next)
+ ilist_d_foreach_modify(curr, next, &BackendList)
{
- next = DLGetSucc(curr);
- bp = (Backend *) DLE_VAL(curr);
+ bp = ilist_container(Backend, elem, curr);
+
if (bp->pid == pid)
{
/*
@@ -2693,7 +2692,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ ilist_d_delete(&BackendList, curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
@@ -3056,7 +3055,7 @@ PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
- if (DLGetHead(BackendList) == NULL &&
+ if (ilist_d_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
@@ -3182,12 +3181,12 @@ signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
bool signaled = false;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -3325,8 +3324,8 @@ BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_head(&BackendList, &bn->elem);
+
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
@@ -4422,12 +4421,12 @@ PostmasterRandom(void)
static int
CountChildren(int target)
{
- Dlelem *curr;
+ ilist_d_node *curr;
int cnt = 0;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ ilist_d_foreach(curr, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = ilist_container(Backend, elem, curr);
if (bp->dead_end)
continue;
@@ -4606,8 +4605,7 @@ StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ ilist_d_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 0307b96..efe34d9 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -353,6 +353,8 @@ CatCachePrintStats(int code, Datum arg)
static void
CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
{
+ Index hashIndex;
+
Assert(ct->refcount == 0);
Assert(ct->my_cache == cache);
@@ -369,7 +371,9 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
}
/* delink from linked list */
- DLRemove(&ct->cache_elem);
+ hashIndex = HASH_INDEX(ct->hash_value, cache->cc_nbuckets);
+
+ ilist_d_delete(&cache->cc_bucket[hashIndex], &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
@@ -412,7 +416,7 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
- DLRemove(&cl->cache_elem);
+ ilist_d_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
@@ -452,8 +456,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
+ ilist_d_head *bucket;
if (cacheId != ccp->id)
continue;
@@ -468,11 +473,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -484,12 +487,10 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
-
- for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
+ bucket = &ccp->cc_bucket[hashIndex];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (hashValue == ct->hash_value)
{
@@ -561,13 +562,13 @@ AtEOXact_CatCache(bool isCommit)
for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
- Dlelem *elt;
+ ilist_d_node *elt;
int i;
/* Check CatCLists */
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
@@ -577,11 +578,10 @@ AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&ccp->cc_bucket[i]);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_head *bucket = &ccp->cc_bucket[i];
+ ilist_d_foreach(elt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
@@ -604,16 +604,14 @@ AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
- Dlelem *elt,
+ ilist_d_node *elt,
*nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
- for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
+ ilist_d_foreach_modify(elt, nextelt, &cache->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = ilist_container(CatCList, cache_elem, elt);
if (cl->refcount > 0)
cl->dead = true;
@@ -624,11 +622,10 @@ ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
+ ilist_d_head *bucket = &cache->cc_bucket[i];
+ ilist_d_foreach_modify(elt, nextelt, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
@@ -770,10 +767,8 @@ InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
- cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
+ cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(ilist_d_node));
/*
* initialize the cache's relation information for the relation
@@ -792,6 +787,12 @@ InitCatCache(int id,
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ ilist_d_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++){
+ ilist_d_init(&cp->cc_bucket[i]);
+ }
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
@@ -1060,7 +1061,8 @@ SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
- Dlelem *elt;
+ ilist_d_node *elt;
+ ilist_d_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
@@ -1094,13 +1096,11 @@ SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
- {
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket){
bool res;
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead)
continue; /* ignore dead entries */
@@ -1125,7 +1125,7 @@ SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
- DLMoveToFront(&ct->cache_elem);
+ ilist_d_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
@@ -1340,7 +1340,7 @@ SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
- Dlelem *elt;
+ ilist_d_node *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
@@ -1382,13 +1382,11 @@ SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
- for (elt = DLGetHead(&cache->cc_lists);
- elt;
- elt = DLGetSucc(elt))
+ ilist_d_foreach(elt, &cache->cc_lists)
{
bool res;
- cl = (CatCList *) DLE_VAL(elt);
+ cl = ilist_container(CatCList, cache_elem, elt);
if (cl->dead)
continue; /* ignore dead entries */
@@ -1416,7 +1414,7 @@ SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
- DLMoveToFront(&cl->cache_elem);
+ ilist_d_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
@@ -1468,7 +1466,8 @@ SearchCatCacheList(CatCache *cache,
{
uint32 hashValue;
Index hashIndex;
-
+ bool found = false;
+ ilist_d_head *bucket;
/*
* See if there's an entry for this tuple already.
*/
@@ -1476,11 +1475,10 @@ SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
+ bucket = &cache->cc_bucket[hashIndex];
+ ilist_d_foreach(elt, bucket)
{
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = ilist_container(CatCTup, cache_elem, elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
@@ -1498,10 +1496,11 @@ SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
- if (elt == NULL)
+ if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
@@ -1564,7 +1563,6 @@ SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
@@ -1587,7 +1585,7 @@ SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
- DLAddHead(&cache->cc_lists, &cl->cache_elem);
+ ilist_d_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
@@ -1664,14 +1662,14 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
- DLInitElem(&ct->cache_elem, (void *) ct);
+
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
- DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
+ ilist_d_push_head(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index d91700a..20f2fa8 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -22,7 +22,7 @@
#include "access/htup.h"
#include "access/skey.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "utils/relcache.h"
/*
@@ -51,7 +51,7 @@ typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
- Dllist cc_lists; /* list of CatCList structs */
+ ilist_d_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
@@ -66,7 +66,7 @@ typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
- Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
+ ilist_d_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
@@ -77,11 +77,11 @@ typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
- * Each tuple in a cache is a member of a Dllist that stores the elements
- * of its hash bucket. We keep each Dllist in LRU order to speed repeated
+ * Each tuple in a cache is a member of a ilist_d that stores the elements
+ * of its hash bucket. We keep each ilist_d in LRU order to speed repeated
* lookups.
*/
- Dlelem cache_elem; /* list member of per-bucket list */
+ ilist_d_node cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
@@ -139,7 +139,7 @@ typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
- Dlelem cache_elem; /* list member of per-catcache list */
+ ilist_d_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--
1.7.10.rc3.3.g19a6c.dirty
0003-convert-catcache.h-cc_next-into-an-slist.patchapplication/octet-stream; name=0003-convert-catcache.h-cc_next-into-an-slist.patchDownload
From d670675371644e8905944e65b56b967d5b01f572 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Fri, 7 Sep 2012 13:24:52 -0300
Subject: [PATCH 3/3] convert catcache.h cc_next into an slist
---
src/backend/utils/cache/catcache.c | 40 ++++++++++++++++++++++-------------
src/include/utils/catcache.h | 4 +-
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 6ebb3ce..14ddcb0 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -291,7 +291,7 @@ CatalogCacheComputeTupleHashValue(CatCache *cache, HeapTuple tuple)
static void
CatCachePrintStats(int code, Datum arg)
{
- CatCache *cache;
+ slist_node *elem;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
@@ -300,8 +300,10 @@ CatCachePrintStats(int code, Datum arg)
long cc_lsearches = 0;
long cc_lhits = 0;
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(elem, &CacheHdr->ch_caches)
{
+ CatCache *cache = ilist_container(CatCache, cc_next, elem);
+
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
@@ -441,19 +443,20 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
- CatCache *ccp;
+ slist_node *elem;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(elem, &CacheHdr->ch_caches)
{
Index hashIndex;
dlist_node *elt,
*nextelt;
dlist_head *bucket;
+ CatCache *ccp = ilist_container(CatCache, cc_next, elem);
if (cacheId != ccp->id)
continue;
@@ -553,10 +556,11 @@ AtEOXact_CatCache(bool isCommit)
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
- CatCache *ccp;
+ slist_node *elem;
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(elem, &(CacheHdr->ch_caches))
{
+ CatCache *ccp = ilist_container(CatCache, cc_next, elem);
dlist_node *elt;
int i;
@@ -648,12 +652,16 @@ ResetCatalogCache(CatCache *cache)
void
ResetCatalogCaches(void)
{
- CatCache *cache;
+ slist_node *elem;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(elem, &CacheHdr->ch_caches)
+ {
+ CatCache *cache = ilist_container(CatCache, cc_next, elem);
+
ResetCatalogCache(cache);
+ }
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
@@ -674,12 +682,14 @@ ResetCatalogCaches(void)
void
CatalogCacheFlushCatalog(Oid catId)
{
- CatCache *cache;
+ slist_node *elem;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(elem, &(CacheHdr->ch_caches))
{
+ CatCache *cache = ilist_container(CatCache, cc_next, elem);
+
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
@@ -754,7 +764,7 @@ InitCatCache(int id,
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
- CacheHdr->ch_caches = NULL;
+ slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
@@ -798,8 +808,7 @@ InitCatCache(int id,
/*
* add completed cache to top of group header's list
*/
- cp->cc_next = CacheHdr->ch_caches;
- CacheHdr->ch_caches = cp;
+ slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
/*
* back to the old context before we return...
@@ -1782,7 +1791,7 @@ PrepareToInvalidateCacheTuple(Relation relation,
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
- CatCache *ccp;
+ slist_node *elem;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
@@ -1805,10 +1814,11 @@ PrepareToInvalidateCacheTuple(Relation relation,
* ----------------
*/
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(elem, &(CacheHdr->ch_caches))
{
uint32 hashvalue;
Oid dbid;
+ CatCache *ccp = ilist_container(CatCache, cc_next, elem);
if (ccp->cc_reloid != reloid)
continue;
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index 1e59ea4..cc6dab2 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -37,7 +37,7 @@
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
- struct catcache *cc_next; /* link to next catcache */
+ slist_node cc_next; /* list link */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
@@ -154,7 +154,7 @@ typedef struct catclist
typedef struct catcacheheader
{
- CatCache *ch_caches; /* head of list of CatCache structs */
+ slist_head ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
--
1.7.2.5
Excerpts from Alvaro Herrera's message of vie sep 14 14:22:18 -0300 2012:
Here's an updated version of both patches, as well as a third patch that
converts the cc_node list link in catcache.c into an slist.
One thing I would like more input in, is whether people think it's
worthwhile to split dlists and slists into separate files. Thus far
this has been mentioned by three people independently.
Another question is whether ilist_container() should actually be a more
general macro "containerof" defined in c.h. (ISTM it would be necessary
to have this macro if we want to split into two files; that way we don't
need to have two macros dlist_container and slist_container with
identical definition, or alternatively a third file that defines just
ilist_container)
Third question is about the INLINE_IF_POSSIBLE business as commented by
Peter. It seems to me that the simple technique used here to avoid
having two copies of the source could be used by memcxt.c, list.c,
sortsupport.c as well (maybe clean up fastgetattr too).
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Here's an updated version of both patches, as well as a third patch that
converts the cc_node list link in catcache.c into an slist.
There's a lot of stuff here that seems rather unfortunate and/or sloppy.
Does it even compile? The 0002 patch refers to a typedef ilist_d_head
that I don't see defined anywhere. (It would be good to shorten that
name by a couple of characters anyway, for tab-stop alignment reasons.)
The documentation (such as it is) claims that the lists are circularly
linked, which doesn't seem to be the case from the code; slist_foreach
for instance certainly won't work if that's true. (As far as the
docs go, I'd get rid of the README file and put the how-to-use-it
comments into the header file, where they have some chance of being
(a) seen and (b) maintained. But first they need to be rewritten.)
The distinction between head and node structs seems rather useless,
and it's certainly notationally tedious. Do we need it?
I find the need for this change quite unfortunate:
@@ -256,7 +258,7 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
/* the database list in the launcher, and the context that contains it */
-static Dllist *DatabaseList = NULL;
+static ilist_d_head DatabaseList;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
@@ -403,6 +405,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* Identify myself via ps */
init_ps_display("autovacuum launcher process", "", "", "");
+ /* initialize to be empty */
+ ilist_d_init(&DatabaseList);
+
ereport(LOG,
(errmsg("autovacuum launcher started")));
Instead let's provide a macro for an empty list value, so that you can
do something like
static ilist_d_head DatabaseList = EMPTY_DLIST;
and not require a new assumption that there will be a convenient place
to initialize static list variables. In general I think it'd be a lot
better if the lists were defined such that all-zeroes is a valid empty
list header, anyway.
The apparently random decisions to make some things inline functions
and other things macros (even though they evaluate their args multiple
times) will come back to bite us. I am much more interested in
unsurprising behavior of these constructs than I am in squeezing out
an instruction or two, so I'm very much against the unsafe macros.
I think we could do without such useless distinctions as slist_get_head
vs slist_get_head_unchecked, too. We've already got Assert and
ILIST_DEBUG, we do not need even more layers of check-or-not
distinctions.
Meanwhile, obvious checks that *should* be made, like slist_pop_head
asserting that there be something to pop, don't seem to be made.
Not a full review, just some things that struck me in a quick scan...
regards, tom lane
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
One thing I would like more input in, is whether people think it's
worthwhile to split dlists and slists into separate files. Thus far
this has been mentioned by three people independently.
They're small enough and similar enough that one header and one .c file
seem like plenty; but I don't really have a strong opinion about it.
Another question is whether ilist_container() should actually be a more
general macro "containerof" defined in c.h. (ISTM it would be necessary
to have this macro if we want to split into two files; that way we don't
need to have two macros dlist_container and slist_container with
identical definition, or alternatively a third file that defines just
ilist_container)
I'd vote for not having that at all, but rather two separate macros
dlist_container and slist_container. If we had a bunch of operations
that could work interchangeably on dlists and slists, it might be worth
having a concept of "ilist" --- but if we only have this, it would be
better to remove the concept from the API altogether.
Third question is about the INLINE_IF_POSSIBLE business as commented by
Peter. It seems to me that the simple technique used here to avoid
having two copies of the source could be used by memcxt.c, list.c,
sortsupport.c as well (maybe clean up fastgetattr too).
Yeah, looks reasonable ... material for a different patch of course.
But that would mean INLINE_IF_POSSIBLE should be defined someplace else,
perhaps c.h. Also, I'm not that thrilled with having the header file
define ILIST_USE_DEFINITION. I suggest that it might be better to do
#if defined(USE_INLINE) || defined(DEFINE_ILIST_FUNCTIONS)
... function decls here ...
#else
... extern decls here ...
#endif
where ilist.c defines DEFINE_ILIST_FUNCTIONS before including the
header.
regards, tom lane
Excerpts from Tom Lane's message of vie sep 14 17:48:35 -0300 2012:
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Here's an updated version of both patches, as well as a third patch that
converts the cc_node list link in catcache.c into an slist.There's a lot of stuff here that seems rather unfortunate and/or sloppy.
Does it even compile? The 0002 patch refers to a typedef ilist_d_head
that I don't see defined anywhere. (It would be good to shorten that
name by a couple of characters anyway, for tab-stop alignment reasons.)
Hm, I might have submitted the wrong 0002 file. Sorry about that. (The
correct file would have the right typedef names and a couple of bugfixes
but it'd be pretty similar to what you read.)
[many useful comments]
Not a full review, just some things that struck me in a quick scan...
Great stuff nonetheless, many thanks. I will see about submitting an
improved version.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Hi,
On Friday, September 14, 2012 10:48:35 PM Tom Lane wrote:
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Here's an updated version of both patches, as well as a third patch that
converts the cc_node list link in catcache.c into an slist.There's a lot of stuff here that seems rather unfortunate and/or sloppy.
Most of that unfortunately my fault not Alvaro's...
The documentation (such as it is) claims that the lists are circularly
linked, which doesn't seem to be the case from the code; slist_foreach
for instance certainly won't work if that's true. (As far as the
docs go, I'd get rid of the README file and put the how-to-use-it
comments into the header file, where they have some chance of being
(a) seen and (b) maintained. But first they need to be rewritten.)
True, only dlist's are circular. The docs were in the header at first, then
somebody voted for moving them ;)
The distinction between head and node structs seems rather useless,
and it's certainly notationally tedious. Do we need it?
I think its useful. It makes the usage in structs its embedded to way much
clearer. The head struct actually has a different meaning than normal node
structs because its there even if the list is empty...
I find the need for this change quite unfortunate:
@@ -256,7 +258,7 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;/* the database list in the launcher, and the context that contains it */ -static Dllist *DatabaseList = NULL; +static ilist_d_head DatabaseList; static MemoryContext DatabaseListCxt = NULL;/* Pointer to my own WorkerInfo, valid on each worker */
@@ -403,6 +405,9 @@ AutoVacLauncherMain(int argc, char *argv[])
/* Identify myself via ps */
init_ps_display("autovacuum launcher process", "", "", "");+ /* initialize to be empty */ + ilist_d_init(&DatabaseList); + ereport(LOG, (errmsg("autovacuum launcher started")));Instead let's provide a macro for an empty list value, so that you can
do something likestatic ilist_d_head DatabaseList = EMPTY_DLIST;
I don't think that can work with dlists because they are circular and need
their pointers to be adjusted. From my POV it seems to be better to keep those
in sync.
and not require a new assumption that there will be a convenient place
to initialize static list variables. In general I think it'd be a lot
better if the lists were defined such that all-zeroes is a valid empty
list header, anyway.
For the dlists thats a major efficiency difference in some use cases.
Unfortunately those are the ones I care about... Due to not needing to check
for NULLs several branches can be removed from the whole thing.
The apparently random decisions to make some things inline functions
and other things macros (even though they evaluate their args multiple
times) will come back to bite us. I am much more interested in
unsurprising behavior of these constructs than I am in squeezing out
an instruction or two, so I'm very much against the unsafe macros.
At the moment the only thing that are macros are things that cannot be
expressed as inline functions because they return the actual contained type
and/or because they contain a for() loop. Do you have a trick in mind to
handle such cases?
Earlier on when talking with Alvaro I mentioned that I would like to add some
more functions that return the [sd]list_node's instead of the contained
elements. Those should be inline functions.
I think we could do without such useless distinctions as slist_get_head
vs slist_get_head_unchecked, too. We've already got Assert and
ILIST_DEBUG, we do not need even more layers of check-or-not
distinctions.
The _unchecked variants remove the check whether the list is already empty and
thus give up some safety for speed. Quite often that check is made before
calling dlist_get_head() or such anyway...
I wondered whether the solution for that would be to remove the variants that
check for an empty list (except an Assert).
Not a full review, just some things that struck me in a quick scan...
Thanks!
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
On Friday, September 14, 2012 10:48:35 PM Tom Lane wrote:
Instead let's provide a macro for an empty list value, so that you can
do something like
static ilist_d_head DatabaseList = EMPTY_DLIST;
I don't think that can work with dlists because they are circular and need
their pointers to be adjusted.
Well, actually, that just brings us to the main point which is: I do not
believe that circular links are a good design choice here. They prevent
the possibility of trivial list initialization, they make plain
iteration over the list more expensive, and you have provided no
evidence that there's any meaningful savings in other operations, much
less enough to justify those disadvantages.
The apparently random decisions to make some things inline functions
and other things macros (even though they evaluate their args multiple
times) will come back to bite us. I am much more interested in
unsurprising behavior of these constructs than I am in squeezing out
an instruction or two, so I'm very much against the unsafe macros.
At the moment the only thing that are macros are things that cannot be
expressed as inline functions because they return the actual contained type
and/or because they contain a for() loop.
I don't really care. If you can't build it without multiple-evaluation
macros, it's too dangerous for a fundamental construct that's meant to
be used all over the place. Time to redesign.
One possible option, though it's a bit restrictive, is to insist that
the list node be the first element of whatever it's embedded in,
eliminating the need for ilist_container altogether. This would not
work if we meant to put these kinds of list links into Node-derived
structs, but I suspect we don't need that. Then for instance (given
the additional choice to not use circular links) dlist_get_head
would degenerate to ((type *) (ptr)->head.next), which eliminates
its multi-evaluation hazard and saves a few instructions as well.
Or if you don't want to do that, dlist_get_head(type, membername, ptr)
could be defined as
((type *) dlist_do_get_head(ptr, offsetof(type, membername)))
where dlist_do_get_head is an inline'able function, eliminating the
multi-evaluation-of-ptr hazard.
regards, tom lane
Hi Tom,
On Saturday, September 15, 2012 07:32:45 AM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
On Friday, September 14, 2012 10:48:35 PM Tom Lane wrote:
Instead let's provide a macro for an empty list value, so that you can
do something like
static ilist_d_head DatabaseList = EMPTY_DLIST;I don't think that can work with dlists because they are circular and
need their pointers to be adjusted.Well, actually, that just brings us to the main point which is: I do not
believe that circular links are a good design choice here. They prevent
the possibility of trivial list initialization, they make plain
iteration over the list more expensive, and you have provided no
evidence that there's any meaningful savings in other operations, much
less enough to justify those disadvantages.
I think I have talked about the reasoning on the list before, but here it
goes: The cases where I primarily used them are cases where the list
*manipulation* is a considerable part of the expense. In those situations
having less branches is beneficial and, to my knowledge, cannot be done in
normal flat lists.
For the initial user of those lists - the slab allocator for postgres which I
intend to finish at some point - the move to circular lists instead of
classical lists was an improvement in the 12-15% range.
Inhowfar do they make iteration more expensive? ptr != head shouldn't be more
expensive than !ptr.
Imo, that leaves initialization where I admit that you have a case. I don't
find it a big problem in practise though.
The apparently random decisions to make some things inline functions
and other things macros (even though they evaluate their args multiple
times) will come back to bite us. I am much more interested in
unsurprising behavior of these constructs than I am in squeezing out
an instruction or two, so I'm very much against the unsafe macros.At the moment the only thing that are macros are things that cannot be
expressed as inline functions because they return the actual contained
type and/or because they contain a for() loop.I don't really care. If you can't build it without multiple-evaluation
macros, it's too dangerous for a fundamental construct that's meant to
be used all over the place. Time to redesign.
Its not like pg doesn't use any other popularly used macros that have multiple
evaluation hazarards. Even in cases where they *could* be replaced by inline
functions without that danger.
One possible option, though it's a bit restrictive, is to insist that
the list node be the first element of whatever it's embedded in,
eliminating the need for ilist_container altogether. This would not
work if we meant to put these kinds of list links into Node-derived
structs, but I suspect we don't need that.
I already had list elements which are in multiple lists at the same time and I
think replacing some of the pg_list.h usages might be a good idea even for
Node derived structures given the List manipulation overhead we have seen in
several profiles.
Then for instance (given the additional choice to not use circular links)
dlist_get_head would degenerate to ((type *) (ptr)->head.next), which
eliminates its multi-evaluation hazard and saves a few instructions as well.
I still fail to see why not using cirular lists removes instructions in such a
situation:
Get the first element in a circular list:
(ptr)->head.next
->head.next is at a constant offset from the start of *ptr, just as a ->first
pointer is.
In iterations like:
for(name = (ptr)->head.next;
name != &(ptr)->head;
name = name->next)
{
}
you have one potentially additional indexed memory access (&ptr->head) for the
whole loop to an address which will normally lie on the same cacheline as the
already accessed ->next pointer.
Or if you don't want to do that, dlist_get_head(type, membername, ptr)
could be defined as
((type *) dlist_do_get_head(ptr, offsetof(type, membername)))
where dlist_do_get_head is an inline'able function, eliminating the
multi-evaluation-of-ptr hazard.
Thats a rather neat idea. Let me play with it for a second, it might make some
of the macros safe, although I don't see how it could work for for() loops.
Just to make that clear, purely accessing the first node can be done without
any macros at al, its just the combination of returning the first node and
getting the contained element that needs to be a macro because of the variadic
type issues (at times, I really wish for c++ style templates...)
I will take a stab at trying to improve Alvaro's current patch wrt to those
issues.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
On Saturday, September 15, 2012 07:32:45 AM Tom Lane wrote:
Well, actually, that just brings us to the main point which is: I do not
believe that circular links are a good design choice here.
I think I have talked about the reasoning on the list before, but here it
goes: The cases where I primarily used them are cases where the list
*manipulation* is a considerable part of the expense. In those situations
having less branches is beneficial and, to my knowledge, cannot be done in
normal flat lists.
For the initial user of those lists - the slab allocator for postgres which I
intend to finish at some point - the move to circular lists instead of
classical lists was an improvement in the 12-15% range.
Let me make my position clear: I will not accept performance as the sole
figure of merit for this datatype. It also has to be easy and reliable
to use, and the current design is a failure on those dimensions.
This question of ease and reliability of use isn't just academic, since
you guys had trouble converting catcache.c without introducing bugs.
That isn't exactly a positive showing for this design.
Furthermore, one datapoint for one use-case (probably measured on only
one CPU architecture) isn't even a very convincing case for the
performance being better. To take a handy example, if we were to
convert dynahash to use dlists, having to initialize each hash bucket
header this way would probably be a pretty substantial cost for a lot
of hashtable use-cases. We have a lot of short-lived dynahash tables.
Inhowfar do they make iteration more expensive? ptr != head shouldn't be more
expensive than !ptr.
That's probably true *as long as the head pointer is available in a
register*. But having to reserve a second register for the loop
mechanics can be a serious loss on register-poor architectures.
This also ties into the other problem, since it's hard to code such
loop control as a macro without multiple evaluation of the list-head
argument. To me that's a show stopper of the first order. I'm not
going to accept a replacement for foreach() that introduces bug hazards
that aren't in foreach().
I don't really care. If you can't build it without multiple-evaluation
macros, it's too dangerous for a fundamental construct that's meant to
be used all over the place. Time to redesign.
Its not like pg doesn't use any other popularly used macros that have multiple
evaluation hazarards.
There are certainly some multiple-evaluation macros, but I don't think
they are associated with core data types. You will not find any in
pg_list.h for instance. I think it's important that these new forms
of list be as easy and reliable to use as List is. I'm willing to trade
off some micro-performance to have that.
regards, tom lane
On Saturday, September 15, 2012 07:21:44 PM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
On Saturday, September 15, 2012 07:32:45 AM Tom Lane wrote:
Well, actually, that just brings us to the main point which is: I do not
believe that circular links are a good design choice here.I think I have talked about the reasoning on the list before, but here it
goes: The cases where I primarily used them are cases where the list
*manipulation* is a considerable part of the expense. In those situations
having less branches is beneficial and, to my knowledge, cannot be done
in normal flat lists.
For the initial user of those lists - the slab allocator for postgres
which I intend to finish at some point - the move to circular lists
instead of classical lists was an improvement in the 12-15% range.Let me make my position clear: I will not accept performance as the sole
figure of merit for this datatype. It also has to be easy and reliable
to use, and the current design is a failure on those dimensions.
This question of ease and reliability of use isn't just academic, since
you guys had trouble converting catcache.c without introducing bugs.
That isn't exactly a positive showing for this design.
Uhm. I had introduced a bug there, true (Maybe Alvaro as well, I can't remember
anything right now). But it was something like getting the tail instead of the
head element due to copy paste. Nothing will be able to protect the code from
me.
Furthermore, one datapoint for one use-case (probably measured on only
one CPU architecture) isn't even a very convincing case for the
performance being better. To take a handy example, if we were to
convert dynahash to use dlists, having to initialize each hash bucket
header this way would probably be a pretty substantial cost for a lot
of hashtable use-cases. We have a lot of short-lived dynahash tables.
What do you think about doing something like:
#define DLIST_INIT(name) {{&name.head, &name.head}}
static dlist_head DatabaseList = DLIST_INIT(DatabaseList);
That works, but obviously the initialization will have to be performed at some
point.
This also ties into the other problem, since it's hard to code such
loop control as a macro without multiple evaluation of the list-head
argument. To me that's a show stopper of the first order. I'm not
going to accept a replacement for foreach() that introduces bug hazards
that aren't in foreach().
What do you think about something like:
typedef struct dlist_iter
{
/*
* Use a union with equivalent storage as dlist_node to make it possible to
* initialize the struct inside a macro without multiple evaluation.
*/
union {
struct {
dlist_node *cur;
dlist_node *end;
};
dlist_node init;
};
} dlist_iter;
typedef struct dlist_mutable_iter
{
union {
struct {
dlist_node *cur;
dlist_node *end;
};
dlist_node init;
};
dlist_node *next;
} dlist_mutable_iter;
#define dlist_iter_foreach(iter, ptr) \
for (iter.init = (ptr)->head; iter.cur != iter.end; \
iter.cur = iter.cur->next)
#define dlist_iter_foreach_modify(iter, ptr) \
for (iter.init = (ptr)->head, iter.next = iter.cur->next; \
iter.cur != iter.end \
iter.cur = iter.next, iter.next = iter.cur->next)
With that and some trivial changes *all* multiple evaluation possibilities are
gone.
(_iter_ in there would go, thats just so I can have both in the same file for
now).
There are certainly some multiple-evaluation macros, but I don't think
they are associated with core data types. You will not find any in
pg_list.h for instance. I think it's important that these new forms
of list be as easy and reliable to use as List is. I'm willing to trade
off some micro-performance to have that.
Just from what I had in open emacs frames without switching buffers when I read
that email:
Min/Max in c.h and about half of the macros in htup.h (I had the 9.1 tree open
at that point)...
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Hi,
On Sunday, September 16, 2012 04:23:14 PM Andres Freund wrote:
What do you think about something like:
typedef struct dlist_iter
{
/*
* Use a union with equivalent storage as dlist_node to make it possible
to * initialize the struct inside a macro without multiple evaluation. */
union {
struct {
dlist_node *cur;
dlist_node *end;
};
dlist_node init;
};
} dlist_iter;typedef struct dlist_mutable_iter
{
union {
struct {
dlist_node *cur;
dlist_node *end;
};
dlist_node init;
};
dlist_node *next;
} dlist_mutable_iter;#define dlist_iter_foreach(iter, ptr) \
for (iter.init = (ptr)->head; iter.cur != iter.end; \
iter.cur = iter.cur->next)#define dlist_iter_foreach_modify(iter, ptr)
\
for (iter.init = (ptr)->head, iter.next = iter.cur->next; \
iter.cur != iter.end \
iter.cur = iter.next, iter.next = iter.cur->next)With that and some trivial changes *all* multiple evaluation possibilities
are gone.(_iter_ in there would go, thats just so I can have both in the same file
for now).
I am thinking whether a macro like:
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#define assert_compatible_types(a, b) _Static_assert( \
__builtin_types_compatible_p(a, __typeof__ (b) ), \
"variable `" #b "` is not compatible to type `" #a "`" )
#else
#define assert_compatible_types(a, b) (void)0
#endif
used like:
#define dlist_iter_foreach(iter, ptr) \
assert_compatible_types(dlist_iter, iter); \
for (iter.init = (ptr)->head; iter.cur != iter.end; \
iter.cur = iter.cur->next)
would be useful.
If you use the wrong type you get an error like:
error: static assertion failed: "variable `iter` is not compatible to type
`dlist_iter`"
Do people think this is something worthwile for some of the macros in pg? At
times the compiler errors that get generated in larger macros can be a bit
confusing and something like that would make it easier to see the originating
error.
I found __builtin_types_compatible while perusing the gcc docs to find whether
there is something like __builtin_constant_p for checking the pureness of an
expression ;)
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Friday, September 14, 2012 10:57:54 PM Tom Lane wrote:
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
One thing I would like more input in, is whether people think it's
worthwhile to split dlists and slists into separate files. Thus far
this has been mentioned by three people independently.They're small enough and similar enough that one header and one .c file
seem like plenty; but I don't really have a strong opinion about it.Another question is whether ilist_container() should actually be a more
general macro "containerof" defined in c.h. (ISTM it would be necessary
to have this macro if we want to split into two files; that way we don't
need to have two macros dlist_container and slist_container with
identical definition, or alternatively a third file that defines just
ilist_container)I'd vote for not having that at all, but rather two separate macros
dlist_container and slist_container. If we had a bunch of operations
that could work interchangeably on dlists and slists, it might be worth
having a concept of "ilist" --- but if we only have this, it would be
better to remove the concept from the API altogether.Third question is about the INLINE_IF_POSSIBLE business as commented by
Peter. It seems to me that the simple technique used here to avoid
having two copies of the source could be used by memcxt.c, list.c,
sortsupport.c as well (maybe clean up fastgetattr too).Yeah, looks reasonable ... material for a different patch of course.
But that would mean INLINE_IF_POSSIBLE should be defined someplace else,
perhaps c.h. Also, I'm not that thrilled with having the header file
define ILIST_USE_DEFINITION. I suggest that it might be better to do#if defined(USE_INLINE) || defined(DEFINE_ILIST_FUNCTIONS)
... function decls here ...
#else
... extern decls here ...
#endifwhere ilist.c defines DEFINE_ILIST_FUNCTIONS before including the
header.
I am preparing a new version of this right now. So, some last ditch questions
are coming up...
The reason I had the header declare DEFINE_ILIST_FUNCTIONS (or rather
ILIST_USE_DEFINITION back then) instead of reusing USE_INLINE directly is that
it makes it easier to locally change a "module" to not inlining which makes
testing the !USE_INLINE case easier. Does anybody think this is worth
something? I have no strong feelings but found it convenient.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
The reason I had the header declare DEFINE_ILIST_FUNCTIONS (or rather
ILIST_USE_DEFINITION back then) instead of reusing USE_INLINE directly is that
it makes it easier to locally change a "module" to not inlining which makes
testing the !USE_INLINE case easier. Does anybody think this is worth
something? I have no strong feelings but found it convenient.
Right offhand it doesn't seem like it really gains that much even for
that use-case. You'd end up editing the include file either way, just
slightly differently.
regards, tom lane
On Saturday, September 29, 2012 01:39:03 AM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
The reason I had the header declare DEFINE_ILIST_FUNCTIONS (or rather
ILIST_USE_DEFINITION back then) instead of reusing USE_INLINE directly is
that it makes it easier to locally change a "module" to not inlining
which makes testing the !USE_INLINE case easier. Does anybody think this
is worth something? I have no strong feelings but found it convenient.Right offhand it doesn't seem like it really gains that much even for
that use-case. You'd end up editing the include file either way, just
slightly differently.
Well, with USE_INLINE you have to recompile the whole backend because you
otherwise easily end up with strange incompatibilities between files.
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
On Saturday, September 29, 2012 01:39:03 AM Tom Lane wrote:
Right offhand it doesn't seem like it really gains that much even for
that use-case. You'd end up editing the include file either way, just
slightly differently.
Well, with USE_INLINE you have to recompile the whole backend because you
otherwise easily end up with strange incompatibilities between files.
Eh? You would anyway, or at least recompile every .o file depending on
that header, if what you want is to inline or de-inline the functions.
There's no magic shortcut for that.
regards, tom lane
Hi,
Current version is available at branch ilist in:
git://git.postgresql.org/git/users/andresfreund/postgres.git
ssh://git@git.postgresql.org/users/andresfreund/postgres.git
Based on Alvaro's last version I made several changes, including:
* naming is now [ds]list_*
* README is split back into the header
* README is rewritten
* much improved comments
* no non-error checking for empty lists anymore, Asserts added everywhere
* no multiple evaluation at all anymore
* introduction of [ds]list_iterator structs
* typechecking added to macros
* DLIST_STATIC_INIT added to initialize list elements at declaration time.
* added some more functions (symetry, new users)
* s/ILIST_USE_DEFINITION/ILIST_DEFINE_FUNCTIONS/
* don't declare ILIST_DEFINE_FUNCTIONS in the header, rely on USE_INLINE
* pgindent compatible styling
Patch 0001 contains a assert_compatible_types(a, b) and a
assert_compatible_types_bool(a, b) macro which I found very useful to make it
harder to misuse the api. I think its generally useful and possibly should be
used in more places.
Opinions?
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
0001-Add-compile-time-checked-assert_compatible_types-mac.patchtext/x-patch; charset=UTF-8; name=0001-Add-compile-time-checked-assert_compatible_types-mac.patchDownload
From 52a9d3740158c00a19d3939dc91c2a88afca3480 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 28 Sep 2012 13:18:26 +0200
Subject: [PATCH 1/4] Add compile time checked assert_compatible_types macro
to c.h
Useful for generating better error messages in other macros.
---
src/include/c.h | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/src/include/c.h b/src/include/c.h
index 50e1ecf..024ed5c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -857,4 +857,31 @@ extern int fdatasync(int fildes);
/* /port compatibility functions */
#include "port.h"
+/*
+ * Macro that makes sure two types are the same at compile time
+ *
+ * Used only to emit better error messages, we support compilers without
+ * support for _Static_assert and without support for
+ * __builtin_types_compatible_p and compound expressions which are a gcc
+ * extensions
+ */
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#define assert_compatible_types(a, b) \
+ do { \
+ _Static_assert( \
+ __builtin_types_compatible_p(a, __typeof__ (b) ), \
+ "variable `" #b "` is not compatible to type `" #a "`" ); \
+ } while(0)
+
+#define assert_compatible_types_bool(a, b) \
+ ({ \
+ assert_compatible_types(a, b); \
+ true; \
+ })
+#else
+#define assert_compatible_types(a, b) (void)0
+#define assert_compatible_types_bool(a, b) true
+#endif
+
+
#endif /* C_H */
--
1.7.12.289.g0ce9864.dirty
0002-Add-ds-list-s-which-can-be-used-to-embed-lists-in-bi.patchtext/x-patch; charset=UTF-8; name=0002-Add-ds-list-s-which-can-be-used-to-embed-lists-in-bi.patchDownload
From 5274d608523393cb849044b629513d5680fd572a Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 28 Sep 2012 16:41:20 +0200
Subject: [PATCH 2/4] Add [ds]list's which can be used to embed lists in
bigger data structures without additional memory
management
Alvaro, Andres, Review by Peter G. and Tom
Changes since last submission:
* naming is now [ds]list_*
* README is split back into the header
* README is rewritten
* much improved comments
* no error checking for empty lists anymore, Asserts added everywhere
* no multiple evaluation at all anymore
* introduction of [ds]list_iterator structs
* typechecking added to macros
* DLIST_STATIC_INIT added to initialize list elements at declaration time.
* added some more functions (symetry, new users)
* s/ILIST_USE_DEFINITION/ILIST_DEFINE_FUNCTIONS/
* don't declare ILIST_DEFINE_FUNCTIONS in the header, rely on USE_INLINE
* pgindent compatible styling
---
src/backend/lib/Makefile | 2 +-
src/backend/lib/ilist.c | 117 ++++++++
src/include/lib/ilist.h | 760 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 878 insertions(+), 1 deletion(-)
create mode 100644 src/backend/lib/ilist.c
create mode 100644 src/include/lib/ilist.h
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 2e1061e..c94297a 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS = dllist.o stringinfo.o
+OBJS = dllist.o stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/ilist.c b/src/backend/lib/ilist.c
new file mode 100644
index 0000000..de4a27d
--- /dev/null
+++ b/src/backend/lib/ilist.c
@@ -0,0 +1,117 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+/*
+ * If inlines are in use, include the header normally which will get us only
+ * the function definitions as inlines. But if inlines aren't available,
+ * include the header with ILIST_DEFINE_FUNCTIONS defined; this makes it pour
+ * in regular (not inline) function declarations and the corresponding (non
+ * inline) definitions.
+ */
+#ifndef USE_INLINE
+#define ILIST_DEFINE_FUNCTIONS
+#endif
+
+#include "lib/ilist.h"
+
+/*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+void
+slist_delete(slist_head *head, slist_node *node)
+{
+ slist_node *last = &head->head;
+ slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+#ifdef USE_ASSERT_CHECKING
+ found = true;
+#endif
+ break;
+ }
+ last = cur;
+ }
+
+ slist_check(head);
+ Assert(found);
+}
+
+#ifdef ILIST_DEBUG
+/*
+ * Verify integrity of a doubly linked list
+ */
+void
+dlist_check(dlist_head *head)
+{
+ dlist_node *cur;
+
+ if (head == NULL || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ /* iterate in forward direction */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ /* iterate in backward direction */
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+}
+
+/*
+ * Verify integrity of a singly linked list
+ */
+void
+slist_check(slist_head *head)
+{
+ slist_node *cur;
+
+ if (head == NULL)
+ elog(ERROR, "singly linked is NULL");
+
+ /*
+ * there isn't much we can test in a singly linked list other that it
+ * actually ends sometime, i.e. hasn't introduced a circle or similar
+ */
+ for (cur = head->head.next; cur != NULL; cur = cur->next)
+ ;
+}
+
+#endif /* ILIST_DEBUG */
diff --git a/src/include/lib/ilist.h b/src/include/lib/ilist.h
new file mode 100644
index 0000000..388e953
--- /dev/null
+++ b/src/include/lib/ilist.h
@@ -0,0 +1,760 @@
+/*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure.
+
+ * Also, the circular lists are always circularly linked, even when empty; this
+ * enables many manipulations to be done without branches, which is beneficial
+ * performance-wise.
+ *
+ * NOTES:
+ *
+ * This is intended to be used in situations where memory for a struct and its
+ * contents already needs to be allocated and the overhead of allocating extra
+ * list cells for every list element is noticeable. The API for singly/doubly
+ * linked lists is identical as far as capabilities of both allow.
+ *
+ * // A oversimplified example demonstrating how this can be used:
+ *
+ * #include "lib/ilist.h"
+ *
+ * // Lets assume we want to store information about the tables contained in a
+ * // database.
+ *
+ * // Define struct for the databases including a list header that will be used
+ * // to access the nodes in the list later on.
+ * typedef struct my_database
+ * {
+ * char* datname;
+ * dlist_head tables;
+ * ...
+ * } my_database;
+ *
+ * // Define struct for the tables. Note the list_node element which stores
+ * // information about prev/next list nodes.
+ * typedef struct my_table
+ * {
+ * char* tablename;
+ * dlist_node list_node;
+ * perm_t permissions;
+ * ...
+ * } value_in_a_list;
+ *
+ * // create a database
+ * my_database *db = create_database();
+ *
+ * // and a few tables
+ * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
+ * ...
+ * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
+ * ...
+ * ...
+ * // to iterate over the table we allocate an iterator element to store
+ * // information about the current position
+ * dlist_iter iter;
+ *
+ * dlist_foreach (iter, &db->tables)
+ * {
+ * // inside an *_foreach the iterator's .cur field can be used to access
+ * // the current element
+ * // iter.cur points to a 'dlist_node', but we want the actual table
+ * // information, use dlist_container to convert
+ * my_table *tbl = dlist_container(my_table, list_node, iter->cur);
+ * elog(NOTICE, 'we have a table: %s in database %s',
+ * val->tablename, db->datname);
+ * }
+ *
+ * // while a simple iteration is useful we sometimes also want to manipulate
+ * // the list while iterating. Say, we want to delete all tables!
+ *
+ * // declare an iterator that allows some list manipulations
+ * dlist_mutable_iter miter;
+ *
+ * // iterate
+ * dlist_foreach_modify(miter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, iter->cur);
+ * // unlink the current table from the linked list
+ * dlist_delete(&db->tables, iter->cur);
+ * // as ilists never manage memory, we can freely access the table
+ * drop_table(db, tbl);
+ * }
+ *
+ * // Note that none of the dlist_* functions did do any memory
+ * // management. They just manipulated externally managed memory.
+ *
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+#ifndef ILIST_H
+#define ILIST_H
+
+/*
+ * enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even with --enable-cassert.
+ */
+/* #define ILIST_DEBUG */
+
+/*
+ * Node of a doubly linked list.
+ *
+ * Embed this in structs that need to be part of a doubly linked list.
+ */
+typedef struct dlist_node dlist_node;
+struct dlist_node
+{
+ dlist_node *prev;
+ dlist_node *next;
+};
+
+/*
+ * Head of a doubly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty. Circular
+ * lists have the advantage of not needing any branches in the most common list
+ * manipulations.
+ */
+typedef struct dlist_head
+{
+ /*
+ * head->next either points to the first element of the list or to &head
+ * if empty.
+ *
+ * head->prev either points to the last element of the list or to &head if
+ * empty.
+ */
+ dlist_node head;
+} dlist_head;
+
+
+/*
+ * Doubly linked list iterator.
+ *
+ * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
+ * current element of the iteration use the 'cur' member.
+ *
+ * Iterations using this are *not* allowed to change the list while iterating!
+ *
+ * NB: We use an extra type for this to make it possible to avoid multiple
+ * evaluations of arguments in the dlist_foreach() macro.
+ */
+typedef struct dlist_iter
+{
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+} dlist_iter;
+
+/*
+ * Doubly linked list iterator allowing some modifications while iterating
+ *
+ * Used as state in dlist_foreach_modify(). To get the current element of the
+ * iteration use the 'cur' member.
+ *
+ * Iterations using this are only allowed to change the list *at the current
+ * point of iteration*. It is fine to delete the current node, but it is *not*
+ * fine to modify other nodes.
+ *
+ * NB: We need a separate type for mutable iterations to avoid having to pass
+ * in two iterators or some other state variable as we need to store the
+ * '->next' node of the current node so it can be deleted or modified by the
+ * user.
+ */
+typedef struct dlist_mutable_iter
+{
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ dlist_node *next; /* next node we iterate to, so we can delete
+ * cur */
+} dlist_mutable_iter;
+
+/*
+ * Node of a singly linked list.
+ *
+ * Embed this in structs that need to be part of a singly linked list.
+ */
+typedef struct slist_node slist_node;
+struct slist_node
+{
+ slist_node *next;
+};
+
+/*
+ * Head of a singly linked list.
+ *
+ * Singly linked lists are *not* circularly linked (how could they be?) in
+ * contrast to the doubly linked lists. As no pointer to the last list element
+ * and to the previous node needs to be maintained this doesn't incur any
+ * additional branches in the usual manipulations.
+ */
+typedef struct slist_head
+{
+ slist_node head;
+} slist_head;
+
+/*
+ * Singly linked list iterator
+ *
+ * Used in slist_foreach(). To get the current element of the iteration use the
+ * 'cur' member.
+ *
+ * Do *not* manipulate the list while iterating!
+ *
+ * NB: this wouldn't really need to be an extra struct, we could use a
+ * slist_node * directly. For consistency reasons with dlist_*iter and
+ * slist_mutable_iter we still use a separate type.
+ */
+typedef struct slist_iter
+{
+ slist_node *cur;
+} slist_iter;
+
+/*
+ * Singly linked list iterator allowing some modifications while iterating
+ *
+ * Used in slist_foreach_modify.
+ *
+ * Iterations using this are allowed to remove the current node and to add more
+ * nodes to the beginning of the list.
+ */
+typedef struct slist_mutable_iter
+{
+ slist_node *cur;
+ slist_node *next;
+} slist_mutable_iter;
+
+
+/*
+ * We take quite some pain to allow this 'module' to be used on compilers
+ * without usable 'static inline' support. If configure detects its not
+ * available all the inline functions will be defined in ilist.c instead by
+ * #define'ing ILIST_USE_DEFINITION there.
+ */
+#ifdef USE_INLINE
+#define INLINE_IF_POSSIBLE static inline
+#else
+/* hide inline declarations from compiler */
+#define INLINE_IF_POSSIBLE
+
+/* Prototypes for functions we want to be inlined if possible */
+extern void dlist_init(dlist_head *head);
+extern bool dlist_is_empty(dlist_head *head);
+extern void dlist_push_head(dlist_head *head, dlist_node *node);
+extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+extern void dlist_insert_after(dlist_head *head,
+ dlist_node *after, dlist_node *node);
+extern void dlist_insert_before(dlist_head *head,
+ dlist_node *before, dlist_node *node);
+extern void dlist_delete(dlist_head *head, dlist_node *node);
+extern dlist_node *dlist_pop_head_node(dlist_head *head);
+extern void dlist_move_head(dlist_head *head, dlist_node *node);
+extern bool dlist_has_next(dlist_head *head, dlist_node *node);
+extern bool dlist_has_prev(dlist_head *head, dlist_node *node);
+extern dlist_node *dlist_next_node(dlist_head *head, dlist_node *node);
+extern dlist_node *dlist_prev_node(dlist_head *head, dlist_node *node);
+extern dlist_node *dlist_head_node(dlist_head *head);
+extern dlist_node *dlist_tail_node(dlist_head *head);
+
+/* dlist macro support functions */
+extern void *dlist_tail_element_off(dlist_head *head, size_t off);
+extern void *dlist_head_element_off(dlist_head *head, size_t off);
+
+extern void slist_init(slist_head *head);
+extern bool slist_is_empty(slist_head *head);
+extern slist_node *slist_head_node(slist_head *head);
+extern void slist_push_head(slist_head *head, slist_node *node);
+extern slist_node *slist_pop_head_node(slist_head *head);
+extern void slist_insert_after(slist_head *head,
+ slist_node *after, slist_node *node);
+extern bool slist_has_next(slist_head *head, slist_node *node);
+extern slist_node *slist_next_node(slist_head *head, slist_node *node);
+
+/* slist macro support function */
+extern void *slist_head_element_off(slist_head *head, size_t off);
+#endif /* !USE_INLINE */
+
+/* These functions are too big to be inline, so they are in the C file always */
+
+/* Attention: O(n) */
+extern void slist_delete(slist_head *head, slist_node *node);
+
+#ifdef ILIST_DEBUG
+extern void dlist_check(dlist_head *head);
+extern void slist_check(slist_head *head);
+#else
+/*
+ * These seemingly useless casts to void are here to keep the compiler quiet
+ * about the argument being unused in many functions in a non-debug compile,
+ * in which functions the only point of passing the list head pointer is to be
+ * able to run these checks.
+ */
+#define dlist_check(head) (void) (head)
+#define slist_check(head) (void) (head)
+#endif /* ILIST_DEBUG */
+
+#define DLIST_STATIC_INIT(name) {{&name.head, &name.head}}
+#define SLIST_STATIC_INIT(name) {{NULL}}
+
+/*
+ * The following function definitions are only used if inlining is supported by
+ * the compiler, or when included from a file that explicitly declares
+ * ILIST_USE_DEFINITION.
+ */
+#if defined(USE_INLINE) || defined(ILIST_DEFINE_FUNCTIONS)
+
+/*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+INLINE_IF_POSSIBLE void
+dlist_init(dlist_head *head)
+{
+ head->head.next = head->head.prev = &head->head;
+
+ dlist_check(head);
+}
+
+/*
+ * Insert a node at the beginning of the list.
+ */
+INLINE_IF_POSSIBLE void
+dlist_push_head(dlist_head *head, dlist_node *node)
+{
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+}
+
+/*
+ * Inserts a node at the end of the list.
+ */
+INLINE_IF_POSSIBLE void
+dlist_push_tail(dlist_head *head, dlist_node *node)
+{
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+}
+
+/*
+ * Insert a node after another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+dlist_insert_after(dlist_head *head, dlist_node *after,
+ dlist_node *node)
+{
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ dlist_check(head);
+}
+
+/*
+ * Insert a node before another *in the same list*
+ */
+INLINE_IF_POSSIBLE void
+dlist_insert_before(dlist_head *head, dlist_node *before,
+ dlist_node *node)
+{
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ dlist_check(head);
+}
+
+/*
+ * Delete 'node' from list.
+ *
+ * It is not allowed to delete a 'node' which is is not in the list 'head'
+ */
+INLINE_IF_POSSIBLE void
+dlist_delete(dlist_head *head, dlist_node *node)
+{
+ dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ dlist_check(head);
+}
+
+/*
+ * Delete and return the first node from a list.
+ *
+ * Undefined behaviour when the list is empty, check with dlist_is_empty if
+ * necessary.
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_pop_head_node(dlist_head *head)
+{
+ dlist_node *ret;
+
+ Assert(&head->head != head->head.next);
+
+ ret = head->head.next;
+ dlist_delete(head, head->head.next);
+ return ret;
+}
+
+/*
+ * Move element from any position in the list to the head position in the same
+ * list.
+ *
+ * Undefined behaviour if 'node' is not already part of the list.
+ */
+INLINE_IF_POSSIBLE void
+dlist_move_head(dlist_head *head, dlist_node *node)
+{
+ /* fast path if it's already at the head */
+ if (&head->head == node)
+ return;
+
+ dlist_delete(head, node);
+ dlist_push_head(head, node);
+
+ dlist_check(head);
+}
+
+/*
+ * Check whether the passed node is the last element in the list.
+ */
+INLINE_IF_POSSIBLE bool
+dlist_has_next(dlist_head *head, dlist_node *node)
+{
+ return node->next != &head->head;
+}
+
+/*
+ * Check whether the passed node is the first element in the list.
+ */
+INLINE_IF_POSSIBLE bool
+dlist_has_prev(dlist_head *head, dlist_node *node)
+{
+ return node->prev != &head->head;
+}
+
+/*
+ * Return the next node in the list.
+ *
+ * Undefined bheaviour when no next node exists, use dlist_has_next to make
+ * sure.
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_next_node(dlist_head *head, dlist_node *node)
+{
+ Assert(dlist_has_next(head, node));
+ return node->next;
+}
+
+/*
+ * Return previous node if there is one, NULL otherwise
+ *
+ * Undefined bheaviour when no prev node exists, use dlist_has_prev to make
+ * sure.
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_prev_node(dlist_head *head, dlist_node *node)
+{
+ Assert(dlist_has_prev(head, node));
+ return node->prev;
+}
+
+/*
+ * Return whether the list is empty.
+ */
+INLINE_IF_POSSIBLE bool
+dlist_is_empty(dlist_head *head)
+{
+ return head->head.next == &(head->head);
+}
+
+/* internal support function */
+INLINE_IF_POSSIBLE void *
+dlist_head_element_off(dlist_head *head, size_t off)
+{
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.next - off;
+}
+
+/*
+ * Return the first node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_head_node(dlist_head *head)
+{
+ return dlist_head_element_off(head, 0);
+}
+
+/* internal support function */
+INLINE_IF_POSSIBLE void *
+dlist_tail_element_off(dlist_head *head, size_t off)
+{
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.prev - off;
+}
+
+/*
+ * Return the last node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+INLINE_IF_POSSIBLE dlist_node *
+dlist_tail_node(dlist_head *head)
+{
+ return dlist_tail_element_off(head, 0);
+}
+#endif /* defined(USE_INLINE) ||
+ * defined(ILIST_DEFINE_FUNCTIONS) */
+
+/*
+ * Return the containing struct of 'type' where 'membername' is the dlist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a dlist_node* returned by some list
+ * navigation/manipulation back to its content.
+ *
+ * Note that assert_compatible_types is a compile time only check, so we don't
+ * have multiple evaluation dangers here.
+ */
+#define dlist_container(type, membername, ptr) \
+ (assert_compatible_types_bool(dlist_node *, ptr), \
+ assert_compatible_types_bool(dlist_node, ((type *)NULL)->membername), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))) \
+ )
+/*
+ * Return the value of first element in the list.
+ *
+ * The list may not be empty.
+ *
+ * Note that assert_compatible_types is a compile time only check, so we don't
+ * have multiple evaluation dangers here.
+ */
+#define dlist_head_element(type, membername, ptr) \
+ (assert_compatible_types_bool(dlist_node, ((type*)NULL)->membername), \
+ ((type *)dlist_get_head_off(ptr, offsetof(type, membername))))
+
+/*
+ * Return the value of first element in the list.
+ *
+ * The list may not be empty.
+ *
+ * Note that assert_compatible_types is a compile time only check, so we don't
+ * have multiple evaluation dangers here.
+ */
+#define dlist_tail_element(type, membername, ptr) \
+ (assert_compatible_types_bool(dlist_node, ((type*)NULL)->membername), \
+ ((type *)dlist_tail_element_off(ptr, offsetof(type, membername))))
+
+/*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define dlist_foreach(iter, ptr) \
+ assert_compatible_types(dlist_iter, iter); \
+ assert_compatible_types(dlist_head *, ptr); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->next)
+
+
+/*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+#define dlist_foreach_modify(iter, ptr) \
+ assert_compatible_types(dlist_mutable_iter, iter); \
+ assert_compatible_types(dlist_head *, ptr); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next, \
+ iter.next = iter.cur->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.next, iter.next = iter.cur->next)
+
+/*
+ * Iterate through the list in reverse order.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define dlist_reverse_foreach(iter, ptr) \
+ assert_compatible_types(dlist_iter, iter); \
+ assert_compatible_types(dlist_head *, ptr); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->prev; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->prev)
+
+#if defined(USE_INLINE) || defined(ILIST_DEFINE_FUNCTIONS)
+
+/*
+ * Initialize a singly linked list.
+ */
+INLINE_IF_POSSIBLE void
+slist_init(slist_head *head)
+{
+ head->head.next = NULL;
+
+ slist_check(head);
+}
+
+/*
+ * Is the list empty?
+ */
+INLINE_IF_POSSIBLE bool
+slist_is_empty(slist_head *head)
+{
+ slist_check(head);
+
+ return head->head.next == NULL;
+}
+
+/* internal support function */
+INLINE_IF_POSSIBLE void *
+slist_head_element_off(slist_head *head, size_t off)
+{
+ Assert(!slist_is_empty(head));
+ return (char *) head->head.next - off;
+}
+
+/*
+ * Push 'node' as the new first node in the list, pushing the original head to
+ * the second position.
+ */
+INLINE_IF_POSSIBLE void
+slist_push_head(slist_head *head, slist_node *node)
+{
+ node->next = head->head.next;
+ head->head.next = node;
+
+ slist_check(head);
+}
+
+/*
+ * Remove and return the first node in the list
+ *
+ * Undefined behaviour if the list is empty.
+ */
+INLINE_IF_POSSIBLE slist_node *
+slist_pop_head_node(slist_head *head)
+{
+ slist_node *node;
+
+ Assert(!slist_is_empty(head));
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ slist_check(head);
+
+ return node;
+}
+
+/*
+ * Insert a new node after another one
+ *
+ * Undefined behaviour if 'after' is not part of the list already.
+ */
+INLINE_IF_POSSIBLE void
+slist_insert_after(slist_head *head, slist_node *after,
+ slist_node *node)
+{
+ node->next = after->next;
+ after->next = node;
+
+ slist_check(head);
+}
+
+/*
+ * Return whether 'node' has a following node
+ */
+INLINE_IF_POSSIBLE bool
+slist_has_next(slist_head *head,
+ slist_node *node)
+{
+ slist_check(head);
+
+ return node->next != NULL;
+}
+#endif /*-- defined(USE_INLINE || ILIST_DEFINE_FUNCTIONS) --*/
+
+/*
+ * Return the containing struct of 'type' where 'membername' is the slist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a slist_node* returned by some list
+ * navigation/manipulation back to its content.
+ *
+ * Note that assert_compatible_types is a compile time only check, so we don't
+ * have multiple evaluation dangers here.
+ */
+#define slist_container(type, membername, ptr) \
+ (assert_compatible_types_bool(slist_node *, ptr), \
+ assert_compatible_types_bool(slist_node, ((type*)NULL)->membername), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))) \
+ )
+/*
+ * Return the value of first element in the list.
+ */
+#define slist_head_element(type, membername, ptr) \
+ (assert_compatible_types_bool(slist_node, ((type*)NULL)->membername), \
+ slist_head_element_off(ptr, offsetoff(type, membername)))
+
+/*
+ * Iterate through the list 'ptr' using the iterator 'iter'.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+#define slist_foreach(iter, ptr) \
+ assert_compatible_types(slist_iter, iter); \
+ assert_compatible_types(slist_head *, ptr); \
+ for (iter.cur = (ptr)->head.next; \
+ iter.cur != NULL; \
+ iter.cur = iter.cur->next)
+
+/*
+ * Iterate through the list 'ptr' using the iterator 'iter' allowing some
+ * modifications.
+ *
+ * It is allowed to delete the current element from the list and add new nodes
+ * before the current position. Every other manipulation can lead to
+ * corruption.
+ */
+#define slist_foreach_modify(iter, ptr) \
+ assert_compatible_types(slist_mutable_iter, iter); \
+ assert_compatible_types(slist_head *, ptr); \
+ for (iter.cur = (ptr)->head.next, \
+ iter.next = iter.cur ? iter.cur->next : NULL; \
+ iter.cur != NULL; \
+ iter.cur = iter.next, iter.next = iter.next ? iter.next->next : NULL)
+
+#endif /* ILIST_H */
--
1.7.12.289.g0ce9864.dirty
0003-Remove-current-users-of-dllist.h.patchtext/x-patch; charset=UTF-8; name=0003-Remove-current-users-of-dllist.h.patchDownload
From 610e58e256ca6240fbfbdc78e527d4f1c4de6882 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 28 Sep 2012 16:42:03 +0200
Subject: [PATCH 3/4] Remove current users of dllist.h
Alvaro, Andres
---
src/backend/postmaster/autovacuum.c | 128 ++++++++++++++++----------------
src/backend/postmaster/postmaster.c | 59 +++++++--------
src/backend/utils/cache/catcache.c | 144 ++++++++++++++++++------------------
src/include/utils/catcache.h | 19 ++---
4 files changed, 175 insertions(+), 175 deletions(-)
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 74db821..c3ac219 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -77,7 +77,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -152,6 +152,7 @@ typedef struct avl_dbase
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ dlist_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
@@ -258,8 +259,11 @@ typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
-/* the database list in the launcher, and the context that contains it */
-static Dllist *DatabaseList = NULL;
+/*
+ * the database list (of avl_dbase elements) in the launcher, and the context
+ * that contains it
+ */
+static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
@@ -508,7 +512,7 @@ AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
- DatabaseList = NULL;
+ dlist_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
@@ -576,7 +580,7 @@ AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
- Dlelem *elem;
+ avl_dbase *avdb;
int rc;
/*
@@ -738,20 +742,7 @@ AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
- elem = DLGetTail(DatabaseList);
- if (elem != NULL)
- {
- avl_dbase *avdb = DLE_VAL(elem);
-
- /*
- * launch a worker if next_worker is right now or it is in the
- * past
- */
- if (TimestampDifferenceExceeds(avdb->adl_next_worker,
- current_time, 0))
- launch_worker(current_time);
- }
- else
+ if (dlist_is_empty(&DatabaseList))
{
/*
* Special case when the list is empty: start a worker right away.
@@ -763,6 +754,23 @@ AutoVacLauncherMain(int argc, char *argv[])
*/
launch_worker(current_time);
}
+ else
+ {
+ /*
+ * because rebuild_database_list constructs a list with most
+ * distant adl_next_worker first, we obtain our database from the
+ * tail of the list.
+ */
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
+ /*
+ * launch a worker if next_worker is right now or it is in the
+ * past
+ */
+ if (TimestampDifferenceExceeds(avdb->adl_next_worker,
+ current_time, 0))
+ launch_worker(current_time);
+ }
}
/* Normal exit from the autovac launcher is here */
@@ -783,7 +791,7 @@ AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
- Dlelem *elem;
+ avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
@@ -796,14 +804,15 @@ launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
- else if ((elem = DLGetTail(DatabaseList)) != NULL)
+ else if (!dlist_is_empty(&DatabaseList))
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
@@ -867,6 +876,7 @@ rebuild_database_list(Oid newdb)
int score;
int nelems;
HTAB *dbhash;
+ dlist_iter iter;
/* use fresh stats */
autovac_refresh_stats();
@@ -927,36 +937,28 @@ rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
- if (DatabaseList != NULL)
+ dlist_foreach(iter, &DatabaseList)
{
- Dlelem *elem;
-
- elem = DLGetHead(DatabaseList);
- while (elem != NULL)
- {
- avl_dbase *avdb = DLE_VAL(elem);
- avl_dbase *db;
- bool found;
- PgStat_StatDBEntry *entry;
-
- elem = DLGetSucc(elem);
+ avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
+ avl_dbase *db;
+ bool found;
+ PgStat_StatDBEntry *entry;
- /*
- * skip databases with no stat entries -- in particular, this gets
- * rid of dropped databases
- */
- entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
- if (entry == NULL)
- continue;
+ /*
+ * skip databases with no stat entries -- in particular, this gets
+ * rid of dropped databases
+ */
+ entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
+ if (entry == NULL)
+ continue;
- db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
+ db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
- if (!found)
- {
- /* hash_search already filled in the key */
- db->adl_score = score++;
- /* next_worker is filled in later */
- }
+ if (!found)
+ {
+ /* hash_search already filled in the key */
+ db->adl_score = score++;
+ /* next_worker is filled in later */
}
}
@@ -987,7 +989,7 @@ rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
- DatabaseList = DLNewList();
+ dlist_init(&DatabaseList);
if (nelems > 0)
{
@@ -1029,15 +1031,13 @@ rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
- DLAddHead(DatabaseList, elem);
+ dlist_push_head(&DatabaseList, &db->adl_node);
}
}
@@ -1147,7 +1147,7 @@ do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
- Dlelem *elem;
+ dlist_iter iter;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
@@ -1179,11 +1179,10 @@ do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
- while (elem != NULL)
+ dlist_reverse_foreach(iter, &DatabaseList)
{
- avl_dbase *dbp = DLE_VAL(elem);
+ avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
if (dbp->adl_datid == tmp->adw_datid)
{
@@ -1200,7 +1199,6 @@ do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
@@ -1274,22 +1272,25 @@ static void
launch_worker(TimestampTz now)
{
Oid dbid;
- Dlelem *elem;
+ dlist_iter iter;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
+ bool found = false;
+
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
- elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
- while (elem != NULL)
+ dlist_foreach(iter, &DatabaseList)
{
- avl_dbase *avdb = DLE_VAL(elem);
+ avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
if (avdb->adl_datid == dbid)
{
+ found = true;
+
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
@@ -1297,10 +1298,9 @@ launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
- DLMoveToFront(elem);
+ dlist_move_head(&DatabaseList, iter.cur);
break;
}
- elem = DLGetSucc(elem);
}
/*
@@ -1310,7 +1310,7 @@ launch_worker(TimestampTz now)
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
- if (elem == NULL)
+ if (!found)
rebuild_database_list(dbid);
}
}
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index dff4c71..7060a1f 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -95,7 +95,7 @@
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
@@ -146,10 +146,10 @@ typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
- Dlelem elem; /* list link in BackendList */
+ dlist_node elem; /* list link in BackendList */
} Backend;
-static Dllist *BackendList;
+static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
@@ -157,8 +157,10 @@ static Backend *ShmemBackendArray;
/* The socket number we are listening for connections on */
int PostPortNumber;
+
/* The directory names for Unix socket(s) */
char *Unix_socket_directories;
+
/* The TCP listen address(es) */
char *ListenAddresses;
@@ -1028,11 +1030,6 @@ PostmasterMain(int argc, char *argv[])
set_stack_base();
/*
- * Initialize the list of active backends.
- */
- BackendList = DLNewList();
-
- /*
* Initialize pipe (or process handle on Windows) that allows children to
* wake up from sleep on postmaster death.
*/
@@ -1872,7 +1869,7 @@ processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
- Dlelem *curr;
+ dlist_iter iter;
#else
int i;
#endif
@@ -1886,9 +1883,9 @@ processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ dlist_foreach(iter, &BackendList)
{
- bp = (Backend *) DLE_VAL(curr);
+ bp = dlist_container(Backend, elem, iter.cur);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
@@ -2648,7 +2645,7 @@ static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
- Dlelem *curr;
+ dlist_mutable_iter iter;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
@@ -2680,9 +2677,9 @@ CleanupBackend(int pid,
return;
}
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ dlist_foreach_modify(iter, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->pid == pid)
{
@@ -2701,7 +2698,7 @@ CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ dlist_delete(&BackendList, iter.cur);
free(bp);
break;
}
@@ -2718,8 +2715,7 @@ CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
- Dlelem *curr,
- *next;
+ dlist_mutable_iter iter;
Backend *bp;
/*
@@ -2734,10 +2730,10 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
- for (curr = DLGetHead(BackendList); curr; curr = next)
+ dlist_foreach_modify(iter, &BackendList)
{
- next = DLGetSucc(curr);
- bp = (Backend *) DLE_VAL(curr);
+ bp = dlist_container(Backend, elem, iter.cur);
+
if (bp->pid == pid)
{
/*
@@ -2750,7 +2746,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
- DLRemove(curr);
+ dlist_delete(&BackendList, iter.cur);
free(bp);
/* Keep looping so we can signal remaining backends */
}
@@ -3113,7 +3109,7 @@ PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
- if (DLGetHead(BackendList) == NULL &&
+ if (dlist_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
@@ -3239,12 +3235,12 @@ signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
- Dlelem *curr;
+ dlist_iter iter;
bool signaled = false;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ dlist_foreach(iter, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
@@ -3382,8 +3378,8 @@ BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ dlist_push_head(&BackendList, &bn->elem);
+
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
@@ -4498,12 +4494,12 @@ PostmasterRandom(void)
static int
CountChildren(int target)
{
- Dlelem *curr;
+ dlist_iter iter;
int cnt = 0;
- for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
+ dlist_foreach(iter, &BackendList)
{
- Backend *bp = (Backend *) DLE_VAL(curr);
+ Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
@@ -4682,8 +4678,7 @@ StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
- DLInitElem(&bn->elem, bn);
- DLAddHead(BackendList, &bn->elem);
+ dlist_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index d6f6b1c..230ddf4 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -291,7 +291,7 @@ CatalogCacheComputeTupleHashValue(CatCache *cache, HeapTuple tuple)
static void
CatCachePrintStats(int code, Datum arg)
{
- CatCache *cache;
+ slist_iter iter;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
@@ -300,8 +300,10 @@ CatCachePrintStats(int code, Datum arg)
long cc_lsearches = 0;
long cc_lhits = 0;
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(iter, &CacheHdr->ch_caches)
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
@@ -368,8 +370,7 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
return; /* nothing left to do */
}
- /* delink from linked list */
- DLRemove(&ct->cache_elem);
+ dlist_delete(ct->cache_bucket, &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
@@ -412,7 +413,7 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
- DLRemove(&cl->cache_elem);
+ dlist_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
@@ -442,18 +443,18 @@ CatCacheRemoveCList(CatCache *cache, CatCList *cl)
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
- CatCache *ccp;
+ slist_iter cache_iter;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(cache_iter, &CacheHdr->ch_caches)
{
Index hashIndex;
- Dlelem *elt,
- *nextelt;
+ dlist_mutable_iter iter;
+ CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
if (cacheId != ccp->id)
continue;
@@ -468,11 +469,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
+ dlist_foreach_modify(iter, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
@@ -484,12 +483,9 @@ CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
-
- for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
+ dlist_foreach_modify(iter, &ccp->cc_bucket[hashIndex])
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (hashValue == ct->hash_value)
{
@@ -557,17 +553,18 @@ AtEOXact_CatCache(bool isCommit)
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
- CatCache *ccp;
+ slist_iter cache_iter;
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(cache_iter, &(CacheHdr->ch_caches))
{
- Dlelem *elt;
+ CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
+ dlist_iter iter;
int i;
/* Check CatCLists */
- for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
+ dlist_foreach(iter, &ccp->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
+ CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
@@ -577,11 +574,11 @@ AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&ccp->cc_bucket[i]);
- elt;
- elt = DLGetSucc(elt))
+ dlist_head *bucket = &ccp->cc_bucket[i];
+
+ dlist_foreach(iter, bucket)
{
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
+ CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
@@ -604,16 +601,13 @@ AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
- Dlelem *elt,
- *nextelt;
+ dlist_mutable_iter iter;
int i;
/* Remove each list in this cache, or at least mark it dead */
- for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
+ dlist_foreach_modify(iter, &cache->cc_lists)
{
- CatCList *cl = (CatCList *) DLE_VAL(elt);
-
- nextelt = DLGetSucc(elt);
+ CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
@@ -624,11 +618,11 @@ ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
- for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
- {
- CatCTup *ct = (CatCTup *) DLE_VAL(elt);
+ dlist_head *bucket = &cache->cc_bucket[i];
- nextelt = DLGetSucc(elt);
+ dlist_foreach_modify(iter, bucket)
+ {
+ CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
@@ -654,12 +648,16 @@ ResetCatalogCache(CatCache *cache)
void
ResetCatalogCaches(void)
{
- CatCache *cache;
+ slist_iter iter;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(iter, &CacheHdr->ch_caches)
+ {
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
ResetCatalogCache(cache);
+ }
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
@@ -680,12 +678,14 @@ ResetCatalogCaches(void)
void
CatalogCacheFlushCatalog(Oid catId)
{
- CatCache *cache;
+ slist_iter iter;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
- for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
+ slist_foreach(iter, &(CacheHdr->ch_caches))
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
@@ -760,7 +760,7 @@ InitCatCache(int id,
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
- CacheHdr->ch_caches = NULL;
+ slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
@@ -770,10 +770,8 @@ InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
- cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
+ cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(dlist_node));
/*
* initialize the cache's relation information for the relation
@@ -792,6 +790,11 @@ InitCatCache(int id,
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ dlist_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++)
+ dlist_init(&cp->cc_bucket[i]);
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
@@ -801,8 +804,7 @@ InitCatCache(int id,
/*
* add completed cache to top of group header's list
*/
- cp->cc_next = CacheHdr->ch_caches;
- CacheHdr->ch_caches = cp;
+ slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
/*
* back to the old context before we return...
@@ -1060,7 +1062,8 @@ SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
- Dlelem *elt;
+ dlist_mutable_iter iter;
+ dlist_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
@@ -1094,13 +1097,13 @@ SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
+ bucket = &cache->cc_bucket[hashIndex];
+
+ dlist_foreach_modify(iter, bucket)
{
bool res;
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead)
continue; /* ignore dead entries */
@@ -1125,7 +1128,7 @@ SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
- DLMoveToFront(&ct->cache_elem);
+ dlist_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
@@ -1340,7 +1343,7 @@ SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
- Dlelem *elt;
+ dlist_iter iter;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
@@ -1382,13 +1385,11 @@ SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
- for (elt = DLGetHead(&cache->cc_lists);
- elt;
- elt = DLGetSucc(elt))
+ dlist_foreach(iter, &cache->cc_lists)
{
bool res;
- cl = (CatCList *) DLE_VAL(elt);
+ cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->dead)
continue; /* ignore dead entries */
@@ -1416,7 +1417,7 @@ SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
- DLMoveToFront(&cl->cache_elem);
+ dlist_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
@@ -1468,6 +1469,8 @@ SearchCatCacheList(CatCache *cache,
{
uint32 hashValue;
Index hashIndex;
+ bool found = false;
+ dlist_head *bucket;
/*
* See if there's an entry for this tuple already.
@@ -1476,11 +1479,10 @@ SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
- for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
- elt;
- elt = DLGetSucc(elt))
+ bucket = &cache->cc_bucket[hashIndex];
+ dlist_foreach(iter, bucket)
{
- ct = (CatCTup *) DLE_VAL(elt);
+ ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
@@ -1498,10 +1500,11 @@ SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
- if (elt == NULL)
+ if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
@@ -1564,7 +1567,6 @@ SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
@@ -1587,7 +1589,7 @@ SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
- DLAddHead(&cache->cc_lists, &cl->cache_elem);
+ dlist_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
@@ -1664,14 +1666,15 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
- DLInitElem(&ct->cache_elem, (void *) ct);
+ ct->cache_bucket = &cache->cc_bucket[hashIndex];
+
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
- DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
+ dlist_push_head(ct->cache_bucket, &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
@@ -1785,7 +1788,7 @@ PrepareToInvalidateCacheTuple(Relation relation,
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
- CatCache *ccp;
+ slist_iter iter;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
@@ -1808,10 +1811,11 @@ PrepareToInvalidateCacheTuple(Relation relation,
* ----------------
*/
- for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
+ slist_foreach(iter, &(CacheHdr->ch_caches))
{
uint32 hashvalue;
Oid dbid;
+ CatCache *ccp = slist_container(CatCache, cc_next, iter.cur);
if (ccp->cc_reloid != reloid)
continue;
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index d91700a..cc6dab2 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -22,7 +22,7 @@
#include "access/htup.h"
#include "access/skey.h"
-#include "lib/dllist.h"
+#include "lib/ilist.h"
#include "utils/relcache.h"
/*
@@ -37,7 +37,7 @@
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
- struct catcache *cc_next; /* link to next catcache */
+ slist_node cc_next; /* list link */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
@@ -51,7 +51,7 @@ typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
- Dllist cc_lists; /* list of CatCList structs */
+ dlist_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
@@ -66,7 +66,7 @@ typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
- Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
+ dlist_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
@@ -77,11 +77,12 @@ typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
- * Each tuple in a cache is a member of a Dllist that stores the elements
- * of its hash bucket. We keep each Dllist in LRU order to speed repeated
+ * Each tuple in a cache is a member of a dlist that stores the elements
+ * of its hash bucket. We keep each dlist in LRU order to speed repeated
* lookups.
*/
- Dlelem cache_elem; /* list member of per-bucket list */
+ dlist_node cache_elem; /* list member of per-bucket list */
+ dlist_head *cache_bucket; /* containing bucket dlist */
/*
* The tuple may also be a member of at most one CatCList. (If a single
@@ -139,7 +140,7 @@ typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
- Dlelem cache_elem; /* list member of per-catcache list */
+ dlist_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
@@ -153,7 +154,7 @@ typedef struct catclist
typedef struct catcacheheader
{
- CatCache *ch_caches; /* head of list of CatCache structs */
+ slist_head ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
--
1.7.12.289.g0ce9864.dirty
0004-Remove-dllist.-ch.patchtext/x-patch; charset=UTF-8; name=0004-Remove-dllist.-ch.patchDownload
From 0a7ccf3e72d9a2a3a4d5e11fdadf5be6d1d4840b Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 28 Sep 2012 16:44:16 +0200
Subject: [PATCH 4/4] Remove dllist.[ch]
---
src/backend/lib/Makefile | 2 +-
src/backend/lib/dllist.c | 214 -----------------------------------------------
src/include/lib/dllist.h | 85 -------------------
3 files changed, 1 insertion(+), 300 deletions(-)
delete mode 100644 src/backend/lib/dllist.c
delete mode 100644 src/include/lib/dllist.h
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index c94297a..7da1c45 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS = dllist.o stringinfo.o ilist.o
+OBJS = stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/dllist.c b/src/backend/lib/dllist.c
deleted file mode 100644
index 52af56a..0000000
--- a/src/backend/lib/dllist.c
+++ /dev/null
@@ -1,214 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * dllist.c
- * this is a simple doubly linked list implementation
- * the elements of the lists are void*
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/backend/lib/dllist.c
- *
- *-------------------------------------------------------------------------
- */
-#include "postgres.h"
-
-#include "lib/dllist.h"
-
-
-Dllist *
-DLNewList(void)
-{
- Dllist *l;
-
- l = (Dllist *) palloc(sizeof(Dllist));
-
- l->dll_head = NULL;
- l->dll_tail = NULL;
-
- return l;
-}
-
-void
-DLInitList(Dllist *list)
-{
- list->dll_head = NULL;
- list->dll_tail = NULL;
-}
-
-/*
- * free up a list and all the nodes in it --- but *not* whatever the nodes
- * might point to!
- */
-void
-DLFreeList(Dllist *list)
-{
- Dlelem *curr;
-
- while ((curr = DLRemHead(list)) != NULL)
- pfree(curr);
-
- pfree(list);
-}
-
-Dlelem *
-DLNewElem(void *val)
-{
- Dlelem *e;
-
- e = (Dlelem *) palloc(sizeof(Dlelem));
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- return e;
-}
-
-void
-DLInitElem(Dlelem *e, void *val)
-{
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
-}
-
-void
-DLFreeElem(Dlelem *e)
-{
- pfree(e);
-}
-
-void
-DLRemove(Dlelem *e)
-{
- Dllist *l = e->dle_list;
-
- if (e->dle_prev)
- e->dle_prev->dle_next = e->dle_next;
- else
- {
- /* must be the head element */
- Assert(e == l->dll_head);
- l->dll_head = e->dle_next;
- }
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_list = NULL;
-}
-
-void
-DLAddHead(Dllist *l, Dlelem *e)
-{
- e->dle_list = l;
-
- if (l->dll_head)
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
-
- if (l->dll_tail == NULL) /* if this is first element added */
- l->dll_tail = e;
-}
-
-void
-DLAddTail(Dllist *l, Dlelem *e)
-{
- e->dle_list = l;
-
- if (l->dll_tail)
- l->dll_tail->dle_next = e;
- e->dle_prev = l->dll_tail;
- e->dle_next = NULL;
- l->dll_tail = e;
-
- if (l->dll_head == NULL) /* if this is first element added */
- l->dll_head = e;
-}
-
-Dlelem *
-DLRemHead(Dllist *l)
-{
- /* remove and return the head */
- Dlelem *result = l->dll_head;
-
- if (result == NULL)
- return result;
-
- if (result->dle_next)
- result->dle_next->dle_prev = NULL;
-
- l->dll_head = result->dle_next;
-
- if (result == l->dll_tail) /* if the head is also the tail */
- l->dll_tail = NULL;
-
- result->dle_next = NULL;
- result->dle_list = NULL;
-
- return result;
-}
-
-Dlelem *
-DLRemTail(Dllist *l)
-{
- /* remove and return the tail */
- Dlelem *result = l->dll_tail;
-
- if (result == NULL)
- return result;
-
- if (result->dle_prev)
- result->dle_prev->dle_next = NULL;
-
- l->dll_tail = result->dle_prev;
-
- if (result == l->dll_head) /* if the tail is also the head */
- l->dll_head = NULL;
-
- result->dle_prev = NULL;
- result->dle_list = NULL;
-
- return result;
-}
-
-/* Same as DLRemove followed by DLAddHead, but faster */
-void
-DLMoveToFront(Dlelem *e)
-{
- Dllist *l = e->dle_list;
-
- if (l->dll_head == e)
- return; /* Fast path if already at front */
-
- Assert(e->dle_prev != NULL); /* since it's not the head */
- e->dle_prev->dle_next = e->dle_next;
-
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
- /* We need not check dll_tail, since there must have been > 1 entry */
-}
diff --git a/src/include/lib/dllist.h b/src/include/lib/dllist.h
deleted file mode 100644
index 25ed64c..0000000
--- a/src/include/lib/dllist.h
+++ /dev/null
@@ -1,85 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * dllist.h
- * simple doubly linked list primitives
- * the elements of the list are void* so the lists can contain anything
- * Dlelem can only be in one list at a time
- *
- *
- * Here's a small example of how to use Dllists:
- *
- * Dllist *lst;
- * Dlelem *elt;
- * void *in_stuff; -- stuff to stick in the list
- * void *out_stuff
- *
- * lst = DLNewList(); -- make a new dllist
- * DLAddHead(lst, DLNewElem(in_stuff)); -- add a new element to the list
- * with in_stuff as the value
- * ...
- * elt = DLGetHead(lst); -- retrieve the head element
- * out_stuff = (void*)DLE_VAL(elt); -- get the stuff out
- * DLRemove(elt); -- removes the element from its list
- * DLFreeElem(elt); -- free the element since we don't
- * use it anymore
- *
- *
- * It is also possible to use Dllist objects that are embedded in larger
- * structures instead of being separately malloc'd. To do this, use
- * DLInitElem() to initialize a Dllist field within a larger object.
- * Don't forget to DLRemove() each field from its list (if any) before
- * freeing the larger object!
- *
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/include/lib/dllist.h
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef DLLIST_H
-#define DLLIST_H
-
-struct Dllist;
-struct Dlelem;
-
-typedef struct Dlelem
-{
- struct Dlelem *dle_next; /* next element */
- struct Dlelem *dle_prev; /* previous element */
- void *dle_val; /* value of the element */
- struct Dllist *dle_list; /* what list this element is in */
-} Dlelem;
-
-typedef struct Dllist
-{
- Dlelem *dll_head;
- Dlelem *dll_tail;
-} Dllist;
-
-extern Dllist *DLNewList(void); /* allocate and initialize a list header */
-extern void DLInitList(Dllist *list); /* init a header alloced by caller */
-extern void DLFreeList(Dllist *list); /* free up a list and all the nodes in
- * it */
-extern Dlelem *DLNewElem(void *val);
-extern void DLInitElem(Dlelem *e, void *val);
-extern void DLFreeElem(Dlelem *e);
-extern void DLRemove(Dlelem *e); /* removes node from list */
-extern void DLAddHead(Dllist *list, Dlelem *node);
-extern void DLAddTail(Dllist *list, Dlelem *node);
-extern Dlelem *DLRemHead(Dllist *list); /* remove and return the head */
-extern Dlelem *DLRemTail(Dllist *list);
-extern void DLMoveToFront(Dlelem *e); /* move node to front of its list */
-
-/* These are macros for speed */
-#define DLGetHead(list) ((list)->dll_head)
-#define DLGetTail(list) ((list)->dll_tail)
-#define DLGetSucc(elem) ((elem)->dle_next)
-#define DLGetPred(elem) ((elem)->dle_prev)
-#define DLGetListHdr(elem) ((elem)->dle_list)
-
-#define DLE_VAL(elem) ((elem)->dle_val)
-
-#endif /* DLLIST_H */
--
1.7.12.289.g0ce9864.dirty
On Saturday, September 29, 2012 01:54:37 AM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
On Saturday, September 29, 2012 01:39:03 AM Tom Lane wrote:
Right offhand it doesn't seem like it really gains that much even for
that use-case. You'd end up editing the include file either way, just
slightly differently.Well, with USE_INLINE you have to recompile the whole backend because you
otherwise easily end up with strange incompatibilities between files.Eh? You would anyway, or at least recompile every .o file depending on
that header, if what you want is to inline or de-inline the functions.
There's no magic shortcut for that.
Well, --enable-depend copes with changing that in the header fine. As long as
its only used in a low number of files thats shorter than a full rebuild ;)
Anyway, changed.
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Add [ds]list's which can be used to embed lists in bigger data structures
without additional memory management
Alvaro, Andres, Review by Peter G. and Tom
This is missing Robert. Sorry for that.
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
Patch 0001 contains a assert_compatible_types(a, b) and a
assert_compatible_types_bool(a, b) macro which I found very useful to make it
harder to misuse the api. I think its generally useful and possibly should be
used in more places.
This seems like basically a good idea, but the macro names are very
unfortunately chosen: they don't comport with our other names for
assertion macros, and they imply that the test is symmetric which it
isn't. It's also unclear what the point of the _bool version is
(namely, to be used in expression contexts in macros).
I suggest instead
AssertVariableIsOfType(varname, typename)
AssertVariableIsOfTypeMacro(varname, typename)
Or possibly we should leave off the "Assert" prefix, since this will be
a compile-time-constant check and thus not really all that much like
the existing run-time Assert mechanism. Or write "Check" instead of
"Assert", or some other verb.
Anybody got another color for this bikeshed?
regards, tom lane
Andres Freund <andres@2ndquadrant.com> writes:
Current version is available at branch ilist in:
git://git.postgresql.org/git/users/andresfreund/postgres.git
ssh://git@git.postgresql.org/users/andresfreund/postgres.git
I'm still pretty desperately unhappy with your insistence on circularly
linked dlists. Not only does that make initialization problematic,
but now it's not even consistent with slists.
A possible compromise that would fix the initialization issue is to
allow an empty dlist to be represented as *either* two NULL pointers
or a pair of self-pointers. Conversion from one case to the other
could happen like this:
INLINE_IF_POSSIBLE void
dlist_push_head(dlist_head *head, dlist_node *node)
{
+ if (head->head.next == NULL)
+ dlist_init(head);
+
node->next = head->head.next;
node->prev = &head->head;
node->next->prev = node;
head->head.next = node;
dlist_check(head);
}
It appears to me that of the inline'able functions, only dlist_push_head
and dlist_push_tail would need to do this; the others presume nonempty
lists and so wouldn't have to deal with the NULL/NULL case.
dlist_is_empty would need one extra test too. dlist_foreach could be
something like
#define dlist_foreach(iter, ptr)
for (AssertVariableIsOfTypeMacro(iter, dlist_iter),
AssertVariableIsOfTypeMacro(ptr, dlist_head *),
iter.end = &(ptr)->head,
iter.cur = iter.end->next ? iter.end->next : iter.end;
iter.cur != iter.end;
iter.cur = iter.cur->next)
I remain unimpressed with the micro-efficiency of this looping code,
but since you're set on pessimizing list iteration to speed up list
alteration, maybe it's the best we can do.
BTW, the "fast path" in dlist_move_head can't be right can it?
regards, tom lane
On Sunday, September 30, 2012 06:57:32 PM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
Patch 0001 contains a assert_compatible_types(a, b) and a
assert_compatible_types_bool(a, b) macro which I found very useful to
make it harder to misuse the api. I think its generally useful and
possibly should be used in more places.This seems like basically a good idea, but the macro names are very
unfortunately chosen: they don't comport with our other names for
assertion macros, and they imply that the test is symmetric which it
isn't. It's also unclear what the point of the _bool version is
(namely, to be used in expression contexts in macros).I suggest instead
AssertVariableIsOfType(varname, typename)
AssertVariableIsOfTypeMacro(varname, typename)
Or possibly we should leave off the "Assert" prefix, since this will be
a compile-time-constant check and thus not really all that much like
the existing run-time Assert mechanism. Or write "Check" instead of
"Assert", or some other verb.Anybody got another color for this bikeshed?
No, happy with the new name.
Thanks for committing! Wondered for a minute what the point of autoconfiscation
is/was but I see that e.g. clang already works... Nice.
The bizarre syntactic placement requirements directly come from the standard
btw. No idea why they thought that would be a good idea... (check 6.7.1,
6.7.2.1, 6.7.10).
Perhaps we need to decouple _Static_assert support from compound statement
support at some point, but we will see.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Andres Freund <andres@2ndquadrant.com> writes:
Perhaps we need to decouple _Static_assert support from compound statement
support at some point, but we will see.
Yeah, possibly, but until we have an example of a non-gcc-compatible
compiler that can do something equivalent, it's hard to guess how we
might need to alter the autoconf tests. Anyway the important thing
for now is the external specification of the macros, and I think we're
good on that (modulo any better naming suggestions).
I'm fairly sure there are already a few cases of Asserts on
compile-time-constant expressions, so I made sure that there was a layer
allowing access to _Static_assert without the type-compatibility code.
I didn't go looking for anything to convert, but I think there's some
in the shared memory initialization code.
regards, tom lane
Hi,
On Sunday, September 30, 2012 10:33:28 PM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
Current version is available at branch ilist in:
git://git.postgresql.org/git/users/andresfreund/postgres.git
ssh://git@git.postgresql.org/users/andresfreund/postgres.gitI'm still pretty desperately unhappy with your insistence on circularly
linked dlists. Not only does that make initialization problematic,
but now it's not even consistent with slists.
The slist code originally grew out of the dlist code and thus was pretty
similar, but at some point (after your dislike of the circular lists?, not
sure) I thought about it again and found no efficiency differences for any of
the common manipulations in single linked lists because you don't need to deal
with prev and tail pointers, so I saw no point in insisting there. No external
user should care.
A possible compromise that would fix the initialization issue is to
allow an empty dlist to be represented as *either* two NULL pointers
or a pair of self-pointers. Conversion from one case to the other
could happen like this:
It appears to me that of the inline'able functions, only dlist_push_head
and dlist_push_tail would need to do this; the others presume nonempty
lists and so wouldn't have to deal with the NULL/NULL case.
dlist_is_empty would need one extra test too.
The problem is that dlist_push_head/tail and and dlist_is_empty are prety hot
code paths.
In transaction reassembly/wal decoding for every wal record that we need to
look at in the context of a transaction the code very roughly does something
like:
/* get change */
Change *change;
if (dlist_is_empty(&apply_cache->cached_changes))
change = dlist_container(..., dlist_pop_head_node(&apply_cache-
cached_changes))
else
change = malloc(...);
/* get data from wal */
fill_change_change(current_wal_record, change);
/* remember change, till TX is complete */
dlist_push_tail(tx->changes, change->node);
(In reality more of those happen, but anyway)
We literally have tens of thousands list manipulation a second if the server is
busy. Iteration only happens once a XLOG_COMMIT/ABORT is found (in combination
with merging the subtransaction's changes).
In the slab allocator I originally built this for it was exactly the same. The
lists are manipulated for every palloc/pfree but only iterated at
MemoryContextReset.
I am really sorry for being stubborn here, but I changed to circular lists
after profiling and finding that pipeline stalls & misprediced branches where
the major thing I could change. Not sure how we can resolv this :(
BTW, the "fast path" in dlist_move_head can't be right can it?
Yea, its crap, thanks for noticing. Shouldn't cause any issues except being
slower, thats why I probably didn't notice I broke it at some point. ->next is
missing.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Sunday, September 30, 2012 10:48:01 PM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
Perhaps we need to decouple _Static_assert support from compound
statement support at some point, but we will see.Yeah, possibly, but until we have an example of a non-gcc-compatible
compiler that can do something equivalent, it's hard to guess how we
might need to alter the autoconf tests. Anyway the important thing
for now is the external specification of the macros, and I think we're
good on that (modulo any better naming suggestions).
I thought msvc supported _Static_assert as well, but after a short search it
seems I misremembered and it only supports static_assert from C++11 (which is
plausible, because I've worked on a C++11 project which was ported to windows
). I don't know how C and C++ compilation modes are different in msvc.
I really don't understand why C and C++ standard development isn't coordinated
more... They seem to come up with annoying incompatibilities all the time.
I'm fairly sure there are already a few cases of Asserts on
compile-time-constant expressions, so I made sure that there was a layer
allowing access to _Static_assert without the type-compatibility code.
I didn't go looking for anything to convert, but I think there's some
in the shared memory initialization code.
Definitely a sensible goal. I wasn't really sure how well the idea would be
received after I asked before and only heard echoes of my excitement and didn't
want to spend too much time on it...
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On 9/30/12 5:42 PM, Andres Freund wrote:
I thought msvc supported _Static_assert as well, but after a short search it
seems I misremembered and it only supports static_assert from C++11 (which is
plausible, because I've worked on a C++11 project which was ported to windows
). I don't know how C and C++ compilation modes are different in msvc.
The issue is rather that the MSVC guys have decided not to bother
supporting C99.
Andres Freund <andres@2ndquadrant.com> writes:
On Sunday, September 30, 2012 10:33:28 PM Tom Lane wrote:
I'm still pretty desperately unhappy with your insistence on circularly
linked dlists. Not only does that make initialization problematic,
but now it's not even consistent with slists.
We literally have tens of thousands list manipulation a second if the server is
busy.
Tens of thousands, with maybe 1ns extra per call, adds up to what?
I am really sorry for being stubborn here, but I changed to circular lists
after profiling and finding that pipeline stalls & misprediced branches where
the major thing I could change. Not sure how we can resolv this :(
I'm going to be stubborn too. I think you're allowing very small
micro-optimization arguments to contort the design of a fundamental data
structure, in a way that makes it harder to use. That's not a tradeoff
I like. Especially when the micro-optimization isn't even uniformly a
win. I remain of the opinion that the extra cycles spent on iteration
(which are real despite your denials) will outweigh any savings in list
alteration in many use-cases.
regards, tom lane
On Monday, October 01, 2012 05:33:01 PM Tom Lane wrote:
Andres Freund <andres@2ndquadrant.com> writes:
On Sunday, September 30, 2012 10:33:28 PM Tom Lane wrote:
I'm still pretty desperately unhappy with your insistence on circularly
linked dlists. Not only does that make initialization problematic,
but now it's not even consistent with slists.We literally have tens of thousands list manipulation a second if the
server is busy.Tens of thousands, with maybe 1ns extra per call, adds up to what?
Well, a pipeline stall is a bit more than a ns.
I am really sorry for being stubborn here, but I changed to circular
lists after profiling and finding that pipeline stalls & misprediced
branches where the major thing I could change. Not sure how we can
resolv this :(I'm going to be stubborn too. I think you're allowing very small
micro-optimization arguments to contort the design of a fundamental data
structure, in a way that makes it harder to use. That's not a tradeoff
I like.
Your usability problem is the initialization? Iteration?
dlist_initialiaized_(push_head|push_tail|is_empty)() + your hybrid approach of
checking for NULL at the plain functions?
Especially when the micro-optimization isn't even uniformly a
win. I remain of the opinion that the extra cycles spent on iteration
(which are real despite your denials) will outweigh any savings in list
alteration in many use-cases.
I am not denying that one more register used which possibly leading to one
more register spill can be an efficiency difference. Just that it is not as big
as the differences are for modification.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On 29 September 2012 01:14, Andres Freund <andres@2ndquadrant.com> wrote:
Current version is available at branch ilist in:
git://git.postgresql.org/git/users/andresfreund/postgres.git
ssh://git@git.postgresql.org/users/andresfreund/postgres.git
I have taken a look at the ilist branch, merged into today's master.
This patch includes changes to core, so that certain call sites now
call this new doubly-linked list infrastructure rather than
infrastructure located in dllist.c, which the patch deprecates, per
Andres' proprosal. As a convenience to others, I attach a patch file
of same. I have taken the liberty of altering it so that it now uses
the static assert infrastructure that Tom split off, altered somewhat,
and committed about a week ago, with commits
ea473fb2dee7dfe61f8fbdfac9d9da87d84e564e and
0d0aa5d29175c539db1981be27dbbf059be6f3b1. I haven't altered anything
beyond what is needed to make the patch build against head, including
changes already suggested by Tom; this is still entirely Andres'
patch.
Pendantry: This should be in alphabetical order:
! OBJS = stringinfo.o ilist.o
I notice that the patch (my revision) produces a whole bunch of
warnings like this with Clang, though not GCC:
""""""""
[peter@peterlaptop cache]$ clang -O2 -g -Wall -Wmissing-prototypes
-Wpointer-arith -Wdeclaration-after-statement -Wendif-labels
-Wmissing-format-attribute -Wformat-security -fno-strict-aliasing
-fwrapv -fexcess-precision=standard -g -I../../../../src/include
-D_GNU_SOURCE -D_FORTIFY_SOURCE=2 -I/usr/include/libxml2 -c -o
catcache.o catcache.c -MMD -MP -MF .deps/catcache.Po
clang: warning: argument unused during compilation:
'-fexcess-precision=standard'
catcache.c:457:21: warning: expression result unused [-Wunused-value]
CatCache *ccp = slist_container(CatCache, cc_next,
cache_iter.cur);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../../src/include/lib/ilist.h:721:3: note: expanded from macro
'slist_container'
(AssertVariableIsOfTypeMacro(ptr, slist_node *),...
^
../../../../src/include/c.h:735:2: note: expanded from macro
'AssertVariableIsOfTypeMacro'
StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname),
typename), \
^
../../../../src/include/c.h:710:46: note: expanded from macro 'StaticAssertExpr'
({ StaticAssertStmt(condition, errmessage); true; })
^
../../../../src/include/c.h:185:15: note: expanded from macro 'true'
#define true ((bool) 1)
^ ~
*** SNIP SNIP SNIP ***
catcache.c:1818:21: warning: expression result unused [-Wunused-value]
CatCache *ccp = slist_container(CatCache, cc_next, iter.cur);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../../src/include/lib/ilist.h:722:3: note: expanded from macro
'slist_container'
AssertVariableIsOfTypeMacro(((type*)NULL)->membername,
slist_node), \
^
../../../../src/include/c.h:735:2: note: expanded from macro
'AssertVariableIsOfTypeMacro'
StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname),
typename), \
^
../../../../src/include/c.h:710:46: note: expanded from macro 'StaticAssertExpr'
({ StaticAssertStmt(condition, errmessage); true; })
^
../../../../src/include/c.h:185:15: note: expanded from macro 'true'
#define true ((bool) 1)
^ ~
28 warnings generated.
""""""""
I suppose that this is something that's going to have to be addressed
sooner or later.
This seems kind of arbitrary:
/* The socket number we are listening for connections on */
int PostPortNumber;
+
/* The directory names for Unix socket(s) */
char *Unix_socket_directories;
+
/* The TCP listen address(es) */
char *ListenAddresses;
This looks funny:
+ #endif /* defined(USE_INLINE) ||
+ * defined(ILIST_DEFINE_FUNCTIONS) */
I tend to consider the 80-column thing a recommendation more than a requirement.
A further stylistic gripe is that this:
+ #define dlist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
+ AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))) \
+ )
Should probably look like this:
+ #define dlist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
+ AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))))
This is a little unclear:
+ * dlist_foreach (iter, &db->tables)
+ * {
+ * // inside an *_foreach the iterator's .cur field can be used to access
+ * // the current element
This comment:
+ * Singly linked lists are *not* circularly linked (how could they be?) in
+ * contrast to the doubly linked lists. As no pointer to the last list element
Isn't quite accurate, if I've understood you correctly; it is surely
possible in principle to have a circular singly-linked list.
This could be a little clearer; its "content"?:
+ * This is used to convert a dlist_node* returned by some list
+ * navigation/manipulation back to its content.
I don't think you should refer to --enable-cassert here; it is
better-principled to refer to USE_ASSERT_CHECKING:
+ /*
+ * enable for extra debugging. This is rather expensive, so it's not
enabled by
+ * default even with --enable-cassert.
+ */
+ /* #define ILIST_DEBUG */
As to the substantial issues, I have no strong opinion either way as
to whether or not the doubly-linked list should be circular. It might
be nice to see some numbers, if that's what is informing Andres'
thinking here.
The code appears to be reasonable to me. The example is generally illustrative.
That's all for now.
--
Peter Geoghegan http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services
Attachments:
ilist.2012_10_08.patchapplication/octet-stream; name=ilist.2012_10_08.patchDownload
diff src/backend/lib/Makefile
index 2e1061e..7da1c45
*** a/src/backend/lib/Makefile
--- b/src/backend/lib/Makefile
*************** subdir = src/backend/lib
*** 12,17 ****
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
--- 12,17 ----
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = stringinfo.o ilist.o
include $(top_srcdir)/src/backend/common.mk
diff src/backend/lib/dllist.c
new file mode .
index 52af56a..e69de29
*** a/src/backend/lib/dllist.c
--- b/src/backend/lib/dllist.c
***************
*** 1,214 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.c
- * this is a simple doubly linked list implementation
- * the elements of the lists are void*
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/backend/lib/dllist.c
- *
- *-------------------------------------------------------------------------
- */
- #include "postgres.h"
-
- #include "lib/dllist.h"
-
-
- Dllist *
- DLNewList(void)
- {
- Dllist *l;
-
- l = (Dllist *) palloc(sizeof(Dllist));
-
- l->dll_head = NULL;
- l->dll_tail = NULL;
-
- return l;
- }
-
- void
- DLInitList(Dllist *list)
- {
- list->dll_head = NULL;
- list->dll_tail = NULL;
- }
-
- /*
- * free up a list and all the nodes in it --- but *not* whatever the nodes
- * might point to!
- */
- void
- DLFreeList(Dllist *list)
- {
- Dlelem *curr;
-
- while ((curr = DLRemHead(list)) != NULL)
- pfree(curr);
-
- pfree(list);
- }
-
- Dlelem *
- DLNewElem(void *val)
- {
- Dlelem *e;
-
- e = (Dlelem *) palloc(sizeof(Dlelem));
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- return e;
- }
-
- void
- DLInitElem(Dlelem *e, void *val)
- {
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- }
-
- void
- DLFreeElem(Dlelem *e)
- {
- pfree(e);
- }
-
- void
- DLRemove(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (e->dle_prev)
- e->dle_prev->dle_next = e->dle_next;
- else
- {
- /* must be the head element */
- Assert(e == l->dll_head);
- l->dll_head = e->dle_next;
- }
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_list = NULL;
- }
-
- void
- DLAddHead(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_head)
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
-
- if (l->dll_tail == NULL) /* if this is first element added */
- l->dll_tail = e;
- }
-
- void
- DLAddTail(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_tail)
- l->dll_tail->dle_next = e;
- e->dle_prev = l->dll_tail;
- e->dle_next = NULL;
- l->dll_tail = e;
-
- if (l->dll_head == NULL) /* if this is first element added */
- l->dll_head = e;
- }
-
- Dlelem *
- DLRemHead(Dllist *l)
- {
- /* remove and return the head */
- Dlelem *result = l->dll_head;
-
- if (result == NULL)
- return result;
-
- if (result->dle_next)
- result->dle_next->dle_prev = NULL;
-
- l->dll_head = result->dle_next;
-
- if (result == l->dll_tail) /* if the head is also the tail */
- l->dll_tail = NULL;
-
- result->dle_next = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- Dlelem *
- DLRemTail(Dllist *l)
- {
- /* remove and return the tail */
- Dlelem *result = l->dll_tail;
-
- if (result == NULL)
- return result;
-
- if (result->dle_prev)
- result->dle_prev->dle_next = NULL;
-
- l->dll_tail = result->dle_prev;
-
- if (result == l->dll_head) /* if the tail is also the head */
- l->dll_head = NULL;
-
- result->dle_prev = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- /* Same as DLRemove followed by DLAddHead, but faster */
- void
- DLMoveToFront(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (l->dll_head == e)
- return; /* Fast path if already at front */
-
- Assert(e->dle_prev != NULL); /* since it's not the head */
- e->dle_prev->dle_next = e->dle_next;
-
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
- /* We need not check dll_tail, since there must have been > 1 entry */
- }
--- 0 ----
diff src/backend/lib/ilist.c
new file mode 100644
index ...de4a27d
*** a/src/backend/lib/ilist.c
--- b/src/backend/lib/ilist.c
***************
*** 0 ****
--- 1,117 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+ #include "postgres.h"
+
+ /*
+ * If inlines are in use, include the header normally which will get us only
+ * the function definitions as inlines. But if inlines aren't available,
+ * include the header with ILIST_DEFINE_FUNCTIONS defined; this makes it pour
+ * in regular (not inline) function declarations and the corresponding (non
+ * inline) definitions.
+ */
+ #ifndef USE_INLINE
+ #define ILIST_DEFINE_FUNCTIONS
+ #endif
+
+ #include "lib/ilist.h"
+
+ /*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+ void
+ slist_delete(slist_head *head, slist_node *node)
+ {
+ slist_node *last = &head->head;
+ slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+ #ifdef USE_ASSERT_CHECKING
+ found = true;
+ #endif
+ break;
+ }
+ last = cur;
+ }
+
+ slist_check(head);
+ Assert(found);
+ }
+
+ #ifdef ILIST_DEBUG
+ /*
+ * Verify integrity of a doubly linked list
+ */
+ void
+ dlist_check(dlist_head *head)
+ {
+ dlist_node *cur;
+
+ if (head == NULL || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ /* iterate in forward direction */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ /* iterate in backward direction */
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+ }
+
+ /*
+ * Verify integrity of a singly linked list
+ */
+ void
+ slist_check(slist_head *head)
+ {
+ slist_node *cur;
+
+ if (head == NULL)
+ elog(ERROR, "singly linked is NULL");
+
+ /*
+ * there isn't much we can test in a singly linked list other that it
+ * actually ends sometime, i.e. hasn't introduced a circle or similar
+ */
+ for (cur = head->head.next; cur != NULL; cur = cur->next)
+ ;
+ }
+
+ #endif /* ILIST_DEBUG */
diff src/backend/postmaster/autovacuum.c
index 74db821..c3ac219
*** a/src/backend/postmaster/autovacuum.c
--- b/src/backend/postmaster/autovacuum.c
***************
*** 77,83 ****
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/dllist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
--- 77,83 ----
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
*************** typedef struct avl_dbase
*** 152,157 ****
--- 152,158 ----
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ dlist_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
*************** typedef struct
*** 258,265 ****
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /* the database list in the launcher, and the context that contains it */
! static Dllist *DatabaseList = NULL;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
--- 259,269 ----
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /*
! * the database list (of avl_dbase elements) in the launcher, and the context
! * that contains it
! */
! static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
*************** AutoVacLauncherMain(int argc, char *argv
*** 508,514 ****
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! DatabaseList = NULL;
/*
* Make sure pgstat also considers our stat data as gone. Note: we
--- 512,518 ----
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! dlist_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
*************** AutoVacLauncherMain(int argc, char *argv
*** 576,582 ****
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! Dlelem *elem;
int rc;
/*
--- 580,586 ----
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! avl_dbase *avdb;
int rc;
/*
*************** AutoVacLauncherMain(int argc, char *argv
*** 738,757 ****
/* We're OK to start a new worker */
! elem = DLGetTail(DatabaseList);
! if (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
!
! /*
! * launch a worker if next_worker is right now or it is in the
! * past
! */
! if (TimestampDifferenceExceeds(avdb->adl_next_worker,
! current_time, 0))
! launch_worker(current_time);
! }
! else
{
/*
* Special case when the list is empty: start a worker right away.
--- 742,748 ----
/* We're OK to start a new worker */
! if (dlist_is_empty(&DatabaseList))
{
/*
* Special case when the list is empty: start a worker right away.
*************** AutoVacLauncherMain(int argc, char *argv
*** 763,768 ****
--- 754,776 ----
*/
launch_worker(current_time);
}
+ else
+ {
+ /*
+ * because rebuild_database_list constructs a list with most
+ * distant adl_next_worker first, we obtain our database from the
+ * tail of the list.
+ */
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
+ /*
+ * launch a worker if next_worker is right now or it is in the
+ * past
+ */
+ if (TimestampDifferenceExceeds(avdb->adl_next_worker,
+ current_time, 0))
+ launch_worker(current_time);
+ }
}
/* Normal exit from the autovac launcher is here */
*************** AutoVacLauncherMain(int argc, char *argv
*** 783,789 ****
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! Dlelem *elem;
/*
* We sleep until the next scheduled vacuum. We trust that when the
--- 791,797 ----
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
*************** launcher_determine_sleep(bool canlaunch,
*** 796,809 ****
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if ((elem = DLGetTail(DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
--- 804,818 ----
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if (!dlist_is_empty(&DatabaseList))
{
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
*************** rebuild_database_list(Oid newdb)
*** 867,872 ****
--- 876,882 ----
int score;
int nelems;
HTAB *dbhash;
+ dlist_iter iter;
/* use fresh stats */
autovac_refresh_stats();
*************** rebuild_database_list(Oid newdb)
*** 927,962 ****
}
/* Now insert the databases from the existing list */
! if (DatabaseList != NULL)
{
! Dlelem *elem;
!
! elem = DLGetHead(DatabaseList);
! while (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
!
! elem = DLGetSucc(elem);
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
! }
}
}
--- 937,964 ----
}
/* Now insert the databases from the existing list */
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
}
}
*************** rebuild_database_list(Oid newdb)
*** 987,993 ****
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! DatabaseList = DLNewList();
if (nelems > 0)
{
--- 989,995 ----
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! dlist_init(&DatabaseList);
if (nelems > 0)
{
*************** rebuild_database_list(Oid newdb)
*** 1029,1043 ****
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
! DLAddHead(DatabaseList, elem);
}
}
--- 1031,1043 ----
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
/* later elements should go closer to the head of the list */
! dlist_push_head(&DatabaseList, &db->adl_node);
}
}
*************** do_start_worker(void)
*** 1147,1153 ****
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! Dlelem *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
--- 1147,1153 ----
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! dlist_iter iter;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
*************** do_start_worker(void)
*** 1179,1189 ****
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
! while (elem != NULL)
{
! avl_dbase *dbp = DLE_VAL(elem);
if (dbp->adl_datid == tmp->adw_datid)
{
--- 1179,1188 ----
* autovacuum time yet.
*/
skipit = false;
! dlist_reverse_foreach(iter, &DatabaseList)
{
! avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
if (dbp->adl_datid == tmp->adw_datid)
{
*************** do_start_worker(void)
*** 1200,1206 ****
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
--- 1199,1204 ----
*************** static void
*** 1274,1295 ****
launch_worker(TimestampTz now)
{
Oid dbid;
! Dlelem *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
! while (elem != NULL)
{
! avl_dbase *avdb = DLE_VAL(elem);
if (avdb->adl_datid == dbid)
{
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
--- 1272,1296 ----
launch_worker(TimestampTz now)
{
Oid dbid;
! dlist_iter iter;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
+ bool found = false;
+
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
if (avdb->adl_datid == dbid)
{
+ found = true;
+
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
*************** launch_worker(TimestampTz now)
*** 1297,1306 ****
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! DLMoveToFront(elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
--- 1298,1306 ----
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! dlist_move_head(&DatabaseList, iter.cur);
break;
}
}
/*
*************** launch_worker(TimestampTz now)
*** 1310,1316 ****
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (elem == NULL)
rebuild_database_list(dbid);
}
}
--- 1310,1316 ----
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (!found)
rebuild_database_list(dbid);
}
}
diff src/backend/postmaster/postmaster.c
index dff4c71..7060a1f
*** a/src/backend/postmaster/postmaster.c
--- b/src/backend/postmaster/postmaster.c
***************
*** 95,101 ****
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/dllist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
--- 95,101 ----
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
*************** typedef struct bkend
*** 146,155 ****
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! Dlelem elem; /* list link in BackendList */
} Backend;
! static Dllist *BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
--- 146,155 ----
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! dlist_node elem; /* list link in BackendList */
} Backend;
! static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
*************** static Backend *ShmemBackendArray;
*** 157,164 ****
--- 157,166 ----
/* The socket number we are listening for connections on */
int PostPortNumber;
+
/* The directory names for Unix socket(s) */
char *Unix_socket_directories;
+
/* The TCP listen address(es) */
char *ListenAddresses;
*************** PostmasterMain(int argc, char *argv[])
*** 1028,1038 ****
set_stack_base();
/*
- * Initialize the list of active backends.
- */
- BackendList = DLNewList();
-
- /*
* Initialize pipe (or process handle on Windows) that allows children to
* wake up from sleep on postmaster death.
*/
--- 1030,1035 ----
*************** processCancelRequest(Port *port, void *p
*** 1872,1878 ****
Backend *bp;
#ifndef EXEC_BACKEND
! Dlelem *curr;
#else
int i;
#endif
--- 1869,1875 ----
Backend *bp;
#ifndef EXEC_BACKEND
! dlist_iter iter;
#else
int i;
#endif
*************** processCancelRequest(Port *port, void *p
*** 1886,1894 ****
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! bp = (Backend *) DLE_VAL(curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
--- 1883,1891 ----
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! dlist_foreach(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
*************** static void
*** 2648,2654 ****
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! Dlelem *curr;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
--- 2645,2651 ----
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! dlist_mutable_iter iter;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
*************** CleanupBackend(int pid,
*** 2680,2688 ****
return;
}
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
--- 2677,2685 ----
return;
}
! dlist_foreach_modify(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->pid == pid)
{
*************** CleanupBackend(int pid,
*** 2701,2707 ****
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
break;
}
--- 2698,2704 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
break;
}
*************** CleanupBackend(int pid,
*** 2718,2725 ****
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! Dlelem *curr,
! *next;
Backend *bp;
/*
--- 2715,2721 ----
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! dlist_mutable_iter iter;
Backend *bp;
/*
*************** HandleChildCrash(int pid, int exitstatus
*** 2734,2743 ****
}
/* Process regular backends */
! for (curr = DLGetHead(BackendList); curr; curr = next)
{
! next = DLGetSucc(curr);
! bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
/*
--- 2730,2739 ----
}
/* Process regular backends */
! dlist_foreach_modify(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
!
if (bp->pid == pid)
{
/*
*************** HandleChildCrash(int pid, int exitstatus
*** 2750,2756 ****
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
--- 2746,2752 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
/* Keep looping so we can signal remaining backends */
}
*************** PostmasterStateMachine(void)
*** 3113,3119 ****
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (DLGetHead(BackendList) == NULL &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
--- 3109,3115 ----
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (dlist_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
*************** signal_child(pid_t pid, int signal)
*** 3239,3250 ****
static bool
SignalSomeChildren(int signal, int target)
{
! Dlelem *curr;
bool signaled = false;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 3235,3246 ----
static bool
SignalSomeChildren(int signal, int target)
{
! dlist_iter iter;
bool signaled = false;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
*************** BackendStartup(Port *port)
*** 3382,3389 ****
*/
bn->pid = pid;
bn->is_autovacuum = false;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
--- 3378,3385 ----
*/
bn->pid = pid;
bn->is_autovacuum = false;
! dlist_push_head(&BackendList, &bn->elem);
!
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
*************** PostmasterRandom(void)
*** 4498,4509 ****
static int
CountChildren(int target)
{
! Dlelem *curr;
int cnt = 0;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 4494,4505 ----
static int
CountChildren(int target)
{
! dlist_iter iter;
int cnt = 0;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
*************** StartAutovacuumWorker(void)
*** 4682,4689 ****
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
--- 4678,4684 ----
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! dlist_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
diff src/backend/utils/cache/catcache.c
index d6f6b1c..230ddf4
*** a/src/backend/utils/cache/catcache.c
--- b/src/backend/utils/cache/catcache.c
*************** CatalogCacheComputeTupleHashValue(CatCac
*** 291,297 ****
static void
CatCachePrintStats(int code, Datum arg)
{
! CatCache *cache;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
--- 291,297 ----
static void
CatCachePrintStats(int code, Datum arg)
{
! slist_iter iter;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
*************** CatCachePrintStats(int code, Datum arg)
*** 300,307 ****
long cc_lsearches = 0;
long cc_lhits = 0;
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
--- 300,309 ----
long cc_lsearches = 0;
long cc_lhits = 0;
! slist_foreach(iter, &CacheHdr->ch_caches)
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
*************** CatCacheRemoveCTup(CatCache *cache, CatC
*** 368,375 ****
return; /* nothing left to do */
}
! /* delink from linked list */
! DLRemove(&ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
--- 370,376 ----
return; /* nothing left to do */
}
! dlist_delete(ct->cache_bucket, &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
*************** CatCacheRemoveCList(CatCache *cache, Cat
*** 412,418 ****
}
/* delink from linked list */
! DLRemove(&cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
--- 413,419 ----
}
/* delink from linked list */
! dlist_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
*************** CatCacheRemoveCList(CatCache *cache, Cat
*** 442,459 ****
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! CatCache *ccp;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
! Dlelem *elt,
! *nextelt;
if (cacheId != ccp->id)
continue;
--- 443,460 ----
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! slist_iter cache_iter;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! slist_foreach(cache_iter, &CacheHdr->ch_caches)
{
Index hashIndex;
! dlist_mutable_iter iter;
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
if (cacheId != ccp->id)
continue;
*************** CatalogCacheIdInvalidate(int cacheId, ui
*** 468,478 ****
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 469,477 ----
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! dlist_foreach_modify(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
*************** CatalogCacheIdInvalidate(int cacheId, ui
*** 484,495 ****
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
!
! for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (hashValue == ct->hash_value)
{
--- 483,491 ----
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
! dlist_foreach_modify(iter, &ccp->cc_bucket[hashIndex])
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (hashValue == ct->hash_value)
{
*************** AtEOXact_CatCache(bool isCommit)
*** 557,573 ****
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! CatCache *ccp;
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
! Dlelem *elt;
int i;
/* Check CatCLists */
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
--- 553,570 ----
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! slist_iter cache_iter;
! slist_foreach(cache_iter, &(CacheHdr->ch_caches))
{
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
! dlist_iter iter;
int i;
/* Check CatCLists */
! dlist_foreach(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
*************** AtEOXact_CatCache(bool isCommit)
*** 577,587 ****
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&ccp->cc_bucket[i]);
! elt;
! elt = DLGetSucc(elt))
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
--- 574,584 ----
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! dlist_head *bucket = &ccp->cc_bucket[i];
!
! dlist_foreach(iter, bucket)
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
*************** AtEOXact_CatCache(bool isCommit)
*** 604,619 ****
static void
ResetCatalogCache(CatCache *cache)
{
! Dlelem *elt,
! *nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
! for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 601,613 ----
static void
ResetCatalogCache(CatCache *cache)
{
! dlist_mutable_iter iter;
int i;
/* Remove each list in this cache, or at least mark it dead */
! dlist_foreach_modify(iter, &cache->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
*************** ResetCatalogCache(CatCache *cache)
*** 624,634 ****
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
! {
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
! nextelt = DLGetSucc(elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
--- 618,628 ----
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! dlist_head *bucket = &cache->cc_bucket[i];
! dlist_foreach_modify(iter, bucket)
! {
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
*************** ResetCatalogCache(CatCache *cache)
*** 654,665 ****
void
ResetCatalogCaches(void)
{
! CatCache *cache;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
ResetCatalogCache(cache);
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
--- 648,663 ----
void
ResetCatalogCaches(void)
{
! slist_iter iter;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! slist_foreach(iter, &CacheHdr->ch_caches)
! {
! CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
!
ResetCatalogCache(cache);
+ }
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
*************** ResetCatalogCaches(void)
*** 680,691 ****
void
CatalogCacheFlushCatalog(Oid catId)
{
! CatCache *cache;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
--- 678,691 ----
void
CatalogCacheFlushCatalog(Oid catId)
{
! slist_iter iter;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
*************** InitCatCache(int id,
*** 760,766 ****
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! CacheHdr->ch_caches = NULL;
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
--- 760,766 ----
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
*************** InitCatCache(int id,
*** 770,779 ****
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
/*
* initialize the cache's relation information for the relation
--- 770,777 ----
/*
* allocate a new cache structure
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(dlist_node));
/*
* initialize the cache's relation information for the relation
*************** InitCatCache(int id,
*** 792,797 ****
--- 790,800 ----
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ dlist_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++)
+ dlist_init(&cp->cc_bucket[i]);
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
*************** InitCatCache(int id,
*** 801,808 ****
/*
* add completed cache to top of group header's list
*/
! cp->cc_next = CacheHdr->ch_caches;
! CacheHdr->ch_caches = cp;
/*
* back to the old context before we return...
--- 804,810 ----
/*
* add completed cache to top of group header's list
*/
! slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
/*
* back to the old context before we return...
*************** SearchCatCache(CatCache *cache,
*** 1060,1066 ****
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! Dlelem *elt;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
--- 1062,1069 ----
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! dlist_mutable_iter iter;
! dlist_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
*************** SearchCatCache(CatCache *cache,
*** 1094,1106 ****
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead)
continue; /* ignore dead entries */
--- 1097,1109 ----
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! bucket = &cache->cc_bucket[hashIndex];
!
! dlist_foreach_modify(iter, bucket)
{
bool res;
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead)
continue; /* ignore dead entries */
*************** SearchCatCache(CatCache *cache,
*** 1125,1131 ****
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! DLMoveToFront(&ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
--- 1128,1134 ----
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! dlist_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
*************** SearchCatCacheList(CatCache *cache,
*** 1340,1346 ****
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! Dlelem *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
--- 1343,1349 ----
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! dlist_iter iter;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
*************** SearchCatCacheList(CatCache *cache,
*** 1382,1394 ****
/*
* scan the items until we find a match or exhaust our list
*/
! for (elt = DLGetHead(&cache->cc_lists);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! cl = (CatCList *) DLE_VAL(elt);
if (cl->dead)
continue; /* ignore dead entries */
--- 1385,1395 ----
/*
* scan the items until we find a match or exhaust our list
*/
! dlist_foreach(iter, &cache->cc_lists)
{
bool res;
! cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->dead)
continue; /* ignore dead entries */
*************** SearchCatCacheList(CatCache *cache,
*** 1416,1422 ****
* since there's no point in that unless they are searched for
* individually.)
*/
! DLMoveToFront(&cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
--- 1417,1423 ----
* since there's no point in that unless they are searched for
* individually.)
*/
! dlist_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
*************** SearchCatCacheList(CatCache *cache,
*** 1468,1473 ****
--- 1469,1476 ----
{
uint32 hashValue;
Index hashIndex;
+ bool found = false;
+ dlist_head *bucket;
/*
* See if there's an entry for this tuple already.
*************** SearchCatCacheList(CatCache *cache,
*** 1476,1486 ****
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
--- 1479,1488 ----
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! bucket = &cache->cc_bucket[hashIndex];
! dlist_foreach(iter, bucket)
{
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
*************** SearchCatCacheList(CatCache *cache,
*** 1498,1507 ****
if (ct->c_list)
continue;
break; /* A-OK */
}
! if (elt == NULL)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
--- 1500,1510 ----
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
! if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
*************** SearchCatCacheList(CatCache *cache,
*** 1564,1570 ****
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
--- 1567,1572 ----
*************** SearchCatCacheList(CatCache *cache,
*** 1587,1593 ****
}
Assert(i == nmembers);
! DLAddHead(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
--- 1589,1595 ----
}
Assert(i == nmembers);
! dlist_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
*************** CatalogCacheCreateEntry(CatCache *cache,
*** 1664,1677 ****
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! DLInitElem(&ct->cache_elem, (void *) ct);
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
--- 1666,1680 ----
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! ct->cache_bucket = &cache->cc_bucket[hashIndex];
!
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! dlist_push_head(ct->cache_bucket, &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
*************** PrepareToInvalidateCacheTuple(Relation r
*** 1785,1791 ****
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! CatCache *ccp;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
--- 1788,1794 ----
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! slist_iter iter;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
*************** PrepareToInvalidateCacheTuple(Relation r
*** 1808,1817 ****
* ----------------
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
uint32 hashvalue;
Oid dbid;
if (ccp->cc_reloid != reloid)
continue;
--- 1811,1821 ----
* ----------------
*/
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
uint32 hashvalue;
Oid dbid;
+ CatCache *ccp = slist_container(CatCache, cc_next, iter.cur);
if (ccp->cc_reloid != reloid)
continue;
diff src/include/lib/dllist.h
new file mode .
index 25ed64c..e69de29
*** a/src/include/lib/dllist.h
--- b/src/include/lib/dllist.h
***************
*** 1,85 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.h
- * simple doubly linked list primitives
- * the elements of the list are void* so the lists can contain anything
- * Dlelem can only be in one list at a time
- *
- *
- * Here's a small example of how to use Dllists:
- *
- * Dllist *lst;
- * Dlelem *elt;
- * void *in_stuff; -- stuff to stick in the list
- * void *out_stuff
- *
- * lst = DLNewList(); -- make a new dllist
- * DLAddHead(lst, DLNewElem(in_stuff)); -- add a new element to the list
- * with in_stuff as the value
- * ...
- * elt = DLGetHead(lst); -- retrieve the head element
- * out_stuff = (void*)DLE_VAL(elt); -- get the stuff out
- * DLRemove(elt); -- removes the element from its list
- * DLFreeElem(elt); -- free the element since we don't
- * use it anymore
- *
- *
- * It is also possible to use Dllist objects that are embedded in larger
- * structures instead of being separately malloc'd. To do this, use
- * DLInitElem() to initialize a Dllist field within a larger object.
- * Don't forget to DLRemove() each field from its list (if any) before
- * freeing the larger object!
- *
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/include/lib/dllist.h
- *
- *-------------------------------------------------------------------------
- */
-
- #ifndef DLLIST_H
- #define DLLIST_H
-
- struct Dllist;
- struct Dlelem;
-
- typedef struct Dlelem
- {
- struct Dlelem *dle_next; /* next element */
- struct Dlelem *dle_prev; /* previous element */
- void *dle_val; /* value of the element */
- struct Dllist *dle_list; /* what list this element is in */
- } Dlelem;
-
- typedef struct Dllist
- {
- Dlelem *dll_head;
- Dlelem *dll_tail;
- } Dllist;
-
- extern Dllist *DLNewList(void); /* allocate and initialize a list header */
- extern void DLInitList(Dllist *list); /* init a header alloced by caller */
- extern void DLFreeList(Dllist *list); /* free up a list and all the nodes in
- * it */
- extern Dlelem *DLNewElem(void *val);
- extern void DLInitElem(Dlelem *e, void *val);
- extern void DLFreeElem(Dlelem *e);
- extern void DLRemove(Dlelem *e); /* removes node from list */
- extern void DLAddHead(Dllist *list, Dlelem *node);
- extern void DLAddTail(Dllist *list, Dlelem *node);
- extern Dlelem *DLRemHead(Dllist *list); /* remove and return the head */
- extern Dlelem *DLRemTail(Dllist *list);
- extern void DLMoveToFront(Dlelem *e); /* move node to front of its list */
-
- /* These are macros for speed */
- #define DLGetHead(list) ((list)->dll_head)
- #define DLGetTail(list) ((list)->dll_tail)
- #define DLGetSucc(elem) ((elem)->dle_next)
- #define DLGetPred(elem) ((elem)->dle_prev)
- #define DLGetListHdr(elem) ((elem)->dle_list)
-
- #define DLE_VAL(elem) ((elem)->dle_val)
-
- #endif /* DLLIST_H */
--- 0 ----
diff src/include/lib/ilist.h
new file mode 100644
index ...9bc97dc
*** a/src/include/lib/ilist.h
--- b/src/include/lib/ilist.h
***************
*** 0 ****
--- 1,760 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure.
+
+ * Also, the circular lists are always circularly linked, even when empty; this
+ * enables many manipulations to be done without branches, which is beneficial
+ * performance-wise.
+ *
+ * NOTES:
+ *
+ * This is intended to be used in situations where memory for a struct and its
+ * contents already needs to be allocated and the overhead of allocating extra
+ * list cells for every list element is noticeable. The API for singly/doubly
+ * linked lists is identical as far as capabilities of both allow.
+ *
+ * // A oversimplified example demonstrating how this can be used:
+ *
+ * #include "lib/ilist.h"
+ *
+ * // Lets assume we want to store information about the tables contained in a
+ * // database.
+ *
+ * // Define struct for the databases including a list header that will be used
+ * // to access the nodes in the list later on.
+ * typedef struct my_database
+ * {
+ * char* datname;
+ * dlist_head tables;
+ * ...
+ * } my_database;
+ *
+ * // Define struct for the tables. Note the list_node element which stores
+ * // information about prev/next list nodes.
+ * typedef struct my_table
+ * {
+ * char* tablename;
+ * dlist_node list_node;
+ * perm_t permissions;
+ * ...
+ * } value_in_a_list;
+ *
+ * // create a database
+ * my_database *db = create_database();
+ *
+ * // and a few tables
+ * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
+ * ...
+ * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
+ * ...
+ * ...
+ * // to iterate over the table we allocate an iterator element to store
+ * // information about the current position
+ * dlist_iter iter;
+ *
+ * dlist_foreach (iter, &db->tables)
+ * {
+ * // inside an *_foreach the iterator's .cur field can be used to access
+ * // the current element
+ * // iter.cur points to a 'dlist_node', but we want the actual table
+ * // information, use dlist_container to convert
+ * my_table *tbl = dlist_container(my_table, list_node, iter->cur);
+ * elog(NOTICE, 'we have a table: %s in database %s',
+ * val->tablename, db->datname);
+ * }
+ *
+ * // while a simple iteration is useful we sometimes also want to manipulate
+ * // the list while iterating. Say, we want to delete all tables!
+ *
+ * // declare an iterator that allows some list manipulations
+ * dlist_mutable_iter miter;
+ *
+ * // iterate
+ * dlist_foreach_modify(miter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, iter->cur);
+ * // unlink the current table from the linked list
+ * dlist_delete(&db->tables, iter->cur);
+ * // as ilists never manage memory, we can freely access the table
+ * drop_table(db, tbl);
+ * }
+ *
+ * // Note that none of the dlist_* functions did do any memory
+ * // management. They just manipulated externally managed memory.
+ *
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+ #ifndef ILIST_H
+ #define ILIST_H
+
+ /*
+ * enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even with --enable-cassert.
+ */
+ /* #define ILIST_DEBUG */
+
+ /*
+ * Node of a doubly linked list.
+ *
+ * Embed this in structs that need to be part of a doubly linked list.
+ */
+ typedef struct dlist_node dlist_node;
+ struct dlist_node
+ {
+ dlist_node *prev;
+ dlist_node *next;
+ };
+
+ /*
+ * Head of a doubly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty. Circular
+ * lists have the advantage of not needing any branches in the most common list
+ * manipulations.
+ */
+ typedef struct dlist_head
+ {
+ /*
+ * head->next either points to the first element of the list or to &head
+ * if empty.
+ *
+ * head->prev either points to the last element of the list or to &head if
+ * empty.
+ */
+ dlist_node head;
+ } dlist_head;
+
+
+ /*
+ * Doubly linked list iterator.
+ *
+ * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
+ * current element of the iteration use the 'cur' member.
+ *
+ * Iterations using this are *not* allowed to change the list while iterating!
+ *
+ * NB: We use an extra type for this to make it possible to avoid multiple
+ * evaluations of arguments in the dlist_foreach() macro.
+ */
+ typedef struct dlist_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ } dlist_iter;
+
+ /*
+ * Doubly linked list iterator allowing some modifications while iterating
+ *
+ * Used as state in dlist_foreach_modify(). To get the current element of the
+ * iteration use the 'cur' member.
+ *
+ * Iterations using this are only allowed to change the list *at the current
+ * point of iteration*. It is fine to delete the current node, but it is *not*
+ * fine to modify other nodes.
+ *
+ * NB: We need a separate type for mutable iterations to avoid having to pass
+ * in two iterators or some other state variable as we need to store the
+ * '->next' node of the current node so it can be deleted or modified by the
+ * user.
+ */
+ typedef struct dlist_mutable_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ dlist_node *next; /* next node we iterate to, so we can delete
+ * cur */
+ } dlist_mutable_iter;
+
+ /*
+ * Node of a singly linked list.
+ *
+ * Embed this in structs that need to be part of a singly linked list.
+ */
+ typedef struct slist_node slist_node;
+ struct slist_node
+ {
+ slist_node *next;
+ };
+
+ /*
+ * Head of a singly linked list.
+ *
+ * Singly linked lists are *not* circularly linked (how could they be?) in
+ * contrast to the doubly linked lists. As no pointer to the last list element
+ * and to the previous node needs to be maintained this doesn't incur any
+ * additional branches in the usual manipulations.
+ */
+ typedef struct slist_head
+ {
+ slist_node head;
+ } slist_head;
+
+ /*
+ * Singly linked list iterator
+ *
+ * Used in slist_foreach(). To get the current element of the iteration use the
+ * 'cur' member.
+ *
+ * Do *not* manipulate the list while iterating!
+ *
+ * NB: this wouldn't really need to be an extra struct, we could use a
+ * slist_node * directly. For consistency reasons with dlist_*iter and
+ * slist_mutable_iter we still use a separate type.
+ */
+ typedef struct slist_iter
+ {
+ slist_node *cur;
+ } slist_iter;
+
+ /*
+ * Singly linked list iterator allowing some modifications while iterating
+ *
+ * Used in slist_foreach_modify.
+ *
+ * Iterations using this are allowed to remove the current node and to add more
+ * nodes to the beginning of the list.
+ */
+ typedef struct slist_mutable_iter
+ {
+ slist_node *cur;
+ slist_node *next;
+ } slist_mutable_iter;
+
+
+ /*
+ * We take quite some pain to allow this 'module' to be used on compilers
+ * without usable 'static inline' support. If configure detects its not
+ * available all the inline functions will be defined in ilist.c instead by
+ * #define'ing ILIST_USE_DEFINITION there.
+ */
+ #ifdef USE_INLINE
+ #define INLINE_IF_POSSIBLE static inline
+ #else
+ /* hide inline declarations from compiler */
+ #define INLINE_IF_POSSIBLE
+
+ /* Prototypes for functions we want to be inlined if possible */
+ extern void dlist_init(dlist_head *head);
+ extern bool dlist_is_empty(dlist_head *head);
+ extern void dlist_push_head(dlist_head *head, dlist_node *node);
+ extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+ extern void dlist_insert_after(dlist_head *head,
+ dlist_node *after, dlist_node *node);
+ extern void dlist_insert_before(dlist_head *head,
+ dlist_node *before, dlist_node *node);
+ extern void dlist_delete(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_pop_head_node(dlist_head *head);
+ extern void dlist_move_head(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_next(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_prev(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_next_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_prev_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_head_node(dlist_head *head);
+ extern dlist_node *dlist_tail_node(dlist_head *head);
+
+ /* dlist macro support functions */
+ extern void *dlist_tail_element_off(dlist_head *head, size_t off);
+ extern void *dlist_head_element_off(dlist_head *head, size_t off);
+
+ extern void slist_init(slist_head *head);
+ extern bool slist_is_empty(slist_head *head);
+ extern slist_node *slist_head_node(slist_head *head);
+ extern void slist_push_head(slist_head *head, slist_node *node);
+ extern slist_node *slist_pop_head_node(slist_head *head);
+ extern void slist_insert_after(slist_head *head,
+ slist_node *after, slist_node *node);
+ extern bool slist_has_next(slist_head *head, slist_node *node);
+ extern slist_node *slist_next_node(slist_head *head, slist_node *node);
+
+ /* slist macro support function */
+ extern void *slist_head_element_off(slist_head *head, size_t off);
+ #endif /* !USE_INLINE */
+
+ /* These functions are too big to be inline, so they are in the C file always */
+
+ /* Attention: O(n) */
+ extern void slist_delete(slist_head *head, slist_node *node);
+
+ #ifdef ILIST_DEBUG
+ extern void dlist_check(dlist_head *head);
+ extern void slist_check(slist_head *head);
+ #else
+ /*
+ * These seemingly useless casts to void are here to keep the compiler quiet
+ * about the argument being unused in many functions in a non-debug compile,
+ * in which functions the only point of passing the list head pointer is to be
+ * able to run these checks.
+ */
+ #define dlist_check(head) (void) (head)
+ #define slist_check(head) (void) (head)
+ #endif /* ILIST_DEBUG */
+
+ #define DLIST_STATIC_INIT(name) {{&name.head, &name.head}}
+ #define SLIST_STATIC_INIT(name) {{NULL}}
+
+ /*
+ * The following function definitions are only used if inlining is supported by
+ * the compiler, or when included from a file that explicitly declares
+ * ILIST_USE_DEFINITION.
+ */
+ #if defined(USE_INLINE) || defined(ILIST_DEFINE_FUNCTIONS)
+
+ /*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_init(dlist_head *head)
+ {
+ head->head.next = head->head.prev = &head->head;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node at the beginning of the list.
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_push_head(dlist_head *head, dlist_node *node)
+ {
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Inserts a node at the end of the list.
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_push_tail(dlist_head *head, dlist_node *node)
+ {
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node after another *in the same list*
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_insert_after(dlist_head *head, dlist_node *after,
+ dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node before another *in the same list*
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_insert_before(dlist_head *head, dlist_node *before,
+ dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete 'node' from list.
+ *
+ * It is not allowed to delete a 'node' which is is not in the list 'head'
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_delete(dlist_head *head, dlist_node *node)
+ {
+ dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete and return the first node from a list.
+ *
+ * Undefined behaviour when the list is empty, check with dlist_is_empty if
+ * necessary.
+ */
+ INLINE_IF_POSSIBLE dlist_node *
+ dlist_pop_head_node(dlist_head *head)
+ {
+ dlist_node *ret;
+
+ Assert(&head->head != head->head.next);
+
+ ret = head->head.next;
+ dlist_delete(head, head->head.next);
+ return ret;
+ }
+
+ /*
+ * Move element from any position in the list to the head position in the same
+ * list.
+ *
+ * Undefined behaviour if 'node' is not already part of the list.
+ */
+ INLINE_IF_POSSIBLE void
+ dlist_move_head(dlist_head *head, dlist_node *node)
+ {
+ /* fast path if it's already at the head */
+ if (&head->head == node)
+ return;
+
+ dlist_delete(head, node);
+ dlist_push_head(head, node);
+
+ dlist_check(head);
+ }
+
+ /*
+ * Check whether the passed node is the last element in the list.
+ */
+ INLINE_IF_POSSIBLE bool
+ dlist_has_next(dlist_head *head, dlist_node *node)
+ {
+ return node->next != &head->head;
+ }
+
+ /*
+ * Check whether the passed node is the first element in the list.
+ */
+ INLINE_IF_POSSIBLE bool
+ dlist_has_prev(dlist_head *head, dlist_node *node)
+ {
+ return node->prev != &head->head;
+ }
+
+ /*
+ * Return the next node in the list.
+ *
+ * Undefined bheaviour when no next node exists, use dlist_has_next to make
+ * sure.
+ */
+ INLINE_IF_POSSIBLE dlist_node *
+ dlist_next_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_next(head, node));
+ return node->next;
+ }
+
+ /*
+ * Return previous node if there is one, NULL otherwise
+ *
+ * Undefined bheaviour when no prev node exists, use dlist_has_prev to make
+ * sure.
+ */
+ INLINE_IF_POSSIBLE dlist_node *
+ dlist_prev_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_prev(head, node));
+ return node->prev;
+ }
+
+ /*
+ * Return whether the list is empty.
+ */
+ INLINE_IF_POSSIBLE bool
+ dlist_is_empty(dlist_head *head)
+ {
+ return head->head.next == &(head->head);
+ }
+
+ /* internal support function */
+ INLINE_IF_POSSIBLE void *
+ dlist_head_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Return the first node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ INLINE_IF_POSSIBLE dlist_node *
+ dlist_head_node(dlist_head *head)
+ {
+ return dlist_head_element_off(head, 0);
+ }
+
+ /* internal support function */
+ INLINE_IF_POSSIBLE void *
+ dlist_tail_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.prev - off;
+ }
+
+ /*
+ * Return the last node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ INLINE_IF_POSSIBLE dlist_node *
+ dlist_tail_node(dlist_head *head)
+ {
+ return dlist_tail_element_off(head, 0);
+ }
+ #endif /* defined(USE_INLINE) ||
+ * defined(ILIST_DEFINE_FUNCTIONS) */
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the dlist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a dlist_node* returned by some list
+ * navigation/manipulation back to its content.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
+ AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))) \
+ )
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list may not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_get_head_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list may not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_tail_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_tail_element_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->next)
+
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+ #define dlist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_mutable_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next, \
+ iter.next = iter.cur->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.next, iter.next = iter.cur->next)
+
+ /*
+ * Iterate through the list in reverse order.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_reverse_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->prev; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->prev)
+
+ #if defined(USE_INLINE) || defined(ILIST_DEFINE_FUNCTIONS)
+
+ /*
+ * Initialize a singly linked list.
+ */
+ INLINE_IF_POSSIBLE void
+ slist_init(slist_head *head)
+ {
+ head->head.next = NULL;
+
+ slist_check(head);
+ }
+
+ /*
+ * Is the list empty?
+ */
+ INLINE_IF_POSSIBLE bool
+ slist_is_empty(slist_head *head)
+ {
+ slist_check(head);
+
+ return head->head.next == NULL;
+ }
+
+ /* internal support function */
+ INLINE_IF_POSSIBLE void *
+ slist_head_element_off(slist_head *head, size_t off)
+ {
+ Assert(!slist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Push 'node' as the new first node in the list, pushing the original head to
+ * the second position.
+ */
+ INLINE_IF_POSSIBLE void
+ slist_push_head(slist_head *head, slist_node *node)
+ {
+ node->next = head->head.next;
+ head->head.next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Remove and return the first node in the list
+ *
+ * Undefined behaviour if the list is empty.
+ */
+ INLINE_IF_POSSIBLE slist_node *
+ slist_pop_head_node(slist_head *head)
+ {
+ slist_node *node;
+
+ Assert(!slist_is_empty(head));
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ slist_check(head);
+
+ return node;
+ }
+
+ /*
+ * Insert a new node after another one
+ *
+ * Undefined behaviour if 'after' is not part of the list already.
+ */
+ INLINE_IF_POSSIBLE void
+ slist_insert_after(slist_head *head, slist_node *after,
+ slist_node *node)
+ {
+ node->next = after->next;
+ after->next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Return whether 'node' has a following node
+ */
+ INLINE_IF_POSSIBLE bool
+ slist_has_next(slist_head *head,
+ slist_node *node)
+ {
+ slist_check(head);
+
+ return node->next != NULL;
+ }
+ #endif /*-- defined(USE_INLINE || ILIST_DEFINE_FUNCTIONS) --*/
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the slist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a slist_node* returned by some list
+ * navigation/manipulation back to its content.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define slist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, slist_node *), \
+ AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))) \
+ )
+ /*
+ * Return the value of first element in the list.
+ */
+ #define slist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ slist_head_element_off(ptr, offsetoff(type, membername)))
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter'.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define slist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next; \
+ iter.cur != NULL; \
+ iter.cur = iter.cur->next)
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter' allowing some
+ * modifications.
+ *
+ * It is allowed to delete the current element from the list and add new nodes
+ * before the current position. Every other manipulation can lead to
+ * corruption.
+ */
+ #define slist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_mutable_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next, \
+ iter.next = iter.cur ? iter.cur->next : NULL; \
+ iter.cur != NULL; \
+ iter.cur = iter.next, iter.next = iter.next ? iter.next->next : NULL)
+
+ #endif /* ILIST_H */
diff src/include/utils/catcache.h
index d91700a..cc6dab2
*** a/src/include/utils/catcache.h
--- b/src/include/utils/catcache.h
***************
*** 22,28 ****
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/dllist.h"
#include "utils/relcache.h"
/*
--- 22,28 ----
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/ilist.h"
#include "utils/relcache.h"
/*
***************
*** 37,43 ****
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! struct catcache *cc_next; /* link to next catcache */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
--- 37,43 ----
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! slist_node cc_next; /* list link */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
*************** typedef struct catcache
*** 51,57 ****
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! Dllist cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
--- 51,57 ----
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! dlist_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
*************** typedef struct catcache
*** 66,72 ****
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
--- 66,72 ----
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! dlist_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
*************** typedef struct catctup
*** 77,87 ****
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a Dllist that stores the elements
! * of its hash bucket. We keep each Dllist in LRU order to speed repeated
* lookups.
*/
! Dlelem cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
--- 77,88 ----
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a dlist that stores the elements
! * of its hash bucket. We keep each dlist in LRU order to speed repeated
* lookups.
*/
! dlist_node cache_elem; /* list member of per-bucket list */
! dlist_head *cache_bucket; /* containing bucket dlist */
/*
* The tuple may also be a member of at most one CatCList. (If a single
*************** typedef struct catclist
*** 139,145 ****
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! Dlelem cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--- 140,146 ----
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! dlist_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
*************** typedef struct catclist
*** 153,159 ****
typedef struct catcacheheader
{
! CatCache *ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
--- 154,160 ----
typedef struct catcacheheader
{
! slist_head ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
Hi Peter,
On Monday, October 08, 2012 09:43:51 PM Peter Geoghegan wrote:
Pendantry: This should be in alphabetical order:
! OBJS = stringinfo.o ilist.o
Argh. Youve said that before. Somehow I reintroduced it...
I notice that the patch (my revision) produces a whole bunch of
warnings like this with Clang, though not GCC:""""""""
[peter@peterlaptop cache]$ clang -O2 -g -Wall -Wmissing-prototypes
-Wpointer-arith -Wdeclaration-after-statement -Wendif-labels
-Wmissing-format-attribute -Wformat-security -fno-strict-aliasing
-fwrapv -fexcess-precision=standard -g -I../../../../src/include
-D_GNU_SOURCE -D_FORTIFY_SOURCE=2 -I/usr/include/libxml2 -c -o
catcache.o catcache.c -MMD -MP -MF .deps/catcache.Po
clang: warning: argument unused during compilation:
'-fexcess-precision=standard'
catcache.c:457:21: warning: expression result unused [-Wunused-value]
CatCache *ccp = slist_container(CatCache, cc_next,
cache_iter.cur);^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../../src/include/lib/ilist.h:721:3: note: expanded from macro
'slist_container'
(AssertVariableIsOfTypeMacro(ptr, slist_node *),...
^
../../../../src/include/c.h:735:2: note: expanded from macro
'AssertVariableIsOfTypeMacro'
StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname),
typename), \
^
../../../../src/include/c.h:710:46: note: expanded from macro
'StaticAssertExpr' ({ StaticAssertStmt(condition, errmessage); true; })
^
../../../../src/include/c.h:185:15: note: expanded from macro 'true'
#define true ((bool) 1)
^ ~*** SNIP SNIP SNIP ***
catcache.c:1818:21: warning: expression result unused [-Wunused-value]
CatCache *ccp = slist_container(CatCache, cc_next,
iter.cur); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../../src/include/lib/ilist.h:722:3: note: expanded from macro
'slist_container'
AssertVariableIsOfTypeMacro(((type*)NULL)->membername,
slist_node), \
^
../../../../src/include/c.h:735:2: note: expanded from macro
'AssertVariableIsOfTypeMacro'
StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname),
typename), \
^
../../../../src/include/c.h:710:46: note: expanded from macro
'StaticAssertExpr' ({ StaticAssertStmt(condition, errmessage); true; })
^
../../../../src/include/c.h:185:15: note: expanded from macro 'true'
#define true ((bool) 1)
^ ~
28 warnings generated.""""""""
I suppose that this is something that's going to have to be addressed
sooner or later.
That looks like an annoying warning. Split of StaticAssertExpr into
StaticAssertExpr and StaticAssertExprBool?
This seems kind of arbitrary:
/* The socket number we are listening for connections on */
int PostPortNumber;
+
/* The directory names for Unix socket(s) */
char *Unix_socket_directories;
+
/* The TCP listen address(es) */
char *ListenAddresses;
Yep, no idea why I added the spaces.
This looks funny:
+ #endif /* defined(USE_INLINE) || + * defined(ILIST_DEFINE_FUNCTIONS) */I tend to consider the 80-column thing a recommendation more than a
requirement.
Thats pgindent's doing. I couldn't convince it not to do that short of making
it a multiline comment with ----'s.
A further stylistic gripe is that this:
+ #define dlist_container(type, membername, ptr)
\
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \ + AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \ + ((type *)((char *)(ptr) - offsetof(type, membername)))
\
+ )
Should probably look like this:
+ #define dlist_container(type, membername, ptr)
\
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \ + AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \ + ((type *)((char *)(ptr) - offsetof(type, membername))))
Why? I find the former better readable.
This is a little unclear:
+ * dlist_foreach (iter, &db->tables) + * { + * // inside an *_foreach the iterator's .cur field can be used to access + * // the current element
This comment:
+ * Singly linked lists are *not* circularly linked (how could they be?)
in + * contrast to the doubly linked lists. As no pointer to the last
list elementIsn't quite accurate, if I've understood you correctly; it is surely
possible in principle to have a circular singly-linked list.
Its complete crap. One shouldn't write comments after a 2h delayed 6h train
ride. No idea what exactly warped my mind there.
This could be a little clearer; its "content"?:
+ * This is used to convert a dlist_node* returned by some list + * navigation/manipulation back to its content.
Youre right.'containing element'? 'containing struct'?
I don't think you should refer to --enable-cassert here; it is
better-principled to refer to USE_ASSERT_CHECKING:
Fine with me.
That's all for now.
Thanks.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Here's a new version of this patch, updated to STATIC_IF_INLINE. It
addresses most stuff mentioned so far, excepting only the ugly clang
warning in the static assert macros as pointed out by Peter. The
explanatory comment at the top of ilist.h got reformatted a bit and
slightly reworded as well.
The first file, ilist-4.patch, follows the same semantics implemented by
Andres originally: doubly linked lists are always circular, even when
empty. If you apply the second file (ilist-4-circular.patch) on top of
that, you get lists that can be validly initialized to two NULL
pointers. catcache.c gets an example of such lists: instead of calling
dlist_init repeatedly for each cache, we just MemSet() the whole bunch.
I also included two new functions in that patch, dlisti_push_head and
dlisti_push_tail. These functions are identical to dlist_push_head and
dlist_push_tail, except that they do not accept non-circular lists.
What this means is that callers that find the non-circularity acceptable
can use the regular version, and will run dlist_init() on demand;
callers that want the super-tight code can use the other version.
I didn't bother with a new dlist_is_empty.
I imagine both sides will have much to say about this approach. Please
opine.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
ilist-4.patchtext/x-diff; charset=us-asciiDownload
*** a/src/backend/lib/Makefile
--- b/src/backend/lib/Makefile
***************
*** 12,17 **** subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
--- 12,17 ----
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = ilist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
*** a/src/backend/lib/dllist.c
--- /dev/null
***************
*** 1,214 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.c
- * this is a simple doubly linked list implementation
- * the elements of the lists are void*
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/backend/lib/dllist.c
- *
- *-------------------------------------------------------------------------
- */
- #include "postgres.h"
-
- #include "lib/dllist.h"
-
-
- Dllist *
- DLNewList(void)
- {
- Dllist *l;
-
- l = (Dllist *) palloc(sizeof(Dllist));
-
- l->dll_head = NULL;
- l->dll_tail = NULL;
-
- return l;
- }
-
- void
- DLInitList(Dllist *list)
- {
- list->dll_head = NULL;
- list->dll_tail = NULL;
- }
-
- /*
- * free up a list and all the nodes in it --- but *not* whatever the nodes
- * might point to!
- */
- void
- DLFreeList(Dllist *list)
- {
- Dlelem *curr;
-
- while ((curr = DLRemHead(list)) != NULL)
- pfree(curr);
-
- pfree(list);
- }
-
- Dlelem *
- DLNewElem(void *val)
- {
- Dlelem *e;
-
- e = (Dlelem *) palloc(sizeof(Dlelem));
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- return e;
- }
-
- void
- DLInitElem(Dlelem *e, void *val)
- {
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- }
-
- void
- DLFreeElem(Dlelem *e)
- {
- pfree(e);
- }
-
- void
- DLRemove(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (e->dle_prev)
- e->dle_prev->dle_next = e->dle_next;
- else
- {
- /* must be the head element */
- Assert(e == l->dll_head);
- l->dll_head = e->dle_next;
- }
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_list = NULL;
- }
-
- void
- DLAddHead(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_head)
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
-
- if (l->dll_tail == NULL) /* if this is first element added */
- l->dll_tail = e;
- }
-
- void
- DLAddTail(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_tail)
- l->dll_tail->dle_next = e;
- e->dle_prev = l->dll_tail;
- e->dle_next = NULL;
- l->dll_tail = e;
-
- if (l->dll_head == NULL) /* if this is first element added */
- l->dll_head = e;
- }
-
- Dlelem *
- DLRemHead(Dllist *l)
- {
- /* remove and return the head */
- Dlelem *result = l->dll_head;
-
- if (result == NULL)
- return result;
-
- if (result->dle_next)
- result->dle_next->dle_prev = NULL;
-
- l->dll_head = result->dle_next;
-
- if (result == l->dll_tail) /* if the head is also the tail */
- l->dll_tail = NULL;
-
- result->dle_next = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- Dlelem *
- DLRemTail(Dllist *l)
- {
- /* remove and return the tail */
- Dlelem *result = l->dll_tail;
-
- if (result == NULL)
- return result;
-
- if (result->dle_prev)
- result->dle_prev->dle_next = NULL;
-
- l->dll_tail = result->dle_prev;
-
- if (result == l->dll_head) /* if the tail is also the head */
- l->dll_head = NULL;
-
- result->dle_prev = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- /* Same as DLRemove followed by DLAddHead, but faster */
- void
- DLMoveToFront(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (l->dll_head == e)
- return; /* Fast path if already at front */
-
- Assert(e->dle_prev != NULL); /* since it's not the head */
- e->dle_prev->dle_next = e->dle_next;
-
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
- /* We need not check dll_tail, since there must have been > 1 entry */
- }
--- 0 ----
*** /dev/null
--- b/src/backend/lib/ilist.c
***************
*** 0 ****
--- 1,109 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+ #include "postgres.h"
+
+ /* See ilist.h */
+ #define ILIST_INCLUDE_DEFINITIONS
+
+ #include "lib/ilist.h"
+
+ /*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+ void
+ slist_delete(slist_head *head, slist_node *node)
+ {
+ slist_node *last = &head->head;
+ slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+ #ifdef USE_ASSERT_CHECKING
+ found = true;
+ #endif
+ break;
+ }
+ last = cur;
+ }
+
+ slist_check(head);
+ Assert(found);
+ }
+
+ #ifdef ILIST_DEBUG
+ /*
+ * Verify integrity of a doubly linked list
+ */
+ void
+ dlist_check(dlist_head *head)
+ {
+ dlist_node *cur;
+
+ if (head == NULL || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ /* iterate in forward direction */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ /* iterate in backward direction */
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+ }
+
+ /*
+ * Verify integrity of a singly linked list
+ */
+ void
+ slist_check(slist_head *head)
+ {
+ slist_node *cur;
+
+ if (head == NULL)
+ elog(ERROR, "singly linked is NULL");
+
+ /*
+ * there isn't much we can test in a singly linked list other that it
+ * actually ends sometime, i.e. hasn't introduced a cycle or similar
+ */
+ for (cur = head->head.next; cur != NULL; cur = cur->next)
+ ;
+ }
+
+ #endif /* ILIST_DEBUG */
*** a/src/backend/postmaster/autovacuum.c
--- b/src/backend/postmaster/autovacuum.c
***************
*** 77,83 ****
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/dllist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
--- 77,83 ----
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
***************
*** 152,157 **** typedef struct avl_dbase
--- 152,158 ----
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ dlist_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
***************
*** 258,265 **** typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /* the database list in the launcher, and the context that contains it */
! static Dllist *DatabaseList = NULL;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
--- 259,269 ----
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /*
! * the database list (of avl_dbase elements) in the launcher, and the context
! * that contains it
! */
! static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
***************
*** 508,514 **** AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! DatabaseList = NULL;
/*
* Make sure pgstat also considers our stat data as gone. Note: we
--- 512,518 ----
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! dlist_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
***************
*** 576,582 **** AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! Dlelem *elem;
int rc;
/*
--- 580,586 ----
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! avl_dbase *avdb;
int rc;
/*
***************
*** 738,757 **** AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
! elem = DLGetTail(DatabaseList);
! if (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
!
! /*
! * launch a worker if next_worker is right now or it is in the
! * past
! */
! if (TimestampDifferenceExceeds(avdb->adl_next_worker,
! current_time, 0))
! launch_worker(current_time);
! }
! else
{
/*
* Special case when the list is empty: start a worker right away.
--- 742,748 ----
/* We're OK to start a new worker */
! if (dlist_is_empty(&DatabaseList))
{
/*
* Special case when the list is empty: start a worker right away.
***************
*** 763,768 **** AutoVacLauncherMain(int argc, char *argv[])
--- 754,776 ----
*/
launch_worker(current_time);
}
+ else
+ {
+ /*
+ * because rebuild_database_list constructs a list with most
+ * distant adl_next_worker first, we obtain our database from the
+ * tail of the list.
+ */
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
+ /*
+ * launch a worker if next_worker is right now or it is in the
+ * past
+ */
+ if (TimestampDifferenceExceeds(avdb->adl_next_worker,
+ current_time, 0))
+ launch_worker(current_time);
+ }
}
/* Normal exit from the autovac launcher is here */
***************
*** 783,789 **** AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! Dlelem *elem;
/*
* We sleep until the next scheduled vacuum. We trust that when the
--- 791,797 ----
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
***************
*** 796,809 **** launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if ((elem = DLGetTail(DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
--- 804,818 ----
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if (!dlist_is_empty(&DatabaseList))
{
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
***************
*** 867,872 **** rebuild_database_list(Oid newdb)
--- 876,882 ----
int score;
int nelems;
HTAB *dbhash;
+ dlist_iter iter;
/* use fresh stats */
autovac_refresh_stats();
***************
*** 927,962 **** rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
! if (DatabaseList != NULL)
{
! Dlelem *elem;
!
! elem = DLGetHead(DatabaseList);
! while (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
!
! elem = DLGetSucc(elem);
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
! }
}
}
--- 937,964 ----
}
/* Now insert the databases from the existing list */
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
}
}
***************
*** 987,993 **** rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! DatabaseList = DLNewList();
if (nelems > 0)
{
--- 989,995 ----
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! dlist_init(&DatabaseList);
if (nelems > 0)
{
***************
*** 1029,1043 **** rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
! DLAddHead(DatabaseList, elem);
}
}
--- 1031,1043 ----
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
/* later elements should go closer to the head of the list */
! dlist_push_head(&DatabaseList, &db->adl_node);
}
}
***************
*** 1147,1153 **** do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! Dlelem *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
--- 1147,1153 ----
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! dlist_iter iter;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
***************
*** 1179,1189 **** do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
! while (elem != NULL)
{
! avl_dbase *dbp = DLE_VAL(elem);
if (dbp->adl_datid == tmp->adw_datid)
{
--- 1179,1188 ----
* autovacuum time yet.
*/
skipit = false;
! dlist_reverse_foreach(iter, &DatabaseList)
{
! avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
if (dbp->adl_datid == tmp->adw_datid)
{
***************
*** 1200,1206 **** do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
--- 1199,1204 ----
***************
*** 1274,1295 **** static void
launch_worker(TimestampTz now)
{
Oid dbid;
! Dlelem *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
! while (elem != NULL)
{
! avl_dbase *avdb = DLE_VAL(elem);
if (avdb->adl_datid == dbid)
{
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
--- 1272,1296 ----
launch_worker(TimestampTz now)
{
Oid dbid;
! dlist_iter iter;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
+ bool found = false;
+
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
if (avdb->adl_datid == dbid)
{
+ found = true;
+
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
***************
*** 1297,1306 **** launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! DLMoveToFront(elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
--- 1298,1306 ----
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! dlist_move_head(&DatabaseList, iter.cur);
break;
}
}
/*
***************
*** 1310,1316 **** launch_worker(TimestampTz now)
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (elem == NULL)
rebuild_database_list(dbid);
}
}
--- 1310,1316 ----
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (!found)
rebuild_database_list(dbid);
}
}
*** a/src/backend/postmaster/postmaster.c
--- b/src/backend/postmaster/postmaster.c
***************
*** 95,101 ****
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/dllist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
--- 95,101 ----
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
***************
*** 146,155 **** typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! Dlelem elem; /* list link in BackendList */
} Backend;
! static Dllist *BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
--- 146,155 ----
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! dlist_node elem; /* list link in BackendList */
} Backend;
! static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
***************
*** 1028,1038 **** PostmasterMain(int argc, char *argv[])
set_stack_base();
/*
- * Initialize the list of active backends.
- */
- BackendList = DLNewList();
-
- /*
* Initialize pipe (or process handle on Windows) that allows children to
* wake up from sleep on postmaster death.
*/
--- 1028,1033 ----
***************
*** 1872,1878 **** processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
! Dlelem *curr;
#else
int i;
#endif
--- 1867,1873 ----
Backend *bp;
#ifndef EXEC_BACKEND
! dlist_iter iter;
#else
int i;
#endif
***************
*** 1886,1894 **** processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! bp = (Backend *) DLE_VAL(curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
--- 1881,1889 ----
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! dlist_foreach(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
***************
*** 2648,2654 **** static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! Dlelem *curr;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
--- 2643,2649 ----
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! dlist_mutable_iter iter;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
***************
*** 2680,2688 **** CleanupBackend(int pid,
return;
}
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
--- 2675,2683 ----
return;
}
! dlist_foreach_modify(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->pid == pid)
{
***************
*** 2701,2707 **** CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
break;
}
--- 2696,2702 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
break;
}
***************
*** 2718,2725 **** CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! Dlelem *curr,
! *next;
Backend *bp;
/*
--- 2713,2719 ----
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! dlist_mutable_iter iter;
Backend *bp;
/*
***************
*** 2734,2743 **** HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
! for (curr = DLGetHead(BackendList); curr; curr = next)
{
! next = DLGetSucc(curr);
! bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
/*
--- 2728,2737 ----
}
/* Process regular backends */
! dlist_foreach_modify(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
!
if (bp->pid == pid)
{
/*
***************
*** 2750,2756 **** HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
--- 2744,2750 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
/* Keep looping so we can signal remaining backends */
}
***************
*** 3113,3119 **** PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (DLGetHead(BackendList) == NULL &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
--- 3107,3113 ----
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (dlist_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
***************
*** 3239,3250 **** signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
! Dlelem *curr;
bool signaled = false;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 3233,3244 ----
static bool
SignalSomeChildren(int signal, int target)
{
! dlist_iter iter;
bool signaled = false;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
***************
*** 3382,3389 **** BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
--- 3376,3383 ----
*/
bn->pid = pid;
bn->is_autovacuum = false;
! dlist_push_head(&BackendList, &bn->elem);
!
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
***************
*** 4491,4502 **** PostmasterRandom(void)
static int
CountChildren(int target)
{
! Dlelem *curr;
int cnt = 0;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 4485,4496 ----
static int
CountChildren(int target)
{
! dlist_iter iter;
int cnt = 0;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
***************
*** 4675,4682 **** StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
--- 4669,4675 ----
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! dlist_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
*** a/src/backend/utils/cache/catcache.c
--- b/src/backend/utils/cache/catcache.c
***************
*** 291,297 **** CatalogCacheComputeTupleHashValue(CatCache *cache, HeapTuple tuple)
static void
CatCachePrintStats(int code, Datum arg)
{
! CatCache *cache;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
--- 291,297 ----
static void
CatCachePrintStats(int code, Datum arg)
{
! slist_iter iter;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
***************
*** 300,307 **** CatCachePrintStats(int code, Datum arg)
long cc_lsearches = 0;
long cc_lhits = 0;
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
--- 300,309 ----
long cc_lsearches = 0;
long cc_lhits = 0;
! slist_foreach(iter, &CacheHdr->ch_caches)
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
***************
*** 368,375 **** CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
return; /* nothing left to do */
}
! /* delink from linked list */
! DLRemove(&ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
--- 370,376 ----
return; /* nothing left to do */
}
! dlist_delete(ct->cache_bucket, &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
***************
*** 412,418 **** CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
! DLRemove(&cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
--- 413,419 ----
}
/* delink from linked list */
! dlist_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
***************
*** 442,459 **** CatCacheRemoveCList(CatCache *cache, CatCList *cl)
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! CatCache *ccp;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
! Dlelem *elt,
! *nextelt;
if (cacheId != ccp->id)
continue;
--- 443,460 ----
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! slist_iter cache_iter;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! slist_foreach(cache_iter, &CacheHdr->ch_caches)
{
Index hashIndex;
! dlist_mutable_iter iter;
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
if (cacheId != ccp->id)
continue;
***************
*** 468,478 **** CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 469,477 ----
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! dlist_foreach_modify(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
***************
*** 484,495 **** CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
!
! for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (hashValue == ct->hash_value)
{
--- 483,491 ----
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
! dlist_foreach_modify(iter, &ccp->cc_bucket[hashIndex])
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (hashValue == ct->hash_value)
{
***************
*** 557,573 **** AtEOXact_CatCache(bool isCommit)
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! CatCache *ccp;
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
! Dlelem *elt;
int i;
/* Check CatCLists */
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
--- 553,570 ----
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! slist_iter cache_iter;
! slist_foreach(cache_iter, &(CacheHdr->ch_caches))
{
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
! dlist_iter iter;
int i;
/* Check CatCLists */
! dlist_foreach(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
***************
*** 577,587 **** AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&ccp->cc_bucket[i]);
! elt;
! elt = DLGetSucc(elt))
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
--- 574,584 ----
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! dlist_head *bucket = &ccp->cc_bucket[i];
!
! dlist_foreach(iter, bucket)
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
***************
*** 604,619 **** AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
! Dlelem *elt,
! *nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
! for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 601,613 ----
static void
ResetCatalogCache(CatCache *cache)
{
! dlist_mutable_iter iter;
int i;
/* Remove each list in this cache, or at least mark it dead */
! dlist_foreach_modify(iter, &cache->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
***************
*** 624,634 **** ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
! {
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
! nextelt = DLGetSucc(elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
--- 618,628 ----
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! dlist_head *bucket = &cache->cc_bucket[i];
! dlist_foreach_modify(iter, bucket)
! {
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
***************
*** 654,665 **** ResetCatalogCache(CatCache *cache)
void
ResetCatalogCaches(void)
{
! CatCache *cache;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
ResetCatalogCache(cache);
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
--- 648,663 ----
void
ResetCatalogCaches(void)
{
! slist_iter iter;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! slist_foreach(iter, &CacheHdr->ch_caches)
! {
! CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
!
ResetCatalogCache(cache);
+ }
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
***************
*** 680,691 **** ResetCatalogCaches(void)
void
CatalogCacheFlushCatalog(Oid catId)
{
! CatCache *cache;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
--- 678,691 ----
void
CatalogCacheFlushCatalog(Oid catId)
{
! slist_iter iter;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
***************
*** 760,766 **** InitCatCache(int id,
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! CacheHdr->ch_caches = NULL;
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
--- 760,766 ----
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
***************
*** 770,779 **** InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
/*
* initialize the cache's relation information for the relation
--- 770,777 ----
/*
* allocate a new cache structure
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(dlist_node));
/*
* initialize the cache's relation information for the relation
***************
*** 792,797 **** InitCatCache(int id,
--- 790,800 ----
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ dlist_init(&cp->cc_lists);
+
+ for (i = 0; i < nbuckets; i++)
+ dlist_init(&cp->cc_bucket[i]);
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
***************
*** 801,808 **** InitCatCache(int id,
/*
* add completed cache to top of group header's list
*/
! cp->cc_next = CacheHdr->ch_caches;
! CacheHdr->ch_caches = cp;
/*
* back to the old context before we return...
--- 804,810 ----
/*
* add completed cache to top of group header's list
*/
! slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
/*
* back to the old context before we return...
***************
*** 1060,1066 **** SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! Dlelem *elt;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
--- 1062,1069 ----
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! dlist_mutable_iter iter;
! dlist_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
***************
*** 1094,1106 **** SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead)
continue; /* ignore dead entries */
--- 1097,1109 ----
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! bucket = &cache->cc_bucket[hashIndex];
!
! dlist_foreach_modify(iter, bucket)
{
bool res;
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead)
continue; /* ignore dead entries */
***************
*** 1125,1131 **** SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! DLMoveToFront(&ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
--- 1128,1134 ----
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! dlist_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
***************
*** 1340,1346 **** SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! Dlelem *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
--- 1343,1349 ----
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! dlist_iter iter;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
***************
*** 1382,1394 **** SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
! for (elt = DLGetHead(&cache->cc_lists);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! cl = (CatCList *) DLE_VAL(elt);
if (cl->dead)
continue; /* ignore dead entries */
--- 1385,1395 ----
/*
* scan the items until we find a match or exhaust our list
*/
! dlist_foreach(iter, &cache->cc_lists)
{
bool res;
! cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->dead)
continue; /* ignore dead entries */
***************
*** 1416,1422 **** SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
! DLMoveToFront(&cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
--- 1417,1423 ----
* since there's no point in that unless they are searched for
* individually.)
*/
! dlist_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
***************
*** 1468,1473 **** SearchCatCacheList(CatCache *cache,
--- 1469,1476 ----
{
uint32 hashValue;
Index hashIndex;
+ bool found = false;
+ dlist_head *bucket;
/*
* See if there's an entry for this tuple already.
***************
*** 1476,1486 **** SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
--- 1479,1488 ----
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! bucket = &cache->cc_bucket[hashIndex];
! dlist_foreach(iter, bucket)
{
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
***************
*** 1498,1507 **** SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
break; /* A-OK */
}
! if (elt == NULL)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
--- 1500,1510 ----
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
! if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
***************
*** 1564,1570 **** SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
--- 1567,1572 ----
***************
*** 1587,1593 **** SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
! DLAddHead(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
--- 1589,1595 ----
}
Assert(i == nmembers);
! dlist_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
***************
*** 1664,1677 **** CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! DLInitElem(&ct->cache_elem, (void *) ct);
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
--- 1666,1680 ----
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! ct->cache_bucket = &cache->cc_bucket[hashIndex];
!
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! dlist_push_head(ct->cache_bucket, &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
***************
*** 1785,1791 **** PrepareToInvalidateCacheTuple(Relation relation,
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! CatCache *ccp;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
--- 1788,1794 ----
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! slist_iter iter;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
***************
*** 1808,1817 **** PrepareToInvalidateCacheTuple(Relation relation,
* ----------------
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
uint32 hashvalue;
Oid dbid;
if (ccp->cc_reloid != reloid)
continue;
--- 1811,1821 ----
* ----------------
*/
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
uint32 hashvalue;
Oid dbid;
+ CatCache *ccp = slist_container(CatCache, cc_next, iter.cur);
if (ccp->cc_reloid != reloid)
continue;
*** a/src/include/lib/dllist.h
--- /dev/null
***************
*** 1,85 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.h
- * simple doubly linked list primitives
- * the elements of the list are void* so the lists can contain anything
- * Dlelem can only be in one list at a time
- *
- *
- * Here's a small example of how to use Dllists:
- *
- * Dllist *lst;
- * Dlelem *elt;
- * void *in_stuff; -- stuff to stick in the list
- * void *out_stuff
- *
- * lst = DLNewList(); -- make a new dllist
- * DLAddHead(lst, DLNewElem(in_stuff)); -- add a new element to the list
- * with in_stuff as the value
- * ...
- * elt = DLGetHead(lst); -- retrieve the head element
- * out_stuff = (void*)DLE_VAL(elt); -- get the stuff out
- * DLRemove(elt); -- removes the element from its list
- * DLFreeElem(elt); -- free the element since we don't
- * use it anymore
- *
- *
- * It is also possible to use Dllist objects that are embedded in larger
- * structures instead of being separately malloc'd. To do this, use
- * DLInitElem() to initialize a Dllist field within a larger object.
- * Don't forget to DLRemove() each field from its list (if any) before
- * freeing the larger object!
- *
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/include/lib/dllist.h
- *
- *-------------------------------------------------------------------------
- */
-
- #ifndef DLLIST_H
- #define DLLIST_H
-
- struct Dllist;
- struct Dlelem;
-
- typedef struct Dlelem
- {
- struct Dlelem *dle_next; /* next element */
- struct Dlelem *dle_prev; /* previous element */
- void *dle_val; /* value of the element */
- struct Dllist *dle_list; /* what list this element is in */
- } Dlelem;
-
- typedef struct Dllist
- {
- Dlelem *dll_head;
- Dlelem *dll_tail;
- } Dllist;
-
- extern Dllist *DLNewList(void); /* allocate and initialize a list header */
- extern void DLInitList(Dllist *list); /* init a header alloced by caller */
- extern void DLFreeList(Dllist *list); /* free up a list and all the nodes in
- * it */
- extern Dlelem *DLNewElem(void *val);
- extern void DLInitElem(Dlelem *e, void *val);
- extern void DLFreeElem(Dlelem *e);
- extern void DLRemove(Dlelem *e); /* removes node from list */
- extern void DLAddHead(Dllist *list, Dlelem *node);
- extern void DLAddTail(Dllist *list, Dlelem *node);
- extern Dlelem *DLRemHead(Dllist *list); /* remove and return the head */
- extern Dlelem *DLRemTail(Dllist *list);
- extern void DLMoveToFront(Dlelem *e); /* move node to front of its list */
-
- /* These are macros for speed */
- #define DLGetHead(list) ((list)->dll_head)
- #define DLGetTail(list) ((list)->dll_tail)
- #define DLGetSucc(elem) ((elem)->dle_next)
- #define DLGetPred(elem) ((elem)->dle_prev)
- #define DLGetListHdr(elem) ((elem)->dle_list)
-
- #define DLE_VAL(elem) ((elem)->dle_val)
-
- #endif /* DLLIST_H */
--- 0 ----
*** /dev/null
--- b/src/include/lib/ilist.h
***************
*** 0 ****
--- 1,749 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure.
+
+ * Also, the doubly-linked lists are always circularly linked, even when empty;
+ * this enables many manipulations to be done without branches, which is
+ * beneficial performance-wise.
+ *
+ * NOTES
+ * This is intended to be used in situations where memory for a struct and
+ * its contents already needs to be allocated and the overhead of
+ * allocating extra list cells for every list element is noticeable. Thus,
+ * none of the functions here allocate any memory; they just manipulate
+ * externally managed memory. The API for singly/doubly linked lists is
+ * identical as far as capabilities of both allow.
+ *
+ * EXAMPLES
+ *
+ * An oversimplified example demonstrating how this can be used. Let's assume
+ * we want to store information about the tables contained in a database.
+ *
+ * #include "lib/ilist.h"
+ *
+ * // Define struct for the databases including a list header that will be used
+ * // to access the nodes in the list later on.
+ * typedef struct my_database
+ * {
+ * char *datname;
+ * dlist_head tables;
+ * // ...
+ * } my_database;
+ *
+ * // Define struct for the tables. Note the list_node element which stores
+ * // information about prev/next list nodes.
+ * typedef struct my_table
+ * {
+ * char *tablename;
+ * dlist_node list_node;
+ * perm_t permissions;
+ * // ...
+ * } value_in_a_list;
+ *
+ * // create a database
+ * my_database *db = create_database();
+ *
+ * // and add a few tables to its table list
+ * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
+ * ...
+ * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
+ *
+ *
+ * To iterate over the table list, we allocate an iterator element and use
+ * a specialized looping construct. Inside a dlist_foreach, the iterator's
+ * 'cur' field can be used to access the current element. iter.cur points to a
+ * 'dlist_node', but most of the time what we want is the actual table
+ * information; dlist_container() gives us that, like so:
+ *
+ * dlist_iter iter;
+ * dlist_foreach(iter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, iter.cur);
+ * printf("we have a table: %s in database %s\n",
+ * val->tablename, db->datname);
+ * }
+ *
+ *
+ * While a simple iteration is useful, we sometimes also want to manipulate
+ * the list while iterating. There is a different iterator element and looping
+ * construct for that. Suppose we want to delete tables that meet a certain
+ * criterion:
+ *
+ * dlist_mutable_iter miter;
+ * dlist_foreach_modify(miter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, miter.cur);
+ *
+ * if (!tbl->to_be_deleted)
+ * continue; // don't touch this one
+ *
+ * // unlink the current table from the linked list
+ * dlist_delete(&db->tables, miter.cur);
+ * // as these lists never manage memory, we can freely access the table
+ * // after it's been deleted
+ * drop_table(db, tbl);
+ * }
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+ #ifndef ILIST_H
+ #define ILIST_H
+
+ /*
+ * Enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even when USE_ASSERT_CHECKING.
+ */
+ /* #define ILIST_DEBUG */
+
+ /*
+ * Node of a doubly linked list.
+ *
+ * Embed this in structs that need to be part of a doubly linked list.
+ */
+ typedef struct dlist_node dlist_node;
+ struct dlist_node
+ {
+ dlist_node *prev;
+ dlist_node *next;
+ };
+
+ /*
+ * Head of a doubly linked list.
+ *
+ * Lists are internally *always* circularly linked, even when empty. Circular
+ * lists have the advantage of not needing any branches in the most common list
+ * manipulations.
+ */
+ typedef struct dlist_head
+ {
+ /*
+ * head->next either points to the first element of the list or to &head
+ * if empty.
+ *
+ * head->prev either points to the last element of the list or to &head if
+ * empty.
+ */
+ dlist_node head;
+ } dlist_head;
+
+
+ /*
+ * Doubly linked list iterator.
+ *
+ * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
+ * current element of the iteration use the 'cur' member.
+ *
+ * Iterations using this are *not* allowed to change the list while iterating!
+ *
+ * NB: We use an extra type for this to make it possible to avoid multiple
+ * evaluations of arguments in the dlist_foreach() macro.
+ */
+ typedef struct dlist_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ } dlist_iter;
+
+ /*
+ * Doubly linked list iterator allowing some modifications while iterating
+ *
+ * Used as state in dlist_foreach_modify(). To get the current element of the
+ * iteration use the 'cur' member.
+ *
+ * Iterations using this are only allowed to change the list *at the current
+ * point of iteration*. It is fine to delete the current node, but it is *not*
+ * fine to modify other nodes.
+ *
+ * NB: We need a separate type for mutable iterations to avoid having to pass
+ * in two iterators or some other state variable as we need to store the
+ * '->next' node of the current node so it can be deleted or modified by the
+ * user.
+ */
+ typedef struct dlist_mutable_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ dlist_node *next; /* next node we iterate to, so we can delete
+ * cur */
+ } dlist_mutable_iter;
+
+ /*
+ * Node of a singly linked list.
+ *
+ * Embed this in structs that need to be part of a singly linked list.
+ */
+ typedef struct slist_node slist_node;
+ struct slist_node
+ {
+ slist_node *next;
+ };
+
+ /*
+ * Head of a singly linked list.
+ *
+ * Singly linked lists are not circularly linked, in contrast to doubly linked
+ * lists. As no pointer to the last list element and to the previous node needs
+ * to be maintained this doesn't incur any additional branches in the usual
+ * manipulations.
+ */
+ typedef struct slist_head
+ {
+ slist_node head;
+ } slist_head;
+
+ /*
+ * Singly linked list iterator
+ *
+ * Used in slist_foreach(). To get the current element of the iteration use the
+ * 'cur' member.
+ *
+ * Do *not* manipulate the list while iterating!
+ *
+ * NB: this wouldn't really need to be an extra struct, we could use a
+ * slist_node * directly. We still use a separate type for consistency.
+ */
+ typedef struct slist_iter
+ {
+ slist_node *cur;
+ } slist_iter;
+
+ /*
+ * Singly linked list iterator allowing some modifications while iterating
+ *
+ * Used in slist_foreach_modify.
+ *
+ * Iterations using this are allowed to remove the current node and to add more
+ * nodes to the beginning of the list.
+ */
+ typedef struct slist_mutable_iter
+ {
+ slist_node *cur;
+ slist_node *next;
+ } slist_mutable_iter;
+
+
+ /* Prototypes for functions too big to be inline */
+
+ /* Attention: O(n) */
+ extern void slist_delete(slist_head *head, slist_node *node);
+
+ #ifdef ILIST_DEBUG
+ extern void dlist_check(dlist_head *head);
+ extern void slist_check(slist_head *head);
+ #else
+ /*
+ * These seemingly useless casts to void are here to keep the compiler quiet
+ * about the argument being unused in many functions in a non-debug compile,
+ * in which functions the only point of passing the list head pointer is to be
+ * able to run these checks.
+ */
+ #define dlist_check(head) (void) (head)
+ #define slist_check(head) (void) (head)
+ #endif /* ILIST_DEBUG */
+
+ /* Static initializers */
+ #define DLIST_STATIC_INIT(name) {{&name.head, &name.head}}
+ #define SLIST_STATIC_INIT(name) {{NULL}}
+
+
+ /*
+ * We want the functions below to be inline; but if the compiler doesn't
+ * support that, fall back on providing them as regular functions. See
+ * STATIC_IF_INLINE in c.h.
+ */
+ #ifndef PG_USE_INLINE
+ extern void dlist_init(dlist_head *head);
+ extern bool dlist_is_empty(dlist_head *head);
+ extern void dlist_push_head(dlist_head *head, dlist_node *node);
+ extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+ extern void dlist_insert_after(dlist_head *head,
+ dlist_node *after, dlist_node *node);
+ extern void dlist_insert_before(dlist_head *head,
+ dlist_node *before, dlist_node *node);
+ extern void dlist_delete(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_pop_head_node(dlist_head *head);
+ extern void dlist_move_head(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_next(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_prev(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_next_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_prev_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_head_node(dlist_head *head);
+ extern dlist_node *dlist_tail_node(dlist_head *head);
+
+ /* dlist macro support functions */
+ extern void *dlist_tail_element_off(dlist_head *head, size_t off);
+ extern void *dlist_head_element_off(dlist_head *head, size_t off);
+ #endif /* !PG_USE_INLINE */
+
+ #if defined(PG_USE_INLINE) || defined(ILIST_INCLUDE_DEFINITIONS)
+ /*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+ STATIC_IF_INLINE void
+ dlist_init(dlist_head *head)
+ {
+ head->head.next = head->head.prev = &head->head;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node at the beginning of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_push_head(dlist_head *head, dlist_node *node)
+ {
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Inserts a node at the end of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_push_tail(dlist_head *head, dlist_node *node)
+ {
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node after another *in the same list*
+ */
+ STATIC_IF_INLINE void
+ dlist_insert_after(dlist_head *head, dlist_node *after, dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node before another *in the same list*
+ */
+ STATIC_IF_INLINE void
+ dlist_insert_before(dlist_head *head, dlist_node *before, dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'before' is in 'head'? */
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete 'node' from list.
+ *
+ * It is not allowed to delete a 'node' which is is not in the list 'head'
+ */
+ STATIC_IF_INLINE void
+ dlist_delete(dlist_head *head, dlist_node *node)
+ {
+ dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete and return the first node from a list.
+ *
+ * Undefined behaviour when the list is empty. Check with dlist_is_empty if
+ * necessary.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_pop_head_node(dlist_head *head)
+ {
+ dlist_node *ret;
+
+ Assert(&head->head != head->head.next);
+
+ ret = head->head.next;
+ dlist_delete(head, head->head.next);
+ return ret;
+ }
+
+ /*
+ * Move element from its current position in the list to the head position in
+ * the same list.
+ *
+ * Undefined behaviour if 'node' is not already part of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_move_head(dlist_head *head, dlist_node *node)
+ {
+ /* fast path if it's already at the head */
+ if (head->head.next == node)
+ return;
+
+ dlist_delete(head, node);
+ dlist_push_head(head, node);
+
+ dlist_check(head);
+ }
+
+ /*
+ * Check whether the passed node is the last element in the list.
+ */
+ STATIC_IF_INLINE bool
+ dlist_has_next(dlist_head *head, dlist_node *node)
+ {
+ return node->next != &head->head;
+ }
+
+ /*
+ * Check whether the passed node is the first element in the list.
+ */
+ STATIC_IF_INLINE bool
+ dlist_has_prev(dlist_head *head, dlist_node *node)
+ {
+ return node->prev != &head->head;
+ }
+
+ /*
+ * Return the next node in the list.
+ *
+ * Undefined behaviour when no next node exists. Use dlist_has_next to make
+ * sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_next_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_next(head, node));
+ return node->next;
+ }
+
+ /*
+ * Return previous node in the list.
+ *
+ * Undefined behaviour when no prev node exists. Use dlist_has_prev to make
+ * sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_prev_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_prev(head, node));
+ return node->prev;
+ }
+
+ /*
+ * Return whether the list is empty.
+ */
+ STATIC_IF_INLINE bool
+ dlist_is_empty(dlist_head *head)
+ {
+ return head->head.next == &(head->head);
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ dlist_head_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Return the first node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_head_node(dlist_head *head)
+ {
+ return dlist_head_element_off(head, 0);
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ dlist_tail_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.prev - off;
+ }
+
+ /*
+ * Return the last node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_tail_node(dlist_head *head)
+ {
+ return dlist_tail_element_off(head, 0);
+ }
+ #endif /* PG_USE_INLINE || ILIST_INCLUDE_DEFINITIONS */
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the dlist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a dlist_node * back to its containing struct.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
+ AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list must not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_get_head_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list must not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_tail_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_tail_element_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->next)
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+ #define dlist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_mutable_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->next, \
+ iter.next = iter.cur->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.next, iter.next = iter.cur->next)
+
+ /*
+ * Iterate through the list in reverse order.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_reverse_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, iter.cur = iter.end->prev; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->prev)
+
+
+ /*
+ * We want the functions below to be inline; but if the compiler doesn't
+ * support that, fall back on providing them as regular functions. See
+ * STATIC_IF_INLINE in c.h.
+ */
+ #ifndef PG_USE_INLINE
+ extern void slist_init(slist_head *head);
+ extern bool slist_is_empty(slist_head *head);
+ extern slist_node *slist_head_node(slist_head *head);
+ extern void slist_push_head(slist_head *head, slist_node *node);
+ extern slist_node *slist_pop_head_node(slist_head *head);
+ extern void slist_insert_after(slist_head *head,
+ slist_node *after, slist_node *node);
+ extern bool slist_has_next(slist_head *head, slist_node *node);
+ extern slist_node *slist_next_node(slist_head *head, slist_node *node);
+
+ /* slist macro support function */
+ extern void *slist_head_element_off(slist_head *head, size_t off);
+ #endif
+
+ #if defined(PG_USE_INLINE) || defined(ILIST_INCLUDE_DEFINITIONS)
+ /*
+ * Initialize a singly linked list.
+ */
+ STATIC_IF_INLINE void
+ slist_init(slist_head *head)
+ {
+ head->head.next = NULL;
+
+ slist_check(head);
+ }
+
+ /*
+ * Is the list empty?
+ */
+ STATIC_IF_INLINE bool
+ slist_is_empty(slist_head *head)
+ {
+ slist_check(head);
+
+ return head->head.next == NULL;
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ slist_head_element_off(slist_head *head, size_t off)
+ {
+ Assert(!slist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Push 'node' as the new first node in the list, pushing the original head to
+ * the second position.
+ */
+ STATIC_IF_INLINE void
+ slist_push_head(slist_head *head, slist_node *node)
+ {
+ node->next = head->head.next;
+ head->head.next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Remove and return the first node in the list
+ *
+ * Undefined behaviour if the list is empty.
+ */
+ STATIC_IF_INLINE slist_node *
+ slist_pop_head_node(slist_head *head)
+ {
+ slist_node *node;
+
+ Assert(!slist_is_empty(head));
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ slist_check(head);
+
+ return node;
+ }
+
+ /*
+ * Insert a new node after another one
+ *
+ * Undefined behaviour if 'after' is not part of the list already.
+ */
+ STATIC_IF_INLINE void
+ slist_insert_after(slist_head *head, slist_node *after,
+ slist_node *node)
+ {
+ node->next = after->next;
+ after->next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Return whether 'node' has a following node
+ */
+ STATIC_IF_INLINE bool
+ slist_has_next(slist_head *head,
+ slist_node *node)
+ {
+ slist_check(head);
+
+ return node->next != NULL;
+ }
+ #endif /* PG_USE_INLINE || ILIST_INCLUDE_DEFINITIONS */
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the slist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a slist_node * back to its containing struct.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define slist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, slist_node *), \
+ AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ */
+ #define slist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ slist_head_element_off(ptr, offsetoff(type, membername)))
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter'.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define slist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next; \
+ iter.cur != NULL; \
+ iter.cur = iter.cur->next)
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter' allowing some
+ * modifications.
+ *
+ * It is allowed to delete the current element from the list and add new nodes
+ * before the current position. Other manipulations can lead to corruption.
+ */
+ #define slist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_mutable_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next, \
+ iter.next = iter.cur ? iter.cur->next : NULL; \
+ iter.cur != NULL; \
+ iter.cur = iter.next, iter.next = iter.next ? iter.next->next : NULL)
+
+ #endif /* ILIST_H */
*** a/src/include/utils/catcache.h
--- b/src/include/utils/catcache.h
***************
*** 22,28 ****
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/dllist.h"
#include "utils/relcache.h"
/*
--- 22,28 ----
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/ilist.h"
#include "utils/relcache.h"
/*
***************
*** 37,43 ****
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! struct catcache *cc_next; /* link to next catcache */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
--- 37,43 ----
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! slist_node cc_next; /* list link */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
***************
*** 51,57 **** typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! Dllist cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
--- 51,57 ----
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! dlist_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
***************
*** 66,72 **** typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
--- 66,72 ----
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! dlist_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
***************
*** 77,87 **** typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a Dllist that stores the elements
! * of its hash bucket. We keep each Dllist in LRU order to speed repeated
* lookups.
*/
! Dlelem cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
--- 77,88 ----
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a dlist that stores the elements
! * of its hash bucket. We keep each dlist in LRU order to speed repeated
* lookups.
*/
! dlist_node cache_elem; /* list member of per-bucket list */
! dlist_head *cache_bucket; /* containing bucket dlist */
/*
* The tuple may also be a member of at most one CatCList. (If a single
***************
*** 139,145 **** typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! Dlelem cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--- 140,146 ----
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! dlist_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
***************
*** 153,159 **** typedef struct catclist
typedef struct catcacheheader
{
! CatCache *ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
--- 154,160 ----
typedef struct catcacheheader
{
! slist_head ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
ilist-4-circular.patchtext/x-diff; charset=us-asciiDownload
*** a/src/backend/utils/cache/catcache.c
--- b/src/backend/utils/cache/catcache.c
***************
*** 791,799 **** InitCatCache(int id,
cp->cc_key[i] = key[i];
dlist_init(&cp->cc_lists);
!
! for (i = 0; i < nbuckets; i++)
! dlist_init(&cp->cc_bucket[i]);
/*
* new cache is initialized as far as we can go for now. print some
--- 791,797 ----
cp->cc_key[i] = key[i];
dlist_init(&cp->cc_lists);
! MemSet(&cp->cc_bucket, 0, nbuckets * sizeof(dlist_head));
/*
* new cache is initialized as far as we can go for now. print some
*** a/src/include/lib/ilist.h
--- b/src/include/lib/ilist.h
***************
*** 6,15 ****
* This implementation is as efficient as possible: the lists don't have
* any memory management overhead, because the list pointers are embedded
* within some larger structure.
!
! * Also, the doubly-linked lists are always circularly linked, even when empty;
! * this enables many manipulations to be done without branches, which is
! * beneficial performance-wise.
*
* NOTES
* This is intended to be used in situations where memory for a struct and
--- 6,18 ----
* This implementation is as efficient as possible: the lists don't have
* any memory management overhead, because the list pointers are embedded
* within some larger structure.
! *
! * There are two kinds of empty doubly linked lists: those that have been
! * initialized to NULL, and those that have been initialized to circularity.
! * The second kind is useful for tight optimization, because there are some
! * operations that can be done without branches (and thus faster) on lists that
! * have been initialized to circularity. Most users don't care all that much,
! * and so can skip the initialization step until really required.
*
* NOTES
* This is intended to be used in situations where memory for a struct and
***************
*** 120,137 **** struct dlist_node
/*
* Head of a doubly linked list.
*
! * Lists are internally *always* circularly linked, even when empty. Circular
! * lists have the advantage of not needing any branches in the most common list
! * manipulations.
*/
typedef struct dlist_head
{
/*
! * head->next either points to the first element of the list or to &head
! * if empty.
*
! * head->prev either points to the last element of the list or to &head if
! * empty.
*/
dlist_node head;
} dlist_head;
--- 123,141 ----
/*
* Head of a doubly linked list.
*
! * Non-empty lists are internally circularly linked. Circular lists have the
! * advantage of not needing any branches in the most common list manipulations.
! * An empty list can also be represented as a pair of NULL pointers, making
! * initialization easier.
*/
typedef struct dlist_head
{
/*
! * head->next either points to the first element of the list; to &head if
! * it's a circular empty list; or to NULL if empty and not circular.
*
! * head->prev either points to the last element of the list; to &head if
! * it's a circular empty list; or to NULL if empty and not circular.
*/
dlist_node head;
} dlist_head;
***************
*** 265,271 **** extern void slist_check(slist_head *head);
--- 269,277 ----
extern void dlist_init(dlist_head *head);
extern bool dlist_is_empty(dlist_head *head);
extern void dlist_push_head(dlist_head *head, dlist_node *node);
+ extern void dlisti_push_head(dlist_head *head, dlist_node *node);
extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+ extern void dlisti_push_tail(dlist_head *head, dlist_node *node);
extern void dlist_insert_after(dlist_head *head,
dlist_node *after, dlist_node *node);
extern void dlist_insert_before(dlist_head *head,
***************
*** 304,309 **** dlist_init(dlist_head *head)
--- 310,334 ----
STATIC_IF_INLINE void
dlist_push_head(dlist_head *head, dlist_node *node)
{
+ if (head->head.next == NULL)
+ dlist_init(head);
+
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * As above, except assume the list has been initialized to circularity.
+ */
+ STATIC_IF_INLINE void
+ dlisti_push_head(dlist_head *head, dlist_node *node)
+ {
+ Assert(head->head.next != NULL);
+
node->next = head->head.next;
node->prev = &head->head;
node->next->prev = node;
***************
*** 318,323 **** dlist_push_head(dlist_head *head, dlist_node *node)
--- 343,367 ----
STATIC_IF_INLINE void
dlist_push_tail(dlist_head *head, dlist_node *node)
{
+ if (head->head.next == NULL)
+ dlist_init(head);
+
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * As above, except assume the list has been initialized to circularity.
+ */
+ STATIC_IF_INLINE void
+ dlisti_push_tail(dlist_head *head, dlist_node *node)
+ {
+ Assert(head->head.next != NULL);
+
node->next = &head->head;
node->prev = head->head.prev;
node->prev->next = node;
***************
*** 379,385 **** dlist_delete(dlist_head *head, dlist_node *node)
/*
* Delete and return the first node from a list.
*
! * Undefined behaviour when the list is empty. Check with dlist_is_empty if
* necessary.
*/
STATIC_IF_INLINE dlist_node *
--- 423,429 ----
/*
* Delete and return the first node from a list.
*
! * Undefined behaviour when the list is empty. Check with dlist_is_empty if
* necessary.
*/
STATIC_IF_INLINE dlist_node *
***************
*** 459,469 **** dlist_prev_node(dlist_head *head, dlist_node *node)
/*
* Return whether the list is empty.
*/
STATIC_IF_INLINE bool
dlist_is_empty(dlist_head *head)
{
! return head->head.next == &(head->head);
}
/* internal support function */
--- 503,517 ----
/*
* Return whether the list is empty.
+ *
+ * An empty list has either its first 'next' pointer set to NULL, or to itself.
*/
STATIC_IF_INLINE bool
dlist_is_empty(dlist_head *head)
{
! dlist_check(head);
!
! return head->head.next == NULL || head->head.next == &(head->head);
}
/* internal support function */
***************
*** 553,559 **** dlist_tail_node(dlist_head *head)
#define dlist_foreach(iter, ptr) \
AssertVariableIsOfType(iter, dlist_iter); \
AssertVariableIsOfType(ptr, dlist_head *); \
! for (iter.end = &(ptr)->head, iter.cur = iter.end->next; \
iter.cur != iter.end; \
iter.cur = iter.cur->next)
--- 601,608 ----
#define dlist_foreach(iter, ptr) \
AssertVariableIsOfType(iter, dlist_iter); \
AssertVariableIsOfType(ptr, dlist_head *); \
! for (iter.end = &(ptr)->head, \
! iter.cur = iter.end->next ? iter.end->next : iter.end; \
iter.cur != iter.end; \
iter.cur = iter.cur->next)
***************
*** 568,575 **** dlist_tail_node(dlist_head *head)
#define dlist_foreach_modify(iter, ptr) \
AssertVariableIsOfType(iter, dlist_mutable_iter); \
AssertVariableIsOfType(ptr, dlist_head *); \
! for (iter.end = &(ptr)->head, iter.cur = iter.end->next, \
! iter.next = iter.cur->next; \
iter.cur != iter.end; \
iter.cur = iter.next, iter.next = iter.cur->next)
--- 617,625 ----
#define dlist_foreach_modify(iter, ptr) \
AssertVariableIsOfType(iter, dlist_mutable_iter); \
AssertVariableIsOfType(ptr, dlist_head *); \
! for (iter.end = &(ptr)->head, \
! iter.cur = iter.end->next ? iter.end->next : iter.end, \
! iter.next = iter.cur->next; \
iter.cur != iter.end; \
iter.cur = iter.next, iter.next = iter.cur->next)
Alvaro Herrera escribió:
I also included two new functions in that patch, dlisti_push_head and
dlisti_push_tail. These functions are identical to dlist_push_head and
dlist_push_tail, except that they do not accept non-circular lists.
What this means is that callers that find the non-circularity acceptable
can use the regular version, and will run dlist_init() on demand;
callers that want the super-tight code can use the other version.
I didn't bother with a new dlist_is_empty.
Is there any more input on this? At this point I would recommend
committing this patch _without_ these dlisti functions, i.e. we will
only have the functions that check for NULL-initialized dlists. We can
later discuss whether to include them or not (it would be a much smaller
patch and would not affect the existing functionality in any way.)
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Thursday, October 11, 2012 03:23:12 PM Alvaro Herrera wrote:
Alvaro Herrera escribió:
I also included two new functions in that patch, dlisti_push_head and
dlisti_push_tail. These functions are identical to dlist_push_head and
dlist_push_tail, except that they do not accept non-circular lists.
What this means is that callers that find the non-circularity acceptable
can use the regular version, and will run dlist_init() on demand;
callers that want the super-tight code can use the other version.
I didn't bother with a new dlist_is_empty.Is there any more input on this? At this point I would recommend
committing this patch _without_ these dlisti functions, i.e. we will
only have the functions that check for NULL-initialized dlists. We can
later discuss whether to include them or not (it would be a much smaller
patch and would not affect the existing functionality in any way.)
I can live with that. I didn't have a chance to look at the newest revision
yet, will do that after I finish my first pass through foreign key locks.
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Thursday, October 11, 2012 03:27:17 PM Andres Freund wrote:
On Thursday, October 11, 2012 03:23:12 PM Alvaro Herrera wrote:
Alvaro Herrera escribió:
I also included two new functions in that patch, dlisti_push_head and
dlisti_push_tail. These functions are identical to dlist_push_head and
dlist_push_tail, except that they do not accept non-circular lists.
What this means is that callers that find the non-circularity
acceptable can use the regular version, and will run dlist_init() on
demand; callers that want the super-tight code can use the other
version. I didn't bother with a new dlist_is_empty.Is there any more input on this? At this point I would recommend
committing this patch _without_ these dlisti functions, i.e. we will
only have the functions that check for NULL-initialized dlists. We can
later discuss whether to include them or not (it would be a much smaller
patch and would not affect the existing functionality in any way.)I can live with that. I didn't have a chance to look at the newest revision
yet, will do that after I finish my first pass through foreign key locks.
I looked at and I am happy enough ;)
One thing:
I think you forgot to adjust dlist_reverse_foreach to the NULL list header.
Thanks!
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
On Thursday, October 11, 2012 06:37:59 PM Andres Freund wrote:
On Thursday, October 11, 2012 03:27:17 PM Andres Freund wrote:
On Thursday, October 11, 2012 03:23:12 PM Alvaro Herrera wrote:
Alvaro Herrera escribió:
I also included two new functions in that patch, dlisti_push_head and
dlisti_push_tail. These functions are identical to dlist_push_head
and dlist_push_tail, except that they do not accept non-circular
lists. What this means is that callers that find the non-circularity
acceptable can use the regular version, and will run dlist_init() on
demand; callers that want the super-tight code can use the other
version. I didn't bother with a new dlist_is_empty.Is there any more input on this? At this point I would recommend
committing this patch _without_ these dlisti functions, i.e. we will
only have the functions that check for NULL-initialized dlists. We can
later discuss whether to include them or not (it would be a much
smaller patch and would not affect the existing functionality in any
way.)I can live with that. I didn't have a chance to look at the newest
revision yet, will do that after I finish my first pass through foreign
key locks.I looked at and I am happy enough ;)
One thing:
I think you forgot to adjust dlist_reverse_foreach to the NULL list header.
Tom, whats your thought about Alvaro's latest version (except the "bug"
mentioned above)? It looks like a somewhat fair compromise between our
positions and I don't think the external interface needs to change if we decide
to resolve any of our differences differently.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Here's the final version. I think this is ready to go in.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:
ilist-5.patchtext/x-diff; charset=us-asciiDownload
*** a/src/backend/lib/Makefile
--- b/src/backend/lib/Makefile
***************
*** 12,17 **** subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = dllist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
--- 12,17 ----
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
! OBJS = ilist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
*** a/src/backend/lib/dllist.c
--- /dev/null
***************
*** 1,214 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.c
- * this is a simple doubly linked list implementation
- * the elements of the lists are void*
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/backend/lib/dllist.c
- *
- *-------------------------------------------------------------------------
- */
- #include "postgres.h"
-
- #include "lib/dllist.h"
-
-
- Dllist *
- DLNewList(void)
- {
- Dllist *l;
-
- l = (Dllist *) palloc(sizeof(Dllist));
-
- l->dll_head = NULL;
- l->dll_tail = NULL;
-
- return l;
- }
-
- void
- DLInitList(Dllist *list)
- {
- list->dll_head = NULL;
- list->dll_tail = NULL;
- }
-
- /*
- * free up a list and all the nodes in it --- but *not* whatever the nodes
- * might point to!
- */
- void
- DLFreeList(Dllist *list)
- {
- Dlelem *curr;
-
- while ((curr = DLRemHead(list)) != NULL)
- pfree(curr);
-
- pfree(list);
- }
-
- Dlelem *
- DLNewElem(void *val)
- {
- Dlelem *e;
-
- e = (Dlelem *) palloc(sizeof(Dlelem));
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- return e;
- }
-
- void
- DLInitElem(Dlelem *e, void *val)
- {
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_val = val;
- e->dle_list = NULL;
- }
-
- void
- DLFreeElem(Dlelem *e)
- {
- pfree(e);
- }
-
- void
- DLRemove(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (e->dle_prev)
- e->dle_prev->dle_next = e->dle_next;
- else
- {
- /* must be the head element */
- Assert(e == l->dll_head);
- l->dll_head = e->dle_next;
- }
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- e->dle_next = NULL;
- e->dle_prev = NULL;
- e->dle_list = NULL;
- }
-
- void
- DLAddHead(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_head)
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
-
- if (l->dll_tail == NULL) /* if this is first element added */
- l->dll_tail = e;
- }
-
- void
- DLAddTail(Dllist *l, Dlelem *e)
- {
- e->dle_list = l;
-
- if (l->dll_tail)
- l->dll_tail->dle_next = e;
- e->dle_prev = l->dll_tail;
- e->dle_next = NULL;
- l->dll_tail = e;
-
- if (l->dll_head == NULL) /* if this is first element added */
- l->dll_head = e;
- }
-
- Dlelem *
- DLRemHead(Dllist *l)
- {
- /* remove and return the head */
- Dlelem *result = l->dll_head;
-
- if (result == NULL)
- return result;
-
- if (result->dle_next)
- result->dle_next->dle_prev = NULL;
-
- l->dll_head = result->dle_next;
-
- if (result == l->dll_tail) /* if the head is also the tail */
- l->dll_tail = NULL;
-
- result->dle_next = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- Dlelem *
- DLRemTail(Dllist *l)
- {
- /* remove and return the tail */
- Dlelem *result = l->dll_tail;
-
- if (result == NULL)
- return result;
-
- if (result->dle_prev)
- result->dle_prev->dle_next = NULL;
-
- l->dll_tail = result->dle_prev;
-
- if (result == l->dll_head) /* if the tail is also the head */
- l->dll_head = NULL;
-
- result->dle_prev = NULL;
- result->dle_list = NULL;
-
- return result;
- }
-
- /* Same as DLRemove followed by DLAddHead, but faster */
- void
- DLMoveToFront(Dlelem *e)
- {
- Dllist *l = e->dle_list;
-
- if (l->dll_head == e)
- return; /* Fast path if already at front */
-
- Assert(e->dle_prev != NULL); /* since it's not the head */
- e->dle_prev->dle_next = e->dle_next;
-
- if (e->dle_next)
- e->dle_next->dle_prev = e->dle_prev;
- else
- {
- /* must be the tail element */
- Assert(e == l->dll_tail);
- l->dll_tail = e->dle_prev;
- }
-
- l->dll_head->dle_prev = e;
- e->dle_next = l->dll_head;
- e->dle_prev = NULL;
- l->dll_head = e;
- /* We need not check dll_tail, since there must have been > 1 entry */
- }
--- 0 ----
*** /dev/null
--- b/src/backend/lib/ilist.c
***************
*** 0 ****
--- 1,109 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.c
+ * support for integrated/inline doubly- and singly- linked lists
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/lib/ilist.c
+ *
+ * NOTES
+ * This file only contains functions that are too big to be considered
+ * for inlining. See ilist.h for most of the goodies.
+ *
+ *-------------------------------------------------------------------------
+ */
+ #include "postgres.h"
+
+ /* See ilist.h */
+ #define ILIST_INCLUDE_DEFINITIONS
+
+ #include "lib/ilist.h"
+
+ /*
+ * removes a node from a list
+ *
+ * Attention: O(n)
+ */
+ void
+ slist_delete(slist_head *head, slist_node *node)
+ {
+ slist_node *last = &head->head;
+ slist_node *cur;
+ bool found PG_USED_FOR_ASSERTS_ONLY = false;
+
+ while ((cur = last->next) != NULL)
+ {
+ if (cur == node)
+ {
+ last->next = cur->next;
+ #ifdef USE_ASSERT_CHECKING
+ found = true;
+ #endif
+ break;
+ }
+ last = cur;
+ }
+
+ slist_check(head);
+ Assert(found);
+ }
+
+ #ifdef ILIST_DEBUG
+ /*
+ * Verify integrity of a doubly linked list
+ */
+ void
+ dlist_check(dlist_head *head)
+ {
+ dlist_node *cur;
+
+ if (head == NULL || !(&head->head))
+ elog(ERROR, "doubly linked list head is not properly initialized");
+
+ /* iterate in forward direction */
+ for (cur = head->head.next; cur != &head->head; cur = cur->next)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+
+ /* iterate in backward direction */
+ for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
+ {
+ if (cur == NULL ||
+ cur->next == NULL ||
+ cur->prev == NULL ||
+ cur->prev->next != cur ||
+ cur->next->prev != cur)
+ elog(ERROR, "doubly linked list is corrupted");
+ }
+ }
+
+ /*
+ * Verify integrity of a singly linked list
+ */
+ void
+ slist_check(slist_head *head)
+ {
+ slist_node *cur;
+
+ if (head == NULL)
+ elog(ERROR, "singly linked is NULL");
+
+ /*
+ * there isn't much we can test in a singly linked list other that it
+ * actually ends sometime, i.e. hasn't introduced a cycle or similar
+ */
+ for (cur = head->head.next; cur != NULL; cur = cur->next)
+ ;
+ }
+
+ #endif /* ILIST_DEBUG */
*** a/src/backend/postmaster/autovacuum.c
--- b/src/backend/postmaster/autovacuum.c
***************
*** 77,83 ****
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/dllist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
--- 77,83 ----
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
! #include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
***************
*** 152,157 **** typedef struct avl_dbase
--- 152,158 ----
Oid adl_datid; /* hash key -- must be first */
TimestampTz adl_next_worker;
int adl_score;
+ dlist_node adl_node;
} avl_dbase;
/* struct to keep track of databases in worker */
***************
*** 258,265 **** typedef struct
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /* the database list in the launcher, and the context that contains it */
! static Dllist *DatabaseList = NULL;
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
--- 259,269 ----
static AutoVacuumShmemStruct *AutoVacuumShmem;
! /*
! * the database list (of avl_dbase elements) in the launcher, and the context
! * that contains it
! */
! static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
static MemoryContext DatabaseListCxt = NULL;
/* Pointer to my own WorkerInfo, valid on each worker */
***************
*** 508,514 **** AutoVacLauncherMain(int argc, char *argv[])
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! DatabaseList = NULL;
/*
* Make sure pgstat also considers our stat data as gone. Note: we
--- 512,518 ----
/* don't leave dangling pointers to freed memory */
DatabaseListCxt = NULL;
! dlist_init(&DatabaseList);
/*
* Make sure pgstat also considers our stat data as gone. Note: we
***************
*** 576,582 **** AutoVacLauncherMain(int argc, char *argv[])
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! Dlelem *elem;
int rc;
/*
--- 580,586 ----
struct timeval nap;
TimestampTz current_time = 0;
bool can_launch;
! avl_dbase *avdb;
int rc;
/*
***************
*** 738,757 **** AutoVacLauncherMain(int argc, char *argv[])
/* We're OK to start a new worker */
! elem = DLGetTail(DatabaseList);
! if (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
!
! /*
! * launch a worker if next_worker is right now or it is in the
! * past
! */
! if (TimestampDifferenceExceeds(avdb->adl_next_worker,
! current_time, 0))
! launch_worker(current_time);
! }
! else
{
/*
* Special case when the list is empty: start a worker right away.
--- 742,748 ----
/* We're OK to start a new worker */
! if (dlist_is_empty(&DatabaseList))
{
/*
* Special case when the list is empty: start a worker right away.
***************
*** 763,768 **** AutoVacLauncherMain(int argc, char *argv[])
--- 754,776 ----
*/
launch_worker(current_time);
}
+ else
+ {
+ /*
+ * because rebuild_database_list constructs a list with most
+ * distant adl_next_worker first, we obtain our database from the
+ * tail of the list.
+ */
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
+ /*
+ * launch a worker if next_worker is right now or it is in the
+ * past
+ */
+ if (TimestampDifferenceExceeds(avdb->adl_next_worker,
+ current_time, 0))
+ launch_worker(current_time);
+ }
}
/* Normal exit from the autovac launcher is here */
***************
*** 783,789 **** AutoVacLauncherMain(int argc, char *argv[])
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! Dlelem *elem;
/*
* We sleep until the next scheduled vacuum. We trust that when the
--- 791,797 ----
static void
launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
{
! avl_dbase *avdb;
/*
* We sleep until the next scheduled vacuum. We trust that when the
***************
*** 796,809 **** launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval * nap)
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if ((elem = DLGetTail(DatabaseList)) != NULL)
{
- avl_dbase *avdb = DLE_VAL(elem);
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
--- 804,818 ----
nap->tv_sec = autovacuum_naptime;
nap->tv_usec = 0;
}
! else if (!dlist_is_empty(&DatabaseList))
{
TimestampTz current_time = GetCurrentTimestamp();
TimestampTz next_wakeup;
long secs;
int usecs;
+ avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
+
next_wakeup = avdb->adl_next_worker;
TimestampDifference(current_time, next_wakeup, &secs, &usecs);
***************
*** 867,872 **** rebuild_database_list(Oid newdb)
--- 876,882 ----
int score;
int nelems;
HTAB *dbhash;
+ dlist_iter iter;
/* use fresh stats */
autovac_refresh_stats();
***************
*** 927,962 **** rebuild_database_list(Oid newdb)
}
/* Now insert the databases from the existing list */
! if (DatabaseList != NULL)
{
! Dlelem *elem;
!
! elem = DLGetHead(DatabaseList);
! while (elem != NULL)
! {
! avl_dbase *avdb = DLE_VAL(elem);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
!
! elem = DLGetSucc(elem);
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
! }
}
}
--- 937,964 ----
}
/* Now insert the databases from the existing list */
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
! avl_dbase *db;
! bool found;
! PgStat_StatDBEntry *entry;
! /*
! * skip databases with no stat entries -- in particular, this gets
! * rid of dropped databases
! */
! entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
! if (entry == NULL)
! continue;
! db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
! if (!found)
! {
! /* hash_search already filled in the key */
! db->adl_score = score++;
! /* next_worker is filled in later */
}
}
***************
*** 987,993 **** rebuild_database_list(Oid newdb)
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! DatabaseList = DLNewList();
if (nelems > 0)
{
--- 989,995 ----
/* from here on, the allocated memory belongs to the new list */
MemoryContextSwitchTo(newcxt);
! dlist_init(&DatabaseList);
if (nelems > 0)
{
***************
*** 1029,1043 **** rebuild_database_list(Oid newdb)
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
- Dlelem *elem;
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
- elem = DLNewElem(db);
/* later elements should go closer to the head of the list */
! DLAddHead(DatabaseList, elem);
}
}
--- 1031,1043 ----
for (i = 0; i < nelems; i++)
{
avl_dbase *db = &(dbary[i]);
current_time = TimestampTzPlusMilliseconds(current_time,
millis_increment);
db->adl_next_worker = current_time;
/* later elements should go closer to the head of the list */
! dlist_push_head(&DatabaseList, &db->adl_node);
}
}
***************
*** 1147,1153 **** do_start_worker(void)
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! Dlelem *elem;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
--- 1147,1153 ----
foreach(cell, dblist)
{
avw_dbase *tmp = lfirst(cell);
! dlist_iter iter;
/* Check to see if this one is at risk of wraparound */
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
***************
*** 1179,1189 **** do_start_worker(void)
* autovacuum time yet.
*/
skipit = false;
- elem = DatabaseList ? DLGetTail(DatabaseList) : NULL;
! while (elem != NULL)
{
! avl_dbase *dbp = DLE_VAL(elem);
if (dbp->adl_datid == tmp->adw_datid)
{
--- 1179,1188 ----
* autovacuum time yet.
*/
skipit = false;
! dlist_reverse_foreach(iter, &DatabaseList)
{
! avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
if (dbp->adl_datid == tmp->adw_datid)
{
***************
*** 1200,1206 **** do_start_worker(void)
break;
}
- elem = DLGetPred(elem);
}
if (skipit)
continue;
--- 1199,1204 ----
***************
*** 1274,1295 **** static void
launch_worker(TimestampTz now)
{
Oid dbid;
! Dlelem *elem;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! elem = (DatabaseList == NULL) ? NULL : DLGetHead(DatabaseList);
! while (elem != NULL)
{
! avl_dbase *avdb = DLE_VAL(elem);
if (avdb->adl_datid == dbid)
{
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
--- 1272,1296 ----
launch_worker(TimestampTz now)
{
Oid dbid;
! dlist_iter iter;
dbid = do_start_worker();
if (OidIsValid(dbid))
{
+ bool found = false;
+
/*
* Walk the database list and update the corresponding entry. If the
* database is not on the list, we'll recreate the list.
*/
! dlist_foreach(iter, &DatabaseList)
{
! avl_dbase *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
if (avdb->adl_datid == dbid)
{
+ found = true;
+
/*
* add autovacuum_naptime seconds to the current time, and use
* that as the new "next_worker" field for this database.
***************
*** 1297,1306 **** launch_worker(TimestampTz now)
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! DLMoveToFront(elem);
break;
}
- elem = DLGetSucc(elem);
}
/*
--- 1298,1306 ----
avdb->adl_next_worker =
TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
! dlist_move_head(&DatabaseList, iter.cur);
break;
}
}
/*
***************
*** 1310,1316 **** launch_worker(TimestampTz now)
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (elem == NULL)
rebuild_database_list(dbid);
}
}
--- 1310,1316 ----
* pgstat entry, but this is not a problem because we don't want to
* schedule workers regularly into those in any case.
*/
! if (!found)
rebuild_database_list(dbid);
}
}
*** a/src/backend/postmaster/postmaster.c
--- b/src/backend/postmaster/postmaster.c
***************
*** 95,101 ****
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/dllist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
--- 95,101 ----
#include "access/xlog.h"
#include "bootstrap/bootstrap.h"
#include "catalog/pg_control.h"
! #include "lib/ilist.h"
#include "libpq/auth.h"
#include "libpq/ip.h"
#include "libpq/libpq.h"
***************
*** 146,155 **** typedef struct bkend
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! Dlelem elem; /* list link in BackendList */
} Backend;
! static Dllist *BackendList;
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
--- 146,155 ----
int child_slot; /* PMChildSlot for this backend, if any */
bool is_autovacuum; /* is it an autovacuum process? */
bool dead_end; /* is it going to send an error and quit? */
! dlist_node elem; /* list link in BackendList */
} Backend;
! static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
#ifdef EXEC_BACKEND
static Backend *ShmemBackendArray;
***************
*** 1028,1038 **** PostmasterMain(int argc, char *argv[])
set_stack_base();
/*
- * Initialize the list of active backends.
- */
- BackendList = DLNewList();
-
- /*
* Initialize pipe (or process handle on Windows) that allows children to
* wake up from sleep on postmaster death.
*/
--- 1028,1033 ----
***************
*** 1872,1878 **** processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
! Dlelem *curr;
#else
int i;
#endif
--- 1867,1873 ----
Backend *bp;
#ifndef EXEC_BACKEND
! dlist_iter iter;
#else
int i;
#endif
***************
*** 1886,1894 **** processCancelRequest(Port *port, void *pkt)
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! bp = (Backend *) DLE_VAL(curr);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
--- 1881,1889 ----
* duplicate array in shared memory.
*/
#ifndef EXEC_BACKEND
! dlist_foreach(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
#else
for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
{
***************
*** 2648,2654 **** static void
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! Dlelem *curr;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
--- 2643,2649 ----
CleanupBackend(int pid,
int exitstatus) /* child's exit status. */
{
! dlist_mutable_iter iter;
LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
***************
*** 2680,2688 **** CleanupBackend(int pid,
return;
}
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
--- 2675,2683 ----
return;
}
! dlist_foreach_modify(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->pid == pid)
{
***************
*** 2701,2707 **** CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
break;
}
--- 2696,2702 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
break;
}
***************
*** 2718,2725 **** CleanupBackend(int pid,
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! Dlelem *curr,
! *next;
Backend *bp;
/*
--- 2713,2719 ----
static void
HandleChildCrash(int pid, int exitstatus, const char *procname)
{
! dlist_mutable_iter iter;
Backend *bp;
/*
***************
*** 2734,2743 **** HandleChildCrash(int pid, int exitstatus, const char *procname)
}
/* Process regular backends */
! for (curr = DLGetHead(BackendList); curr; curr = next)
{
! next = DLGetSucc(curr);
! bp = (Backend *) DLE_VAL(curr);
if (bp->pid == pid)
{
/*
--- 2728,2737 ----
}
/* Process regular backends */
! dlist_foreach_modify(iter, &BackendList)
{
! bp = dlist_container(Backend, elem, iter.cur);
!
if (bp->pid == pid)
{
/*
***************
*** 2750,2756 **** HandleChildCrash(int pid, int exitstatus, const char *procname)
ShmemBackendArrayRemove(bp);
#endif
}
! DLRemove(curr);
free(bp);
/* Keep looping so we can signal remaining backends */
}
--- 2744,2750 ----
ShmemBackendArrayRemove(bp);
#endif
}
! dlist_delete(&BackendList, iter.cur);
free(bp);
/* Keep looping so we can signal remaining backends */
}
***************
*** 3113,3119 **** PostmasterStateMachine(void)
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (DLGetHead(BackendList) == NULL &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
--- 3107,3113 ----
* normal state transition leading up to PM_WAIT_DEAD_END, or during
* FatalError processing.
*/
! if (dlist_is_empty(&BackendList) &&
PgArchPID == 0 && PgStatPID == 0)
{
/* These other guys should be dead already */
***************
*** 3239,3250 **** signal_child(pid_t pid, int signal)
static bool
SignalSomeChildren(int signal, int target)
{
! Dlelem *curr;
bool signaled = false;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 3233,3244 ----
static bool
SignalSomeChildren(int signal, int target)
{
! dlist_iter iter;
bool signaled = false;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
***************
*** 3382,3389 **** BackendStartup(Port *port)
*/
bn->pid = pid;
bn->is_autovacuum = false;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
--- 3376,3383 ----
*/
bn->pid = pid;
bn->is_autovacuum = false;
! dlist_push_head(&BackendList, &bn->elem);
!
#ifdef EXEC_BACKEND
if (!bn->dead_end)
ShmemBackendArrayAdd(bn);
***************
*** 4491,4502 **** PostmasterRandom(void)
static int
CountChildren(int target)
{
! Dlelem *curr;
int cnt = 0;
! for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
{
! Backend *bp = (Backend *) DLE_VAL(curr);
if (bp->dead_end)
continue;
--- 4485,4496 ----
static int
CountChildren(int target)
{
! dlist_iter iter;
int cnt = 0;
! dlist_foreach(iter, &BackendList)
{
! Backend *bp = dlist_container(Backend, elem, iter.cur);
if (bp->dead_end)
continue;
***************
*** 4675,4682 **** StartAutovacuumWorker(void)
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! DLInitElem(&bn->elem, bn);
! DLAddHead(BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
--- 4669,4675 ----
if (bn->pid > 0)
{
bn->is_autovacuum = true;
! dlist_push_head(&BackendList, &bn->elem);
#ifdef EXEC_BACKEND
ShmemBackendArrayAdd(bn);
#endif
*** a/src/backend/utils/cache/catcache.c
--- b/src/backend/utils/cache/catcache.c
***************
*** 291,297 **** CatalogCacheComputeTupleHashValue(CatCache *cache, HeapTuple tuple)
static void
CatCachePrintStats(int code, Datum arg)
{
! CatCache *cache;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
--- 291,297 ----
static void
CatCachePrintStats(int code, Datum arg)
{
! slist_iter iter;
long cc_searches = 0;
long cc_hits = 0;
long cc_neg_hits = 0;
***************
*** 300,307 **** CatCachePrintStats(int code, Datum arg)
long cc_lsearches = 0;
long cc_lhits = 0;
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
--- 300,309 ----
long cc_lsearches = 0;
long cc_lhits = 0;
! slist_foreach(iter, &CacheHdr->ch_caches)
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
if (cache->cc_ntup == 0 && cache->cc_searches == 0)
continue; /* don't print unused caches */
elog(DEBUG2, "catcache %s/%u: %d tup, %ld srch, %ld+%ld=%ld hits, %ld+%ld=%ld loads, %ld invals, %ld lsrch, %ld lhits",
***************
*** 368,375 **** CatCacheRemoveCTup(CatCache *cache, CatCTup *ct)
return; /* nothing left to do */
}
! /* delink from linked list */
! DLRemove(&ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
--- 370,376 ----
return; /* nothing left to do */
}
! dlist_delete(ct->cache_bucket, &ct->cache_elem);
/* free associated tuple data */
if (ct->tuple.t_data != NULL)
***************
*** 412,418 **** CatCacheRemoveCList(CatCache *cache, CatCList *cl)
}
/* delink from linked list */
! DLRemove(&cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
--- 413,419 ----
}
/* delink from linked list */
! dlist_delete(&cache->cc_lists, &cl->cache_elem);
/* free associated tuple data */
if (cl->tuple.t_data != NULL)
***************
*** 442,459 **** CatCacheRemoveCList(CatCache *cache, CatCList *cl)
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! CatCache *ccp;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
Index hashIndex;
! Dlelem *elt,
! *nextelt;
if (cacheId != ccp->id)
continue;
--- 443,460 ----
void
CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
{
! slist_iter cache_iter;
CACHE1_elog(DEBUG2, "CatalogCacheIdInvalidate: called");
/*
* inspect caches to find the proper cache
*/
! slist_foreach(cache_iter, &CacheHdr->ch_caches)
{
Index hashIndex;
! dlist_mutable_iter iter;
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
if (cacheId != ccp->id)
continue;
***************
*** 468,478 **** CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 469,477 ----
* Invalidate *all* CatCLists in this cache; it's too hard to tell
* which searches might still be correct, so just zap 'em all.
*/
! dlist_foreach_modify(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
***************
*** 484,495 **** CatalogCacheIdInvalidate(int cacheId, uint32 hashValue)
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
!
! for (elt = DLGetHead(&ccp->cc_bucket[hashIndex]); elt; elt = nextelt)
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (hashValue == ct->hash_value)
{
--- 483,491 ----
* inspect the proper hash bucket for tuple matches
*/
hashIndex = HASH_INDEX(hashValue, ccp->cc_nbuckets);
! dlist_foreach_modify(iter, &ccp->cc_bucket[hashIndex])
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (hashValue == ct->hash_value)
{
***************
*** 557,573 **** AtEOXact_CatCache(bool isCommit)
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! CatCache *ccp;
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
! Dlelem *elt;
int i;
/* Check CatCLists */
! for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt))
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
--- 553,570 ----
#ifdef USE_ASSERT_CHECKING
if (assert_enabled)
{
! slist_iter cache_iter;
! slist_foreach(cache_iter, &(CacheHdr->ch_caches))
{
! CatCache *ccp = slist_container(CatCache, cc_next, cache_iter.cur);
! dlist_iter iter;
int i;
/* Check CatCLists */
! dlist_foreach(iter, &ccp->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
Assert(cl->cl_magic == CL_MAGIC);
Assert(cl->refcount == 0);
***************
*** 577,587 **** AtEOXact_CatCache(bool isCommit)
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&ccp->cc_bucket[i]);
! elt;
! elt = DLGetSucc(elt))
{
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
--- 574,584 ----
/* Check individual tuples */
for (i = 0; i < ccp->cc_nbuckets; i++)
{
! dlist_head *bucket = &ccp->cc_bucket[i];
!
! dlist_foreach(iter, bucket)
{
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
Assert(ct->ct_magic == CT_MAGIC);
Assert(ct->refcount == 0);
***************
*** 604,619 **** AtEOXact_CatCache(bool isCommit)
static void
ResetCatalogCache(CatCache *cache)
{
! Dlelem *elt,
! *nextelt;
int i;
/* Remove each list in this cache, or at least mark it dead */
! for (elt = DLGetHead(&cache->cc_lists); elt; elt = nextelt)
{
! CatCList *cl = (CatCList *) DLE_VAL(elt);
!
! nextelt = DLGetSucc(elt);
if (cl->refcount > 0)
cl->dead = true;
--- 601,613 ----
static void
ResetCatalogCache(CatCache *cache)
{
! dlist_mutable_iter iter;
int i;
/* Remove each list in this cache, or at least mark it dead */
! dlist_foreach_modify(iter, &cache->cc_lists)
{
! CatCList *cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->refcount > 0)
cl->dead = true;
***************
*** 624,634 **** ResetCatalogCache(CatCache *cache)
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! for (elt = DLGetHead(&cache->cc_bucket[i]); elt; elt = nextelt)
! {
! CatCTup *ct = (CatCTup *) DLE_VAL(elt);
! nextelt = DLGetSucc(elt);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
--- 618,628 ----
/* Remove each tuple in this cache, or at least mark it dead */
for (i = 0; i < cache->cc_nbuckets; i++)
{
! dlist_head *bucket = &cache->cc_bucket[i];
! dlist_foreach_modify(iter, bucket)
! {
! CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->refcount > 0 ||
(ct->c_list && ct->c_list->refcount > 0))
***************
*** 654,665 **** ResetCatalogCache(CatCache *cache)
void
ResetCatalogCaches(void)
{
! CatCache *cache;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
ResetCatalogCache(cache);
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
--- 648,663 ----
void
ResetCatalogCaches(void)
{
! slist_iter iter;
CACHE1_elog(DEBUG2, "ResetCatalogCaches called");
! slist_foreach(iter, &CacheHdr->ch_caches)
! {
! CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
!
ResetCatalogCache(cache);
+ }
CACHE1_elog(DEBUG2, "end of ResetCatalogCaches call");
}
***************
*** 680,691 **** ResetCatalogCaches(void)
void
CatalogCacheFlushCatalog(Oid catId)
{
! CatCache *cache;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! for (cache = CacheHdr->ch_caches; cache; cache = cache->cc_next)
{
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
--- 678,691 ----
void
CatalogCacheFlushCatalog(Oid catId)
{
! slist_iter iter;
CACHE2_elog(DEBUG2, "CatalogCacheFlushCatalog called for %u", catId);
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
+ CatCache *cache = slist_container(CatCache, cc_next, iter.cur);
+
/* Does this cache store tuples of the target catalog? */
if (cache->cc_reloid == catId)
{
***************
*** 760,766 **** InitCatCache(int id,
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! CacheHdr->ch_caches = NULL;
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
--- 760,766 ----
if (CacheHdr == NULL)
{
CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
! slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
/* set up to dump stats at backend exit */
***************
*** 770,779 **** InitCatCache(int id,
/*
* allocate a new cache structure
- *
- * Note: we assume zeroing initializes the Dllist headers correctly
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(Dllist));
/*
* initialize the cache's relation information for the relation
--- 770,777 ----
/*
* allocate a new cache structure
*/
! cp = (CatCache *) palloc0(sizeof(CatCache) + nbuckets * sizeof(dlist_node));
/*
* initialize the cache's relation information for the relation
***************
*** 792,797 **** InitCatCache(int id,
--- 790,798 ----
for (i = 0; i < nkeys; ++i)
cp->cc_key[i] = key[i];
+ dlist_init(&cp->cc_lists);
+ MemSet(&cp->cc_bucket, 0, nbuckets * sizeof(dlist_head));
+
/*
* new cache is initialized as far as we can go for now. print some
* debugging information, if appropriate.
***************
*** 801,808 **** InitCatCache(int id,
/*
* add completed cache to top of group header's list
*/
! cp->cc_next = CacheHdr->ch_caches;
! CacheHdr->ch_caches = cp;
/*
* back to the old context before we return...
--- 802,808 ----
/*
* add completed cache to top of group header's list
*/
! slist_push_head(&CacheHdr->ch_caches, &cp->cc_next);
/*
* back to the old context before we return...
***************
*** 1060,1066 **** SearchCatCache(CatCache *cache,
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! Dlelem *elt;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
--- 1060,1067 ----
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 hashValue;
Index hashIndex;
! dlist_mutable_iter iter;
! dlist_head *bucket;
CatCTup *ct;
Relation relation;
SysScanDesc scandesc;
***************
*** 1094,1106 **** SearchCatCache(CatCache *cache,
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead)
continue; /* ignore dead entries */
--- 1095,1107 ----
/*
* scan the hash bucket until we find a match or exhaust our tuples
*/
! bucket = &cache->cc_bucket[hashIndex];
!
! dlist_foreach_modify(iter, bucket)
{
bool res;
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead)
continue; /* ignore dead entries */
***************
*** 1125,1131 **** SearchCatCache(CatCache *cache,
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! DLMoveToFront(&ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
--- 1126,1132 ----
* most frequently accessed elements in any hashbucket will tend to be
* near the front of the hashbucket's list.)
*/
! dlist_move_head(bucket, &ct->cache_elem);
/*
* If it's a positive entry, bump its refcount and return it. If it's
***************
*** 1340,1346 **** SearchCatCacheList(CatCache *cache,
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! Dlelem *elt;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
--- 1341,1347 ----
{
ScanKeyData cur_skey[CATCACHE_MAXKEYS];
uint32 lHashValue;
! dlist_iter iter;
CatCList *cl;
CatCTup *ct;
List *volatile ctlist;
***************
*** 1382,1394 **** SearchCatCacheList(CatCache *cache,
/*
* scan the items until we find a match or exhaust our list
*/
! for (elt = DLGetHead(&cache->cc_lists);
! elt;
! elt = DLGetSucc(elt))
{
bool res;
! cl = (CatCList *) DLE_VAL(elt);
if (cl->dead)
continue; /* ignore dead entries */
--- 1383,1393 ----
/*
* scan the items until we find a match or exhaust our list
*/
! dlist_foreach(iter, &cache->cc_lists)
{
bool res;
! cl = dlist_container(CatCList, cache_elem, iter.cur);
if (cl->dead)
continue; /* ignore dead entries */
***************
*** 1416,1422 **** SearchCatCacheList(CatCache *cache,
* since there's no point in that unless they are searched for
* individually.)
*/
! DLMoveToFront(&cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
--- 1415,1421 ----
* since there's no point in that unless they are searched for
* individually.)
*/
! dlist_move_head(&cache->cc_lists, &cl->cache_elem);
/* Bump the list's refcount and return it */
ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner);
***************
*** 1468,1473 **** SearchCatCacheList(CatCache *cache,
--- 1467,1474 ----
{
uint32 hashValue;
Index hashIndex;
+ bool found = false;
+ dlist_head *bucket;
/*
* See if there's an entry for this tuple already.
***************
*** 1476,1486 **** SearchCatCacheList(CatCache *cache,
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! for (elt = DLGetHead(&cache->cc_bucket[hashIndex]);
! elt;
! elt = DLGetSucc(elt))
{
! ct = (CatCTup *) DLE_VAL(elt);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
--- 1477,1486 ----
hashValue = CatalogCacheComputeTupleHashValue(cache, ntp);
hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
! bucket = &cache->cc_bucket[hashIndex];
! dlist_foreach(iter, bucket)
{
! ct = dlist_container(CatCTup, cache_elem, iter.cur);
if (ct->dead || ct->negative)
continue; /* ignore dead and negative entries */
***************
*** 1498,1507 **** SearchCatCacheList(CatCache *cache,
if (ct->c_list)
continue;
break; /* A-OK */
}
! if (elt == NULL)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
--- 1498,1508 ----
if (ct->c_list)
continue;
+ found = true;
break; /* A-OK */
}
! if (!found)
{
/* We didn't find a usable entry, so make a new one */
ct = CatalogCacheCreateEntry(cache, ntp,
***************
*** 1564,1570 **** SearchCatCacheList(CatCache *cache,
cl->cl_magic = CL_MAGIC;
cl->my_cache = cache;
- DLInitElem(&cl->cache_elem, cl);
cl->refcount = 0; /* for the moment */
cl->dead = false;
cl->ordered = ordered;
--- 1565,1570 ----
***************
*** 1587,1593 **** SearchCatCacheList(CatCache *cache,
}
Assert(i == nmembers);
! DLAddHead(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
--- 1587,1593 ----
}
Assert(i == nmembers);
! dlist_push_head(&cache->cc_lists, &cl->cache_elem);
/* Finally, bump the list's refcount and return it */
cl->refcount++;
***************
*** 1664,1677 **** CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp,
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! DLInitElem(&ct->cache_elem, (void *) ct);
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
--- 1664,1678 ----
*/
ct->ct_magic = CT_MAGIC;
ct->my_cache = cache;
! ct->cache_bucket = &cache->cc_bucket[hashIndex];
!
ct->c_list = NULL;
ct->refcount = 0; /* for the moment */
ct->dead = false;
ct->negative = negative;
ct->hash_value = hashValue;
! dlist_push_head(ct->cache_bucket, &ct->cache_elem);
cache->cc_ntup++;
CacheHdr->ch_ntup++;
***************
*** 1785,1791 **** PrepareToInvalidateCacheTuple(Relation relation,
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! CatCache *ccp;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
--- 1786,1792 ----
HeapTuple newtuple,
void (*function) (int, uint32, Oid))
{
! slist_iter iter;
Oid reloid;
CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called");
***************
*** 1808,1817 **** PrepareToInvalidateCacheTuple(Relation relation,
* ----------------
*/
! for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next)
{
uint32 hashvalue;
Oid dbid;
if (ccp->cc_reloid != reloid)
continue;
--- 1809,1819 ----
* ----------------
*/
! slist_foreach(iter, &(CacheHdr->ch_caches))
{
uint32 hashvalue;
Oid dbid;
+ CatCache *ccp = slist_container(CatCache, cc_next, iter.cur);
if (ccp->cc_reloid != reloid)
continue;
*** a/src/include/lib/dllist.h
--- /dev/null
***************
*** 1,85 ****
- /*-------------------------------------------------------------------------
- *
- * dllist.h
- * simple doubly linked list primitives
- * the elements of the list are void* so the lists can contain anything
- * Dlelem can only be in one list at a time
- *
- *
- * Here's a small example of how to use Dllists:
- *
- * Dllist *lst;
- * Dlelem *elt;
- * void *in_stuff; -- stuff to stick in the list
- * void *out_stuff
- *
- * lst = DLNewList(); -- make a new dllist
- * DLAddHead(lst, DLNewElem(in_stuff)); -- add a new element to the list
- * with in_stuff as the value
- * ...
- * elt = DLGetHead(lst); -- retrieve the head element
- * out_stuff = (void*)DLE_VAL(elt); -- get the stuff out
- * DLRemove(elt); -- removes the element from its list
- * DLFreeElem(elt); -- free the element since we don't
- * use it anymore
- *
- *
- * It is also possible to use Dllist objects that are embedded in larger
- * structures instead of being separately malloc'd. To do this, use
- * DLInitElem() to initialize a Dllist field within a larger object.
- * Don't forget to DLRemove() each field from its list (if any) before
- * freeing the larger object!
- *
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/include/lib/dllist.h
- *
- *-------------------------------------------------------------------------
- */
-
- #ifndef DLLIST_H
- #define DLLIST_H
-
- struct Dllist;
- struct Dlelem;
-
- typedef struct Dlelem
- {
- struct Dlelem *dle_next; /* next element */
- struct Dlelem *dle_prev; /* previous element */
- void *dle_val; /* value of the element */
- struct Dllist *dle_list; /* what list this element is in */
- } Dlelem;
-
- typedef struct Dllist
- {
- Dlelem *dll_head;
- Dlelem *dll_tail;
- } Dllist;
-
- extern Dllist *DLNewList(void); /* allocate and initialize a list header */
- extern void DLInitList(Dllist *list); /* init a header alloced by caller */
- extern void DLFreeList(Dllist *list); /* free up a list and all the nodes in
- * it */
- extern Dlelem *DLNewElem(void *val);
- extern void DLInitElem(Dlelem *e, void *val);
- extern void DLFreeElem(Dlelem *e);
- extern void DLRemove(Dlelem *e); /* removes node from list */
- extern void DLAddHead(Dllist *list, Dlelem *node);
- extern void DLAddTail(Dllist *list, Dlelem *node);
- extern Dlelem *DLRemHead(Dllist *list); /* remove and return the head */
- extern Dlelem *DLRemTail(Dllist *list);
- extern void DLMoveToFront(Dlelem *e); /* move node to front of its list */
-
- /* These are macros for speed */
- #define DLGetHead(list) ((list)->dll_head)
- #define DLGetTail(list) ((list)->dll_tail)
- #define DLGetSucc(elem) ((elem)->dle_next)
- #define DLGetPred(elem) ((elem)->dle_prev)
- #define DLGetListHdr(elem) ((elem)->dle_list)
-
- #define DLE_VAL(elem) ((elem)->dle_val)
-
- #endif /* DLLIST_H */
--- 0 ----
*** /dev/null
--- b/src/include/lib/ilist.h
***************
*** 0 ****
--- 1,766 ----
+ /*-------------------------------------------------------------------------
+ *
+ * ilist.h
+ * integrated/inline doubly- and singly-linked lists
+ *
+ * This implementation is as efficient as possible: the lists don't have
+ * any memory management overhead, because the list pointers are embedded
+ * within some larger structure.
+ *
+ * There are two kinds of empty doubly linked lists: those that have been
+ * initialized to NULL, and those that have been initialized to circularity.
+ * The second kind is useful for tight optimization, because there are some
+ * operations that can be done without branches (and thus faster) on lists that
+ * have been initialized to circularity. Most users don't care all that much,
+ * and so can skip the initialization step until really required.
+ *
+ * NOTES
+ * This is intended to be used in situations where memory for a struct and
+ * its contents already needs to be allocated and the overhead of
+ * allocating extra list cells for every list element is noticeable. Thus,
+ * none of the functions here allocate any memory; they just manipulate
+ * externally managed memory. The API for singly/doubly linked lists is
+ * identical as far as capabilities of both allow.
+ *
+ * EXAMPLES
+ *
+ * An oversimplified example demonstrating how this can be used. Let's assume
+ * we want to store information about the tables contained in a database.
+ *
+ * #include "lib/ilist.h"
+ *
+ * // Define struct for the databases including a list header that will be used
+ * // to access the nodes in the list later on.
+ * typedef struct my_database
+ * {
+ * char *datname;
+ * dlist_head tables;
+ * // ...
+ * } my_database;
+ *
+ * // Define struct for the tables. Note the list_node element which stores
+ * // information about prev/next list nodes.
+ * typedef struct my_table
+ * {
+ * char *tablename;
+ * dlist_node list_node;
+ * perm_t permissions;
+ * // ...
+ * } value_in_a_list;
+ *
+ * // create a database
+ * my_database *db = create_database();
+ *
+ * // and add a few tables to its table list
+ * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
+ * ...
+ * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
+ *
+ *
+ * To iterate over the table list, we allocate an iterator element and use
+ * a specialized looping construct. Inside a dlist_foreach, the iterator's
+ * 'cur' field can be used to access the current element. iter.cur points to a
+ * 'dlist_node', but most of the time what we want is the actual table
+ * information; dlist_container() gives us that, like so:
+ *
+ * dlist_iter iter;
+ * dlist_foreach(iter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, iter.cur);
+ * printf("we have a table: %s in database %s\n",
+ * val->tablename, db->datname);
+ * }
+ *
+ *
+ * While a simple iteration is useful, we sometimes also want to manipulate
+ * the list while iterating. There is a different iterator element and looping
+ * construct for that. Suppose we want to delete tables that meet a certain
+ * criterion:
+ *
+ * dlist_mutable_iter miter;
+ * dlist_foreach_modify(miter, &db->tables)
+ * {
+ * my_table *tbl = dlist_container(my_table, list_node, miter.cur);
+ *
+ * if (!tbl->to_be_deleted)
+ * continue; // don't touch this one
+ *
+ * // unlink the current table from the linked list
+ * dlist_delete(&db->tables, miter.cur);
+ * // as these lists never manage memory, we can freely access the table
+ * // after it's been deleted
+ * drop_table(db, tbl);
+ * }
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/include/lib/ilist.h
+ *-------------------------------------------------------------------------
+ */
+ #ifndef ILIST_H
+ #define ILIST_H
+
+ /*
+ * Enable for extra debugging. This is rather expensive, so it's not enabled by
+ * default even when USE_ASSERT_CHECKING.
+ */
+ /* #define ILIST_DEBUG */
+
+ /*
+ * Node of a doubly linked list.
+ *
+ * Embed this in structs that need to be part of a doubly linked list.
+ */
+ typedef struct dlist_node dlist_node;
+ struct dlist_node
+ {
+ dlist_node *prev;
+ dlist_node *next;
+ };
+
+ /*
+ * Head of a doubly linked list.
+ *
+ * Non-empty lists are internally circularly linked. Circular lists have the
+ * advantage of not needing any branches in the most common list manipulations.
+ * An empty list can also be represented as a pair of NULL pointers, making
+ * initialization easier.
+ */
+ typedef struct dlist_head
+ {
+ /*
+ * head->next either points to the first element of the list; to &head if
+ * it's a circular empty list; or to NULL if empty and not circular.
+ *
+ * head->prev either points to the last element of the list; to &head if
+ * it's a circular empty list; or to NULL if empty and not circular.
+ */
+ dlist_node head;
+ } dlist_head;
+
+
+ /*
+ * Doubly linked list iterator.
+ *
+ * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
+ * current element of the iteration use the 'cur' member.
+ *
+ * Iterations using this are *not* allowed to change the list while iterating!
+ *
+ * NB: We use an extra type for this to make it possible to avoid multiple
+ * evaluations of arguments in the dlist_foreach() macro.
+ */
+ typedef struct dlist_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ } dlist_iter;
+
+ /*
+ * Doubly linked list iterator allowing some modifications while iterating
+ *
+ * Used as state in dlist_foreach_modify(). To get the current element of the
+ * iteration use the 'cur' member.
+ *
+ * Iterations using this are only allowed to change the list *at the current
+ * point of iteration*. It is fine to delete the current node, but it is *not*
+ * fine to modify other nodes.
+ *
+ * NB: We need a separate type for mutable iterations to avoid having to pass
+ * in two iterators or some other state variable as we need to store the
+ * '->next' node of the current node so it can be deleted or modified by the
+ * user.
+ */
+ typedef struct dlist_mutable_iter
+ {
+ dlist_node *end; /* last node we iterate to */
+ dlist_node *cur; /* current element */
+ dlist_node *next; /* next node we iterate to, so we can delete
+ * cur */
+ } dlist_mutable_iter;
+
+ /*
+ * Node of a singly linked list.
+ *
+ * Embed this in structs that need to be part of a singly linked list.
+ */
+ typedef struct slist_node slist_node;
+ struct slist_node
+ {
+ slist_node *next;
+ };
+
+ /*
+ * Head of a singly linked list.
+ *
+ * Singly linked lists are not circularly linked, in contrast to doubly linked
+ * lists. As no pointer to the last list element and to the previous node needs
+ * to be maintained this doesn't incur any additional branches in the usual
+ * manipulations.
+ */
+ typedef struct slist_head
+ {
+ slist_node head;
+ } slist_head;
+
+ /*
+ * Singly linked list iterator
+ *
+ * Used in slist_foreach(). To get the current element of the iteration use the
+ * 'cur' member.
+ *
+ * Do *not* manipulate the list while iterating!
+ *
+ * NB: this wouldn't really need to be an extra struct, we could use a
+ * slist_node * directly. We still use a separate type for consistency.
+ */
+ typedef struct slist_iter
+ {
+ slist_node *cur;
+ } slist_iter;
+
+ /*
+ * Singly linked list iterator allowing some modifications while iterating
+ *
+ * Used in slist_foreach_modify.
+ *
+ * Iterations using this are allowed to remove the current node and to add more
+ * nodes to the beginning of the list.
+ */
+ typedef struct slist_mutable_iter
+ {
+ slist_node *cur;
+ slist_node *next;
+ } slist_mutable_iter;
+
+
+ /* Prototypes for functions too big to be inline */
+
+ /* Attention: O(n) */
+ extern void slist_delete(slist_head *head, slist_node *node);
+
+ #ifdef ILIST_DEBUG
+ extern void dlist_check(dlist_head *head);
+ extern void slist_check(slist_head *head);
+ #else
+ /*
+ * These seemingly useless casts to void are here to keep the compiler quiet
+ * about the argument being unused in many functions in a non-debug compile,
+ * in which functions the only point of passing the list head pointer is to be
+ * able to run these checks.
+ */
+ #define dlist_check(head) (void) (head)
+ #define slist_check(head) (void) (head)
+ #endif /* ILIST_DEBUG */
+
+ /* Static initializers */
+ #define DLIST_STATIC_INIT(name) {{&name.head, &name.head}}
+ #define SLIST_STATIC_INIT(name) {{NULL}}
+
+
+ /*
+ * We want the functions below to be inline; but if the compiler doesn't
+ * support that, fall back on providing them as regular functions. See
+ * STATIC_IF_INLINE in c.h.
+ */
+ #ifndef PG_USE_INLINE
+ extern void dlist_init(dlist_head *head);
+ extern bool dlist_is_empty(dlist_head *head);
+ extern void dlist_push_head(dlist_head *head, dlist_node *node);
+ extern void dlist_push_tail(dlist_head *head, dlist_node *node);
+ extern void dlist_insert_after(dlist_head *head,
+ dlist_node *after, dlist_node *node);
+ extern void dlist_insert_before(dlist_head *head,
+ dlist_node *before, dlist_node *node);
+ extern void dlist_delete(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_pop_head_node(dlist_head *head);
+ extern void dlist_move_head(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_next(dlist_head *head, dlist_node *node);
+ extern bool dlist_has_prev(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_next_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_prev_node(dlist_head *head, dlist_node *node);
+ extern dlist_node *dlist_head_node(dlist_head *head);
+ extern dlist_node *dlist_tail_node(dlist_head *head);
+
+ /* dlist macro support functions */
+ extern void *dlist_tail_element_off(dlist_head *head, size_t off);
+ extern void *dlist_head_element_off(dlist_head *head, size_t off);
+ #endif /* !PG_USE_INLINE */
+
+ #if defined(PG_USE_INLINE) || defined(ILIST_INCLUDE_DEFINITIONS)
+ /*
+ * Initialize the head of a list. Previous state will be thrown away without
+ * any cleanup.
+ */
+ STATIC_IF_INLINE void
+ dlist_init(dlist_head *head)
+ {
+ head->head.next = head->head.prev = &head->head;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node at the beginning of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_push_head(dlist_head *head, dlist_node *node)
+ {
+ if (head->head.next == NULL)
+ dlist_init(head);
+
+ node->next = head->head.next;
+ node->prev = &head->head;
+ node->next->prev = node;
+ head->head.next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Inserts a node at the end of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_push_tail(dlist_head *head, dlist_node *node)
+ {
+ if (head->head.next == NULL)
+ dlist_init(head);
+
+ node->next = &head->head;
+ node->prev = head->head.prev;
+ node->prev->next = node;
+ head->head.prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node after another *in the same list*
+ */
+ STATIC_IF_INLINE void
+ dlist_insert_after(dlist_head *head, dlist_node *after, dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'after' is in 'head'? */
+
+ node->prev = after;
+ node->next = after->next;
+ after->next = node;
+ node->next->prev = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Insert a node before another *in the same list*
+ */
+ STATIC_IF_INLINE void
+ dlist_insert_before(dlist_head *head, dlist_node *before, dlist_node *node)
+ {
+ dlist_check(head);
+ /* XXX: assert 'before' is in 'head'? */
+
+ node->prev = before->prev;
+ node->next = before;
+ before->prev = node;
+ node->prev->next = node;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete 'node' from list.
+ *
+ * It is not allowed to delete a 'node' which is is not in the list 'head'
+ */
+ STATIC_IF_INLINE void
+ dlist_delete(dlist_head *head, dlist_node *node)
+ {
+ dlist_check(head);
+
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+
+ dlist_check(head);
+ }
+
+ /*
+ * Delete and return the first node from a list.
+ *
+ * Undefined behaviour when the list is empty. Check with dlist_is_empty if
+ * necessary.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_pop_head_node(dlist_head *head)
+ {
+ dlist_node *ret;
+
+ Assert(&head->head != head->head.next);
+
+ ret = head->head.next;
+ dlist_delete(head, head->head.next);
+ return ret;
+ }
+
+ /*
+ * Move element from its current position in the list to the head position in
+ * the same list.
+ *
+ * Undefined behaviour if 'node' is not already part of the list.
+ */
+ STATIC_IF_INLINE void
+ dlist_move_head(dlist_head *head, dlist_node *node)
+ {
+ /* fast path if it's already at the head */
+ if (head->head.next == node)
+ return;
+
+ dlist_delete(head, node);
+ dlist_push_head(head, node);
+
+ dlist_check(head);
+ }
+
+ /*
+ * Check whether the passed node is the last element in the list.
+ */
+ STATIC_IF_INLINE bool
+ dlist_has_next(dlist_head *head, dlist_node *node)
+ {
+ return node->next != &head->head;
+ }
+
+ /*
+ * Check whether the passed node is the first element in the list.
+ */
+ STATIC_IF_INLINE bool
+ dlist_has_prev(dlist_head *head, dlist_node *node)
+ {
+ return node->prev != &head->head;
+ }
+
+ /*
+ * Return the next node in the list.
+ *
+ * Undefined behaviour when no next node exists. Use dlist_has_next to make
+ * sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_next_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_next(head, node));
+ return node->next;
+ }
+
+ /*
+ * Return previous node in the list.
+ *
+ * Undefined behaviour when no prev node exists. Use dlist_has_prev to make
+ * sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_prev_node(dlist_head *head, dlist_node *node)
+ {
+ Assert(dlist_has_prev(head, node));
+ return node->prev;
+ }
+
+ /*
+ * Return whether the list is empty.
+ *
+ * An empty list has either its first 'next' pointer set to NULL, or to itself.
+ */
+ STATIC_IF_INLINE bool
+ dlist_is_empty(dlist_head *head)
+ {
+ dlist_check(head);
+
+ return head->head.next == NULL || head->head.next == &(head->head);
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ dlist_head_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Return the first node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_head_node(dlist_head *head)
+ {
+ return dlist_head_element_off(head, 0);
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ dlist_tail_element_off(dlist_head *head, size_t off)
+ {
+ Assert(!dlist_is_empty(head));
+ return (char *) head->head.prev - off;
+ }
+
+ /*
+ * Return the last node in the list.
+ *
+ * Use dlist_is_empty to make sure the list is not empty if not sure.
+ */
+ STATIC_IF_INLINE dlist_node *
+ dlist_tail_node(dlist_head *head)
+ {
+ return dlist_tail_element_off(head, 0);
+ }
+ #endif /* PG_USE_INLINE || ILIST_INCLUDE_DEFINITIONS */
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the dlist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a dlist_node * back to its containing struct.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
+ AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list must not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_head_element_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ *
+ * The list must not be empty.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define dlist_tail_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*) NULL)->membername, dlist_node), \
+ ((type *)dlist_tail_element_off(ptr, offsetof(type, membername))))
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, \
+ iter.cur = iter.end->next ? iter.end->next : iter.end; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->next)
+
+ /*
+ * Iterate through the list pointed at by 'ptr' storing the state in 'iter'.
+ *
+ * Access the current element with iter.cur.
+ *
+ * It is allowed to delete the current element from the list. Every other
+ * manipulation can lead to corruption.
+ */
+ #define dlist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_mutable_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, \
+ iter.cur = iter.end->next ? iter.end->next : iter.end, \
+ iter.next = iter.cur->next; \
+ iter.cur != iter.end; \
+ iter.cur = iter.next, iter.next = iter.cur->next)
+
+ /*
+ * Iterate through the list in reverse order.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define dlist_reverse_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, dlist_iter); \
+ AssertVariableIsOfType(ptr, dlist_head *); \
+ for (iter.end = &(ptr)->head, \
+ iter.cur = iter.end->prev ? iter.end->prev : iter.end; \
+ iter.cur != iter.end; \
+ iter.cur = iter.cur->prev)
+
+
+ /*
+ * We want the functions below to be inline; but if the compiler doesn't
+ * support that, fall back on providing them as regular functions. See
+ * STATIC_IF_INLINE in c.h.
+ */
+ #ifndef PG_USE_INLINE
+ extern void slist_init(slist_head *head);
+ extern bool slist_is_empty(slist_head *head);
+ extern slist_node *slist_head_node(slist_head *head);
+ extern void slist_push_head(slist_head *head, slist_node *node);
+ extern slist_node *slist_pop_head_node(slist_head *head);
+ extern void slist_insert_after(slist_head *head,
+ slist_node *after, slist_node *node);
+ extern bool slist_has_next(slist_head *head, slist_node *node);
+ extern slist_node *slist_next_node(slist_head *head, slist_node *node);
+
+ /* slist macro support function */
+ extern void *slist_head_element_off(slist_head *head, size_t off);
+ #endif
+
+ #if defined(PG_USE_INLINE) || defined(ILIST_INCLUDE_DEFINITIONS)
+ /*
+ * Initialize a singly linked list.
+ */
+ STATIC_IF_INLINE void
+ slist_init(slist_head *head)
+ {
+ head->head.next = NULL;
+
+ slist_check(head);
+ }
+
+ /*
+ * Is the list empty?
+ */
+ STATIC_IF_INLINE bool
+ slist_is_empty(slist_head *head)
+ {
+ slist_check(head);
+
+ return head->head.next == NULL;
+ }
+
+ /* internal support function */
+ STATIC_IF_INLINE void *
+ slist_head_element_off(slist_head *head, size_t off)
+ {
+ Assert(!slist_is_empty(head));
+ return (char *) head->head.next - off;
+ }
+
+ /*
+ * Push 'node' as the new first node in the list, pushing the original head to
+ * the second position.
+ */
+ STATIC_IF_INLINE void
+ slist_push_head(slist_head *head, slist_node *node)
+ {
+ node->next = head->head.next;
+ head->head.next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Remove and return the first node in the list
+ *
+ * Undefined behaviour if the list is empty.
+ */
+ STATIC_IF_INLINE slist_node *
+ slist_pop_head_node(slist_head *head)
+ {
+ slist_node *node;
+
+ Assert(!slist_is_empty(head));
+
+ node = head->head.next;
+ head->head.next = head->head.next->next;
+
+ slist_check(head);
+
+ return node;
+ }
+
+ /*
+ * Insert a new node after another one
+ *
+ * Undefined behaviour if 'after' is not part of the list already.
+ */
+ STATIC_IF_INLINE void
+ slist_insert_after(slist_head *head, slist_node *after,
+ slist_node *node)
+ {
+ node->next = after->next;
+ after->next = node;
+
+ slist_check(head);
+ }
+
+ /*
+ * Return whether 'node' has a following node
+ */
+ STATIC_IF_INLINE bool
+ slist_has_next(slist_head *head,
+ slist_node *node)
+ {
+ slist_check(head);
+
+ return node->next != NULL;
+ }
+ #endif /* PG_USE_INLINE || ILIST_INCLUDE_DEFINITIONS */
+
+ /*
+ * Return the containing struct of 'type' where 'membername' is the slist_node
+ * pointed at by 'ptr'.
+ *
+ * This is used to convert a slist_node * back to its containing struct.
+ *
+ * Note that AssertVariableIsOfTypeMacro is a compile time only check, so we
+ * don't have multiple evaluation dangers here.
+ */
+ #define slist_container(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(ptr, slist_node *), \
+ AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ ((type *)((char *)(ptr) - offsetof(type, membername))))
+
+ /*
+ * Return the value of first element in the list.
+ */
+ #define slist_head_element(type, membername, ptr) \
+ (AssertVariableIsOfTypeMacro(((type*)NULL)->membername, slist_node), \
+ slist_head_element_off(ptr, offsetoff(type, membername)))
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter'.
+ *
+ * It is *not* allowed to manipulate the list during iteration.
+ */
+ #define slist_foreach(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next; \
+ iter.cur != NULL; \
+ iter.cur = iter.cur->next)
+
+ /*
+ * Iterate through the list 'ptr' using the iterator 'iter' allowing some
+ * modifications.
+ *
+ * It is allowed to delete the current element from the list and add new nodes
+ * before the current position. Other manipulations can lead to corruption.
+ */
+ #define slist_foreach_modify(iter, ptr) \
+ AssertVariableIsOfType(iter, slist_mutable_iter); \
+ AssertVariableIsOfType(ptr, slist_head *); \
+ for (iter.cur = (ptr)->head.next, \
+ iter.next = iter.cur ? iter.cur->next : NULL; \
+ iter.cur != NULL; \
+ iter.cur = iter.next, iter.next = iter.next ? iter.next->next : NULL)
+
+ #endif /* ILIST_H */
*** a/src/include/utils/catcache.h
--- b/src/include/utils/catcache.h
***************
*** 22,28 ****
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/dllist.h"
#include "utils/relcache.h"
/*
--- 22,28 ----
#include "access/htup.h"
#include "access/skey.h"
! #include "lib/ilist.h"
#include "utils/relcache.h"
/*
***************
*** 37,43 ****
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! struct catcache *cc_next; /* link to next catcache */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
--- 37,43 ----
typedef struct catcache
{
int id; /* cache identifier --- see syscache.h */
! slist_node cc_next; /* list link */
const char *cc_relname; /* name of relation the tuples come from */
Oid cc_reloid; /* OID of relation the tuples come from */
Oid cc_indexoid; /* OID of index matching cache keys */
***************
*** 51,57 **** typedef struct catcache
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! Dllist cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
--- 51,57 ----
ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
* heap scans */
bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
! dlist_head cc_lists; /* list of CatCList structs */
#ifdef CATCACHE_STATS
long cc_searches; /* total # searches against this cache */
long cc_hits; /* # of matches against existing entry */
***************
*** 66,72 **** typedef struct catcache
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
--- 66,72 ----
long cc_lsearches; /* total # list-searches */
long cc_lhits; /* # of matches against existing lists */
#endif
! dlist_head cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
} CatCache; /* VARIABLE LENGTH STRUCT */
***************
*** 77,87 **** typedef struct catctup
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a Dllist that stores the elements
! * of its hash bucket. We keep each Dllist in LRU order to speed repeated
* lookups.
*/
! Dlelem cache_elem; /* list member of per-bucket list */
/*
* The tuple may also be a member of at most one CatCList. (If a single
--- 77,88 ----
CatCache *my_cache; /* link to owning catcache */
/*
! * Each tuple in a cache is a member of a dlist that stores the elements
! * of its hash bucket. We keep each dlist in LRU order to speed repeated
* lookups.
*/
! dlist_node cache_elem; /* list member of per-bucket list */
! dlist_head *cache_bucket; /* containing bucket dlist */
/*
* The tuple may also be a member of at most one CatCList. (If a single
***************
*** 139,145 **** typedef struct catclist
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! Dlelem cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
--- 140,146 ----
* might not be true during bootstrap or recovery operations. (namespace.c
* is able to save some cycles when it is true.)
*/
! dlist_node cache_elem; /* list member of per-catcache list */
int refcount; /* number of active references */
bool dead; /* dead but not yet removed? */
bool ordered; /* members listed in index order? */
***************
*** 153,159 **** typedef struct catclist
typedef struct catcacheheader
{
! CatCache *ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
--- 154,160 ----
typedef struct catcacheheader
{
! slist_head ch_caches; /* head of list of CatCache structs */
int ch_ntup; /* # of tuples in all caches */
} CatCacheHeader;
Alvaro Herrera escribió:
Here's the final version. I think this is ready to go in.
Committed.
There are several uses of SHM_QUEUE in the backend code which AFAICS can
be replaced with dlist. If someone's looking for an easy project,
here's one.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Here's the final version. I think this is ready to go in.
I got around to reviewing this today. I'm pretty seriously annoyed at
the definition of dlist_delete: it should *not* require the list header.
The present coding simply throws away one of the primary advantages of
a doubly-linked list over a singly-linked list, namely that you don't
have to have your hands on the list header in order to unlink a node.
This isn't merely academic either, as I see that the patch to catcache
code actually added a field to struct catctup to support making the
list header available. That's a complete waste of 8 bytes (on a 64-bit
machine) per catalog cache entry. The only thing it buys for us is
the ability to run dlist_check, which is something that isn't even
compiled (not even in an Assert build), and which doesn't actually do
that much useful even if it is compiled --- for instance, there's no way
to verify that the nodes were actually in the list claimed.
I think we should remove the head argument at least from dlist_delete,
and probably also dlist_insert_after and dlist_insert_before.
regards, tom lane
Tom Lane escribió:
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Here's the final version. I think this is ready to go in.
I got around to reviewing this today. I'm pretty seriously annoyed at
the definition of dlist_delete: it should *not* require the list header.
The present coding simply throws away one of the primary advantages of
a doubly-linked list over a singly-linked list, namely that you don't
have to have your hands on the list header in order to unlink a node.
This isn't merely academic either, as I see that the patch to catcache
code actually added a field to struct catctup to support making the
list header available. That's a complete waste of 8 bytes (on a 64-bit
machine) per catalog cache entry. The only thing it buys for us is
the ability to run dlist_check, which is something that isn't even
compiled (not even in an Assert build), and which doesn't actually do
that much useful even if it is compiled --- for instance, there's no way
to verify that the nodes were actually in the list claimed.
Oops. I mentioned this explicitely somewhere in the discussion. I
assumed you had seen that, and that you would have complained had you
found it objectionable. (It's hard enough to figure out if people don't
respond because they don't have a problem with something, or just
because they didn't see it.)
On the other hand, it's convenient to remove them, because in
predicate.c there are plenty of SHM_QUEUE node removals which is clearly
easier to port over to dlist if we don't have to figure out exactly
which list each node is in. (Maybe in other SHM_QUEUE users as well,
but that's the most complex of the bunch.)
I think we should remove the head argument at least from dlist_delete,
and probably also dlist_insert_after and dlist_insert_before.
There are more functions that get the list head just to run the check.
Can I assume that you don't propose removing the argument from those?
(dlist_next_node, dlist_prev_node I think are the only ones).
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
Oops. I mentioned this explicitely somewhere in the discussion. I
assumed you had seen that, and that you would have complained had you
found it objectionable.
Sorry, I've been too busy to pay very much attention to this patch.
I think we should remove the head argument at least from dlist_delete,
and probably also dlist_insert_after and dlist_insert_before.
There are more functions that get the list head just to run the check.
Can I assume that you don't propose removing the argument from those?
(dlist_next_node, dlist_prev_node I think are the only ones).
Yeah, I wondered whether to do the same for those. But it's less of an
issue there, because in practice the caller is almost certainly going to
also need to do dlist_has_next or dlist_has_prev respectively, and those
require the list header.
On the other hand, applying the same principle to slists, you could
argue that slist_has_next and slist_next_node should not require the
head pointer (since that's throwing away an advantage of slists).
If we wanted to remove the head pointer from those, there would be some
value in not having the head argument in dlist_next_node/dlist_prev_node
for symmetry with slist_next_node.
I'm not as excited about these since it seems relatively less likely to
matter. What do you think?
regards, tom lane