Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
klist.h
Go to the documentation of this file.
1 /* The MIT License
2 
3  Copyright (c) 2008-2009, by Attractive Chaos <attractor@live.co.uk>
4 
5  Permission is hereby granted, free of charge, to any person obtaining
6  a copy of this software and associated documentation files (the
7  "Software"), to deal in the Software without restriction, including
8  without limitation the rights to use, copy, modify, merge, publish,
9  distribute, sublicense, and/or sell copies of the Software, and to
10  permit persons to whom the Software is furnished to do so, subject to
11  the following conditions:
12 
13  The above copyright notice and this permission notice shall be
14  included in all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  SOFTWARE.
24 */
25 
26 #ifndef _AC_KLIST_H
27 #define _AC_KLIST_H
28 
29 #include <stdlib.h>
30 
31 #define KMEMPOOL_INIT(name, kmptype_t, kmpfree_f) \
32  typedef struct { \
33  size_t cnt, n, max; \
34  kmptype_t **buf; \
35  } kmp_##name##_t; \
36  static inline kmp_##name##_t *kmp_init_##name(void) { \
37  return calloc(1, sizeof(kmp_##name##_t)); \
38  } \
39  static inline void kmp_destroy_##name(kmp_##name##_t *mp) { \
40  size_t k; \
41  for (k = 0; k < mp->n; ++k) { \
42  kmpfree_f(mp->buf[k]); free(mp->buf[k]); \
43  } \
44  free(mp->buf); free(mp); \
45  } \
46  static inline kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) { \
47  ++mp->cnt; \
48  if (mp->n == 0) return calloc(1, sizeof(kmptype_t)); \
49  return mp->buf[--mp->n]; \
50  } \
51  static inline void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \
52  --mp->cnt; \
53  if (mp->n == mp->max) { \
54  mp->max = mp->max? mp->max<<1 : 16; \
55  mp->buf = realloc(mp->buf, sizeof(kmptype_t *) * mp->max); \
56  } \
57  mp->buf[mp->n++] = p; \
58  }
59 
60 #define kmempool_t(name) kmp_##name##_t
61 #define kmp_init(name) kmp_init_##name()
62 #define kmp_destroy(name, mp) kmp_destroy_##name(mp)
63 #define kmp_alloc(name, mp) kmp_alloc_##name(mp)
64 #define kmp_free(name, mp, p) kmp_free_##name(mp, p)
65 
66 #define KLIST_INIT(name, kltype_t, kmpfree_t) \
67  struct __kl1_##name { \
68  kltype_t data; \
69  struct __kl1_##name *next; \
70  }; \
71  typedef struct __kl1_##name kl1_##name; \
72  KMEMPOOL_INIT(name, kl1_##name, kmpfree_t) \
73  typedef struct { \
74  kl1_##name *head, *tail; \
75  kmp_##name##_t *mp; \
76  size_t size; \
77  } kl_##name##_t; \
78  static inline kl_##name##_t *kl_init_##name(void) { \
79  kl_##name##_t *kl = calloc(1, sizeof(kl_##name##_t)); \
80  kl->mp = kmp_init(name); \
81  kl->head = kl->tail = kmp_alloc(name, kl->mp); \
82  kl->head->next = 0; \
83  return kl; \
84  } \
85  static inline void kl_destroy_##name(kl_##name##_t *kl) { \
86  kl1_##name *p; \
87  for (p = kl->head; p != kl->tail; p = p->next) \
88  kmp_free(name, kl->mp, p); \
89  kmp_free(name, kl->mp, p); \
90  kmp_destroy(name, kl->mp); \
91  free(kl); \
92  } \
93  static inline kltype_t *kl_pushp_##name(kl_##name##_t *kl) { \
94  kl1_##name *q, *p = kmp_alloc(name, kl->mp); \
95  q = kl->tail; p->next = 0; kl->tail->next = p; kl->tail = p; \
96  ++kl->size; \
97  return &q->data; \
98  } \
99  static inline int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \
100  kl1_##name *p; \
101  if (kl->head->next == 0) return -1; \
102  --kl->size; \
103  p = kl->head; kl->head = kl->head->next; \
104  if (d) *d = p->data; \
105  kmp_free(name, kl->mp, p); \
106  return 0; \
107  }
108 
109 #define kliter_t(name) kl1_##name
110 #define klist_t(name) kl_##name##_t
111 #define kl_val(iter) ((iter)->data)
112 #define kl_next(iter) ((iter)->next)
113 #define kl_begin(kl) ((kl)->head)
114 #define kl_end(kl) ((kl)->tail)
115 
116 #define kl_init(name) kl_init_##name()
117 #define kl_destroy(name, kl) kl_destroy_##name(kl)
118 #define kl_pushp(name, kl) kl_pushp_##name(kl)
119 #define kl_shift(name, kl, d) kl_shift_##name(kl, d)
120 
121 #endif