diff options
Diffstat (limited to 'util')
| -rw-r--r-- | util/arena.h | 10 | ||||
| -rw-r--r-- | util/list.h | 79 |
2 files changed, 88 insertions, 1 deletions
diff --git a/util/arena.h b/util/arena.h index 3d82b95..1142321 100644 --- a/util/arena.h +++ b/util/arena.h @@ -9,18 +9,26 @@ struct arena_ctx { size_t offset; }; + #define ARENA_CTX_INIT(buffer, sz) (struct arena_ctx){(buffer), (sz), 0} void *arena_allocate(struct arena_ctx *ctx, size_t sz); void arena_reset(struct arena_ctx *ctx); #ifdef ARENA_IMPLEMENTATION +#define ARENA_ALLIGNMENT 2 +// #include <assert.h> void *arena_allocate(struct arena_ctx *ctx, size_t sz) { + if(sz % ARENA_ALLIGNMENT != 0) + sz += ARENA_ALLIGNMENT - (sz % ARENA_ALLIGNMENT); + if(ctx->offset + sz > ctx->size) return NULL; - void *off = ctx->buffer + ctx->offset; + void *off = (void *)((intptr_t)ctx->buffer + ctx->offset); ctx->offset += sz; + + // assert(((intptr_t)off & 0x1) == 0); return off; } diff --git a/util/list.h b/util/list.h new file mode 100644 index 0000000..d9531ea --- /dev/null +++ b/util/list.h @@ -0,0 +1,79 @@ +#ifndef LIST_H +#define LIST_H + +// #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) + +struct list_head { + struct list_head *prev; + struct list_head *next; +}; + +#define LIST_END NULL + +#define LIST_EMPTY(list) do { \ + (list)->next = LIST_END; \ + (list)->prev = LIST_END; \ + } while(0); + +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +#define list_next_entry(entry, type, member) \ + list_entry(entry->member.next, type, member) + +#define list_for_each(pos, start) \ + for(struct list_head *pos = start; pos; pos = pos->next) +#define list_for_each_entry(type, entry, member, start) \ + for(type *entry = list_entry((start), type, member); \ + entry; \ + entry = (entry->member.next == LIST_END ? NULL : \ + list_next_entry(entry, type, member))) + +#define list_for_each_safe(pos, start) \ + for(struct list_head *pos = (start), *__next = LIST_END; \ + pos && (__next = pos->next,1); \ + pos = __next) + +static inline int list_is_head(struct list_head *l) +{ + return l->prev == LIST_END; +} + +static inline int list_is_tail(struct list_head *l) +{ + return l->next == LIST_END; +} + +static inline struct list_head *list_get_head(struct list_head *l) +{ + while(!list_is_head(l)) l = l->prev; + return l; +} + +static inline struct list_head *list_get_tail(struct list_head *l) +{ + while(!list_is_tail(l)) l = l->next; + return l; +} + +static inline struct list_head *list_add( + struct list_head *head, + struct list_head *new) +{ + if(head) { + // new->next = head->next; + head->next = new; + } + new->prev = head; + return new; +} + +static inline size_t list_len(struct list_head *head) +{ + size_t n = 0; + list_for_each(pos, list_get_head(head)) n++; + return n; +} + +#endif |
