| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
/* The tmp file stores lines of text for JOVE buffers as contiguous |
| 9 |
* sequences of chars, terminated by NUL. New lines are added to |
| 10 |
* the end of the tmp file. The file is not garbage collected |
| 11 |
* because that would be too painful. As a result, commands like |
| 12 |
* Yank and Kill are really easy; basically all we do is make copies |
| 13 |
* of the disk addresses of the lines (as opposed to the contents). |
| 14 |
* So, jputline(char *buf) writes buf to the disk and returns a new |
| 15 |
* disk address. jgetline(daddr addr, char *buf) is the opposite of |
| 16 |
* jputline(). f_getputl(LinePtr line, File fp) reads from open fp |
| 17 |
* directly into the tmp file (into the buffer cache (see below)) |
| 18 |
* and stores the address in line. This is used during read_file to |
| 19 |
* minimize copying. |
| 20 |
* |
| 21 |
* To reduce the size of a tmp file disk address, each disk address |
| 22 |
* is limited to point to positions that are the start of a chunk. |
| 23 |
* A chunk has exactly CHNK_CHARS characters, so that an disk |
| 24 |
* address can be reduced by log2(CHNK_CHARS) bits. These extra |
| 25 |
* bits allow for a larger tmp file. This is exploited on machines |
| 26 |
* where the disk address (daddr) is stored in a 16-bit type (for other |
| 27 |
* machines, a chunk is simply one character). Using chunks wastes |
| 28 |
* some tmp file (and b_cache) space since lines must be aligned on |
| 29 |
* CHNK_CHARS boundaries. A CHNK_CHARS value large enough to hold |
| 30 |
* the vast majority of lines inside a single chunk means that each |
| 31 |
* unique daddr value refers to a chunk/line, making best use of |
| 32 |
* precious daddr bits (lines larger than CHNK_CHARS mean that daddr |
| 33 |
* values are skipped, which is not a big deal for 32bit or 64it |
| 34 |
* machines but really cuts into the max# of lines on PCDOS, for example. |
| 35 |
* Tmp file space, on the other hand, is relatively cheap, and wasting |
| 36 |
* b_cache only causes a lot more I/O slowness, but is not as painful |
| 37 |
* as running out of lines. As a point of reference, JOVE 4.17 compiled |
| 38 |
* for large model on PCDOS with Open Watcom 1.9 (space-optimized -os) |
| 39 |
* can fit around 32000 lines in memory from a 1.4MB text file before |
| 40 |
* starting to hit limits, and/or it can hold about 30 JOVE source files |
| 41 |
* simultaenously before running out of memory. |
| 42 |
* |
| 43 |
* The tmp file is accessed by blocks of size JBUFSIZ (typically |
| 44 |
* 512 bytes or larger). Lines do NOT cross block boundaries in the |
| 45 |
* tmp file so that accessing the contents of lines can be much |
| 46 |
* faster. Pointers into the interior of disk buffers are returned |
| 47 |
* instead of copying the contents into local arrays and then using |
| 48 |
* them. This cuts down on the amount of copying a great deal, at |
| 49 |
* the expense of space efficiency. |
| 50 |
* |
| 51 |
* The high bit of each disk address is used for marking the line as |
| 52 |
* needing redisplay (DDIRTY). In theory, this has nothing to do |
| 53 |
* with a disk address, but it is the cheapest place to hide a bit |
| 54 |
* in struct line (this is important since instances of this struct |
| 55 |
* are the major consumer of memory). |
| 56 |
* |
| 57 |
* The type daddr is used to represent the disk address of a line. |
| 58 |
* It is an integer containing three packed fields: the dirty bit, |
| 59 |
* the block number (within the tmp file), and the chunk number |
| 60 |
* (within the block). Many of the following definitions are for |
| 61 |
* packing and unpacking daddr values. |
| 62 |
* On a system which limits JOVE to a very small data segment, |
| 63 |
* it may be worthwhile limiting daddr to a short. This reduces |
| 64 |
* the size of a Line descriptor (so more line references fit in memory), |
| 65 |
* but reduces the addressable size of the temp file (so fewer |
| 66 |
* line contents can be stored on disk), which is reasonable |
| 67 |
* on 16-bit or very-small-memory machines (e.g. PDP-11 and iAPX 8088/8086). |
| 68 |
* NOTE: logically, daddr is unsigned, but a signed type will work |
| 69 |
* if you cannot use an unsigned type. |
| 70 |
* |
| 71 |
* There is a buffer cache of NBUF buffers (3 on SMALL machine, |
| 72 |
* and some larger value (e.g. 64) on !SMALL machines. For 8086 |
| 73 |
* memory models, NBUF*buffersize must be less than 64K). The |
| 74 |
* blocks are stored in LRU order and each block is also stored |
| 75 |
* in a hash table by block #. When a block is requested, |
| 76 |
* it can quickly be looked up in the hash table. If it's not there the |
| 77 |
* LRU block is assigned the new block #. If it finds that the LRU block |
| 78 |
* is dirty (i.e., has pending IO) it syncs the WHOLE tmp file, i.e., |
| 79 |
* does all the pending writes. This works much better on floppy disk |
| 80 |
* systems, like the IBM PC, if the blocks are sorted before sync'ing. |
| 81 |
* |
| 82 |
*/ |
| 83 |
|
| 84 |
#ifndef LG_CHNK_CHARS |
| 85 |
# ifdef JSMALL |
| 86 |
# define LG_CHNK_CHARS 7 /* save bits in daddr at cost of space in tempfile */ |
| 87 |
# else |
| 88 |
# define LG_CHNK_CHARS 0 |
| 89 |
# endif |
| 90 |
#endif |
| 91 |
|
| 92 |
/* MAX_BLOCKS is the number of distinct block numbers that |
| 93 |
* can be represented in a daddr (a power of two!). |
| 94 |
* In fact, we don't allow block number MAX_BLOCKS-1 to be |
| 95 |
* used because NOWHERE_DADDR and UNSAVED_CURLINE_DADDR (disp.c) must not |
| 96 |
* be valid disk references, and we want to prevent space overflow |
| 97 |
* from being undetected through arithmetic overflow. |
| 98 |
* |
| 99 |
* MAX_BLOCKS is based on the number of bits remaining in daddr to |
| 100 |
* represent it, after space for the offset (in chunks) and the |
| 101 |
* DDIRTY bit is accounted for. |
| 102 |
*/ |
| 103 |
#define MAX_BLOCKS ((daddr) 1 << (sizeof(daddr)*CHAR_BIT - LG_JBUFSIZ + LG_CHNK_CHARS - 1)) |
| 104 |
|
| 105 |
/* CHNK_CHARS is how big each chunk is. For each 1 the DFree pointer |
| 106 |
* is incremented we extend the tmp file by CHNK_CHARS characters. |
| 107 |
* BLK_CHNKS is the # of chunks per block. |
| 108 |
* |
| 109 |
* NOTE: It's pretty important that these numbers be powers of 2. |
| 110 |
* Be careful if you change things. |
| 111 |
*/ |
| 112 |
#define CHNK_CHARS ((daddr)1 << LG_CHNK_CHARS) |
| 113 |
#define REQ_CHNKS(chars) (((daddr)(chars) + (CHNK_CHARS - 1)) >> LG_CHNK_CHARS) |
| 114 |
#define BLK_CHNKS ((daddr)JBUFSIZ / CHNK_CHARS) |
| 115 |
#define blk_chop(addr) ((daddr)(addr) & ~(BLK_CHNKS - 1)) |
| 116 |
#define da_to_bno(addr) (((daddr)(addr) >> (LG_JBUFSIZ - LG_CHNK_CHARS)) & (MAX_BLOCKS - 1)) |
| 117 |
#define da_to_off(addr) (((daddr)(addr) << LG_CHNK_CHARS) & ((daddr)JBUFSIZ - 1)) |
| 118 |
#define bno_to_seek_off(bno) (((off_t)bno) << LG_JBUFSIZ) |