xref: /plan9/sys/src/cmd/gs/src/gxclmem.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gxclmem.h,v 1.5 2002/06/16 08:45:43 lpd Exp $ */
18 /* Definitions and declarations for clist implementation in memory. */
19 
20 #ifndef gxclmem_INCLUDED
21 #  define gxclmem_INCLUDED
22 
23 #include "gxclio.h"		/* defines interface */
24 #include "strimpl.h"		/* stream structures      */
25 
26 /*
27  * The best values of MEMFILE_DATA_SIZE are slightly less than a power of 2,
28  * to allow typical malloc implementations to allocate in units of a power
29  * of 2 rather than having to go slightly over.
30  */
31 #define MEMFILE_DATA_SIZE	(16384 - 160)
32 
33    /*   ============================================================    */
34    /*                                                                   */
35    /*   Memfile structure definitions.                                  */
36    /*                                                                   */
37    /*   The PHYS structures are the elements actually allocated in      */
38    /*   RAM, containing the compressed data (or optionally raw data)    */
39    /*                                                                   */
40    /*   There can be several LOG (logical) elements per physical        */
41    /*   element, depending on the compression. The MEMFILE pdata        */
42    /*   item always points into a raw block of data.                    */
43    /*                                                                   */
44    /*   ============================================================    */
45 
46 typedef struct RAW_BUFFER {
47     struct RAW_BUFFER *fwd, *back;
48     struct LOG_MEMFILE_BLK *log_blk;
49     char data[MEMFILE_DATA_SIZE];
50 } RAW_BUFFER;
51 
52 typedef struct PHYS_MEMFILE_BLK {
53     struct PHYS_MEMFILE_BLK *link;
54     char *data_limit;		/* end of data when compressed  */
55     /* NULL if not compressed       */
56     char data_spare[4];		/* used during de-compress      */
57     char data[MEMFILE_DATA_SIZE];
58 } PHYS_MEMFILE_BLK;
59 
60 typedef struct LOG_MEMFILE_BLK {
61     struct LOG_MEMFILE_BLK *link;
62     PHYS_MEMFILE_BLK *phys_blk;
63     char *phys_pdata;
64     RAW_BUFFER *raw_block;	/* or NULL */
65 } LOG_MEMFILE_BLK;
66 
67 typedef struct MEMFILE {
68     gs_memory_t *memory;	/* storage allocator */
69     gs_memory_t *data_memory;	/* storage allocator for data */
70     bool ok_to_compress;	/* if true, OK to compress this file */
71 	/*
72 	 * Reserve memory blocks: these are used to guarantee that a
73 	 * given-sized write (or sequence of writes) will always succeed.
74 	 * More specifically, the guarantee is that N bytes can successfully
75 	 * be written after a low-memory warning is first returned by
76 	 * fwrite.  The reserve of N bytes for a given file is (re)allocated
77 	 * by a successful call to memfile_set_memory_warning(N).  Fwrite
78 	 * allocates memory only from the reserve when its normal allocation
79 	 * attempts fail; in such cases, it allocates blocks from the
80 	 * reserve pool as needed and completes normally, but returns a
81 	 * low-memory warning status. Once a low-memory warning has been
82 	 * returned, fwrite will continue to attempt to allocate memory from
83 	 * the usual allocator on subsequent fwrites, but does *not* try to
84 	 * "top up" the reserve if becomes available -- only an explicit
85 	 * memfile_set_memory_warning does so.
86 	 */
87     PHYS_MEMFILE_BLK *reservePhysBlockChain;  /* chain of reserve phys blks */
88     int reservePhysBlockCount; 	/* count of entries on reservePhysBlockChain */
89     LOG_MEMFILE_BLK *reserveLogBlockChain;	/* chain of reserve log blks */
90     int reserveLogBlockCount; 	/* count of entries on reserveLogBlockChain */
91     /* logical file properties */
92     LOG_MEMFILE_BLK *log_head;
93     LOG_MEMFILE_BLK *log_curr_blk;
94     long log_length;		/* updated during write             */
95     long log_curr_pos;		/* updated during seek, close, read */
96     char *pdata;		/* raw data */
97     char *pdata_end;
98     /* physical file properties */
99     long total_space;		/* so we know when to start compress */
100     PHYS_MEMFILE_BLK *phys_curr;	/* NULL if not compressing      */
101     RAW_BUFFER *raw_head, *raw_tail;
102     int error_code;		/* used by CLIST_ferror         */
103     stream_cursor_read rd;	/* use .ptr, .limit */
104     stream_cursor_write wt;	/* use .ptr, .limit */
105     bool compressor_initialized;
106     stream_state *compress_state;
107     stream_state *decompress_state;
108 } MEMFILE;
109 
110 /*
111  * Only the MEMFILE and stream_state structures are GC-compatible, so we
112  * allocate all the other structures on the C heap.
113  */
114 
115 #define private_st_MEMFILE()	/* in gxclmem.c */\
116   gs_private_st_ptrs2(st_MEMFILE, MEMFILE, "MEMFILE",\
117     MEMFILE_enum_ptrs, MEMFILE_reloc_ptrs, compress_state, decompress_state)
118 
119 /* Make the memfile_... operations aliases for the clist_... operations. */
120 
121 #define memfile_fopen(fname, fmode, pcf, mem, data_mem, compress)\
122   clist_fopen(fname, fmode, pcf, mem, data_mem, compress)
123 #define memfile_fclose(cf, fname, delete)\
124   clist_fclose(cf, fname, delete)
125 #define memfile_unlink(fname)\
126   clist_unlink(fname)
127 
128 #define memfile_space_available(req)\
129   clist_space_available(req)
130 #define memfile_fwrite_chars(data, len, cf)\
131   clist_fwrite_chars(data, len, cf)
132 
133 #define memfile_fread_chars(data, len, cf)\
134   clist_fread_chars(data, len, cf)
135 
136 #define memfile_set_memory_warning(cf, nbytes) clist_set_memory_warning(cf, nbytes)
137 #define memfile_ferror_code(cf) clist_ferror_code(cf)
138 #define memfile_ftell(cf) clist_ftell(cf)
139 #define memfile_rewind(cf, discard, fname) clist_rewind(cf, discard, fname)
140 #define memfile_fseek(cf, offset, mode, fname) clist_fseek(cf, offset, mode, fname)
141 
142 /* Declare the procedures for returning the prototype filter states */
143 /* for compressing and decompressing the band list. */
144 const stream_state *clist_compressor_state(void *);
145 const stream_state *clist_decompressor_state(void *);
146 
147 #endif /* gxclmem_INCLUDED */
148