forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DVRBuffer.m
executable file
·180 lines (159 loc) · 4.05 KB
/
DVRBuffer.m
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -*- mode:objc -*-
/*
** DVRBuffer.m
**
** Copyright 20101
**
** Author: George Nachman
**
** Project: iTerm2
**
** Description: Implements a circular in-memory buffer for storing
** screen images plus some metadata associated with each frame.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "DVRBuffer.h"
@implementation DVRBuffer
- (id)initWithBufferCapacity:(long long)maxsize
{
if ([super init] == nil) {
return nil;
}
capacity_ = maxsize;
store_ = malloc(maxsize);
index_ = [[NSMutableDictionary alloc] init];
sanityCheck = index_;
firstKey_ = 0;
nextKey_ = 0;
begin_ = 0;
end_ = 0;
return self;
}
- (void)dealloc
{
assert(index_ == sanityCheck);
[index_ release];
index_ = nil;
sanityCheck = nil;
free(store_);
[super dealloc];
}
- (BOOL)reserve:(long long)length
{
assert(index_ == sanityCheck);
BOOL hadToFree = NO;
while (![self hasSpaceAvailable:length]) {
assert(nextKey_ > firstKey_);
[self deallocateBlock];
hadToFree = YES;
}
if (begin_ <= end_) {
if (capacity_ - end_ >= length) {
scratch_ = store_ + end_;
} else {
scratch_ = store_;
}
} else {
scratch_ = store_ + end_;
}
assert(index_ == sanityCheck);
return hadToFree;
}
- (long long)allocateBlock:(long long)length
{
assert(index_ == sanityCheck);
assert([self hasSpaceAvailable:length]);
DVRIndexEntry* entry = [[DVRIndexEntry alloc] init];
entry->position = scratch_ - store_;
end_ = entry->position + length;
entry->frameLength = length;
scratch_ = 0;
long long key = nextKey_++;
[index_ setObject:entry forKey:[NSNumber numberWithLongLong:key]];
[entry release];
assert(index_ == sanityCheck);
return key;
}
- (void)deallocateBlock
{
assert(index_ == sanityCheck);
long long key = firstKey_++;
DVRIndexEntry* entry = [self entryForKey:key];
begin_ = entry->position + entry->frameLength;
[index_ removeObjectForKey:[NSNumber numberWithLongLong:key]];
assert(index_ == sanityCheck);
}
- (void*)blockForKey:(long long)key
{
assert(index_ == sanityCheck);
DVRIndexEntry* entry = [self entryForKey:key];
assert(entry);
assert(index_ == sanityCheck);
return store_ + entry->position;
}
- (BOOL)hasSpaceAvailable:(long long)length
{
assert(index_ == sanityCheck);
if (begin_ <= end_) {
// ---begin*******end-----
if (capacity_ - end_ > length) {
return YES;
} else if (begin_ > length) {
return YES;
} else {
return NO;
}
} else {
// ***end----begin****
if (begin_ - end_ > length) {
return YES;
} else {
return NO;
}
}
}
- (long long)firstKey
{
assert(index_ == sanityCheck);
return firstKey_;
}
- (long long)lastKey
{
assert(index_ == sanityCheck);
return nextKey_ - 1;
}
- (DVRIndexEntry*)entryForKey:(long long)key
{
assert(index_ == sanityCheck);
assert(index_);
return [index_ objectForKey:[NSNumber numberWithLongLong:key]];
}
- (char*)scratch
{
assert(index_ == sanityCheck);
return scratch_;
}
- (long long)capacity
{
assert(index_ == sanityCheck);
return capacity_;
}
- (BOOL)isEmpty
{
assert(index_ == sanityCheck);
return [index_ count] == 0;
}
@end