queue.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef _QUEUE_H_
  2. #define _QUEUE_H_
  3. /*
  4. * Queue of void pointers, implemented as a ring buffer.
  5. *
  6. * Functions:
  7. * q_create - allocate a new queue, with initial size SIZE.
  8. * Returns NULL on error.
  9. * q_preallocate - extend the queue if necessary so its size is
  10. * at least SIZE. Returns an error code.
  11. * q_empty - return true if queue is empty.
  12. * q_addtail - add a pointer to the tail of the queue. If the queue
  13. * overflows, it will extend itself, but this is slow.
  14. * Returns an error code if the extension fails. Does not
  15. * fail if no extension is required.
  16. * q_remhead - remove a pointer from the head of the queue. If the
  17. * queue is empty, panics.
  18. * q_destroy - dispose of the queue.
  19. * q_peek - return pointer to front element, if empty returns null
  20. * q_len - returns the number of elements in the queue
  21. * (q_getsize is the maximum number of elements that
  22. * can be in the queue)
  23. */
  24. struct queue; /* Opaque. */
  25. struct queue *q_create(int size);
  26. int q_preallocate(struct queue *, int size);
  27. int q_empty(struct queue *);
  28. int q_addtail(struct queue *, void *ptr);
  29. void *q_remhead(struct queue *);
  30. void q_destroy(struct queue *);
  31. void *q_peek(struct queue *q);
  32. int q_len(struct queue *theq);
  33. /*
  34. * These are really intended only for debugging. Using them encodes
  35. * knowledge of how the queue works, which is usually undesirable.
  36. *
  37. * q_getstart - return the index of the front of the queue
  38. * q_getend - return the index of the back of the queue
  39. * q_getsize - return the current size of the queue
  40. * q_getguy - return queue member by index
  41. *
  42. * To iterate over the queue, do something like
  43. * struct queue *q;
  44. * int i;
  45. *
  46. * for (i=q_getstart(q); i!=q_getend(q); i=(i+1)%q_getsize(q)) {
  47. * void *ptr = q_getguy(q, i);
  48. * :
  49. * }
  50. *
  51. * If you do this, synchronization is your problem.
  52. */
  53. int q_getstart(struct queue *);
  54. int q_getend(struct queue *);
  55. int q_getsize(struct queue *);
  56. void *q_getguy(struct queue *, int index);
  57. #endif /* _QUEUE_H_ */