Repository navigation
Expand file tree
/
Copy pathrecycle.c
More file actions
183 lines (157 loc) · 3.72 KB
/
Copy pathrecycle.c
File metadata and controls
183 lines (157 loc) · 3.72 KB
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
/* recycle.c */
/* Author:
* Steve Kirkendall
* 14407 SW Teal Blvd. #C
* Beaverton, OR 97005
* kirkenda@cs.pdx.edu
*/
/* This file contains the functions perform garbage collection and allocate
* reusable blocks.
*/
#include "config.h"
#include "vi.h"
#ifndef NO_RECYCLE
/* this whole file would have be skipped if NO_RECYCLE is defined */
#define BTST(bitno, byte) ((byte) & (1 << (bitno)))
#define BSET(bitno, byte) ((byte) |= (1 << (bitno)))
#define BCLR(bitno, byte) ((byte) &= ~(1 << (bitno)))
#define TST(blkno) ((blkno) < MAXBIT ? BTST((blkno) & 7, bitmap[(blkno) >> 3]) : 1)
#define SET(blkno) if ((blkno) < MAXBIT) BSET((blkno) & 7, bitmap[(blkno) >> 3])
#define CLR(blkno) if ((blkno) < MAXBIT) BCLR((blkno) & 7, bitmap[(blkno) >> 3])
/* bitmap of free blocks in first 4096k of tmp file */
static unsigned char bitmap[512];
#define MAXBIT (sizeof bitmap << 3)
/* this function locates all free blocks in the current tmp file */
void garbage()
{
int i;
BLK oldhdr;
/* start by assuming every block is free */
for (i = 0; i < sizeof bitmap; i++)
{
bitmap[i] = 255;
}
/* header blocks aren't free */
#ifndef lint
CLR(0);
CLR(1);
#endif
/* blocks needed for current hdr aren't free */
for (i = 1; i < MAXBLKS; i++)
{
CLR(hdr.n[i]);
}
/* blocks needed for undo version aren't free */
lseek(tmpfd, 0L, 0);
if (read(tmpfd, &oldhdr, (unsigned)sizeof oldhdr) != sizeof oldhdr)
{
msg("garbage() failed to read oldhdr??");
for (i = 0; i < sizeof bitmap; i++)
{
bitmap[i] = 0;
}
return;
}
for (i = 1; i < MAXBLKS; i++)
{
CLR(oldhdr.n[i]);
}
/* blocks needed for cut buffers aren't free */
for (i = cutneeds(&oldhdr) - 1; i >= 0; i--)
{
CLR(oldhdr.n[i]);
}
}
/* This function allocates the first available block in the tmp file */
long allocate()
{
int i;
long offset;
/* search for the first byte with a free bit set */
for (i = 0; i < sizeof bitmap && bitmap[i] == 0; i++)
{
}
/* if we hit the end of the bitmap, return the end of the file */
if (i == sizeof bitmap)
{
offset = lseek(tmpfd, 0L, 2);
}
else /* compute the offset for the free block */
{
for (i <<= 3; TST(i) == 0; i++)
{
}
offset = (long)i * (long)BLKSIZE;
/* mark the block as "allocated" */
CLR(i);
}
return offset;
}
#endif
#ifdef DEBUG
# include <stdio.h>
# undef malloc
# undef free
# define MEMMAGIC 0x19f72cc0L
# define MAXALLOC 800
static char *allocated[MAXALLOC];
static char *fromfile[MAXALLOC];
static int fromline[MAXALLOC];
static int sizes[MAXALLOC];
char *dbmalloc(size, file, line)
int size;
char *file;
int line;
{
char *ret;
int i;
size = size + sizeof(long) - (size % sizeof(long));
ret = (char *)malloc(size + 2 * sizeof(long)) + sizeof(long);
for (i = 0; i < MAXALLOC && allocated[i]; i++)
{
}
if (i == MAXALLOC)
{
endwin();
fprintf(stderr, "\r\n%s(%d): Too many malloc calls!\n", file, line);
abort();
}
sizes[i] = size/sizeof(long);
allocated[i] = ret;
fromfile[i] = file;
fromline[i] = line;
((long *)ret)[-1] = MEMMAGIC;
((long *)ret)[sizes[i]] = MEMMAGIC;
return ret;
}
dbfree(ptr, file, line)
char *ptr;
char *file;
int line;
{
int i;
for (i = 0; i < MAXALLOC && allocated[i] != ptr; i++)
{
}
if (i == MAXALLOC)
{
endwin();
fprintf(stderr, "\r\n%s(%d): attempt to free mem that wasn't allocated\n", file, line);
abort();
}
allocated[i] = (char *)0;
if (((long *)ptr)[-1] != MEMMAGIC)
{
endwin();
fprintf(stderr, "\r\n%s(%d): underflowed malloc space, allocated at %s(%d)\n", file, line, fromfile[i], fromline[i]);
abort();
}
if (((long *)ptr)[sizes[i]] != MEMMAGIC)
{
endwin();
fprintf(stderr, "\r\n%s(%d): overflowed malloc space, allocated at %s(%d)\n", file, line, fromfile[i], fromline[i]);
abort();
}
free(ptr - sizeof(long));
}
#endif