queue.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (c) 2009 Joseph A. Adams
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The name of the author may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CCAN_QUEUE_H
  26. #define CCAN_QUEUE_H
  27. #include <stdint.h>
  28. #include <ccan/talloc/talloc.h>
  29. #ifndef HAVE_ATTRIBUTE_MAY_ALIAS
  30. #define HAVE_ATTRIBUTE_MAY_ALIAS 1
  31. #endif
  32. #if HAVE_ATTRIBUTE_MAY_ALIAS==1
  33. #define queue_alias(ptr) /* nothing */
  34. #define queue(type) struct {size_t head, tail, flag; type *item;} __attribute__((__may_alias__))
  35. #else
  36. #define queue_alias(ptr) qsort(ptr, 0, 1, queue_alias_helper) //hack
  37. #define queue(type) struct {size_t head, tail, flag; type *item;}
  38. #endif
  39. int queue_alias_helper(const void *a, const void *b);
  40. #define queue_init(queue, ctx) do {(queue).head = (queue).tail = 0; (queue).flag = 3; (queue).item = talloc_size(ctx, sizeof(*(queue).item)*4);} while(0)
  41. #define queue_free(queue) do {talloc_free((queue).item);} while(0)
  42. #define queue_count(queue) (((queue).tail-(queue).head) & (queue).flag)
  43. #define enqueue(queue, ...) \
  44. do { \
  45. (queue).item[(queue).tail++] = (__VA_ARGS__); \
  46. (queue).tail &= (queue).flag; \
  47. if ((queue).tail == (queue).head) { \
  48. queue_enqueue_helper(&(queue), sizeof(*(queue).item)); \
  49. queue_alias(&(queue)); \
  50. } \
  51. } while(0)
  52. #define dequeue_check(queue) ((queue).head != (queue).tail ? dequeue(queue) : NULL)
  53. #define dequeue(queue) ((queue).item[queue_dequeue_helper(&(queue).head, (queue).flag)])
  54. //TODO: Test us
  55. #define queue_next(queue) ((queue).item[(queue).head])
  56. #define queue_item(queue, pos) ((queue).item[((queue).head+(pos)) & (queue).flag])
  57. #define queue_skip(queue) do {(queue).head++; (queue).head &= (queue).flag;} while(0)
  58. void queue_enqueue_helper(void *qp, size_t itemSize);
  59. static inline size_t queue_dequeue_helper(size_t *head, size_t flag) {
  60. size_t ret = (*head)++;
  61. *head &= flag;
  62. return ret;
  63. }
  64. #endif