scsi request, part usb mass storage class driver, bulk transfers
[quarnos.git] / libs / buffer.h
blob40aa17574ae7f960375572c6e98e6accb2132ade
1 /* Quarn OS
3 * Buffer
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _BUFFER_H_
24 #define _BUFFER_H_
26 #include "pointer.h"
27 #include "math.h"
29 void *memcpy(void *,const void*,int);
30 void *operator new[](unsigned int, int);
32 class buffer {
33 private:
34 char *address;
35 unsigned int size;
37 public:
38 static const buffer empty;
40 buffer() : address((char*)0), size(0) { }
42 buffer(unsigned int bytes) : address(new char[bytes]), size(bytes) { }
44 buffer(void *addr, unsigned int siz) : address((char*)addr), size(siz) { }
46 buffer(const buffer &x) : address(x.address), size(x.size) { }
48 buffer &operator=(const buffer &x) {
49 address = x.address;
50 size = x.size;
52 return *this;
55 operator void*() {
56 return address;
59 char &operator[](int i) {
60 if (i >= 0 && i < size)
61 return address[i];
64 const char &operator[](int i) const {
65 if (i >= 0 && i < size)
66 return address[i];
69 buffer &operator+=(const buffer &x) {
70 if (!x.size)
71 return *this;
73 if (!size) {
74 copy_data(x);
75 return *this;
78 char *buf = new char[x.size + size];
79 memcpy(buf, address, size);
80 memcpy((void*)((unsigned int)buf + size), x.address, x.size);
82 delete address;
83 address = buf;
84 size += x.size;
86 return *this;
89 template<typename T>
90 p<T> cast() const {
91 if (size >= sizeof(T))
92 return p<T>(reinterpret_cast<T*>(address));
95 int get_size() const {
96 return size;
99 void *get_address() const {
100 return address;
103 buffer cut_first(unsigned int cut_size) const {
104 if (cut_size >= size)
105 return empty;
107 return buffer(address + cut_size, size - cut_size);
110 void align(int al) {
111 void *naddr = (void*)new (al) char[size];
112 memcpy(naddr, (void*)address, size);
113 address = (char*)naddr;
116 buffer get_next(unsigned int bytes) const {
117 bytes = math::min(bytes, size);
119 if (!bytes)
120 return empty;
122 return buffer(address, bytes);
125 buffer copy() const {
126 buffer buf(new char[size], size);
127 memcpy(buf.address, address, size);
128 return buf;
131 buffer copy_data(const buffer &buf) {
132 memcpy(address, buf.address, math::min(size, buf.size));
135 template<typename T>
136 static buffer to_mem(p<T> addr) {
137 return buffer((void*)addr, sizeof(T));
140 template<typename T>
141 static buffer to_mem(T &addr) {
142 return buffer((void*)&addr, sizeof(T));
146 #endif