Commit 444e0b6c authored by Maria Matejka's avatar Maria Matejka
Browse files

Merge branch 'mq-typed-list' into mq-parallel-import

parents 9f23625c ceeb22ec
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
#ifndef _BIRD_BIRDLIB_H_
#define _BIRD_BIRDLIB_H_

#include "sysdep/config.h"
#include "lib/alloca.h"

/* Ugly structure offset handling macros */
+0 −1
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@

#define _BIRD_LISTS_C_

#include "nest/bird.h"
#include "lib/lists.h"

/**
+70 −1
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@
#ifndef _BIRD_LISTS_H_
#define _BIRD_LISTS_H_

#include "lib/birdlib.h"

/*
 * I admit the list structure is very tricky and also somewhat awkward,
 * but it's both efficient and easy to manipulate once one understands the
@@ -42,7 +44,6 @@ typedef union list { /* In fact two overlayed nodes */
  };
} list;


#define NODE (node *)
#define HEAD(list) ((void *)((list).head))
#define TAIL(list) ((void *)((list).tail))
@@ -85,4 +86,72 @@ void insert_node(node *, node *);
uint list_length(list *);
#endif

/* Typed lists */
#define TLIST_NODE(_type) struct { _type *next, *prev; } _tln
#define TLIST(_type) union { \
  struct { \
    union { \
      struct { _type *next, *prev; }; \
      _type node[0]; \
    } head_node; \
    void *head_padding; \
  }; \
  struct { \
    void *tail_padding; \
    union { \
      struct { _type *next, *prev; }; \
      _type node[0]; \
    } tail_node; \
  }; \
  struct { \
    _type *head, *null, *tail; \
  }; \
}

#define TNODE(n) (n)->node
#define THEAD(list) list.head
#define TTAIL(list) list.tail
#define TNODE_IN_LIST(n) (((n)->_tln.next) && ((n)->_tln.prev))

#define WALK_TLIST(n_, list) for (n_ = (list).head; n_->_tln.next; n_ = n_->_tln.next)
#define WALK_TLIST_DELSAFE(n_, list) for (typeof(n_) next_ = n_ = THEAD(list); next_ = n_->_tln.next; n_ = next_)

#define INIT_TLIST(list) do { \
  typeof(list) l_ = list; \
  l_->head = l_->tail_node.node; \
  l_->tail = l_->head_node.node; \
  l_->null = NULL; \
} while (0)

#define TADD_HEAD(list_, node_) do { \
  typeof(node_) n_ = node_; \
  typeof(list_) l_ = list_; \
  n_->_tln.next = l_->head; \
  n_->_tln.prev = l_->head_node.node; \
  l_->head->_tln.prev = n_; \
  l_->head = n_; \
} while (0)

#define TADD_TAIL(list_, node_) do { \
  typeof(node_) n_ = node_; \
  typeof(list_) l_ = list_; \
  n_->_tln.next = l_->tail_node.node; \
  n_->_tln.prev = l_->tail; \
  l_->tail->_tln.next = n_; \
  l_->tail = n_; \
} while (0)

#define TREM_NODE(node) do { \
  typeof(node) n_ = node; \
  n_->_tln.prev->_tln.next = n_->_tln.next; \
  n_->_tln.next->_tln.prev = n_->_tln.prev; \
  n_->_tln.prev = n_->_tln.next = NULL; \
} while (0)

#define TFIX_NODE(node) do { \
  typeof(node) n_ = node; \
  n_->_tln.next->_tln.prev = n_; \
  n_->_tln.prev->_tln.next = n_; \
 } while (0)

#endif
+14 −16
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
#include <stdlib.h>
#include <stdint.h>

#include "nest/bird.h"
#include "lib/birdlib.h"
#include "lib/resource.h"
#include "lib/string.h"

@@ -30,7 +30,7 @@

struct pool {
  resource r;
  list inside;
  TLIST(resource) inside;
  const char *name;
};

@@ -65,7 +65,7 @@ rp_new(pool *p, const char *name)
{
  pool *z = ralloc(p, &pool_class);
  z->name = name;
  init_list(&z->inside);
  INIT_TLIST(&z->inside);
  return z;
}

@@ -73,14 +73,12 @@ static void
pool_free(resource *P)
{
  pool *p = (pool *) P;
  resource *r, *rr;
  resource *r;

  r = HEAD(p->inside);
  while (rr = (resource *) r->n.next)
  WALK_TLIST_DELSAFE(r, p->inside)
    {
      r->class->free(r);
      xfree(r);
      r = rr;
    }
}

@@ -136,9 +134,9 @@ void rmove(void *res, pool *p)

  if (r)
    {
      if (r->n.next)
        rem_node(&r->n);
      add_tail(&p->inside, &r->n);
      if (TNODE_IN_LIST(r))
        TREM_NODE(r);
      TADD_TAIL(&p->inside, r);
    }
}

@@ -160,8 +158,8 @@ rfree(void *res)
  if (!r)
    return;

  if (r->n.next)
    rem_node(&r->n);
  if (TNODE_IN_LIST(r))
    TREM_NODE(r);
  r->class->free(r);
  r->class = NULL;
  xfree(r);
@@ -222,7 +220,7 @@ ralloc(pool *p, struct resclass *c)

  r->class = c;
  if (p)
    add_tail(&p->inside, &r->n);
    TADD_TAIL(&p->inside, r);
  return r;
}

@@ -261,7 +259,7 @@ resource_init(void)
{
  root_pool.r.class = &pool_class;
  root_pool.name = "Root";
  init_list(&root_pool.inside);
  INIT_TLIST(&root_pool.inside);
}

/**
@@ -340,7 +338,7 @@ mb_alloc(pool *p, unsigned size)
  struct mblock *b = xmalloc(sizeof(struct mblock) + size);

  b->r.class = &mb_class;
  add_tail(&p->inside, &b->r.n);
  TADD_TAIL(&p->inside, &b->r);
  b->size = size;
  return b->data;
}
@@ -387,7 +385,7 @@ mb_realloc(void *m, unsigned size)
  struct mblock *b = SKIP_BACK(struct mblock, data, m);

  b = xrealloc(b, sizeof(struct mblock) + size);
  replace_node(&b->r.n, &b->r.n);
  TFIX_NODE(&b->r);
  b->size = size;
  return b->data;
}
+2 −2
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
/* Resource */

typedef struct resource {
  node n;				/* Inside resource pool */
  TLIST_NODE(struct resource);	/* Inside resource pool */
  struct resclass *class;	/* Resource class */
} resource;

Loading