pages.h (1006B)
1 // managing paginated memory buffers 2 3 enum { 4 PageSize = 2048, 5 }; 6 7 typedef struct Page Page; 8 typedef struct Array Array; 9 typedef struct ArHeader ArHeader; 10 typedef struct ArSlice ArSlice; 11 12 struct ArHeader { 13 int ref; 14 long len; 15 }; 16 17 struct Array { 18 ArHeader; 19 char p[1]; 20 /* I don't think plan9 C allows 21 * zero-length arrays */ 22 }; 23 24 struct ArSlice { 25 Array *ar; 26 char *p; 27 long len, cap; 28 }; 29 30 struct Page { 31 ArSlice *as; 32 Page *prev; 33 Page *next; 34 }; 35 36 typedef struct PBuf PBuf; 37 struct PBuf { 38 Page *start; 39 Page *end; 40 vlong size; // how many bytes were allocated in total 41 vlong length; // how many bytes are used for storage 42 // should be count ≤ size 43 }; 44 45 long pbwrite(PBuf *pb, void *buf, long nbytes, vlong offset); 46 long pbread(PBuf *pb, void *buf, long nbytes, vlong offset); 47 48 Array * allocarray(long); 49 ArSlice * allocarslice(Array *, long, long); 50 51 Page * allocpage(void); 52 Page * duppage(Page *); 53 void freepage(Page *); 54 Page * addpage(PBuf *pb); 55 Page * splitpage(PBuf *pb, vlong offset);