Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / HeapRing.h
1 #ifndef _HEAPRING_H
2 #define _HEAPRING_H
3
4 template<class kind> class HeapRing {
5
6 // smoothie-specific friend classes
7 friend class Planner;
8 friend class Conveyor;
9 friend class Block;
10
11 public:
12 HeapRing();
13 HeapRing(unsigned int length);
14
15 ~HeapRing();
16
17 /*
18 * direct accessors
19 */
20 kind& head();
21 kind& tail();
22
23 void push_front(kind&) __attribute__ ((warning("Not thread-safe if pop_back() is used in ISR context!"))); // instead, prepare(head_ref()); produce_head();
24 kind& pop_back(void) __attribute__ ((warning("Not thread-safe if head_ref() is used to prepare new items, or push_front() is used in ISR context!"))); // instead, consume(tail_ref()); consume_tail();
25
26 /*
27 * pointer accessors
28 */
29 kind* head_ref();
30 kind* tail_ref();
31
32 void produce_head(void);
33 void consume_tail(void);
34
35 /*
36 * queue status
37 */
38 bool is_empty(void) const;
39 bool is_full(void) const;
40
41 /*
42 * resize
43 *
44 * returns true on success, or false if queue is not empty or not enough memory available
45 */
46 bool resize(unsigned int);
47
48 /*
49 * provide
50 * kind* - new buffer pointer
51 * int length - number of items in buffer (NOT size in bytes!)
52 *
53 * cause HeapRing to use a specific memory location instead of allocating its own
54 *
55 * returns true on success, or false if queue is not empty
56 */
57 bool provide(kind*, unsigned int length);
58
59 protected:
60 /*
61 * these functions are protected as they should only be used internally
62 * or in extremely specific circumstances
63 */
64 kind& item(unsigned int);
65 kind* item_ref(unsigned int);
66
67 unsigned int next(unsigned int) const;
68 unsigned int prev(unsigned int) const;
69
70 /*
71 * buffer variables
72 */
73 unsigned int length;
74
75 volatile unsigned int head_i;
76 volatile unsigned int tail_i;
77 volatile unsigned int isr_tail_i;
78
79 private:
80 kind* ring;
81 };
82
83 #endif /* _HEAPRING_H */