File: buffer.c

package info (click to toggle)
aoeui 1.1.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 348 kB
  • ctags: 487
  • sloc: ansic: 5,248; makefile: 67
file content (259 lines) | stat: -rw-r--r-- 6,145 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "all.h"
#include <sys/mman.h>
#include <fcntl.h>

#ifndef S_IRUSR
#define S_IRUSR 0400
#endif
#ifndef S_IWUSR
#define S_IWUSR 0200
#endif

/*
 *	Buffers contain payload bytes and a "gap" of unused space.
 *	The gap is contiguous, and may appear in the midst of the
 *	payload.  Editing operations shift the gap around in order
 *	to enlarge it (when bytes are to be deleted) or to add new
 *	bytes to its boundaries.
 *
 *	Buffers are represented by pages mmap'ed from the file's
 *	temporary# file, or anonymous storage for things that aren't
 *	file texts.
 *
 *	Besides being used to hold the content of files, buffers
 *	are used for cut/copied text (the "clip buffer"), macros,
 *	and for undo histories.
 */

struct buffer *buffer_create(char *path)
{
	struct buffer *buffer = allocate0(sizeof *buffer);
	if (path && *path) {
		buffer->path = allocate(NULL, strlen(path) + 2);
		sprintf(buffer->path, "%s#", path);
		errno = 0;
		buffer->fd = open(buffer->path, O_CREAT|O_TRUNC|O_RDWR,
				  S_IRUSR|S_IWUSR);
		if (buffer->fd < 0)
			message("could not create temporary file %s",
				buffer->path);
	} else
		buffer->fd = -1;
	return buffer;
}

void buffer_destroy(struct buffer *buffer)
{
	if (!buffer)
		return;
	munmap(buffer->data, buffer->allocated);
	if (buffer->fd >= 0) {
		close(buffer->fd);
		unlink(buffer->path);
	}
	allocate(buffer->path, 0);
	allocate(buffer, 0);
}

static void place_gap(struct buffer *buffer, unsigned offset)
{
	unsigned gapsize = buffer_gap_bytes(buffer);

	if (offset > buffer->payload)
		offset = buffer->payload;
	if (offset <= buffer->gap)
		memmove(buffer->data + offset + gapsize, buffer->data + offset,
			buffer->gap - offset);
	else
		memmove(buffer->data + buffer->gap,
			buffer->data + buffer->gap + gapsize,
			offset - buffer->gap);
	buffer->gap = offset;
	if (buffer->fd >= 0 && gapsize)
		memset(buffer->data + buffer->gap, ' ', gapsize);
}

static void resize(struct buffer *buffer, unsigned bytes)
{
	void *p;
	char *old = buffer->data;
	int fd, mapflags = 0;
	static unsigned pagesize;

	/* Whole pages, with extras as size increases */
	if (!pagesize)
		pagesize = getpagesize();
	bytes += pagesize-1;
	bytes /= pagesize;
	bytes *= 11;
	bytes /= 10;
	bytes *= pagesize;

	if (bytes < buffer->allocated)
		munmap(old + bytes, buffer->allocated - bytes);
	if (buffer->fd >= 0 && bytes != buffer->allocated) {
		errno = 0;
		if (ftruncate(buffer->fd, bytes))
			die("could not adjust %s from 0x%x to 0x%x bytes",
			    buffer->path, buffer->allocated, bytes);
	}
	if (bytes <= buffer->allocated) {
		buffer->allocated = bytes;
		return;
	}
#ifdef MREMAP_MAYMOVE
	if (old) {
		/* attempt extension */
		errno = 0;
		p = mremap(old, buffer->allocated, bytes, MREMAP_MAYMOVE);
		if (p != MAP_FAILED)
			goto done;
#define NEED_DONE_LABEL
	}
#endif

	/* new/replacement allocation */
	if ((fd = buffer->fd) >= 0) {
		mapflags |= MAP_SHARED;
		if (old) {
			munmap(old, buffer->allocated);
			old = NULL;
		}
	} else {
#ifdef MAP_ANONYMOUS
		mapflags |= MAP_ANONYMOUS;
#elif defined MAP_ANON
		mapflags |= MAP_ANON;
#else
		static int anonymous_fd = -1;
		if (anonymous_fd < 0) {
			errno = 0;
			anonymous_fd = open("/dev/zero", O_RDWR);
			if (anonymous_fd < 0)
				die("could not open /dev/zero for "
				    "anonymous mappings");
		}
		fd = anonymous_fd;
#endif
		mapflags |= MAP_PRIVATE;
	}

	errno = 0;
	p = mmap(0, bytes, PROT_READ|PROT_WRITE, mapflags, fd, 0);
	if (p == MAP_FAILED)
		die("mmap(0x%x bytes, fd %d) failed", bytes, fd);

	if (old) {
		memcpy(p, old, buffer->allocated);
		munmap(old, buffer->allocated);
	}

#ifdef NEED_DONE_LABEL
done:
#endif
	buffer->data = p;
	buffer->allocated = bytes;
}

unsigned buffer_raw(struct buffer *buffer, char **out,
		    unsigned offset, unsigned bytes)
{
	if (!buffer)
		return 0;
	if (offset >= buffer->payload)
		offset = buffer->payload;
	if (offset + bytes > buffer->payload)
		bytes = buffer->payload - offset;
	if (!bytes)
		return 0;

	if (offset < buffer->gap && offset + bytes > buffer->gap)
		place_gap(buffer, offset + bytes);
	*out = buffer->data + offset;
	if (offset >= buffer->gap)
		*out += buffer_gap_bytes(buffer);
	return bytes;
}

unsigned buffer_get(struct buffer *buffer, void *out,
		    unsigned offset, unsigned bytes)
{
	unsigned left;

	if (!buffer)
		return 0;
	if (offset >= buffer->payload)
		offset = buffer->payload;
	if (offset + bytes > buffer->payload)
		bytes = buffer->payload - offset;
	if (!bytes)
		return 0;
	left = bytes;
	if (offset < buffer->gap) {
		unsigned before = buffer->gap - offset;
		if (before > bytes)
			before = bytes;
		memcpy(out, buffer->data + offset, before);
		out = (char *) out + before;
		offset += before;
		left -= before;
		if (!left)
			return bytes;
	}
	offset += buffer_gap_bytes(buffer);
	memcpy(out, buffer->data + offset, left);
	return bytes;
}

unsigned buffer_delete(struct buffer *buffer,
		       unsigned offset, unsigned bytes)
{
	if (!buffer)
		return 0;
	if (offset > buffer->payload)
		offset = buffer->payload;
	if (offset + bytes > buffer->payload)
		bytes = buffer->payload - offset;
	place_gap(buffer, offset);
	buffer->payload -= bytes;
	return bytes;
}

unsigned buffer_insert(struct buffer *buffer, const void *in,
		       unsigned offset, unsigned bytes)
{
	if (!buffer)
		return 0;
	if (offset > buffer->payload)
		offset = buffer->payload;
	if (bytes > buffer_gap_bytes(buffer)) {
		place_gap(buffer, buffer->payload);
		resize(buffer, buffer->payload + bytes);
	}
	place_gap(buffer, offset);
	if (in)
		memcpy(buffer->data + offset, in, bytes);
	else
		memset(buffer->data + offset, 0, bytes);
	buffer->gap += bytes;
	buffer->payload += bytes;
	return bytes;
}

unsigned buffer_move(struct buffer *to, unsigned to_offset,
		     struct buffer *from, unsigned from_offset,
		     unsigned bytes)
{
	char *raw = NULL;
	bytes = buffer_raw(from, &raw, from_offset, bytes);
	buffer_insert(to, raw, to_offset, bytes);
	return buffer_delete(from, from_offset, bytes);
}

void buffer_snap(struct buffer *buffer)
{
	if (buffer && buffer->fd >= 0) {
		place_gap(buffer, buffer->payload);
		ftruncate(buffer->fd, buffer->payload);
	}
}