[core] calloc plugin_config for consistent init
[lighttpd.git] / src / chunk.h
blob6751511f00d875695f60eafd8cd6843636e107fa
1 #ifndef _CHUNK_H_
2 #define _CHUNK_H_
3 #include "first.h"
5 #include "buffer.h"
6 #include "array.h"
8 typedef struct chunk {
9 enum { MEM_CHUNK, FILE_CHUNK } type;
11 buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */
13 struct {
14 /* filechunk */
15 buffer *name; /* name of the file */
16 off_t start; /* starting offset in the file */
17 off_t length; /* octets to send from the starting offset */
19 int fd;
20 struct {
21 char *start; /* the start pointer of the mmap'ed area */
22 size_t length; /* size of the mmap'ed area */
23 off_t offset; /* start is <n> octet away from the start of the file */
24 } mmap;
26 int is_temp; /* file is temporary and will be deleted if on cleanup */
27 } file;
29 /* the size of the chunk is either:
30 * - mem-chunk: buffer_string_length(chunk::mem)
31 * - file-chunk: chunk::file.length
33 off_t offset; /* octets sent from this chunk */
35 struct chunk *next;
36 } chunk;
38 typedef struct {
39 chunk *first;
40 chunk *last;
42 chunk *unused;
43 size_t unused_chunks;
45 off_t bytes_in, bytes_out;
47 array *tempdirs;
48 unsigned int upload_temp_file_size;
49 unsigned int tempdir_idx;
50 } chunkqueue;
52 chunkqueue *chunkqueue_init(void);
53 void chunkqueue_set_tempdirs_default_reset (void);
54 void chunkqueue_set_tempdirs_default (array *tempdirs, unsigned int upload_temp_file_size);
55 void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len); /* copies "fn" */
56 void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len); /* copies "fn" */
57 void chunkqueue_append_mem(chunkqueue *cq, const char *mem, size_t len); /* copies memory */
58 void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
59 void chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
60 void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src);
62 struct server; /*(declaration)*/
63 int chunkqueue_append_mem_to_tempfile(struct server *srv, chunkqueue *cq, const char *mem, size_t len);
65 /* functions to handle buffers to read into: */
66 /* return a pointer to a buffer in *mem with size *len;
67 * it should be at least min_size big, and use alloc_size if
68 * new memory is allocated.
69 * modifying the chunkqueue invalidates the memory area.
70 * should always be followed by chunkqueue_get_memory(),
71 * even if nothing was read.
72 * pass 0 for min_size/alloc_size for default values
74 void chunkqueue_get_memory(chunkqueue *cq, char **mem, size_t *len, size_t min_size, size_t alloc_size);
75 /* append first len bytes of the memory queried with
76 * chunkqueue_get_memory to the chunkqueue
78 void chunkqueue_use_memory(chunkqueue *cq, size_t len);
80 /* mark first "len" bytes as written (incrementing chunk offsets)
81 * and remove finished chunks
83 void chunkqueue_mark_written(chunkqueue *cq, off_t len);
85 void chunkqueue_remove_finished_chunks(chunkqueue *cq);
87 void chunkqueue_steal(chunkqueue *dest, chunkqueue *src, off_t len);
88 struct server;
89 int chunkqueue_steal_with_tempfiles(struct server *srv, chunkqueue *dest, chunkqueue *src, off_t len);
91 int chunkqueue_open_file_chunk(struct server *srv, chunkqueue *cq);
93 off_t chunkqueue_length(chunkqueue *cq);
94 void chunkqueue_free(chunkqueue *cq);
95 void chunkqueue_reset(chunkqueue *cq);
97 int chunkqueue_is_empty(chunkqueue *cq);
99 #endif