ring buffer
rbuf.c@5:ddc33cb62d27, 2011-07-12 (annotated)
- Committer:
- est2fe
- Date:
- Tue Jul 12 20:13:35 2011 +0000
- Revision:
- 5:ddc33cb62d27
- Parent:
- 4:192672304400
- Child:
- 7:5e960f7b1f79
V 0.97
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
est2fe | 4:192672304400 | 1 | #ifndef __rbuf_c_ |
est2fe | 4:192672304400 | 2 | #define __rbuf_c_ |
est2fe | 0:eae8cffdd121 | 3 | |
est2fe | 0:eae8cffdd121 | 4 | #include "mbed.h" |
est2fe | 0:eae8cffdd121 | 5 | #include "rbuf.h" |
est2fe | 0:eae8cffdd121 | 6 | |
est2fe | 4:192672304400 | 7 | void init_header (rbuf_t *h, void *buf, uint16_t objektanzahl, uint16_t Schwelle, uint16_t objektgroesse) |
est2fe | 0:eae8cffdd121 | 8 | { |
est2fe | 0:eae8cffdd121 | 9 | h->buflen = objektanzahl; |
est2fe | 0:eae8cffdd121 | 10 | h->bufcnt = 0; |
est2fe | 0:eae8cffdd121 | 11 | h->in = buf; |
est2fe | 0:eae8cffdd121 | 12 | h->out = buf; |
est2fe | 0:eae8cffdd121 | 13 | h->buf = buf; |
est2fe | 0:eae8cffdd121 | 14 | h->anzahl = 0; |
est2fe | 0:eae8cffdd121 | 15 | h->fehleranzahl = 0; |
est2fe | 0:eae8cffdd121 | 16 | h->Schwelle = Schwelle; |
est2fe | 0:eae8cffdd121 | 17 | h->flags = 0; |
est2fe | 0:eae8cffdd121 | 18 | h->peak_max = 0; |
est2fe | 0:eae8cffdd121 | 19 | h->blockgroesse = objektgroesse; |
est2fe | 0:eae8cffdd121 | 20 | } |
est2fe | 0:eae8cffdd121 | 21 | |
est2fe | 4:192672304400 | 22 | rbuf_t *rbuf_init (uint16_t b_size, uint16_t b_schwelle, uint16_t blockgroesse) |
est2fe | 0:eae8cffdd121 | 23 | { |
est2fe | 4:192672304400 | 24 | void *buffer_p; |
est2fe | 4:192672304400 | 25 | rbuf_t *kopf_p; |
est2fe | 0:eae8cffdd121 | 26 | |
est2fe | 0:eae8cffdd121 | 27 | // Zuerst mal Speicher fuer die Verwaltungsstruktur holen |
est2fe | 4:192672304400 | 28 | kopf_p = (rbuf_t *) calloc (1, (sizeof(rbuf_t))); |
est2fe | 0:eae8cffdd121 | 29 | if (kopf_p > 0x00) |
est2fe | 0:eae8cffdd121 | 30 | { |
est2fe | 4:192672304400 | 31 | memset (kopf_p, 0x00, (sizeof(rbuf_t))); |
est2fe | 0:eae8cffdd121 | 32 | } |
est2fe | 0:eae8cffdd121 | 33 | else |
est2fe | 0:eae8cffdd121 | 34 | { |
est2fe | 0:eae8cffdd121 | 35 | return 0x00; |
est2fe | 0:eae8cffdd121 | 36 | } |
est2fe | 0:eae8cffdd121 | 37 | |
est2fe | 0:eae8cffdd121 | 38 | // Jetzt noch die eigentlichen Receive- und send-char-Speicher |
est2fe | 0:eae8cffdd121 | 39 | size_t blen = b_size * blockgroesse; |
est2fe | 0:eae8cffdd121 | 40 | buffer_p = calloc (1, blen); |
est2fe | 0:eae8cffdd121 | 41 | if (buffer_p > 0x00) |
est2fe | 0:eae8cffdd121 | 42 | { |
est2fe | 0:eae8cffdd121 | 43 | memset (buffer_p, 0x00, blen); |
est2fe | 0:eae8cffdd121 | 44 | } |
est2fe | 0:eae8cffdd121 | 45 | else |
est2fe | 0:eae8cffdd121 | 46 | { |
est2fe | 4:192672304400 | 47 | // gibt es noch nichts! -> evtl. Fehlemeldung, dass nicht mehr genuegend Speicher vorhanden ist. |
est2fe | 0:eae8cffdd121 | 48 | return 0x00; |
est2fe | 0:eae8cffdd121 | 49 | } |
est2fe | 0:eae8cffdd121 | 50 | |
est2fe | 5:ddc33cb62d27 | 51 | // Und die Buffer-Verwaltung noch initialisieren |
est2fe | 0:eae8cffdd121 | 52 | init_header (kopf_p, buffer_p, b_size, b_schwelle, blockgroesse); |
est2fe | 0:eae8cffdd121 | 53 | return kopf_p; |
est2fe | 0:eae8cffdd121 | 54 | } |
est2fe | 0:eae8cffdd121 | 55 | |
est2fe | 4:192672304400 | 56 | void *rbuf_look (rbuf_t * r) // Nur schauen, ob im Buffer neue Ojekte drin sind. |
est2fe | 0:eae8cffdd121 | 57 | { |
est2fe | 4:192672304400 | 58 | // return r->bufcnt; |
est2fe | 0:eae8cffdd121 | 59 | if (r->bufcnt == 0) |
est2fe | 0:eae8cffdd121 | 60 | { |
est2fe | 0:eae8cffdd121 | 61 | return 0; |
est2fe | 0:eae8cffdd121 | 62 | } |
est2fe | 0:eae8cffdd121 | 63 | else |
est2fe | 0:eae8cffdd121 | 64 | { |
est2fe | 0:eae8cffdd121 | 65 | return r->out; // ?!? |
est2fe | 0:eae8cffdd121 | 66 | } |
est2fe | 0:eae8cffdd121 | 67 | } |
est2fe | 0:eae8cffdd121 | 68 | |
est2fe | 4:192672304400 | 69 | void *rbuf_get (rbuf_t *r, void *target) // Einen char/Block rausholen mit Readzeiger++ |
est2fe | 0:eae8cffdd121 | 70 | { |
est2fe | 0:eae8cffdd121 | 71 | void *p_b; // Pointer block |
est2fe | 0:eae8cffdd121 | 72 | if (r->bufcnt == 0) |
est2fe | 0:eae8cffdd121 | 73 | { |
est2fe | 0:eae8cffdd121 | 74 | r->flags = r->flags & ((uint8_t) (~NOT_EMPTY)); |
est2fe | 0:eae8cffdd121 | 75 | return 0; |
est2fe | 0:eae8cffdd121 | 76 | } |
est2fe | 0:eae8cffdd121 | 77 | p_b = r->out; |
est2fe | 0:eae8cffdd121 | 78 | r->out = (void *) (((char *) r->out) + r->blockgroesse ); |
est2fe | 4:192672304400 | 79 | r->bufcnt--; // Ein Objekt weniger im Buffer |
est2fe | 0:eae8cffdd121 | 80 | if (r->bufcnt >= r->Schwelle) |
est2fe | 0:eae8cffdd121 | 81 | { |
est2fe | 0:eae8cffdd121 | 82 | r->flags = r->flags | ((uint8_t) (FAST_VOLL)); |
est2fe | 0:eae8cffdd121 | 83 | } |
est2fe | 0:eae8cffdd121 | 84 | else |
est2fe | 0:eae8cffdd121 | 85 | { |
est2fe | 0:eae8cffdd121 | 86 | r->flags = r->flags & ((uint8_t) (~FAST_VOLL)); |
est2fe | 0:eae8cffdd121 | 87 | } |
est2fe | 0:eae8cffdd121 | 88 | if (r->bufcnt == 0) |
est2fe | 0:eae8cffdd121 | 89 | { |
est2fe | 0:eae8cffdd121 | 90 | r->flags = r->flags & ((uint8_t) (~NOT_EMPTY)); |
est2fe | 0:eae8cffdd121 | 91 | } |
est2fe | 0:eae8cffdd121 | 92 | memcpy (target, p_b, r->blockgroesse); |
est2fe | 0:eae8cffdd121 | 93 | |
est2fe | 0:eae8cffdd121 | 94 | // (1 - 8) |
est2fe | 0:eae8cffdd121 | 95 | // if (r->out >= r->buf + (r->buflen)) // ?? Hier ist die Zeigerarithmetik noch unklar! |
est2fe | 0:eae8cffdd121 | 96 | if (r->out >= (void *)(((char *)(r->buf)) + (r->buflen * r->blockgroesse))) // ?? Hier ist die Zeigerarithmetik noch unklar! |
est2fe | 0:eae8cffdd121 | 97 | { |
est2fe | 0:eae8cffdd121 | 98 | // wrap around to beginning |
est2fe | 0:eae8cffdd121 | 99 | r->out = r->buf; |
est2fe | 0:eae8cffdd121 | 100 | } |
est2fe | 0:eae8cffdd121 | 101 | return p_b; // ?!? |
est2fe | 0:eae8cffdd121 | 102 | } |
est2fe | 0:eae8cffdd121 | 103 | |
est2fe | 4:192672304400 | 104 | void *rbuf_put (rbuf_t *r, void *b_p) // Ein Zeichen/Block einstellen |
est2fe | 0:eae8cffdd121 | 105 | { |
est2fe | 0:eae8cffdd121 | 106 | void *target; |
est2fe | 0:eae8cffdd121 | 107 | if (r->bufcnt >= r->buflen) |
est2fe | 0:eae8cffdd121 | 108 | { |
est2fe | 0:eae8cffdd121 | 109 | r->fehleranzahl++; |
est2fe | 0:eae8cffdd121 | 110 | r->flags = r->flags | ((uint8_t) (FEHLER)); |
est2fe | 0:eae8cffdd121 | 111 | return 0; // Wenn Buffer voll dann mit Nullpointer zurueck |
est2fe | 0:eae8cffdd121 | 112 | } |
est2fe | 0:eae8cffdd121 | 113 | target = r->in; |
est2fe | 0:eae8cffdd121 | 114 | memcpy (r->in, b_p, r->blockgroesse); |
est2fe | 0:eae8cffdd121 | 115 | r->bufcnt++; if (r->bufcnt > r->peak_max) {r->peak_max = r->bufcnt;} |
est2fe | 0:eae8cffdd121 | 116 | if (r->bufcnt >= r->Schwelle) |
est2fe | 0:eae8cffdd121 | 117 | { |
est2fe | 0:eae8cffdd121 | 118 | r->flags = r->flags | ((uint8_t) (FAST_VOLL)); |
est2fe | 0:eae8cffdd121 | 119 | } |
est2fe | 0:eae8cffdd121 | 120 | else |
est2fe | 0:eae8cffdd121 | 121 | { |
est2fe | 0:eae8cffdd121 | 122 | r->flags = r->flags & ((uint8_t) (~FAST_VOLL)); |
est2fe | 0:eae8cffdd121 | 123 | } |
est2fe | 0:eae8cffdd121 | 124 | r->anzahl++; // Anzahl aller Zeichen/Bloecke aufsummiert |
est2fe | 0:eae8cffdd121 | 125 | r->flags = r->flags | ((uint8_t) (NOT_EMPTY)); |
est2fe | 0:eae8cffdd121 | 126 | |
est2fe | 0:eae8cffdd121 | 127 | // r->in++; // Falls er das zuerst machen sollte! |
est2fe | 0:eae8cffdd121 | 128 | r->in = (void *) (((char *) r->in) + r->blockgroesse ); |
est2fe | 0:eae8cffdd121 | 129 | //if (r->in >= r->buf + r->buflen) |
est2fe | 0:eae8cffdd121 | 130 | if (r->in >= (void *) (((char *)r->buf) + (r->buflen * r->blockgroesse))) |
est2fe | 0:eae8cffdd121 | 131 | { |
est2fe | 0:eae8cffdd121 | 132 | // Bei Zeiger > Buffer, Zeiger wieder an Anfang vom Buffer |
est2fe | 0:eae8cffdd121 | 133 | r->in = r->buf; |
est2fe | 0:eae8cffdd121 | 134 | } |
est2fe | 3:c9c7c8e20336 | 135 | return target; |
est2fe | 0:eae8cffdd121 | 136 | } |
est2fe | 0:eae8cffdd121 | 137 | |
est2fe | 5:ddc33cb62d27 | 138 | uint32_t rbuf_getcnt (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 139 | { |
est2fe | 5:ddc33cb62d27 | 140 | return buffer->bufcnt; |
est2fe | 5:ddc33cb62d27 | 141 | } |
est2fe | 5:ddc33cb62d27 | 142 | |
est2fe | 5:ddc33cb62d27 | 143 | uint32_t rbuf_getflags (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 144 | { |
est2fe | 5:ddc33cb62d27 | 145 | return buffer->flags; |
est2fe | 5:ddc33cb62d27 | 146 | } |
est2fe | 5:ddc33cb62d27 | 147 | |
est2fe | 5:ddc33cb62d27 | 148 | uint32_t rbuf_getfehler (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 149 | { |
est2fe | 5:ddc33cb62d27 | 150 | return buffer->fehleranzahl; |
est2fe | 5:ddc33cb62d27 | 151 | } |
est2fe | 5:ddc33cb62d27 | 152 | |
est2fe | 5:ddc33cb62d27 | 153 | uint32_t rbuf_leer (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 154 | { |
est2fe | 5:ddc33cb62d27 | 155 | return !(buffer->flags && NOT_EMPTY); |
est2fe | 5:ddc33cb62d27 | 156 | } |
est2fe | 5:ddc33cb62d27 | 157 | |
est2fe | 5:ddc33cb62d27 | 158 | uint32_t rbuf_fast_voll (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 159 | { |
est2fe | 5:ddc33cb62d27 | 160 | return (buffer->flags && FAST_VOLL); |
est2fe | 5:ddc33cb62d27 | 161 | } |
est2fe | 5:ddc33cb62d27 | 162 | |
est2fe | 5:ddc33cb62d27 | 163 | uint32_t rbuf_fehler (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 164 | { |
est2fe | 5:ddc33cb62d27 | 165 | return (buffer->flags && FEHLER); |
est2fe | 5:ddc33cb62d27 | 166 | } |
est2fe | 5:ddc33cb62d27 | 167 | |
est2fe | 5:ddc33cb62d27 | 168 | uint32_t rbuf_getfehleranz (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 169 | { |
est2fe | 5:ddc33cb62d27 | 170 | return buffer->fehleranzahl; |
est2fe | 5:ddc33cb62d27 | 171 | } |
est2fe | 5:ddc33cb62d27 | 172 | |
est2fe | 5:ddc33cb62d27 | 173 | uint32_t rbuf_get_gesendet (rbuf_t *buffer) |
est2fe | 5:ddc33cb62d27 | 174 | { |
est2fe | 5:ddc33cb62d27 | 175 | return buffer->anzahl; |
est2fe | 5:ddc33cb62d27 | 176 | } |
est2fe | 5:ddc33cb62d27 | 177 | |
est2fe | 5:ddc33cb62d27 | 178 | #endif // von __rbuf_c_ |