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) 26 * - file-chunk: chunk::file.length 27 */ 28 off_t offset; /* octets sent from this chunk */ 29 30 struct { 31 /* filechunk */ 32 off_t start; /* starting offset in the file */ 33 off_t length; /* octets to send from the starting offset */ 34 35 int fd; 36 int is_temp; /* file is temporary and will be deleted if on cleanup */ 37 struct { 38 char *start; /* the start pointer of the mmap'ed area */ 39 size_t length; /* size of the mmap'ed area */ 40 off_t offset; /* start is <n> octet away from the start of the file */ 41 } mmap; 42 } file; 43 } chunk; 44 45 typedef struct chunkqueue { 46 chunk *first; 47 chunk *last; 48 49 off_t bytes_in, bytes_out; 50 51 const array *tempdirs; 52 off_t upload_temp_file_size; 53 unsigned int tempdir_idx; 54 } chunkqueue; 55 56 __attribute_returns_nonnull__ 57 buffer * chunk_buffer_acquire(void); 58 59 void chunk_buffer_release(buffer *b); 60 61 void chunkqueue_chunk_pool_clear(void); 62 void chunkqueue_chunk_pool_free(void); 63 64 __attribute_returns_nonnull__ 65 chunkqueue *chunkqueue_init(void); 66 67 void chunkqueue_set_chunk_size (size_t sz); 68 void chunkqueue_set_tempdirs_default_reset (void); 69 void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_file_size); 70 void chunkqueue_set_tempdirs(chunkqueue * restrict cq, const array * restrict tempdirs, off_t upload_temp_file_size); 71 void chunkqueue_append_file(chunkqueue * restrict cq, const buffer * restrict fn, off_t offset, off_t len); /* copies "fn" */ 72 void chunkqueue_append_file_fd(chunkqueue * restrict cq, const buffer * restrict fn, int fd, off_t offset, off_t len); /* copies "fn" */ 73 void chunkqueue_append_mem(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */ 74 void chunkqueue_append_mem_min(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */ 75 void chunkqueue_append_buffer(chunkqueue * restrict cq, buffer * restrict mem); /* may reset "mem" */ 76 void chunkqueue_append_chunkqueue(chunkqueue * restrict cq, chunkqueue * restrict src); 77 78 __attribute_returns_nonnull__ 79 buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz); 80 81 __attribute_returns_nonnull__ 82 buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq); 83 84 void chunkqueue_prepend_buffer_commit(chunkqueue *cq); 85 86 __attribute_returns_nonnull__ 87 buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz); 88 89 __attribute_returns_nonnull__ 90 buffer * chunkqueue_append_buffer_open(chunkqueue *cq); 91 92 void chunkqueue_append_buffer_commit(chunkqueue *cq); 93 94 int chunkqueue_append_mem_to_tempfile(chunkqueue * restrict cq, const char * restrict mem, size_t len, struct log_error_st * const restrict errh); 95 96 /* functions to handle buffers to read into: */ 97 /* obtain/reserve memory in chunkqueue at least len (input) size, 98 * return pointer to memory with len (output) available for use 99 * modifying the chunkqueue invalidates the memory area. 100 * should always be followed by chunkqueue_get_memory(), 101 * even if nothing was read. 102 * pass 0 in len for mem at least half of chunk_buf_sz 103 */ 104 __attribute_returns_nonnull__ 105 char * chunkqueue_get_memory(chunkqueue * restrict cq, size_t * restrict len); 106 /* commit len bytes of mem obtained from chunkqueue_get_memory() */ 107 void chunkqueue_use_memory(chunkqueue * restrict cq, chunk *ckpt, size_t len); 108 void chunkqueue_update_file(chunkqueue * restrict cq, chunk *c, off_t len); 109 110 /* mark first "len" bytes as written (incrementing chunk offsets) 111 * and remove finished chunks 112 */ 113 void chunkqueue_mark_written(chunkqueue *cq, off_t len); 114 115 void chunkqueue_remove_finished_chunks(chunkqueue *cq); 116 117 void chunkqueue_steal(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len); 118 int chunkqueue_steal_with_tempfiles(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len, struct log_error_st * const restrict errh); 119 120 int chunkqueue_open_file_chunk(chunkqueue * restrict cq, struct log_error_st * const restrict errh); 121 122 void chunkqueue_compact_mem(chunkqueue *cq, size_t clen); 123 124 void chunkqueue_small_resp_optim (chunkqueue * restrict cq); 125 126 __attribute_pure__ 127 static inline off_t chunkqueue_length(const chunkqueue *cq); 128 static inline off_t chunkqueue_length(const chunkqueue *cq) { 129 return cq->bytes_in - cq->bytes_out; 130 } 131 132 void chunkqueue_free(chunkqueue *cq); 133 void chunkqueue_reset(chunkqueue *cq); 134 135 __attribute_pure__ 136 static inline int chunkqueue_is_empty(const chunkqueue *cq); 137 static inline int chunkqueue_is_empty(const chunkqueue *cq) { 138 return NULL == cq->first; 139 } 140 141 #endif 142