cache: Block-Based Least-Recently-Used (LRU) Disk Array Cache
Overview & Purpose
cache (cache.c / cache.h) implements a block-oriented, Least-Recently-Used (LRU) virtual memory disk cache for reading and writing large 2D byte arrays (such as sidescan sonar mosaics and satellite imagery) pixel-by-pixel without triggering operating-system page thrashing or exhausting physical RAM. Originally written by Guy Carpenter at James Cook University of North Queensland (JCUNQ) in February 1990 for the OMG sidescan mosaicking suite and integrated with JHC_header arrays by John Hughes Clarke in December 1991, it divides an on-disk image file (including an arbitrary header offset) into fixed 1,024-byte blocks and maintains up to 1,500 active blocks in memory using a doubly linked LRU list.
Key Constants & Data Structures
Cache Sizing Constants
#define CACHE_MAXBLOCKS 1500 /* Maximum number of 1 KB blocks held in memory (~1.5 MB per cache) */
#define CACHE_BLOCKSIZE 1024 /* Size of each cached file block in bytes */
cache Structure
typedef struct cache {
int x0, y0; /* Pixel coordinate origin offset (default 0, 0 at lower-left) */
int dx, dy; /* Array dimensions in pixels (width, height) */
FILE *file; /* Backing disk file stream */
int blocks; /* Total number of whole & partial 1024-byte blocks in file */
char **index; /* Array of pointers to in-memory block buffers (NULL if not resident) */
int *lrufwd; /* Forward links for doubly linked LRU block chain */
int *lruback; /* Backward links for doubly linked LRU block chain */
int lru; /* Block index of the Least Recently Used resident block */
int mru; /* Block index of the Most Recently Used resident block */
int lastblock; /* Block index of the final (partial) block */
int lastblocksize; /* Byte size of the final partial block */
int avail; /* Number of free memory block slots remaining (starts at CACHE_MAXBLOCKS) */
int header; /* Byte length of file header preceding pixel (0,0) (e.g., sizeof(JHC_header)) */
} cache;
Public Functions & Signatures
Lifecycle & Origin Management
cache *cache_open(FILE *file, int header, int dx, int dy)Initializes acachedescriptor for an openFILE *filewithheaderleading bytes and dimensionsdx$\times$dypixels. Computes the total block count(dx * dy + header + CACHE_BLOCKSIZE - 1) / CACHE_BLOCKSIZE, allocates theindex,lrufwd, andlrubacktables, and setsavail = CACHE_MAXBLOCKS.void cache_setorigin(cache *c, int x0, int y0)Sets the logical coordinate offset(x0, y0)subtracted from all subsequentcache_readandcache_writepixel coordinates.void cache_flush(cache *c)Writes all currently resident blocks inc->indexback toc->fileviacache_writeback().void cache_flush_clear(cache *c)Flushes all resident blocks to disk, frees their heap buffers, resetsc->avail, and clears the LRU chain (lru = mru = -1).void cache_close(cache *c)Callscache_flush_clear(c), closesc->file, and freesc->index,c->lrufwd,c->lruback, andc.
Pixel-Level Read & Write Operations
void cache_write(cache *c, int x, int y, char z)Checks if(x, y)lies within[x0..x0+dx-1, y0..y0+dy-1](cache_inrange). Computes the linear byte offsetlength = (x - x0) + (y - y0) * dx + header, resolves the block indexb = length / 1024and intra-block offsetd = length % 1024(cache_block), loads blockbinto memory viacache_load(c, b), and writes bytezatp[d].char cache_read(cache *c, int x, int y)Validates range viacache_inrange, resolves(b, d)viacache_block, loads blockbviacache_load(c, b), and returns bytep[d](or0if out of bounds).
Internal Block & LRU Chain Management
char *cache_load(cache *c, int b)Returnsc->index[b]immediately if blockbis already resident in memory. Otherwise, ifc->avail > 0, allocates a new 1,024-byte buffer; ifc->avail == 0, evicts the oldest block (cache_drop(c)), writes it to disk (cache_writeback), and reuses its buffer. Appendsbto the MRU end of the LRU chain (cache_addtolru), seeks tob * 1024(cache_seek), and reads the block from disk.char *cache_writeback(cache *c, int b)Seeks to blockbon disk, writes the buffer (lastblocksizebytes for the final block,1024bytes otherwise), clearsc->index[b] = 0, and returns the buffer pointer for reuse or freeing.int cache_drop(cache *c),void cache_promote(cache *c, int b),void cache_addtolru(cache *c, int b),void dump_lru(cache *c)Doubly linked list primitives maintaining the LRU/MRU block ordering.
How It Fits Into the OMG Architecture
cache is used by the OMG sidescan mosaicking tools (code/newmos/mos2.c and code/newmos/rasterCoords.c) as well as code/sdf/curray/s2_reader.c to allow arbitrary along-track and across-track sidescan ray-tracing writes into large output mosaic rasters on disk.