net: data reception system, fixed bugs in rtl8139
[quarnos.git] / libs / buffer.h
blob7f8f12dd0e4cc3579313b19710f5dc9b95d5edbc
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"
28 void *memcpy(void *,const void*,int);
30 class buffer {
31 private:
32 char *address;
33 int size;
35 public:
36 buffer() : address((char*)0), size(0) { }
38 buffer(unsigned int bytes) : address(new char[bytes]), size(bytes) { }
40 buffer(void *addr, int siz) : address((char*)addr), size(siz) { }
42 buffer(const buffer &x) : address(x.address), size(x.size) { }
44 buffer &operator=(const buffer &x) {
45 address = x.address;
46 size = x.size;
48 return *this;
51 operator void*() {
52 return address;
55 char &operator[](int i) {
56 if (i >= 0 && i < size)
57 return address[i];
60 const char &operator[](int i) const {
61 if (i >= 0 && i < size)
62 return address[i];
65 buffer &operator+=(const buffer &x) {
66 char *buf = new char[x.size + size];
67 memcpy(buf, address, size);
68 memcpy((void*)((unsigned int)buf + size), x.address, x.size);
70 delete address;
71 address = buf;
72 size += x.size;
74 return *this;
77 template<typename T>
78 p<T> cast() const {
79 if (size >= sizeof(T))
80 return p<T>(reinterpret_cast<T*>(address));
83 int get_size() const {
84 return size;
87 void *get_address() const {
88 return address;
91 buffer cut_first(unsigned int cut_size) const {
92 if (cut_size >= size)
93 throw cut_size;
95 return buffer(address + cut_size, size - cut_size);
98 template<typename T>
99 static buffer to_mem(p<T> addr) {
100 return buffer((void*)addr, sizeof(T));
103 template<typename T>
104 static buffer to_mem(T &addr) {
105 return buffer((void*)&addr, sizeof(T));
109 #endif