diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-09-01 13:56:46 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-09-01 13:56:46 +0200 |
commit | 22f703cab05b7cd368f4de9e03991b7664dc5022 (patch) | |
tree | 6f4d50beaa42328e24b1c6b56b6ec059e4ef21a5 /h/xlist.h |
Initial import of argyll version 1.5.1-8debian/1.5.1-8
Diffstat (limited to 'h/xlist.h')
-rw-r--r-- | h/xlist.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/h/xlist.h b/h/xlist.h new file mode 100644 index 0000000..dc03019 --- /dev/null +++ b/h/xlist.h @@ -0,0 +1,56 @@ +/* A set of expandable list utilities, implimented as macros. */ +/* Copyright 2006 Graeme W. Gill */ + +#ifndef _XLIST_H_ + +/* An expanding list structure */ +#define XLIST(objtype, name) \ +struct { \ + int no; /* Number of items in list */ \ + int _no; /* Allocated size of list */ \ + int objsz; /* Size of object */ \ + objtype *list; /* list */ \ +} name; + +/* Initialize the list */ +#define XLIST_INIT(objtype, xlp) \ + ((xlp)->no = (xlp)->_no = 0, \ + (xlp)->objsz = sizeof(objtype), \ + (xlp)->list = NULL \ + ) + +/* test if the list is empty */ +#define IS_XLIST_EMPTY(xlp) \ + ((xlp)->no == 0) + +/* Return the number of items in the list */ +#define XLIST_NO(xlp) \ + ((xlp)->no) + +/* Return the n'th item in the list */ +#define XLIST_ITEM(xlp, n) \ + ((xlp)->list[n]) + +/* Add an item to the end of a list */ +/* We call error() if malloc failes */ +#define XLIST_ADD(xlp, obj) \ + { \ + if ((xlp)->_no <= 0) { \ + (xlp)->_no = 10; \ + if (((xlp)->list = malloc((xlp)->objsz * (xlp)->_no)) == NULL) \ + error("XLIST malloc failed on %d items of size %d", (xlp)->objsz, (xlp)->_no); \ + } else if ((xlp)->_no <= (xlp)->no) { \ + (xlp)->_no *= 2; \ + if (((xlp)->list = realloc((xlp)->list, (xlp)->objsz * (xlp)->_no)) == NULL) \ + error("XLIST realloc failed on %d items of size %d", (xlp)->objsz, (xlp)->_no); \ + } \ + (xlp)->list[(xlp)->no++] = (obj); \ + } + +/* Free up the list */ +#define XLIST_FREE(xlp) \ + { if ((xlp)->_no > 0) free((xlp)->list); } + +#define _XLIST_H_ +#endif /* _XLIST_H_ */ + |