Commit a66ee69a authored by Alvaro Herrera's avatar Alvaro Herrera

Embedded list interface

Provide a common implementation of embedded singly-linked and
doubly-linked lists.  "Embedded" in the sense that the nodes'
next/previous pointers exist within some larger struct; this design
choice reduces memory allocation overhead.

Most of the implementation uses inlineable functions (where supported),
for performance.

Some existing uses of both types of lists have been converted to the new
code, for demonstration purposes.  Other uses can (and probably will) be
converted in the future.  Since dllist.c is unused after this conversion,
it has been removed.

Author: Andres Freund
Some tweaks by me
Reviewed by Tom Lane, Peter Geoghegan
parent f862a326
......@@ -12,6 +12,6 @@ subdir = src/backend/lib
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
OBJS = dllist.o stringinfo.o
OBJS = ilist.o stringinfo.o
include $(top_srcdir)/src/backend/common.mk
/*-------------------------------------------------------------------------
*
* 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 */
}
/*-------------------------------------------------------------------------
*
* 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 */
This diff is collapsed.
......@@ -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;
......@@ -1027,11 +1027,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 +1867,7 @@ processCancelRequest(Port *port, void *pkt)
Backend *bp;
#ifndef EXEC_BACKEND
Dlelem *curr;
dlist_iter iter;
#else
int i;
#endif
......@@ -1886,9 +1881,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 +2643,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 +2675,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 +2696,7 @@ CleanupBackend(int pid,
ShmemBackendArrayRemove(bp);
#endif
}
DLRemove(curr);
dlist_delete(&BackendList, iter.cur);
free(bp);
break;
}
......@@ -2718,8 +2713,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 +2728,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 +2744,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 +3107,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 +3233,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 +3376,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);
......@@ -4491,12 +4485,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;
......@@ -4675,8 +4669,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
......
This diff is collapsed.
/*-------------------------------------------------------------------------
*
* 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 */
This diff is collapsed.
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment