blob: 41b2c388a8115f2d9b959ff857a97be74bdf16ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/*
* buffer.c: Web video plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vdr/tools.h>
#include "buffer.h"
// --- cMemoryBuffer -------------------------------------------------------
cMemoryBuffer::cMemoryBuffer(size_t prealloc) {
capacity = prealloc;
buf = (char *)malloc(capacity*sizeof(char));
offset = 0;
len = 0;
}
cMemoryBuffer::~cMemoryBuffer() {
if (buf)
free(buf);
}
void cMemoryBuffer::Realloc(size_t newsize) {
if (newsize > capacity-offset) {
if (newsize <= capacity) {
// The new buffer fits in the memory if we just move the current
// content offset bytes backwards.
buf = (char *)memmove(buf, &buf[offset], len);
offset = 0;
} else {
// We need to realloc. Move the content to the beginning of the
// buffer while we are at it.
capacity += min(capacity, (size_t)10*1024);
capacity = max(capacity, newsize);
char *newbuf = (char *)malloc(capacity*sizeof(char));
if (newbuf) {
memcpy(newbuf, &buf[offset], len);
offset = 0;
free(buf);
buf = newbuf;
}
}
}
}
ssize_t cMemoryBuffer::Put(const char *data, size_t bytes) {
if (len+bytes > Free()) {
Realloc(len+bytes);
}
if (buf) {
memcpy(&buf[offset+len], data, bytes);
len += bytes;
return bytes;
}
return -1;
}
ssize_t cMemoryBuffer::PutFromFile(int fd, size_t bytes) {
if (len+bytes > Free()) {
Realloc(len+bytes);
}
if (buf) {
ssize_t r = safe_read(fd, &buf[offset+len], bytes);
if (r > 0)
len += r;
return r;
} else
return -1;
}
void cMemoryBuffer::Pop(size_t bytes) {
if (bytes <= len) {
offset += bytes;
len -= bytes;
}
}
|