use strlcpy, strlcat, slprintf everywhere
[mpls-ppp.git] / NeXT / inlines.h
blob88e155a05014e4eaf40891a096632442bedfed2a
1 /*
2 * Netbufs don't come with nifty queuing functions
3 * like mbufs. We therefore make our own quques by
4 * squirreling away an extra pointer before the data
5 * in a netbuf. As we can't guarantee that this will
6 * be aligned to anything in particular I use bcopy to
7 * read and write it. bcopy can use 32 bit if it really
8 * feels like it...
9 * PCF
12 #if defined(m68k)
13 #import "spl.h"
14 #else
15 #import <kernserv/machine/spl.h>
16 #endif
17 #include <kernserv/kern_server_types.h>
18 #include <kernserv/kalloc.h>
19 #include "nbq.h"
22 * There is no driver kit for the Moto release.
24 #ifndef IOLog
25 #define IOLog printf
26 #define IOLogDbg if (sc->sc_flags & SC_DEBUG) printf
27 #else
28 #define IOLogDbg if (sc->sc_flags & SC_DEBUG) IOLog
29 #endif
31 extern kern_server_t instance;
34 * Careful about using this function. Some places
35 * in the code drop packets based on this count
36 * but they never free them.
39 static inline int
40 nbq_full(struct nb_queue* nbq)
42 int rv;
43 rv = (nbq->len >= nbq->max);
44 return rv;
47 static inline int
48 nbq_empty(struct nb_queue* nbq)
50 int rv;
51 rv = (!nbq->head);
52 return rv;
55 static inline int
56 nbq_low(struct nb_queue* nbq)
58 int rv;
59 rv = (nbq->len <= nbq->low);
60 return rv;
63 static inline int
64 nbq_high(struct nb_queue* nbq)
66 int rv;
67 rv = (nbq->len >= nbq->high);
68 return rv;
71 static inline NETBUF_T
72 nbq_peek(struct nb_queue* nbq)
74 int s;
75 NETBUF_T nb;
77 s = splimp();
78 nb = nbq->head;
79 splx(s);
80 return nb;
83 static inline NETBUF_T
84 nbq_dequeue(struct nb_queue* nbq)
86 int s;
87 NETBUF_T nb;
89 if (!nbq->head)
90 return NULL;
92 s = splimp();
93 nb = nbq->head;
94 NB_GET_NEXT(nb,&nbq->head);
95 if (!nbq->head)
96 nbq->tail = NULL;
97 --nbq->len;
98 splx(s);
100 return nb;
104 * One simple note about nbq_enqueue: it will enqueue packets even if
105 * it is full, so the caller is responsible for checking this first...
107 * We return 1 if we added, else we return 0
108 * if there was a problem. We leave it up to the caller
109 * to detect an error return value (0) and print
110 * an appropriate message/update stats. However, in the spirit of
111 * keeping the code as close to the netbsd version as is possible,
112 * WE WILL FREE a packet that can't be enqueued. This should be the
113 * responsibility of the caller but that is currently not the case.
115 * Actually, now I'm using the hidden pointer arrangement then theres
116 * no circumstances under which this can return 0, oh well...
117 * PCF
120 static inline int
121 nbq_enqueue(struct nb_queue* nbq, NETBUF_T nb)
123 int s;
125 NB_SET_NEXT(nb,NULL);
126 s = splimp();
127 if (nbq->tail)
128 NB_SET_NEXT(nbq->tail,nb);
129 else
130 nbq->head = nb;
131 nbq->tail = nb;
132 ++nbq->len;
133 splx(s);
134 return 1;
137 static inline void
138 nbq_flush(struct nb_queue *nbq)
140 NETBUF_T nb,temp;
141 int s;
143 s = splimp();
144 nb = nbq->head;
145 while(nb) {
146 temp=nb;
147 NB_GET_NEXT(nb,&nb);
148 NB_FREE(temp);
151 nbq->head = nbq->tail = NULL;
152 nbq->len = 0;
153 nbq->dropped = 0;
154 splx(s);
158 * Must not be called at interrupt priority
161 static inline void
162 nbq_init(struct nb_queue *nbq, struct qparms *qp)
164 nbq->name = qp->q_name;
165 nbq->head = nbq->tail = NULL;
166 nbq->low = qp->q_low;
167 nbq->high = qp->q_high;
168 nbq->max = qp->q_max;
169 nbq->len = 0;
170 nbq->dropped = 0;
173 static inline void
174 nbq_free(struct nb_queue *nbq)
176 nbq_flush(nbq);
179 static inline void
180 nbq_drop(struct nb_queue *nbq)
182 ++nbq->dropped;
186 * Not very pretty, but it makes for less "diffs"...
188 #define mtod(m,type) ((type) NB_MAP(m))
190 typedef void (*pfv)(void *);
192 /* used by both ppp_tty.c and if_ppp.c */
193 static inline kern_return_t
194 pppsched(pfv func, struct ppp_softc *sc)
196 extern kern_server_t instance;
197 kern_return_t result;
199 if ((result = kern_serv_callout(&instance, func, (void *)sc)) != KERN_SUCCESS)
200 IOLog("kern_serv_callout failed: ret = %x\n", result);
202 return result;
205 #undef u
207 static inline thread_t
208 current_thread(void)
210 extern thread_t active_threads[];
212 return active_threads[0];
215 extern struct proc *proc_from_thread(thread_t);
216 extern struct uthread *uthread_from_thread(thread_t);
218 #define curproc (proc_from_thread(current_thread()))