1: /* bitmap.c 1.9 83/05/13 */
2:
3: #include <stdio.h>
4: #include "cpmio.h"
5: int bm_size;
6:
7: /*
8: * Bit map handling routines;
9: * - build the disk allocation bit map
10: * - allocate a new block
11: * - dump the bitmap in hex to stdout (debugging)
12: * - count the number of blocks in use
13: * Note: the first block is number zero and the
14: * directory always occupies the first few blocks,
15: * depending on the disk format. For a standard
16: * SSSD disk the blocks are numbered 0 thru 243 and
17: * the directory occupies blocks 0 and 1.
18: */
19:
20: /*
21: * Allocate a new disk block, return NULL if disk full
22: */
23: char
24: alloc()
25:
26: {
27:
28: int i, j, blk;
29:
30: for (i=0; i<bm_size; i++) {
31: j = ffc(0, INTSIZE, *(bitmap+i));
32: if ( j < INTSIZE) break;
33: }
34: blk = i * INTSIZE + j;
35: if (blk >= (seclth*sectrk*(tracks-2))/blksiz)
36: return ('\0');
37: *(bitmap+i) |= (1 << j); /* set the appropriate bit in the bitmap */
38: #ifdef DEBUG
39: printf("block number allocated: %d (0x%x)\n", blk, blk);
40: dbmap("new bitmap:");
41: #endif
42: return ((char) blk);
43: }
44:
45:
46: /*
47: * Dump the bitmap in hex to stdout, used only for debugging
48: */
49:
50: dbmap(str)
51: char *str;
52: {
53: #ifdef DEBUG
54:
55: int i;
56:
57: printf("%s\n",str);
58: for (i=0; i<bm_size; i++)
59: printf("%.8x\n", *(bitmap+i));
60: #endif
61: }
62:
63: /*
64: * Return the number of bloks used in the
65: * directory, including the directory blocks
66: */
67:
68: blks_used()
69: {
70:
71: int j, i, temp;
72: int buse = 0;
73:
74: for (i=0; i < bm_size; i++) {
75: if (*(bitmap+i) == 0)
76: continue;
77: if (*(bitmap+i) == -1) {
78: buse += INTSIZE;
79: } else {
80: temp = *(bitmap+i);
81: for (j=0; j < INTSIZE; j++) {
82: if (1 & temp)
83: ++buse;
84: temp >>= 1;
85: }
86: }
87: }
88: return (buse);
89: }
90:
91:
92: build_bmap()
93: {
94:
95: int i, j, *malloc(), offset, block;
96:
97: bm_size = 1 + ((seclth*sectrk*(tracks-2))/blksiz)/INTSIZE;
98:
99: if (!bitmap) {
100: if ((bitmap = malloc(bm_size*4)) == NULL) {
101: printf("can't allocate memory for bitmap\n");
102: exit(1);
103: }
104: }
105: /*
106: * clear bitmap
107: */
108: for (i=0; i<bm_size; i++)
109: bitmap[i] = 0;
110:
111: /* i equals the number of blocks occupied by the directory */
112: i = (maxdir*32)/blksiz;
113:
114: /* set the directory blocks busy in the bitmap */
115: *bitmap = (1 << i) -1;
116: for (i=0; i<maxdir; i++) {
117: if ((dirbuf+i)->status != (char) 0xe5) {
118: #ifdef DEBUG
119: printf("%d ->%8s\n", i, (dirbuf+i)->name);
120: #endif
121: for (j=0; (j<16)&&((dirbuf+i)->pointers[j] != '\0'); j++){
122: block = 0xff & (int)(dirbuf+i)->pointers[j];
123: offset = block / INTSIZE;
124: #ifdef DEBUG
125: printf("blk:%d, offs:%d, bit:%d\n", block,offset,block%INTSIZE);
126: #endif
127: *(bitmap+offset) |= (1 << block % INTSIZE);
128: }
129: }
130: }
131: #ifdef DEBUG
132: dbmap("initial bit map:");
133: #endif
134: }
Defined functions
alloc
defined in line
23; used 4 times
dbmap
defined in line
50; used 3 times
Defined variables