xref: /lighttpd1.4/src/chunk.h (revision fb7e5a78)
1 #ifndef _CHUNK_H_
2 #define _CHUNK_H_
3 #include "first.h"
4 
5 #ifdef _AIX  /*(AIX might #define mmap mmap64)*/
6 #include "sys-mmap.h"
7 #endif
8 
9 #include "buffer.h"
10 #include "array.h"
11 
12 /* both should be way smaller than SSIZE_MAX :) */
13 #define MAX_READ_LIMIT  (256*1024)
14 #define MAX_WRITE_LIMIT (256*1024)
15 
16 struct log_error_st;    /*(declaration)*/
17 
18 typedef struct chunk {
19 	struct chunk *next;
20 	enum { MEM_CHUNK, FILE_CHUNK } type;
21 
22 	buffer *mem; /* either the storage of the mem-chunk or the name of the file */
23 
24 	/* the size of the chunk is either:
25 	 * - mem-chunk: buffer_string_length(chunk::mem) - c->offset
26 	 * - file-chunk: chunk::file.length - c->offset
27 	 */
28 	off_t offset;
29 
30 	struct {
31 		/* filechunk */
32 		off_t  length; /* end pos + 1 in file (octets to send: file.length - c->offset) */
33 
34 		int    fd;
35 		int is_temp; /* file is temporary and will be deleted if on cleanup */
36 		struct {
37 			char   *start; /* the start pointer of the mmap'ed area */
38 			size_t length; /* size of the mmap'ed area */
39 			off_t  offset; /* start is <n> octet away from the start of the file */
40 		} mmap;
41 		void *ref;
42 		void(*refchg)(void *, int);
43 	} file;
44 } chunk;
45 
46 typedef struct chunkqueue {
47 	chunk *first;
48 	chunk *last;
49 
50 	off_t bytes_in, bytes_out;
51 
52 	const array *tempdirs;
53 	off_t upload_temp_file_size;
54 	unsigned int tempdir_idx;
55 } chunkqueue;
56 
57 __attribute_returns_nonnull__
58 buffer * chunk_buffer_acquire(void);
59 
60 void chunk_buffer_release(buffer *b);
61 
62 void chunkqueue_chunk_pool_clear(void);
63 void chunkqueue_chunk_pool_free(void);
64 
65 __attribute_returns_nonnull__
66 chunkqueue *chunkqueue_init(chunkqueue *cq);
67 
68 void chunkqueue_set_chunk_size (size_t sz);
69 void chunkqueue_set_tempdirs_default_reset (void);
70 void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_file_size);
71 void chunkqueue_set_tempdirs(chunkqueue * restrict cq, const array * restrict tempdirs, off_t upload_temp_file_size);
72 void chunkqueue_append_file(chunkqueue * restrict cq, const buffer * restrict fn, off_t offset, off_t len); /* copies "fn" */
73 void chunkqueue_append_file_fd(chunkqueue * restrict cq, const buffer * restrict fn, int fd, off_t offset, off_t len); /* copies "fn" */
74 void chunkqueue_append_mem(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
75 void chunkqueue_append_mem_min(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
76 void chunkqueue_append_buffer(chunkqueue * restrict cq, buffer * restrict mem); /* may reset "mem" */
77 void chunkqueue_append_chunkqueue(chunkqueue * restrict cq, chunkqueue * restrict src);
78 
79 __attribute_returns_nonnull__
80 buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz);
81 
82 __attribute_returns_nonnull__
83 buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq);
84 
85 void chunkqueue_prepend_buffer_commit(chunkqueue *cq);
86 
87 __attribute_returns_nonnull__
88 buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz);
89 
90 __attribute_returns_nonnull__
91 buffer * chunkqueue_append_buffer_open(chunkqueue *cq);
92 
93 void chunkqueue_append_buffer_commit(chunkqueue *cq);
94 
95 int chunkqueue_append_mem_to_tempfile(chunkqueue * restrict cq, const char * restrict mem, size_t len, struct log_error_st * const restrict errh);
96 
97 /* functions to handle buffers to read into: */
98 /* obtain/reserve memory in chunkqueue at least len (input) size,
99  * return pointer to memory with len (output) available for use
100  * modifying the chunkqueue invalidates the memory area.
101  * should always be followed by chunkqueue_get_memory(),
102  *  even if nothing was read.
103  * pass 0 in len for mem at least half of chunk_buf_sz
104  */
105 __attribute_returns_nonnull__
106 char * chunkqueue_get_memory(chunkqueue * restrict cq, size_t * restrict len);
107 /* commit len bytes of mem obtained from chunkqueue_get_memory() */
108 void chunkqueue_use_memory(chunkqueue * restrict cq, chunk *ckpt, size_t len);
109 void chunkqueue_update_file(chunkqueue * restrict cq, chunk *c, off_t len);
110 
111 /* mark first "len" bytes as written (incrementing chunk offsets)
112  * and remove finished chunks
113  */
114 void chunkqueue_mark_written(chunkqueue *cq, off_t len);
115 
116 void chunkqueue_remove_finished_chunks(chunkqueue *cq);
117 
118 void chunkqueue_steal(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len);
119 int chunkqueue_steal_with_tempfiles(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len, struct log_error_st * const restrict errh);
120 
121 int chunkqueue_open_file_chunk(chunkqueue * restrict cq, struct log_error_st * const restrict errh);
122 
123 void chunkqueue_compact_mem_offset(chunkqueue *cq);
124 void chunkqueue_compact_mem(chunkqueue *cq, size_t clen);
125 
126 void chunkqueue_small_resp_optim (chunkqueue * restrict cq);
127 
128 int chunkqueue_peek_data (chunkqueue *cq, char **data, uint32_t *dlen, struct log_error_st * restrict errh);
129 int chunkqueue_read_data (chunkqueue *cq, char *data, uint32_t dlen, struct log_error_st * restrict errh);
130 
131 buffer * chunkqueue_read_squash (chunkqueue * restrict cq, struct log_error_st * restrict errh);
132 
133 __attribute_pure__
134 static inline off_t chunkqueue_length(const chunkqueue *cq);
135 static inline off_t chunkqueue_length(const chunkqueue *cq) {
136 	return cq->bytes_in - cq->bytes_out;
137 }
138 
139 __attribute_cold__
140 void chunkqueue_free(chunkqueue *cq);
141 
142 void chunkqueue_reset(chunkqueue *cq);
143 
144 __attribute_pure__
145 static inline int chunkqueue_is_empty(const chunkqueue *cq);
146 static inline int chunkqueue_is_empty(const chunkqueue *cq) {
147 	return NULL == cq->first;
148 }
149 
150 #endif
151