RAMDisk example for the USBFileSystem

Dependencies:   mbed USBFileSystem

Fork of USBFileSystem_RAMDISK_HelloWorld by Erik -

Committer:
Sissors
Date:
Tue Jul 30 18:27:18 2013 +0000
Revision:
1:e1b0157ce547
Memory leaks -_-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 1:e1b0157ce547 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Sissors 1:e1b0157ce547 2 *
Sissors 1:e1b0157ce547 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Sissors 1:e1b0157ce547 4 * and associated documentation files (the "Software"), to deal in the Software without
Sissors 1:e1b0157ce547 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Sissors 1:e1b0157ce547 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Sissors 1:e1b0157ce547 7 * Software is furnished to do so, subject to the following conditions:
Sissors 1:e1b0157ce547 8 *
Sissors 1:e1b0157ce547 9 * The above copyright notice and this permission notice shall be included in all copies or
Sissors 1:e1b0157ce547 10 * substantial portions of the Software.
Sissors 1:e1b0157ce547 11 *
Sissors 1:e1b0157ce547 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Sissors 1:e1b0157ce547 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Sissors 1:e1b0157ce547 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Sissors 1:e1b0157ce547 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Sissors 1:e1b0157ce547 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sissors 1:e1b0157ce547 17 */
Sissors 1:e1b0157ce547 18
Sissors 1:e1b0157ce547 19 #ifndef CIRCBUFFER_H
Sissors 1:e1b0157ce547 20 #define CIRCBUFFER_H
Sissors 1:e1b0157ce547 21
Sissors 1:e1b0157ce547 22 template <class T>
Sissors 1:e1b0157ce547 23 class CircBuffer {
Sissors 1:e1b0157ce547 24 public:
Sissors 1:e1b0157ce547 25 CircBuffer(int length) {
Sissors 1:e1b0157ce547 26 write = 0;
Sissors 1:e1b0157ce547 27 read = 0;
Sissors 1:e1b0157ce547 28 size = length + 1;
Sissors 1:e1b0157ce547 29 buf = (T *)malloc(size * sizeof(T));
Sissors 1:e1b0157ce547 30 };
Sissors 1:e1b0157ce547 31
Sissors 1:e1b0157ce547 32 bool isFull() {
Sissors 1:e1b0157ce547 33 return ((write + 1) % size == read);
Sissors 1:e1b0157ce547 34 };
Sissors 1:e1b0157ce547 35
Sissors 1:e1b0157ce547 36 bool isEmpty() {
Sissors 1:e1b0157ce547 37 return (read == write);
Sissors 1:e1b0157ce547 38 };
Sissors 1:e1b0157ce547 39
Sissors 1:e1b0157ce547 40 void queue(T k) {
Sissors 1:e1b0157ce547 41 if (isFull()) {
Sissors 1:e1b0157ce547 42 read++;
Sissors 1:e1b0157ce547 43 read %= size;
Sissors 1:e1b0157ce547 44 }
Sissors 1:e1b0157ce547 45 buf[write++] = k;
Sissors 1:e1b0157ce547 46 write %= size;
Sissors 1:e1b0157ce547 47 }
Sissors 1:e1b0157ce547 48
Sissors 1:e1b0157ce547 49 uint16_t available() {
Sissors 1:e1b0157ce547 50 return (write >= read) ? write - read : size - read + write;
Sissors 1:e1b0157ce547 51 };
Sissors 1:e1b0157ce547 52
Sissors 1:e1b0157ce547 53 bool dequeue(T * c) {
Sissors 1:e1b0157ce547 54 bool empty = isEmpty();
Sissors 1:e1b0157ce547 55 if (!empty) {
Sissors 1:e1b0157ce547 56 *c = buf[read++];
Sissors 1:e1b0157ce547 57 read %= size;
Sissors 1:e1b0157ce547 58 }
Sissors 1:e1b0157ce547 59 return(!empty);
Sissors 1:e1b0157ce547 60 };
Sissors 1:e1b0157ce547 61
Sissors 1:e1b0157ce547 62 private:
Sissors 1:e1b0157ce547 63 volatile uint16_t write;
Sissors 1:e1b0157ce547 64 volatile uint16_t read;
Sissors 1:e1b0157ce547 65 uint16_t size;
Sissors 1:e1b0157ce547 66 T * buf;
Sissors 1:e1b0157ce547 67 };
Sissors 1:e1b0157ce547 68
Sissors 1:e1b0157ce547 69 #endif