shmqueue.c 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * shmqueue.c--
 *    shared memory linked lists
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
Marc G. Fournier's avatar
Marc G. Fournier committed
10
 *    $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmqueue.c,v 1.2 1996/11/03 05:06:58 scrappy Exp $
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
 *
 * NOTES
 *
 * Package for managing doubly-linked lists in shared memory.
 * The only tricky thing is that SHM_QUEUE will usually be a field 
 * in a larger record.  SHMQueueGetFirst has to return a pointer
 * to the record itself instead of a pointer to the SHMQueue field
 * of the record.  It takes an extra pointer and does some extra
 * pointer arithmetic to do this correctly.
 *
 * NOTE: These are set up so they can be turned into macros some day.
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>		/* for sprintf() */
#include "postgres.h"
#include "storage/shmem.h"	/* where the declarations go */

/*#define SHMQUEUE_DEBUG*/
#ifdef SHMQUEUE_DEBUG
#define SHMQUEUE_DEBUG_DEL	/* deletions */
#define SHMQUEUE_DEBUG_HD	/* head inserts */
#define SHMQUEUE_DEBUG_TL	/* tail inserts */
#define SHMQUEUE_DEBUG_ELOG NOTICE
#endif /* SHMQUEUE_DEBUG */

/*
 * ShmemQueueInit -- make the head of a new queue point
 * 	to itself
 */
void
SHMQueueInit(SHM_QUEUE *queue)
{
    Assert(SHM_PTR_VALID(queue));
    (queue)->prev = (queue)->next = MAKE_OFFSET(queue);
}

/*
 * SHMQueueIsDetached -- TRUE if element is not currently
 *	in a queue.
 */
bool
SHMQueueIsDetached(SHM_QUEUE *queue)
{
    Assert(SHM_PTR_VALID(queue));
    return ((queue)->prev == INVALID_OFFSET);
}

/*
 * SHMQueueElemInit -- clear an element's links
 */
void
SHMQueueElemInit(SHM_QUEUE *queue)
{
    Assert(SHM_PTR_VALID(queue));
    (queue)->prev = (queue)->next = INVALID_OFFSET;
}

/*
 * SHMQueueDelete -- remove an element from the queue and
 * 	close the links
 */
void
SHMQueueDelete(SHM_QUEUE *queue)
{
    SHM_QUEUE *nextElem = (SHM_QUEUE *) MAKE_PTR((queue)->next);
    SHM_QUEUE *prevElem = (SHM_QUEUE *) MAKE_PTR((queue)->prev);
    
    Assert(SHM_PTR_VALID(queue));
    Assert(SHM_PTR_VALID(nextElem));
    Assert(SHM_PTR_VALID(prevElem));
    
#ifdef SHMQUEUE_DEBUG_DEL
    dumpQ(queue, "in SHMQueueDelete: begin");
#endif /* SHMQUEUE_DEBUG_DEL */
    
    prevElem->next =  (queue)->next;
    nextElem->prev =  (queue)->prev;
    
#ifdef SHMQUEUE_DEBUG_DEL
    dumpQ((SHM_QUEUE *)MAKE_PTR(queue->prev), "in SHMQueueDelete: end");
#endif /* SHMQUEUE_DEBUG_DEL */
}

#ifdef SHMQUEUE_DEBUG
void
dumpQ(SHM_QUEUE *q, char *s)
{
    char elem[16];
    char buf[1024];
    SHM_QUEUE	*start = q;
    int count = 0;
    
    sprintf(buf, "q prevs: %x", MAKE_OFFSET(q));
    q = (SHM_QUEUE *)MAKE_PTR(q->prev);
    while (q != start)
	{
	    sprintf(elem, "--->%x", MAKE_OFFSET(q));
	    strcat(buf, elem);
	    q = (SHM_QUEUE *)MAKE_PTR(q->prev);
	    if (q->prev == MAKE_OFFSET(q))
		break;
	    if (count++ > 40)
		{
		    strcat(buf, "BAD PREV QUEUE!!");
		    break;
		}
	}
    sprintf(elem, "--->%x", MAKE_OFFSET(q));
    strcat(buf, elem);
    elog(SHMQUEUE_DEBUG_ELOG, "%s: %s", s, buf);
    
    sprintf(buf, "q nexts: %x", MAKE_OFFSET(q));
    count = 0;
    q = (SHM_QUEUE *)MAKE_PTR(q->next);
    while (q != start)
	{
	    sprintf(elem, "--->%x", MAKE_OFFSET(q));
	    strcat(buf, elem);
	    q = (SHM_QUEUE *)MAKE_PTR(q->next);
	    if (q->next == MAKE_OFFSET(q))
		break;
	    if (count++ > 10)
		{
		    strcat(buf, "BAD NEXT QUEUE!!");
		    break;
		}
	}
    sprintf(elem, "--->%x", MAKE_OFFSET(q));
    strcat(buf, elem);
    elog(SHMQUEUE_DEBUG_ELOG, "%s: %s", s, buf);
}
#endif /* SHMQUEUE_DEBUG */

/*
 * SHMQueueInsertHD -- put elem in queue between the queue head
 *	and its "prev" element.
 */
void
SHMQueueInsertHD(SHM_QUEUE *queue, SHM_QUEUE *elem)
{
    SHM_QUEUE *prevPtr = (SHM_QUEUE *) MAKE_PTR((queue)->prev);
    SHMEM_OFFSET	elemOffset = MAKE_OFFSET(elem);
    
    Assert(SHM_PTR_VALID(queue));
    Assert(SHM_PTR_VALID(elem));
    
#ifdef SHMQUEUE_DEBUG_HD
    dumpQ(queue, "in SHMQueueInsertHD: begin");
#endif /* SHMQUEUE_DEBUG_HD */
    
    (elem)->next = prevPtr->next;
    (elem)->prev = queue->prev;
    (queue)->prev = elemOffset;
    prevPtr->next = elemOffset;
    
#ifdef SHMQUEUE_DEBUG_HD
    dumpQ(queue, "in SHMQueueInsertHD: end");
#endif /* SHMQUEUE_DEBUG_HD */
}

void
SHMQueueInsertTL(SHM_QUEUE *queue, SHM_QUEUE *elem)
{
    SHM_QUEUE *nextPtr = (SHM_QUEUE *) MAKE_PTR((queue)->next);
    SHMEM_OFFSET	elemOffset = MAKE_OFFSET(elem);
    
    Assert(SHM_PTR_VALID(queue));
    Assert(SHM_PTR_VALID(elem));
    
#ifdef SHMQUEUE_DEBUG_TL
    dumpQ(queue, "in SHMQueueInsertTL: begin");
#endif /* SHMQUEUE_DEBUG_TL */
    
    (elem)->prev = nextPtr->prev;
    (elem)->next = queue->next;
    (queue)->next = elemOffset;
    nextPtr->prev = elemOffset;
    
#ifdef SHMQUEUE_DEBUG_TL
    dumpQ(queue, "in SHMQueueInsertTL: end");
#endif /* SHMQUEUE_DEBUG_TL */
}

/*
 * SHMQueueFirst -- Get the first element from a queue
 *
 * First element is queue->next.  If SHMQueue is part of
 * a larger structure, we want to return a pointer to the
 * whole structure rather than a pointer to its SHMQueue field.
 * I.E. struct {
 *	int 		stuff;
 *	SHMQueue 	elem;
 * } ELEMType; 
 * when this element is in a queue (queue->next) is struct.elem.
 * nextQueue allows us to calculate the offset of the SHMQueue
 * field in the structure.
 *
 * call to SHMQueueFirst should take these parameters:
 *
 *   &(queueHead),&firstElem,&(firstElem->next)
 *
 * Note that firstElem may well be uninitialized.  if firstElem
 * is initially K, &(firstElem->next) will be K+ the offset to
 * next.
 */
void
SHMQueueFirst(SHM_QUEUE *queue, Pointer *nextPtrPtr, SHM_QUEUE *nextQueue)
{
    SHM_QUEUE *elemPtr = (SHM_QUEUE *) MAKE_PTR((queue)->next);
    
    Assert(SHM_PTR_VALID(queue));
    *nextPtrPtr = (Pointer) (((unsigned long) *nextPtrPtr) +
			  ((unsigned long) elemPtr) - ((unsigned long) nextQueue)); 
    
    /*
      nextPtrPtr a ptr to a structure linked in the queue
      nextQueue is the SHMQueue field of the structure
      *nextPtrPtr - nextQueue is 0 minus the offset of the queue 
      field n the record 
      elemPtr + (*nextPtrPtr - nexQueue) is the start of the
      structure containing elemPtr.
      */
}

/*
 * SHMQueueEmpty -- TRUE if queue head is only element, FALSE otherwise
 */
bool
SHMQueueEmpty(SHM_QUEUE *queue)
{
    Assert(SHM_PTR_VALID(queue));
    
    if (queue->prev == MAKE_OFFSET(queue)) 
	{
	    Assert(queue->next = MAKE_OFFSET(queue));
	    return(TRUE);
	}
    return(FALSE);
}