File: lists.c

package info (click to toggle)
rpc2 2.7%2Bdebian-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,852 kB
  • ctags: 2,661
  • sloc: ansic: 19,928; sh: 9,110; lex: 437; yacc: 416; makefile: 126; asm: 35
file content (303 lines) | stat: -rw-r--r-- 9,078 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* BLURB lgpl

                           Coda File System
                              Release 5

          Copyright (c) 1987-1999 Carnegie Mellon University
                  Additional copyrights listed below

This  code  is  distributed "AS IS" without warranty of any kind under
the  terms of the  GNU  Library General Public Licence  Version 2,  as
shown in the file LICENSE. The technical and financial contributors to
Coda are listed in the file CREDITS.

                        Additional copyrights

#*/

/*
                         IBM COPYRIGHT NOTICE

                          Copyright (C) 1986
             International Business Machines Corporation
                         All Rights Reserved

This  file  contains  some  code identical to or derived from the 1986
version of the Andrew File System ("AFS"), which is owned by  the  IBM
Corporation.   This  code is provided "AS IS" and IBM does not warrant
that it is free of infringement of  any  intellectual  rights  of  any
third  party.    IBM  disclaims  liability of any kind for any damages
whatsoever resulting directly or indirectly from use of this  software
or  of  any  derivative work.  Carnegie Mellon University has obtained
permission to  modify,  distribute and sublicense this code,  which is
based on Version 2  of  AFS  and  does  not  contain  the features and
enhancements that are part of  Version 3 of  AFS.  Version 3 of AFS is
commercially   available   and  supported  by   Transarc  Corporation,
Pittsburgh, PA.

*/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <assert.h>
#include "rpc2.private.h"


/* Routines to allocate and manipulate the doubly-linked circular lists 
	used elsewhere in rpc2 */

void rpc2_Replenish(whichList, whichCount, elemSize, creationCount, magicNumber)
    struct LinkEntry  **whichList;
    long *whichCount;
    long elemSize;	/* size of each element in the list */
    long *creationCount;
    long magicNumber;
    /*  Routine to avoid using malloc() too often.  
	Assumes *whichList is empty and grows it by 1 entry of size elemSize.
	Sets *whichCount to 1.
	Bumps creationCount by 1.
    */
    {    
	   
    *whichList = (struct LinkEntry *)malloc(elemSize);
    assert(*whichList != NULL);
    memset(*whichList, 0, elemSize);
    (*whichList)->NextEntry = (*whichList)->PrevEntry = *whichList; /* 1-element circular list */
    (*whichList)->MagicNumber = magicNumber;
    (*whichList)->Qname = whichList;
    *whichCount = 1;
    (*creationCount)++;
    }


/* Generic routine to move elements between lists Assumes p points to
   an entry in the list pointed to by fromPtr.  Moves that entry to
   the list pointed to by toPtr.  If p is NULL, an arbitrary entry in
   the from list is selected as a victim and moved.  If toPtr is NULL,
   the moved entry is made into a singleton list.  In all cases a
   pointer to the moved entry is returned as the value of the
   function.

	*fromCount is decremented by one.
	*toCount is incremented by one.

   Frequently used routine -- optimize the hell out of it.  */
struct LinkEntry *rpc2_MoveEntry(fromPtr, toPtr, p, fromCount, toCount)
    /* pointers to header pointers of from and to lists */
    struct LinkEntry **fromPtr, **toPtr;
    struct LinkEntry *p;	/* pointer to entry to be moved */
    long *fromCount;		/* pointer to count of entries in from list */
    long *toCount;		/* pointer to count of entries in to list */

{
    struct LinkEntry *victim;

    if (p == NULL) 
	    victim = *fromPtr;	
    else 
	    victim = p;
    assert(victim->Qname == fromPtr);    /* sanity check for list corruption */

    /* first remove element from the first list */
    if (victim == *fromPtr) 
	    *fromPtr = victim->NextEntry;

    /* remque(victim); */
    victim->PrevEntry->NextEntry = victim->NextEntry;
    victim->NextEntry->PrevEntry = victim->PrevEntry;
    victim->PrevEntry = victim->NextEntry = victim;

    if (victim == *fromPtr) 
	    *fromPtr = NULL;
    (*fromCount)--;

    /* make victim a singleton list */
    victim->NextEntry = victim->PrevEntry = victim;

    /* then insert into second list */
    if (*toPtr == NULL) 
        *toPtr = victim;
    else {
	/* PrevEntry because semantics of insque() causes non-FIFO queue */
        /* insque(victim, (*toPtr)->PrevEntry); */
        victim->PrevEntry = (*toPtr)->PrevEntry;
        victim->NextEntry = *toPtr;
        (*toPtr)->PrevEntry->NextEntry = victim;
        (*toPtr)->PrevEntry = victim;
    }
    victim->Qname = toPtr;
    (*toCount)++;
    return(victim);
}

/* Allocates an SL entry and binds it to slConn */
struct SL_Entry *rpc2_AllocSle(enum SL_Type slType, struct CEntry *slConn)
{
	struct SL_Entry *sl, **tolist;
	long *tocount;
	
	if (rpc2_SLFreeCount == 0)
		{
			rpc2_Replenish(&rpc2_SLFreeList, &rpc2_SLFreeCount,
				       sizeof(struct SL_Entry), &rpc2_SLCreationCount, OBJ_SLENTRY);
	}

    if (slType == REQ)
	{
	tolist = &rpc2_SLReqList;
	tocount = &rpc2_SLReqCount;
	}
    else
	{
	tolist = &rpc2_SLList;
	tocount = &rpc2_SLCount;
	}

    sl = (struct SL_Entry *)rpc2_MoveEntry(&rpc2_SLFreeList,
	    tolist, NULL, &rpc2_SLFreeCount, tocount);

    assert(sl->MagicNumber == OBJ_SLENTRY);
    sl->Type = slType;
    if (slType != REQ && slConn != NULL) {
	    slConn->MySl = sl;
	    sl->Conn = slConn->UniqueCID;
    }   else 
	    sl->Conn = 0;

    return(sl);
    }

void rpc2_FreeSle(INOUT struct SL_Entry **sl)
    /* Releases the SL_Entry pointed to by sl. Sets sl to NULL.
       Removes binding between sl and its connection */
{
    struct SL_Entry *tsl, **fromlist;
    long *fromcount;
    struct CEntry *ce;
    
    tsl = *sl;
    assert(tsl->MagicNumber == OBJ_SLENTRY);

    if (tsl->Conn != 0) {
	ce = __rpc2_GetConn(tsl->Conn);
	if (ce) ce->MySl = NULL;
    }

    if (tsl->Type == REQ) {
	fromlist = &rpc2_SLReqList;
	fromcount = &rpc2_SLReqCount;
    } else {
	fromlist = &rpc2_SLList;
	fromcount = &rpc2_SLCount;
    }

    rpc2_MoveEntry(fromlist, &rpc2_SLFreeList, tsl, fromcount, &rpc2_SLFreeCount);
    *sl = NULL;
}

void rpc2_ActivateSle (selem, exptime)
    struct SL_Entry *selem;
    struct timeval *exptime;
    {
    struct TM_Elem *t, *oldt;

    assert(selem->MagicNumber == OBJ_SLENTRY);
    selem->TElem.BackPointer = (char *)selem;
    selem->ReturnCode = WAITING;

    t = &selem->TElem;

    if (exptime == NULL)
    	{/* infinite timeout, don't add to timer chain */
	t->TotalTime.tv_sec = -1;
	t->TotalTime.tv_usec = -1;
	return;
	}

    t->TotalTime = *exptime; /* structure assignment */

    oldt = TM_GetEarliest(rpc2_TimerQueue);
    /* if the new entry expires before any previous timeout, signal the socket
     * listener to recheck the timerqueue (being able to rely on the
     * availability of timercmp would be nice) */
    if (!oldt || oldt->TimeLeft.tv_sec > t->TotalTime.tv_sec ||
	(oldt->TimeLeft.tv_sec == t->TotalTime.tv_sec &&
	 oldt->TimeLeft.tv_usec > t->TotalTime.tv_usec))
	IOMGR_Cancel(rpc2_SocketListenerPID);

    TM_Insert(rpc2_TimerQueue, t);
    }

void rpc2_DeactivateSle(sl, rc)
    struct SL_Entry *sl;
    enum RetVal rc;
    {
    struct timeval *t;

    assert(sl->MagicNumber == OBJ_SLENTRY);

    sl->ReturnCode = rc;
    t = &sl->TElem.TotalTime;
    if (t->tv_sec == -1 && t->tv_usec == -1) return; /* not timed */
    else {
	TM_Remove(rpc2_TimerQueue, &sl->TElem);
	t->tv_sec = t->tv_usec = -1;	/* keep routine idempotent */
    }
    }


struct SubsysEntry *rpc2_AllocSubsys()
    /* Allocates a new subsystem entry and returns a pointer to it.
    	Returns NULL if unable to allocate such an entry.
    */
    {
    struct SubsysEntry *ss;
    if (rpc2_SSFreeCount == 0)
    	rpc2_Replenish(&rpc2_SSFreeList,
		&rpc2_SSFreeCount, sizeof(struct SubsysEntry),
		&rpc2_SSCreationCount, OBJ_SSENTRY);
    ss = (struct SubsysEntry *)rpc2_MoveEntry(&rpc2_SSFreeList,
	 &rpc2_SSList, NULL, &rpc2_SSFreeCount, &rpc2_SSCount);
    assert(ss->MagicNumber == OBJ_SSENTRY);
    return(ss);
    }

void rpc2_FreeSubsys(whichSubsys)
    struct SubsysEntry **whichSubsys;
    /* Releases the subsystem  entry pointed to by whichSubsys.
	Sets whichSubsys to NULL;  */
	{
	assert((*whichSubsys)->MagicNumber == OBJ_SSENTRY);
	rpc2_MoveEntry(&rpc2_SSList, &rpc2_SSFreeList, whichSubsys,
		    &rpc2_SSCount, &rpc2_SSFreeCount);
	*whichSubsys = NULL;
	}



/* Moves packet whichPB to hold list from inuse list */
void rpc2_HoldPacket(RPC2_PacketBuffer *whichPB)
{
	assert(whichPB->Prefix.MagicNumber == OBJ_PACKETBUFFER);
	rpc2_MoveEntry(&rpc2_PBList, &rpc2_PBHoldList, whichPB,
		       &rpc2_PBCount, &rpc2_PBHoldCount);
	if (rpc2_HoldHWMark < rpc2_PBHoldCount) 
		rpc2_HoldHWMark = rpc2_PBHoldCount;
}

/* Moves packet whichPB to inuse list from hold list */
void rpc2_UnholdPacket(RPC2_PacketBuffer *whichPB)
{
	assert(whichPB->Prefix.MagicNumber == OBJ_PACKETBUFFER);
	rpc2_MoveEntry(&rpc2_PBHoldList, &rpc2_PBList, whichPB,
		       &rpc2_PBHoldCount, &rpc2_PBCount);
}