Buffer from Sam Grove changed to have static instead of dynamic memory allocation. Fixed size to 256B.
Dependents: BufferedSerialStatic
Fork of Buffer by
Revision 7:e80960adb2ad, committed 2018-05-28
- Comitter:
- goranirnas
- Date:
- Mon May 28 14:34:02 2018 +0000
- Parent:
- 6:89564915f2a7
- Commit message:
- Updated buffer to use static memory allocation. Buffer size 256B.
Changed in this revision
MyBuffer.cpp | Show annotated file Show diff for this revision Revisions of this file |
MyBuffer.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 89564915f2a7 -r e80960adb2ad MyBuffer.cpp --- a/MyBuffer.cpp Mon Mar 07 21:10:13 2016 +0000 +++ b/MyBuffer.cpp Mon May 28 14:34:02 2018 +0000 @@ -24,10 +24,9 @@ #include "MyBuffer.h" template <class T> -MyBuffer<T>::MyBuffer(uint32_t size) +MyBuffer<T>::MyBuffer() { - _buf = new T [size]; - _size = size; + _size = MyBuffer::BUFFER_SIZE; clear(); return;
diff -r 89564915f2a7 -r e80960adb2ad MyBuffer.h --- a/MyBuffer.h Mon Mar 07 21:10:13 2016 +0000 +++ b/MyBuffer.h Mon May 28 14:34:02 2018 +0000 @@ -21,47 +21,23 @@ * limitations under the License. */ +/** + * Modification of original code to implement buffer with static memory + * allocation with size 256. + */ + #ifndef MYBUFFER_H #define MYBUFFER_H #include <stdint.h> #include <string.h> -/** A templated software ring buffer - * - * Example: - * @code - * #include "mbed.h" - * #include "MyBuffer.h" - * - * MyBuffer <char> buf; - * - * int main() - * { - * buf = 'a'; - * buf.put('b'); - * char *head = buf.head(); - * puts(head); - * - * char whats_in_there[2] = {0}; - * int pos = 0; - * - * while(buf.available()) - * { - * whats_in_there[pos++] = buf; - * } - * printf("%c %c\n", whats_in_there[0], whats_in_there[1]); - * buf.clear(); - * error("done\n\n\n"); - * } - * @endcode - */ - template <typename T> class MyBuffer { private: - T *_buf; + const static uint16_t BUFFER_SIZE = 256; + T _buf[BUFFER_SIZE]; volatile uint32_t _wloc; volatile uint32_t _rloc; uint32_t _size; @@ -70,7 +46,7 @@ /** Create a Buffer and allocate memory for it * @param size The size of the buffer */ - MyBuffer(uint32_t size = 0x100); + MyBuffer(); /** Get the size of the ring buffer * @return the size of the ring buffer