os161-1.99
 All Data Structures
queue.h
00001 #ifndef _QUEUE_H_
00002 #define _QUEUE_H_
00003 
00004 /* 
00005  * Queue of void pointers, implemented as a ring buffer.
00006  *
00007  * Functions:
00008  *       q_create  - allocate a new queue, with initial size SIZE.
00009  *                   Returns NULL on error.
00010  *       q_preallocate - extend the queue if necessary so its size is
00011  *                   at least SIZE. Returns an error code.
00012  *       q_empty   - return true if queue is empty.
00013  *       q_addtail - add a pointer to the tail of the queue. If the queue
00014  *                   overflows, it will extend itself, but this is slow.
00015  *                   Returns an error code if the extension fails. Does not
00016  *                   fail if no extension is required.
00017  *       q_remhead - remove a pointer from the head of the queue. If the
00018  *                   queue is empty, panics.
00019  *       q_destroy - dispose of the queue.
00020  *       q_peek    - return pointer to front element, if empty returns null
00021  *       q_len     - returns the number of elements in the queue
00022  *                   (q_getsize is the maximum number of elements that
00023  *                    can be in the queue)
00024  */
00025 
00026 struct queue; /* Opaque. */
00027 
00028 struct queue *q_create(int size);
00029 int           q_preallocate(struct queue *, int size);
00030 int           q_empty(struct queue *);
00031 int           q_addtail(struct queue *, void *ptr);
00032 void         *q_remhead(struct queue *);
00033 void          q_destroy(struct queue *);
00034 void         *q_peek(struct queue *q);
00035 int           q_len(struct queue *theq);
00036 
00037 /* 
00038  * These are really intended only for debugging. Using them encodes 
00039  * knowledge of how the queue works, which is usually undesirable.
00040  *
00041  *      q_getstart - return the index of the front of the queue
00042  *      q_getend   - return the index of the back of the queue
00043  *      q_getsize  - return the current size of the queue
00044  *      q_getguy   - return queue member by index
00045  *
00046  * To iterate over the queue, do something like
00047  *      struct queue *q;
00048  *      int i;
00049  *      
00050  *      for (i=q_getstart(q); i!=q_getend(q); i=(i+1)%q_getsize(q)) {
00051  *              void *ptr = q_getguy(q, i);
00052  *                :
00053  *      }
00054  *
00055  * If you do this, synchronization is your problem.
00056  */
00057 int   q_getstart(struct queue *);
00058 int   q_getend(struct queue *);
00059 int   q_getsize(struct queue *);
00060 void *q_getguy(struct queue *, int index);
00061 
00062 #endif /* _QUEUE_H_ */
 All Data Structures