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