1 #ifndef _CHUNK_H_
2 #define _CHUNK_H_
3 
4 #include "buffer.h"
5 #include "array.h"
6 #include "sys-mmap.h"
7 
8 typedef struct chunk {
9 	enum { UNUSED_CHUNK, MEM_CHUNK, FILE_CHUNK } type;
10 
11 	buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */
12 
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 */
18 
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;
25 
26 		int is_temp; /* file is temporary and will be deleted if on cleanup */
27 	} file;
28 
29 	off_t  offset; /* octets sent from this chunk
30 			  the size of the chunk is either
31 			  - mem-chunk: mem->used - 1
32 			  - file-chunk: file.length
33 			*/
34 
35 	struct chunk *next;
36 } chunk;
37 
38 typedef struct {
39 	chunk *first;
40 	chunk *last;
41 
42 	chunk *unused;
43 	size_t unused_chunks;
44 
45 	array *tempdirs;
46 
47 	off_t  bytes_in, bytes_out;
48 } chunkqueue;
49 
50 chunkqueue *chunkqueue_init(void);
51 int chunkqueue_set_tempdirs(chunkqueue *c, array *tempdirs);
52 int chunkqueue_append_file(chunkqueue *c, buffer *fn, off_t offset, off_t len);
53 int chunkqueue_append_mem(chunkqueue *c, const char *mem, size_t len);
54 int chunkqueue_append_buffer(chunkqueue *c, buffer *mem);
55 int chunkqueue_append_buffer_weak(chunkqueue *c, buffer *mem);
56 int chunkqueue_prepend_buffer(chunkqueue *c, buffer *mem);
57 
58 buffer * chunkqueue_get_append_buffer(chunkqueue *c);
59 buffer * chunkqueue_get_prepend_buffer(chunkqueue *c);
60 chunk * chunkqueue_get_append_tempfile(chunkqueue *cq);
61 
62 int chunkqueue_remove_finished_chunks(chunkqueue *cq);
63 
64 off_t chunkqueue_length(chunkqueue *c);
65 off_t chunkqueue_written(chunkqueue *c);
66 void chunkqueue_free(chunkqueue *c);
67 void chunkqueue_reset(chunkqueue *c);
68 
69 int chunkqueue_is_empty(chunkqueue *c);
70 
71 #endif
72