summaryrefslogtreecommitdiff
path: root/src/chunk.h
blob: c16d1da5cca4c086d52ca11c50141494734e48d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef _CHUNK_H_
#define _CHUNK_H_

#include "buffer.h"
#include "array.h"

typedef struct chunk {
	enum { UNUSED_CHUNK, MEM_CHUNK, FILE_CHUNK } type;

	buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */

	struct {
		/* filechunk */
		buffer *name; /* name of the file */
		off_t  start; /* starting offset in the file */
		off_t  length; /* octets to send from the starting offset */

		int    fd;
		struct {
			char   *start; /* the start pointer of the mmap'ed area */
			size_t length; /* size of the mmap'ed area */
			off_t  offset; /* start is <n> octets away from the start of the file */
		} mmap;

		int is_temp; /* file is temporary and will be deleted on cleanup */

		struct {
			int fd;
			off_t length;
			off_t offset;
		} copy;
	} file;

	off_t  offset; /* octets sent from this chunk
			  the size of the chunk is either
			  - mem-chunk: mem->used - 1
			  - file-chunk: file.length
			*/

	struct {
		off_t written;
		int ret_val;
	} async;

	struct chunk *next;
} chunk;

typedef struct {
	chunk *first;
	chunk *last;

	chunk *unused;
	size_t unused_chunks;

	array *tempdirs;

	int is_closed;   /* the input to this CQ is closed */

	off_t  bytes_in, bytes_out;
} chunkqueue;

LI_EXPORT chunkqueue* chunkqueue_init(void);
LI_EXPORT int chunkqueue_set_tempdirs(chunkqueue *c, array *tempdirs);
LI_EXPORT int chunkqueue_append_file(chunkqueue *c, buffer *fn, off_t offset, off_t len);
LI_EXPORT int chunkqueue_append_mem(chunkqueue *c, const char *mem, size_t len);
LI_EXPORT int chunkqueue_append_buffer(chunkqueue *c, buffer *mem);
LI_EXPORT int chunkqueue_prepend_buffer(chunkqueue *c, buffer *mem);

LI_EXPORT buffer * chunkqueue_get_append_buffer(chunkqueue *c);
LI_EXPORT buffer * chunkqueue_get_prepend_buffer(chunkqueue *c);
LI_EXPORT chunk * chunkqueue_get_append_tempfile(chunkqueue *cq);
LI_EXPORT int chunkqueue_steal_tempfile(chunkqueue *cq, chunk *in);
LI_EXPORT int chunkqueue_steal_chunk(chunkqueue *cq, chunk *c);
LI_EXPORT int chunkqueue_steal_chunks_len(chunkqueue *cq, chunk *c, size_t max_len);
LI_EXPORT int chunkqueue_steal_all_chunks(chunkqueue *cq, chunkqueue *in);
LI_EXPORT int chunkqueue_skip(chunkqueue *cq, off_t skip);
LI_EXPORT void chunkqueue_remove_empty_last_chunk(chunkqueue *cq);

LI_EXPORT int chunkqueue_remove_finished_chunks(chunkqueue *cq);

LI_EXPORT off_t chunkqueue_length(chunkqueue *c);
LI_EXPORT off_t chunkqueue_written(chunkqueue *c);
LI_EXPORT void chunkqueue_free(chunkqueue *c);
LI_EXPORT void chunkqueue_reset(chunkqueue *c);

LI_EXPORT int chunkqueue_is_empty(chunkqueue *c);

LI_EXPORT void chunkqueue_print(chunkqueue *cq);

#endif