use buffer_string_set_length() to truncate strings
[lighttpd.git] / src / chunk.h
blob98fad2efdd73fdcfdca7329cb3a5861cbf5d6c76
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(chunkqueue *cq, array *tempdirs, unsigned int upload_temp_file_size);
54 void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len); /* copies "fn" */
55 void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len); /* copies "fn" */
56 void chunkqueue_append_mem(chunkqueue *cq, const char *mem, size_t len); /* copies memory */
57 void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
58 void chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
60 /* functions to handle buffers to read into: */
61 /* return a pointer to a buffer in *mem with size *len;
62 * it should be at least min_size big, and use alloc_size if
63 * new memory is allocated.
64 * modifying the chunkqueue invalidates the memory area.
65 * should always be followed by chunkqueue_get_memory(),
66 * even if nothing was read.
67 * pass 0 for min_size/alloc_size for default values
69 void chunkqueue_get_memory(chunkqueue *cq, char **mem, size_t *len, size_t min_size, size_t alloc_size);
70 /* append first len bytes of the memory queried with
71 * chunkqueue_get_memory to the chunkqueue
73 void chunkqueue_use_memory(chunkqueue *cq, size_t len);
75 /* mark first "len" bytes as written (incrementing chunk offsets)
76 * and remove finished chunks
78 void chunkqueue_mark_written(chunkqueue *cq, off_t len);
80 void chunkqueue_remove_finished_chunks(chunkqueue *cq);
82 void chunkqueue_steal(chunkqueue *dest, chunkqueue *src, off_t len);
83 struct server;
84 int chunkqueue_steal_with_tempfiles(struct server *srv, chunkqueue *dest, chunkqueue *src, off_t len);
86 off_t chunkqueue_length(chunkqueue *cq);
87 void chunkqueue_free(chunkqueue *cq);
88 void chunkqueue_reset(chunkqueue *cq);
90 int chunkqueue_is_empty(chunkqueue *cq);
92 #endif