From 58ef912c6babf1866193ab04674a5866dd761f13 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 22 Apr 1998 12:58:34 +0000 Subject: First look at data structures. More to come tomorrow... --- lib/Makefile | 3 +++ lib/birdlib.h | 17 +++++++++++++ lib/lists.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/lists.h | 42 +++++++++++++++++++++++++++++++ lib/resource.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/socket.h | 16 ++++++++++++ lib/timer.h | 26 ++++++++++++++++++++ lib/unaligned.h | 12 +++++++++ 8 files changed, 262 insertions(+) create mode 100644 lib/Makefile create mode 100644 lib/birdlib.h create mode 100644 lib/lists.c create mode 100644 lib/lists.h create mode 100644 lib/resource.h create mode 100644 lib/socket.h create mode 100644 lib/timer.h create mode 100644 lib/unaligned.h (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..214f76a --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,3 @@ +OBJS=lists.o + +include $(TOPDIR)/Rules diff --git a/lib/birdlib.h b/lib/birdlib.h new file mode 100644 index 0000000..fb4814f --- /dev/null +++ b/lib/birdlib.h @@ -0,0 +1,17 @@ +/* + * BIRD Library + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_BIRDLIB_H_ +#define _BIRD_BIRDLIB_H_ + +/* Ugly structure offset handling macros */ + +#define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i) +#define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i))) + +#endif diff --git a/lib/lists.c b/lib/lists.c new file mode 100644 index 0000000..321a5f0 --- /dev/null +++ b/lib/lists.c @@ -0,0 +1,76 @@ +/* + * BIRD Library -- Linked Lists + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#define _BIRD_LISTS_C_ + +#include +#include + +LIST_INLINE void +add_tail(list *l, node *n) +{ + node *z = l->tail; + + n->next = (node *) &l->tail; + n->prev = z; + z->next = n; + l->tail = n; +} + +LIST_INLINE void +add_head(list *l, node *n) +{ + node *z = l->head; + + n->next = z; + n->prev = (node *) &l->head; + z->prev = n; + l->head = n; +} + +LIST_INLINE void +insert_node(node *n, node *after) +{ + node *z = after->next; + + n->next = z; + n->prev = after; + after->next = n; + z->prev = n; +} + +LIST_INLINE void +rem_node(node *n) +{ + node *z = n->prev; + node *x = n->next; + + z->next = x; + x->prev = z; +} + +LIST_INLINE void +init_list(list *l) +{ + l->head = (node *) &l->null; + l->null = NULL; + l->tail = (node *) &l->head; +} + +LIST_INLINE void +add_tail_list(list *to, list *l) +{ + node *p = to->tail; + node *q = l->head; + + p->next = q; + q->prev = p; + q = l->tail; + q->next = (node *) &to->null; + to->tail = q; +} diff --git a/lib/lists.h b/lib/lists.h new file mode 100644 index 0000000..acc054d --- /dev/null +++ b/lib/lists.h @@ -0,0 +1,42 @@ +/* + * BIRD Library -- Linked Lists + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_LISTS_H_ +#define _BIRD_LISTS_H_ + +typedef struct node { + struct node *next, *prev; +} node; + +typedef struct list { /* In fact two overlayed nodes */ + struct node *head, *null, *tail; +} list; + +#define NODE (node *) +#define HEAD(list) ((void *)((list).head)) +#define TAIL(list) ((void *)((list).tail)) +#define WALK_LIST(n,list) for((n)=HEAD(list);(NODE (n))->next; \ + n=(void *)((NODE (n))->next)) +#define EMPTY_LIST(list) (!(list).head->next) + +void add_tail(list *, node *); +void add_head(list *, node *); +void rem_node(node *); +void add_tail_list(list *, list *); +void init_list(list *); +void insert_node(node *, node *); + +#ifndef _BIRD_LISTS_C_ +#define LIST_INLINE extern inline +#include +#undef LIST_INLINE +#else +#define LIST_INLINE +#endif + +#endif diff --git a/lib/resource.h b/lib/resource.h new file mode 100644 index 0000000..a1d229e --- /dev/null +++ b/lib/resource.h @@ -0,0 +1,70 @@ +/* + * BIRD Resource Manager + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_RESOURCE_H_ +#define _BIRD_RESOURCE_H_ + +#include + +/* Resource */ + +typedef struct resource { + node n; /* Inside resource pool */ + struct resclass *class; /* Resource class */ +} resource; + +/* Resource class */ + +struct resclass { + char *name; /* Resource class name */ + unsigned size; /* Standard size of single resource */ + void (*free)(resource *); /* Freeing function */ + void (*dump)(resource *); /* Dump to debug output */ +}; + +/* Generic resource manipulation */ + +typedef struct pool pool; + +pool *rp_new(pool *); /* Create new pool */ +void rp_init(pool *); /* Initialize static pool */ +void rp_empty(pool *); /* Free everything in the pool */ +void rfree(void *); /* Free single resource */ +void rdump(void *); /* Dump to debug output */ + +void ralloc(pool *, struct resclass *); + +/* Normal memory blocks */ + +void *mb_alloc(pool *, unsigned size); +void *mb_free(void *); + +/* Memory pools with linear allocation */ + +typedef struct mempool mempool; + +mempool *mp_new(pool *, unsigned blk); +void mp_trim(pool *); /* Free unused memory */ +void *mp_alloc(mempool *, unsigned size); /* Aligned */ +void *mp_allocu(mempool *, unsigned size); /* Unaligned */ +void *mp_allocz(mempool *, unsigned size); /* With clear */ + +/* Slabs */ + +typedef struct slab slab; + +slab *sl_new(pool *, unsigned size); +void *sl_alloc(slab *); +void sl_free(slab *, void *); + +/* Low-level memory allocation functions, please don't use */ + +void *xmalloc(unsigned); +#define xfree(x) free(x) + +#endif diff --git a/lib/socket.h b/lib/socket.h new file mode 100644 index 0000000..4f68a16 --- /dev/null +++ b/lib/socket.h @@ -0,0 +1,16 @@ +/* + * BIRD Socket Interface + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_SOCKET_H_ +#define _BIRD_SOCKET_H_ + +#include + +typedef struct birdsock socket; + +#endif diff --git a/lib/timer.h b/lib/timer.h new file mode 100644 index 0000000..29f20c4 --- /dev/null +++ b/lib/timer.h @@ -0,0 +1,26 @@ +/* + * BIRD Timers + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_TIMER_H_ +#define _BIRD_TIMER_H_ + +#include + +typedef struct timer { + resource r; + void (*hook)(struct timer *); + void *data; + /* internal fields should be here */ +} timer; + +timer *tm_new(pool *, void (*hook)(timer *), void *data); +void tm_start(timer *, unsigned after); +void tm_stop(timer *); +void tm_trigger(timer *); + +#endif diff --git a/lib/unaligned.h b/lib/unaligned.h new file mode 100644 index 0000000..29fcee9 --- /dev/null +++ b/lib/unaligned.h @@ -0,0 +1,12 @@ +/* + * Unaligned Data Accesses + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_UNALIGNED_H_ +#define _BIRD_UNALIGNED_H_ + +#endif -- cgit v1.2.3