5 * 14407 SW Teal Blvd. #C
11 /* This file contains the functions perform garbage collection and allocate
19 /* this whole file would have be skipped if NO_RECYCLE is defined */
22 #define BTST(bitno, byte) ((byte) & (1 << (bitno)))
23 #define BSET(bitno, byte) ((byte) |= (1 << (bitno)))
24 #define BCLR(bitno, byte) ((byte) &= ~(1 << (bitno)))
26 #define TST(blkno) ((blkno) < MAXBIT ? BTST((blkno) & 7, bitmap[(blkno) >> 3]) : 1)
27 #define SET(blkno) if ((blkno) < MAXBIT) BSET((blkno) & 7, bitmap[(blkno) >> 3])
28 #define CLR(blkno) if ((blkno) < MAXBIT) BCLR((blkno) & 7, bitmap[(blkno) >> 3])
30 /* bitmap of free blocks in first 4096k of tmp file */
31 static unsigned char bitmap
[512];
32 #define MAXBIT (sizeof bitmap << 3)
34 /* this function locates all free blocks in the current tmp file */
40 /* start by assuming every block is free */
41 for (i
= 0; i
< sizeof bitmap
; i
++)
46 /* header blocks aren't free */
52 /* blocks needed for current hdr aren't free */
53 for (i
= 1; i
< MAXBLKS
; i
++)
58 /* blocks needed for undo version aren't free */
60 if (read(tmpfd
, &oldhdr
, (unsigned)sizeof oldhdr
) != sizeof oldhdr
)
62 msg("garbage() failed to read oldhdr??");
63 for (i
= 0; i
< sizeof bitmap
; i
++)
69 for (i
= 1; i
< MAXBLKS
; i
++)
74 /* blocks needed for cut buffers aren't free */
75 for (i
= cutneeds(&oldhdr
) - 1; i
>= 0; i
--)
81 /* This function allocates the first available block in the tmp file */
87 /* search for the first byte with a free bit set */
88 for (i
= 0; i
< sizeof bitmap
&& bitmap
[i
] == 0; i
++)
92 /* if we hit the end of the bitmap, return the end of the file */
93 if (i
== sizeof bitmap
)
95 offset
= lseek(tmpfd
, 0L, 2);
97 else /* compute the offset for the free block */
99 for (i
<<= 3; TST(i
) == 0; i
++)
102 offset
= (long)i
* (long)BLKSIZE
;
104 /* mark the block as "allocated" */
117 # define MEMMAGIC 0x19f72cc0L
118 # define MAXALLOC 800
119 static char *allocated
[MAXALLOC
];
120 static char *fromfile
[MAXALLOC
];
121 static int fromline
[MAXALLOC
];
122 static int sizes
[MAXALLOC
];
124 char *dbmalloc(size
, file
, line
)
132 size
= size
+ sizeof(long) - (size
% sizeof(long));
133 ret
= (char *)malloc(size
+ 2 * sizeof(long)) + sizeof(long);
134 for (i
= 0; i
< MAXALLOC
&& allocated
[i
]; i
++)
140 fprintf(stderr
, "\r\n%s(%d): Too many malloc calls!\n", file
, line
);
143 sizes
[i
] = size
/sizeof(long);
147 ((long *)ret
)[-1] = MEMMAGIC
;
148 ((long *)ret
)[sizes
[i
]] = MEMMAGIC
;
152 dbfree(ptr
, file
, line
)
159 for (i
= 0; i
< MAXALLOC
&& allocated
[i
] != ptr
; i
++)
165 fprintf(stderr
, "\r\n%s(%d): attempt to free mem that wasn't allocated\n", file
, line
);
168 allocated
[i
] = (char *)0;
169 if (((long *)ptr
)[-1] != MEMMAGIC
)
172 fprintf(stderr
, "\r\n%s(%d): underflowed malloc space, allocated at %s(%d)\n", file
, line
, fromfile
[i
], fromline
[i
]);
175 if (((long *)ptr
)[sizes
[i
]] != MEMMAGIC
)
178 fprintf(stderr
, "\r\n%s(%d): overflowed malloc space, allocated at %s(%d)\n", file
, line
, fromfile
[i
], fromline
[i
]);
181 free(ptr
- sizeof(long));