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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* Dkbasket.c
*
* $Id$
*
* Baskets
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2018 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _DKBASKET_H
#define _DKBASKET_H
/*
* struct basket_t
*
* This is a queue of tokens. The next_token member of the token is used
* to link successive tokens when they are in a basket. A token is either
* in no basket or in exactly one basket
*/
typedef struct basket_s basket_t;
struct basket_s
{
basket_t * bsk_next;
basket_t * bsk_prev;
union
{
long longval;
void * ptrval;
} bsk_data;
#ifdef MTX_DEBUG
dk_mutex_t * bsk_req_mtx;
#endif
};
#define bsk_count bsk_data.longval
#define bsk_pointer bsk_data.ptrval
/* Dkbasket.c */
void basket_init (basket_t * bsk);
void basket_add (basket_t * bsk, void *token);
void *basket_peek (basket_t * bsk);
void *basket_get (basket_t * bsk);
void *basket_first (basket_t * bsk);
int basket_is_empty (basket_t * bsk);
void mp_basket_add (mem_pool_t * mp, basket_t * bsk, void *token);
void *mp_basket_get (basket_t * bsk);
typedef int (*basket_check_t) (void *elt, void *cd);
void *basket_remove_if (basket_t * bsk, basket_check_t f, void *cd);
typedef struct rbasket_s
{
mem_pool_t * rb_pool;
caddr_t ** rb_array;
int rb_length;
int rb_head;
int rb_tail;
} rbasket_t;
void rbasket_init (rbasket_t * bsk);
void rbasket_add (rbasket_t * bsk, void *token);
void *rbasket_first (rbasket_t * bsk);
void *rbasket_get (rbasket_t * bsk);
int rbasket_count (rbasket_t * bsk);
#ifdef MTX_DEBUG
#define BSK_REQ_MTX(b, m) (b)->bsk_req_mtx = m
#else
#define BSK_REQ_MTX(b, m)
#endif
#ifndef __LIST2_H
#define __LIST2_H
/*#ifdef MALLOC_DEBUG
#define L2_DEBUG
#endif*/
#ifdef L2_DEBUG
#define L2_ASSERT_SOLO(elt, ep) { \
if (NULL != elt->ep##prev) \
{ \
if (elt == elt->ep##prev->ep##next) \
GPF_T1("L2_DEBUG: elt is next of prev of elt before insert, about to destroy other list"); \
} \
if (NULL != elt->ep##next) \
{ \
if (elt == elt->ep##next->ep##prev) \
GPF_T1("L2_DEBUG: elt is prev of next of elt before insert, about to destroy other list"); \
} \
}
#define L2_ASSERT_PROPER_ENDS(first, last, ep) { \
if (NULL != first) \
{ \
if (NULL == last) GPF_T1("L2_DEBUG: last is NULL but first is not"); \
if (NULL != first->ep##prev) GPF_T1("L2_DEBUG: _prev of first is not NULL"); \
if (NULL != last->ep##next) GPF_T1("L2_DEBUG: _next of last is not NULL"); \
} \
else \
if (NULL != last) GPF_T1("L2_DEBUG: first is NULL but last is not"); \
}
#define L2_ASSERT_CONNECTION(first, last, ep) { \
int __prev_ofs = ((char *)(&(first->ep##prev))) - ((char *)(first)); \
int __next_ofs = ((char *)(&(first->ep##next))) - ((char *)(first)); \
char *__iprev = NULL; \
char *__iter = (void *)first; \
while (__iter != last) { \
if (NULL == __iter) GPF_T1("L2_DEBUG: last not found to the right of first"); \
__iter = ((char **)(__iter + __next_ofs))[0]; \
} \
}
#define L2_ASSERT_DISCONNECTION(first, outer, ep) { \
int __prev_ofs = ((char *)(&(first->ep##prev))) - ((char *)(first)); \
int __next_ofs = ((char *)(&(first->ep##next))) - ((char *)(first)); \
char *__iprev = NULL; \
char *__iter = (void *)first; \
while (NULL != __iter) { \
if (outer == __iter) GPF_T1("L2_DEBUG: unexpected occurrence of outer to the right of first"); \
__iter = ((char **)(__iter + __next_ofs))[0]; \
} \
}
#else
#define L2_ASSERT_SOLO(elt, ep)
#define L2_ASSERT_PROPER_ENDS(first, last, ep)
#define L2_ASSERT_CONNECTION(first, last, ep)
#define L2_ASSERT_DISCONNECTION(first, outer, ep)
#endif
#define L2_PUSH(first, last, elt, ep) \
{ \
L2_ASSERT_SOLO(elt, ep) \
L2_ASSERT_PROPER_ENDS(first, last, ep) \
L2_ASSERT_CONNECTION(first, last, ep) \
L2_ASSERT_DISCONNECTION(first, elt, ep) \
elt->ep##next = first; \
if (first) \
first->ep##prev = elt; \
elt->ep##prev = NULL; \
if (!last) last = elt; \
first = elt; \
}
#define L2_PUSH_LAST(first, last, elt, ep) \
{ \
L2_ASSERT_SOLO(elt, ep) \
L2_ASSERT_PROPER_ENDS(first, last, ep) \
L2_ASSERT_CONNECTION(first, last, ep) \
L2_ASSERT_DISCONNECTION(first, elt, ep) \
elt->ep##prev = last; \
if (last) \
last->ep##next = elt; \
elt->ep##next = NULL; \
if (!first) first = elt; \
last = elt; \
}
#define L2_DELETE(first, last, elt, ep) \
{ \
L2_ASSERT_PROPER_ENDS(first, last, ep) \
L2_ASSERT_CONNECTION(first, elt, ep) \
L2_ASSERT_CONNECTION(elt, last, ep) \
if (elt->ep##prev) \
elt->ep##prev->ep##next = elt->ep##next; \
if (elt->ep##next) \
elt->ep##next->ep##prev = elt->ep##prev; \
if (elt == first) \
first = elt->ep##next; \
if (elt == last) \
last = elt->ep##prev; \
elt->ep##prev = elt->ep##next = NULL; \
}
#define L2_INSERT(first, last, before, it, ep) \
{ \
L2_ASSERT_PROPER_ENDS(first, last, ep) \
L2_ASSERT_CONNECTION(first, before, ep) \
L2_ASSERT_CONNECTION(before, last, ep) \
if (before != it->ep##next) \
{ \
L2_ASSERT_SOLO(it, ep) \
L2_ASSERT_DISCONNECTION(first, it, ep) \
} \
L2_ASSERT_DISCONNECTION(first, it, ep) \
if (before == first) \
{ \
L2_PUSH (first, last, it, ep); \
} \
else \
{ \
it->ep##prev = before->ep##prev; \
it->ep##next = before; \
before->ep##prev->ep##next = it; \
before->ep##prev = it; \
} \
}
#define L2_INSERT_AFTER(first, last, after, it, ep) \
{ \
L2_ASSERT_PROPER_ENDS(first, last, ep) \
if (!after) \
{ \
L2_ASSERT_SOLO(it, ep) \
L2_ASSERT_DISCONNECTION(first, it, ep) \
L2_ASSERT_CONNECTION(first, last, ep) \
L2_PUSH (first, last, it, ep); \
} \
else \
{ \
if (after != it->ep##prev) \
{ \
L2_ASSERT_SOLO(it, ep) \
L2_ASSERT_DISCONNECTION(first, it, ep) \
} \
L2_ASSERT_CONNECTION(first, after, ep) \
L2_ASSERT_CONNECTION(after, last, ep) \
it->ep##next = after->ep##next; \
it->ep##prev = after; \
after->ep##next = it; \
if (it->ep##next) \
it->ep##next->ep##prev = it; \
else \
last = it; \
} \
}
#endif
#define RBE_LEN 128
#define RBE_NEXT(rbe, r) ((r + 1) & (RBE_LEN - 1))
typedef struct rbuf_elt_s
{
struct rbuf_elt_s * rbe_next;
struct rbuf_elt_s * rbe_prev;
short rbe_write;
short rbe_read;
short rbe_count;
void * rbe_data[RBE_LEN];
} rbuf_elt_t;
typedef struct rbuf_s rbuf_t;
typedef void (* rbuf_free_t)(caddr_t);
typedef void (*rbuf_add_cb_t) (rbuf_t * rb, void* data);
struct rbuf_s
{
rbuf_elt_t *rb_first;
rbuf_elt_t * rb_last;
rbuf_elt_t * rb_rewrite_last;
rbuf_add_cb_t rb_add_cb;
short rb_rewrite;
char rb_is_new_data;
int rb_count;
rbuf_free_t rb_free_func;
#ifdef MALLOC_DEBUG
void *rb_origin;
#endif
};
#define DO_RBUF(dtp, item, rbe, inx, rb) \
{ \
dtp item; int inx = 0; rbuf_elt_t * rbe; \
rbuf_elt_t * __next; \
for (rbe = (rb)->rb_first; rbe; rbe = __next) \
{ \
__next = rbe->rbe_next; __builtin_prefetch (__next); \
for (inx = inx < -1 ? -inx - 2 : rbe->rbe_read; inx >= 0 && inx != rbe->rbe_write; inx = (inx >= 0 ? RBE_NEXT (rbe, inx) : inx)) \
{ /* a delete in the loop can set the inx to -1 meaning start at the start of the next rbe or to < -1 meaning start in the next rbe at index -inx + 2. Latter if delete of item causes rbe to be merged into next and some elts of the merged have akready been processed in the loop, so no elt comes twice */ \
__builtin_prefetch (rbe->rbe_data[(inx + 2) & (RBE_LEN - 1)]); \
if (!(item = (dtp)rbe->rbe_data[inx])) \
continue;
#define END_DO_RBUF \
}}}
void rbuf_add (rbuf_t * rb, void* elt);
void * rbuf_get (rbuf_t * rb);
void * rbuf_first (rbuf_t * rb);
void rbuf_delete (rbuf_t * rb, rbuf_elt_t * rbe, int * inx);
void rbuf_destroy (rbuf_t * rb);
rbuf_t * rbuf_allocate ();
void rc_destr_rbuf (void *rbuf);
int rbuf_free_cb (rbuf_t * rb);
void rbuf_append (rbuf_t * dest, rbuf_t * src);
void rbuf_delete_all (rbuf_t *);
void rbuf_rewrite (rbuf_t * rb);
void rbuf_keep (rbuf_t * rb, void * elt);
void rbuf_rewrite_done (rbuf_t * rb);
void rb_ck_cnt (rbuf_t * rb);
#define RBUF_REQ_MTX(rb, mtx)
#endif
|