|
Apache Portable Runtime
|
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 /** 00017 * @file apr_buckets.h 00018 * @brief APR-UTIL Buckets/Bucket Brigades 00019 */ 00020 00021 #ifndef APR_BUCKETS_H 00022 #define APR_BUCKETS_H 00023 00024 #if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG) 00025 #define APR_RING_DEBUG 00026 #endif 00027 00028 #include "apu.h" 00029 #include "apr_network_io.h" 00030 #include "apr_file_io.h" 00031 #include "apr_general.h" 00032 #include "apr_mmap.h" 00033 #include "apr_errno.h" 00034 #include "apr_ring.h" 00035 #include "apr.h" 00036 #if APR_HAVE_SYS_UIO_H 00037 #include <sys/uio.h> /* for struct iovec */ 00038 #endif 00039 #if APR_HAVE_STDARG_H 00040 #include <stdarg.h> 00041 #endif 00042 00043 #ifdef __cplusplus 00044 extern "C" { 00045 #endif 00046 00047 /** 00048 * @defgroup APR_Util_Bucket_Brigades Bucket Brigades 00049 * @ingroup APR 00050 * @{ 00051 */ 00052 00053 /** default bucket buffer size - 8KB minus room for memory allocator headers */ 00054 #define APR_BUCKET_BUFF_SIZE 8000 00055 00056 /** Determines how a bucket or brigade should be read */ 00057 typedef enum { 00058 APR_BLOCK_READ, /**< block until data becomes available */ 00059 APR_NONBLOCK_READ /**< return immediately if no data is available */ 00060 } apr_read_type_e; 00061 00062 /** 00063 * The one-sentence buzzword-laden overview: Bucket brigades represent 00064 * a complex data stream that can be passed through a layered IO 00065 * system without unnecessary copying. A longer overview follows... 00066 * 00067 * A bucket brigade is a doubly linked list (ring) of buckets, so we 00068 * aren't limited to inserting at the front and removing at the end. 00069 * Buckets are only passed around as members of a brigade, although 00070 * singleton buckets can occur for short periods of time. 00071 * 00072 * Buckets are data stores of various types. They can refer to data in 00073 * memory, or part of a file or mmap area, or the output of a process, 00074 * etc. Buckets also have some type-dependent accessor functions: 00075 * read, split, copy, setaside, and destroy. 00076 * 00077 * read returns the address and size of the data in the bucket. If the 00078 * data isn't in memory then it is read in and the bucket changes type 00079 * so that it can refer to the new location of the data. If all the 00080 * data doesn't fit in the bucket then a new bucket is inserted into 00081 * the brigade to hold the rest of it. 00082 * 00083 * split divides the data in a bucket into two regions. After a split 00084 * the original bucket refers to the first part of the data and a new 00085 * bucket inserted into the brigade after the original bucket refers 00086 * to the second part of the data. Reference counts are maintained as 00087 * necessary. 00088 * 00089 * setaside ensures that the data in the bucket has a long enough 00090 * lifetime. Sometimes it is convenient to create a bucket referring 00091 * to data on the stack in the expectation that it will be consumed 00092 * (output to the network) before the stack is unwound. If that 00093 * expectation turns out not to be valid, the setaside function is 00094 * called to move the data somewhere safer. 00095 * 00096 * copy makes a duplicate of the bucket structure as long as it's 00097 * possible to have multiple references to a single copy of the 00098 * data itself. Not all bucket types can be copied. 00099 * 00100 * destroy maintains the reference counts on the resources used by a 00101 * bucket and frees them if necessary. 00102 * 00103 * Note: all of the above functions have wrapper macros (apr_bucket_read(), 00104 * apr_bucket_destroy(), etc), and those macros should be used rather 00105 * than using the function pointers directly. 00106 * 00107 * To write a bucket brigade, they are first made into an iovec, so that we 00108 * don't write too little data at one time. Currently we ignore compacting the 00109 * buckets into as few buckets as possible, but if we really want good 00110 * performance, then we need to compact the buckets before we convert to an 00111 * iovec, or possibly while we are converting to an iovec. 00112 */ 00113 00114 /* 00115 * Forward declaration of the main types. 00116 */ 00117 00118 /** @see apr_bucket_brigade */ 00119 typedef struct apr_bucket_brigade apr_bucket_brigade; 00120 /** @see apr_bucket */ 00121 typedef struct apr_bucket apr_bucket; 00122 /** @see apr_bucket_alloc_t */ 00123 typedef struct apr_bucket_alloc_t apr_bucket_alloc_t; 00124 00125 /** @see apr_bucket_type_t */ 00126 typedef struct apr_bucket_type_t apr_bucket_type_t; 00127 00128 /** 00129 * Basic bucket type 00130 */ 00131 struct apr_bucket_type_t { 00132 /** 00133 * The name of the bucket type 00134 */ 00135 const char *name; 00136 /** 00137 * The number of functions this bucket understands. Can not be less than 00138 * five. 00139 */ 00140 int num_func; 00141 /** 00142 * Whether the bucket contains metadata (ie, information that 00143 * describes the regular contents of the brigade). The metadata 00144 * is not returned by apr_bucket_read() and is not indicated by 00145 * the ->length of the apr_bucket itself. In other words, an 00146 * empty bucket is safe to arbitrarily remove if and only if it 00147 * contains no metadata. In this sense, "data" is just raw bytes 00148 * that are the "content" of the brigade and "metadata" describes 00149 * that data but is not a proper part of it. 00150 */ 00151 enum { 00152 /** This bucket type represents actual data to send to the client. */ 00153 APR_BUCKET_DATA = 0, 00154 /** This bucket type represents metadata. */ 00155 APR_BUCKET_METADATA = 1 00156 } is_metadata; 00157 /** 00158 * Free the private data and any resources used by the bucket (if they 00159 * aren't shared with another bucket). This function is required to be 00160 * implemented for all bucket types, though it might be a no-op on some 00161 * of them (namely ones that never allocate any private data structures). 00162 * @param data The private data pointer from the bucket to be destroyed 00163 */ 00164 void (*destroy)(void *data); 00165 00166 /** 00167 * Read the data from the bucket. This is required to be implemented 00168 * for all bucket types. 00169 * @param b The bucket to read from 00170 * @param str A place to store the data read. Allocation should only be 00171 * done if absolutely necessary. 00172 * @param len The amount of data read. 00173 * @param block Should this read function block if there is more data that 00174 * cannot be read immediately. 00175 */ 00176 apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len, 00177 apr_read_type_e block); 00178 00179 /** 00180 * Make it possible to set aside the data for at least as long as the 00181 * given pool. Buckets containing data that could potentially die before 00182 * this pool (e.g. the data resides on the stack, in a child pool of 00183 * the given pool, or in a disjoint pool) must somehow copy, shift, or 00184 * transform the data to have the proper lifetime. 00185 * @param e The bucket to convert 00186 * @remark Some bucket types contain data that will always outlive the 00187 * bucket itself. For example no data (EOS and FLUSH), or the data 00188 * resides in global, constant memory (IMMORTAL), or the data is on 00189 * the heap (HEAP). For these buckets, apr_bucket_setaside_noop can 00190 * be used. 00191 */ 00192 apr_status_t (*setaside)(apr_bucket *e, apr_pool_t *pool); 00193 00194 /** 00195 * Split one bucket in two at the specified position by duplicating 00196 * the bucket structure (not the data) and modifying any necessary 00197 * start/end/offset information. If it's not possible to do this 00198 * for the bucket type (perhaps the length of the data is indeterminate, 00199 * as with pipe and socket buckets), then APR_ENOTIMPL is returned. 00200 * @param e The bucket to split 00201 * @param point The offset of the first byte in the new bucket 00202 */ 00203 apr_status_t (*split)(apr_bucket *e, apr_size_t point); 00204 00205 /** 00206 * Copy the bucket structure (not the data), assuming that this is 00207 * possible for the bucket type. If it's not, APR_ENOTIMPL is returned. 00208 * @param e The bucket to copy 00209 * @param c Returns a pointer to the new bucket 00210 */ 00211 apr_status_t (*copy)(apr_bucket *e, apr_bucket **c); 00212 00213 }; 00214 00215 /** 00216 * apr_bucket structures are allocated on the malloc() heap and 00217 * their lifetime is controlled by the parent apr_bucket_brigade 00218 * structure. Buckets can move from one brigade to another e.g. by 00219 * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has 00220 * the same lifetime as the bucket and is freed when the bucket is 00221 * destroyed; if the data is shared by more than one bucket (e.g. 00222 * after a split) the data is freed when the last bucket goes away. 00223 */ 00224 struct apr_bucket { 00225 /** Links to the rest of the brigade */ 00226 APR_RING_ENTRY(apr_bucket) link; 00227 /** The type of bucket. */ 00228 const apr_bucket_type_t *type; 00229 /** The length of the data in the bucket. This could have been implemented 00230 * with a function, but this is an optimization, because the most 00231 * common thing to do will be to get the length. If the length is unknown, 00232 * the value of this field will be (apr_size_t)(-1). 00233 */ 00234 apr_size_t length; 00235 /** The start of the data in the bucket relative to the private base 00236 * pointer. The vast majority of bucket types allow a fixed block of 00237 * data to be referenced by multiple buckets, each bucket pointing to 00238 * a different segment of the data. That segment starts at base+start 00239 * and ends at base+start+length. 00240 * If the length == (apr_size_t)(-1), then start == -1. 00241 */ 00242 apr_off_t start; 00243 /** type-dependent data hangs off this pointer */ 00244 void *data; 00245 /** 00246 * Pointer to function used to free the bucket. This function should 00247 * always be defined and it should be consistent with the memory 00248 * function used to allocate the bucket. For example, if malloc() is 00249 * used to allocate the bucket, this pointer should point to free(). 00250 * @param e Pointer to the bucket being freed 00251 */ 00252 void (*free)(void *e); 00253 /** The freelist from which this bucket was allocated */ 00254 apr_bucket_alloc_t *list; 00255 }; 00256 00257 /** A list of buckets */ 00258 struct apr_bucket_brigade { 00259 /** The pool to associate the brigade with. The data is not allocated out 00260 * of the pool, but a cleanup is registered with this pool. If the 00261 * brigade is destroyed by some mechanism other than pool destruction, 00262 * the destroying function is responsible for killing the cleanup. 00263 */ 00264 apr_pool_t *p; 00265 /** The buckets in the brigade are on this list. */ 00266 /* 00267 * The apr_bucket_list structure doesn't actually need a name tag 00268 * because it has no existence independent of struct apr_bucket_brigade; 00269 * the ring macros are designed so that you can leave the name tag 00270 * argument empty in this situation but apparently the Windows compiler 00271 * doesn't like that. 00272 */ 00273 APR_RING_HEAD(apr_bucket_list, apr_bucket) list; 00274 /** The freelist from which this bucket was allocated */ 00275 apr_bucket_alloc_t *bucket_alloc; 00276 }; 00277 00278 00279 /** 00280 * Function called when a brigade should be flushed 00281 */ 00282 typedef apr_status_t (*apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx); 00283 00284 /* 00285 * define APR_BUCKET_DEBUG if you want your brigades to be checked for 00286 * validity at every possible instant. this will slow your code down 00287 * substantially but is a very useful debugging tool. 00288 */ 00289 #ifdef APR_BUCKET_DEBUG 00290 00291 #define APR_BRIGADE_CHECK_CONSISTENCY(b) do { \ 00292 APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link); \ 00293 assert(b->p != NULL); \ 00294 assert(b->bucket_alloc != NULL); \ 00295 } while (0) 00296 00297 #define APR_BUCKET_CHECK_CONSISTENCY(e) \ 00298 APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link) 00299 00300 #else 00301 /** 00302 * checks the ring pointers in a bucket brigade for consistency. an 00303 * abort() will be triggered if any inconsistencies are found. 00304 * note: this is a no-op unless APR_BUCKET_DEBUG is defined. 00305 * @param b The brigade 00306 */ 00307 #define APR_BRIGADE_CHECK_CONSISTENCY(b) 00308 /** 00309 * checks the brigade a bucket is in for ring consistency. an 00310 * abort() will be triggered if any inconsistencies are found. 00311 * note: this is a no-op unless APR_BUCKET_DEBUG is defined. 00312 * @param e The bucket 00313 */ 00314 #define APR_BUCKET_CHECK_CONSISTENCY(e) 00315 #endif 00316 00317 00318 /** 00319 * Wrappers around the RING macros to reduce the verbosity of the code 00320 * that handles bucket brigades. 00321 */ 00322 /** 00323 * The magic pointer value that indicates the head of the brigade 00324 * @remark This is used to find the beginning and end of the brigade, eg: 00325 * <pre> 00326 * while (e != APR_BRIGADE_SENTINEL(b)) { 00327 * ... 00328 * e = APR_BUCKET_NEXT(e); 00329 * } 00330 * </pre> 00331 * @param b The brigade 00332 * @return The magic pointer value 00333 */ 00334 #define APR_BRIGADE_SENTINEL(b) APR_RING_SENTINEL(&(b)->list, apr_bucket, link) 00335 00336 /** 00337 * Determine if the bucket brigade is empty 00338 * @param b The brigade to check 00339 * @return true or false 00340 */ 00341 #define APR_BRIGADE_EMPTY(b) APR_RING_EMPTY(&(b)->list, apr_bucket, link) 00342 00343 /** 00344 * Return the first bucket in a brigade 00345 * @param b The brigade to query 00346 * @return The first bucket in the brigade 00347 */ 00348 #define APR_BRIGADE_FIRST(b) APR_RING_FIRST(&(b)->list) 00349 /** 00350 * Return the last bucket in a brigade 00351 * @param b The brigade to query 00352 * @return The last bucket in the brigade 00353 */ 00354 #define APR_BRIGADE_LAST(b) APR_RING_LAST(&(b)->list) 00355 00356 /** 00357 * Insert a list of buckets at the front of a brigade 00358 * @param b The brigade to add to 00359 * @param e The first bucket in a list of buckets to insert 00360 */ 00361 #define APR_BRIGADE_INSERT_HEAD(b, e) do { \ 00362 apr_bucket *ap__b = (e); \ 00363 APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link); \ 00364 APR_BRIGADE_CHECK_CONSISTENCY((b)); \ 00365 } while (0) 00366 00367 /** 00368 * Insert a list of buckets at the end of a brigade 00369 * @param b The brigade to add to 00370 * @param e The first bucket in a list of buckets to insert 00371 */ 00372 #define APR_BRIGADE_INSERT_TAIL(b, e) do { \ 00373 apr_bucket *ap__b = (e); \ 00374 APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link); \ 00375 APR_BRIGADE_CHECK_CONSISTENCY((b)); \ 00376 } while (0) 00377 00378 /** 00379 * Concatenate brigade b onto the end of brigade a, leaving brigade b empty 00380 * @param a The first brigade 00381 * @param b The second brigade 00382 */ 00383 #define APR_BRIGADE_CONCAT(a, b) do { \ 00384 APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link); \ 00385 APR_BRIGADE_CHECK_CONSISTENCY((a)); \ 00386 } while (0) 00387 00388 /** 00389 * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty 00390 * @param a The first brigade 00391 * @param b The second brigade 00392 */ 00393 #define APR_BRIGADE_PREPEND(a, b) do { \ 00394 APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link); \ 00395 APR_BRIGADE_CHECK_CONSISTENCY((a)); \ 00396 } while (0) 00397 00398 /** 00399 * Insert a list of buckets before a specified bucket 00400 * @param a The bucket to insert before 00401 * @param b The buckets to insert 00402 */ 00403 #define APR_BUCKET_INSERT_BEFORE(a, b) do { \ 00404 apr_bucket *ap__a = (a), *ap__b = (b); \ 00405 APR_RING_INSERT_BEFORE(ap__a, ap__b, link); \ 00406 APR_BUCKET_CHECK_CONSISTENCY(ap__a); \ 00407 } while (0) 00408 00409 /** 00410 * Insert a list of buckets after a specified bucket 00411 * @param a The bucket to insert after 00412 * @param b The buckets to insert 00413 */ 00414 #define APR_BUCKET_INSERT_AFTER(a, b) do { \ 00415 apr_bucket *ap__a = (a), *ap__b = (b); \ 00416 APR_RING_INSERT_AFTER(ap__a, ap__b, link); \ 00417 APR_BUCKET_CHECK_CONSISTENCY(ap__a); \ 00418 } while (0) 00419 00420 /** 00421 * Get the next bucket in the list 00422 * @param e The current bucket 00423 * @return The next bucket 00424 */ 00425 #define APR_BUCKET_NEXT(e) APR_RING_NEXT((e), link) 00426 /** 00427 * Get the previous bucket in the list 00428 * @param e The current bucket 00429 * @return The previous bucket 00430 */ 00431 #define APR_BUCKET_PREV(e) APR_RING_PREV((e), link) 00432 00433 /** 00434 * Remove a bucket from its bucket brigade 00435 * @param e The bucket to remove 00436 */ 00437 #define APR_BUCKET_REMOVE(e) APR_RING_REMOVE((e), link) 00438 00439 /** 00440 * Initialize a new bucket's prev/next pointers 00441 * @param e The bucket to initialize 00442 */ 00443 #define APR_BUCKET_INIT(e) APR_RING_ELEM_INIT((e), link) 00444 00445 /** 00446 * Determine if a bucket contains metadata. An empty bucket is 00447 * safe to arbitrarily remove if and only if this is false. 00448 * @param e The bucket to inspect 00449 * @return true or false 00450 */ 00451 #define APR_BUCKET_IS_METADATA(e) ((e)->type->is_metadata) 00452 00453 /** 00454 * Determine if a bucket is a FLUSH bucket 00455 * @param e The bucket to inspect 00456 * @return true or false 00457 */ 00458 #define APR_BUCKET_IS_FLUSH(e) ((e)->type == &apr_bucket_type_flush) 00459 /** 00460 * Determine if a bucket is an EOS bucket 00461 * @param e The bucket to inspect 00462 * @return true or false 00463 */ 00464 #define APR_BUCKET_IS_EOS(e) ((e)->type == &apr_bucket_type_eos) 00465 /** 00466 * Determine if a bucket is a FILE bucket 00467 * @param e The bucket to inspect 00468 * @return true or false 00469 */ 00470 #define APR_BUCKET_IS_FILE(e) ((e)->type == &apr_bucket_type_file) 00471 /** 00472 * Determine if a bucket is a PIPE bucket 00473 * @param e The bucket to inspect 00474 * @return true or false 00475 */ 00476 #define APR_BUCKET_IS_PIPE(e) ((e)->type == &apr_bucket_type_pipe) 00477 /** 00478 * Determine if a bucket is a SOCKET bucket 00479 * @param e The bucket to inspect 00480 * @return true or false 00481 */ 00482 #define APR_BUCKET_IS_SOCKET(e) ((e)->type == &apr_bucket_type_socket) 00483 /** 00484 * Determine if a bucket is a HEAP bucket 00485 * @param e The bucket to inspect 00486 * @return true or false 00487 */ 00488 #define APR_BUCKET_IS_HEAP(e) ((e)->type == &apr_bucket_type_heap) 00489 /** 00490 * Determine if a bucket is a TRANSIENT bucket 00491 * @param e The bucket to inspect 00492 * @return true or false 00493 */ 00494 #define APR_BUCKET_IS_TRANSIENT(e) ((e)->type == &apr_bucket_type_transient) 00495 /** 00496 * Determine if a bucket is a IMMORTAL bucket 00497 * @param e The bucket to inspect 00498 * @return true or false 00499 */ 00500 #define APR_BUCKET_IS_IMMORTAL(e) ((e)->type == &apr_bucket_type_immortal) 00501 #if APR_HAS_MMAP 00502 /** 00503 * Determine if a bucket is a MMAP bucket 00504 * @param e The bucket to inspect 00505 * @return true or false 00506 */ 00507 #define APR_BUCKET_IS_MMAP(e) ((e)->type == &apr_bucket_type_mmap) 00508 #endif 00509 /** 00510 * Determine if a bucket is a POOL bucket 00511 * @param e The bucket to inspect 00512 * @return true or false 00513 */ 00514 #define APR_BUCKET_IS_POOL(e) ((e)->type == &apr_bucket_type_pool) 00515 00516 /* 00517 * General-purpose reference counting for the various bucket types. 00518 * 00519 * Any bucket type that keeps track of the resources it uses (i.e. 00520 * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to 00521 * attach a reference count to the resource so that it can be freed 00522 * when the last bucket that uses it goes away. Resource-sharing may 00523 * occur because of bucket splits or buckets that refer to globally 00524 * cached data. */ 00525 00526 /** @see apr_bucket_refcount */ 00527 typedef struct apr_bucket_refcount apr_bucket_refcount; 00528 /** 00529 * The structure used to manage the shared resource must start with an 00530 * apr_bucket_refcount which is updated by the general-purpose refcount 00531 * code. A pointer to the bucket-type-dependent private data structure 00532 * can be cast to a pointer to an apr_bucket_refcount and vice versa. 00533 */ 00534 struct apr_bucket_refcount { 00535 /** The number of references to this bucket */ 00536 int refcount; 00537 }; 00538 00539 /* ***** Reference-counted bucket types ***** */ 00540 00541 /** @see apr_bucket_heap */ 00542 typedef struct apr_bucket_heap apr_bucket_heap; 00543 /** 00544 * A bucket referring to data allocated off the heap. 00545 */ 00546 struct apr_bucket_heap { 00547 /** Number of buckets using this memory */ 00548 apr_bucket_refcount refcount; 00549 /** The start of the data actually allocated. This should never be 00550 * modified, it is only used to free the bucket. 00551 */ 00552 char *base; 00553 /** how much memory was allocated */ 00554 apr_size_t alloc_len; 00555 /** function to use to delete the data */ 00556 void (*free_func)(void *data); 00557 }; 00558 00559 /** @see apr_bucket_pool */ 00560 typedef struct apr_bucket_pool apr_bucket_pool; 00561 /** 00562 * A bucket referring to data allocated from a pool 00563 */ 00564 struct apr_bucket_pool { 00565 /** The pool bucket must be able to be easily morphed to a heap 00566 * bucket if the pool gets cleaned up before all references are 00567 * destroyed. This apr_bucket_heap structure is populated automatically 00568 * when the pool gets cleaned up, and subsequent calls to pool_read() 00569 * will result in the apr_bucket in question being morphed into a 00570 * regular heap bucket. (To avoid having to do many extra refcount 00571 * manipulations and b->data manipulations, the apr_bucket_pool 00572 * struct actually *contains* the apr_bucket_heap struct that it 00573 * will become as its first element; the two share their 00574 * apr_bucket_refcount members.) 00575 */ 00576 apr_bucket_heap heap; 00577 /** The block of data actually allocated from the pool. 00578 * Segments of this block are referenced by adjusting 00579 * the start and length of the apr_bucket accordingly. 00580 * This will be NULL after the pool gets cleaned up. 00581 */ 00582 const char *base; 00583 /** The pool the data was allocated from. When the pool 00584 * is cleaned up, this gets set to NULL as an indicator 00585 * to pool_read() that the data is now on the heap and 00586 * so it should morph the bucket into a regular heap 00587 * bucket before continuing. 00588 */ 00589 apr_pool_t *pool; 00590 /** The freelist this structure was allocated from, which is 00591 * needed in the cleanup phase in order to allocate space on the heap 00592 */ 00593 apr_bucket_alloc_t *list; 00594 }; 00595 00596 #if APR_HAS_MMAP 00597 /** @see apr_bucket_mmap */ 00598 typedef struct apr_bucket_mmap apr_bucket_mmap; 00599 /** 00600 * A bucket referring to an mmap()ed file 00601 */ 00602 struct apr_bucket_mmap { 00603 /** Number of buckets using this memory */ 00604 apr_bucket_refcount refcount; 00605 /** The mmap this sub_bucket refers to */ 00606 apr_mmap_t *mmap; 00607 }; 00608 #endif 00609 00610 /** @see apr_bucket_file */ 00611 typedef struct apr_bucket_file apr_bucket_file; 00612 /** 00613 * A bucket referring to an file 00614 */ 00615 struct apr_bucket_file { 00616 /** Number of buckets using this memory */ 00617 apr_bucket_refcount refcount; 00618 /** The file this bucket refers to */ 00619 apr_file_t *fd; 00620 /** The pool into which any needed structures should 00621 * be created while reading from this file bucket */ 00622 apr_pool_t *readpool; 00623 #if APR_HAS_MMAP 00624 /** Whether this bucket should be memory-mapped if 00625 * a caller tries to read from it */ 00626 int can_mmap; 00627 #endif /* APR_HAS_MMAP */ 00628 }; 00629 00630 /** @see apr_bucket_structs */ 00631 typedef union apr_bucket_structs apr_bucket_structs; 00632 /** 00633 * A union of all bucket structures so we know what 00634 * the max size is. 00635 */ 00636 union apr_bucket_structs { 00637 apr_bucket b; /**< Bucket */ 00638 apr_bucket_heap heap; /**< Heap */ 00639 apr_bucket_pool pool; /**< Pool */ 00640 #if APR_HAS_MMAP 00641 apr_bucket_mmap mmap; /**< MMap */ 00642 #endif 00643 apr_bucket_file file; /**< File */ 00644 }; 00645 00646 /** 00647 * The amount that apr_bucket_alloc() should allocate in the common case. 00648 * Note: this is twice as big as apr_bucket_structs to allow breathing 00649 * room for third-party bucket types. 00650 */ 00651 #define APR_BUCKET_ALLOC_SIZE APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs)) 00652 00653 /* ***** Bucket Brigade Functions ***** */ 00654 /** 00655 * Create a new bucket brigade. The bucket brigade is originally empty. 00656 * @param p The pool to associate with the brigade. Data is not allocated out 00657 * of the pool, but a cleanup is registered. 00658 * @param list The bucket allocator to use 00659 * @return The empty bucket brigade 00660 */ 00661 APR_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p, 00662 apr_bucket_alloc_t *list) 00663 __attribute__((nonnull(1,2))); 00664 00665 /** 00666 * Destroy an entire bucket brigade. This includes destroying all of the 00667 * buckets within the bucket brigade's bucket list. 00668 * @param b The bucket brigade to destroy 00669 */ 00670 APR_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b) 00671 __attribute__((nonnull(1))); 00672 00673 /** 00674 * Empty out an entire bucket brigade. This includes destroying all of the 00675 * buckets within the bucket brigade's bucket list. This is similar to 00676 * apr_brigade_destroy(), except that it does not deregister the brigade's 00677 * pool cleanup function. 00678 * @param data The bucket brigade to clean up 00679 * @remark Generally, you should use apr_brigade_destroy(). This function 00680 * can be useful in situations where you have a single brigade that 00681 * you wish to reuse many times by destroying all of the buckets in 00682 * the brigade and putting new buckets into it later. 00683 */ 00684 APR_DECLARE(apr_status_t) apr_brigade_cleanup(void *data) 00685 __attribute__((nonnull(1))); 00686 00687 /** 00688 * Move the buckets from the tail end of the existing brigade @a b into 00689 * the brigade @a a. If @a a is NULL a new brigade is created. Buckets 00690 * from @a e to the last bucket (inclusively) of brigade @a b are moved 00691 * from @a b to the returned brigade @a a. 00692 * 00693 * @param b The brigade to split 00694 * @param e The first bucket to move 00695 * @param a The brigade which should be used for the result or NULL if 00696 * a new brigade should be created. 00697 * @return The brigade supplied in @param a or a new one if @param a was NULL. 00698 * @warning Note that this function allocates a new brigade if @param a is 00699 * NULL so memory consumption should be carefully considered. 00700 */ 00701 APR_DECLARE(apr_bucket_brigade *) apr_brigade_split_ex(apr_bucket_brigade *b, 00702 apr_bucket *e, 00703 apr_bucket_brigade *a) 00704 __attribute__((nonnull(1,2))); 00705 00706 /** 00707 * Create a new bucket brigade and move the buckets from the tail end 00708 * of an existing brigade into the new brigade. Buckets from 00709 * @param e to the last bucket (inclusively) of brigade @param b 00710 * are moved from @param b to the returned brigade. 00711 * @param b The brigade to split 00712 * @param e The first bucket to move 00713 * @return The new brigade 00714 * @warning Note that this function always allocates a new brigade 00715 * so memory consumption should be carefully considered. 00716 */ 00717 APR_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b, 00718 apr_bucket *e) 00719 __attribute__((nonnull(1,2))); 00720 00721 /** 00722 * Partition a bucket brigade at a given offset (in bytes from the start of 00723 * the brigade). This is useful whenever a filter wants to use known ranges 00724 * of bytes from the brigade; the ranges can even overlap. 00725 * @param b The brigade to partition 00726 * @param point The offset at which to partition the brigade 00727 * @param after_point Returns a pointer to the first bucket after the partition 00728 * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the 00729 * brigade were shorter than @a point, or an error code. 00730 * @remark if APR_INCOMPLETE is returned, @a after_point will be set to 00731 * the brigade sentinel. 00732 */ 00733 APR_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b, 00734 apr_off_t point, 00735 apr_bucket **after_point) 00736 __attribute__((nonnull(1,3))); 00737 00738 /** 00739 * Return the total length of the brigade. 00740 * @param bb The brigade to compute the length of 00741 * @param read_all Read unknown-length buckets to force a size 00742 * @param length Returns the length of the brigade (up to the end, or up 00743 * to a bucket read error), or -1 if the brigade has buckets 00744 * of indeterminate length and read_all is 0. 00745 */ 00746 APR_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb, 00747 int read_all, 00748 apr_off_t *length) 00749 __attribute__((nonnull(1,3))); 00750 00751 /** 00752 * Take a bucket brigade and store the data in a flat char* 00753 * @param bb The bucket brigade to create the char* from 00754 * @param c The char* to write into 00755 * @param len The maximum length of the char array. On return, it is the 00756 * actual length of the char array. 00757 */ 00758 APR_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb, 00759 char *c, 00760 apr_size_t *len) 00761 __attribute__((nonnull(1,2,3))); 00762 00763 /** 00764 * Creates a pool-allocated string representing a flat bucket brigade 00765 * @param bb The bucket brigade to create the char array from 00766 * @param c On return, the allocated char array 00767 * @param len On return, the length of the char array. 00768 * @param pool The pool to allocate the string from. 00769 */ 00770 APR_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb, 00771 char **c, 00772 apr_size_t *len, 00773 apr_pool_t *pool) 00774 __attribute__((nonnull(1,2,3,4))); 00775 00776 /** 00777 * Split a brigade to represent one LF line. 00778 * @param bbOut The bucket brigade that will have the LF line appended to. 00779 * @param bbIn The input bucket brigade to search for a LF-line. 00780 * @param block The blocking mode to be used to split the line. 00781 * @param maxbytes The maximum bytes to read. If this many bytes are seen 00782 * without a LF, the brigade will contain a partial line. 00783 */ 00784 APR_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut, 00785 apr_bucket_brigade *bbIn, 00786 apr_read_type_e block, 00787 apr_off_t maxbytes) 00788 __attribute__((nonnull(1,2))); 00789 00790 /** 00791 * Create an iovec of the elements in a bucket_brigade... return number 00792 * of elements used. This is useful for writing to a file or to the 00793 * network efficiently. 00794 * @param b The bucket brigade to create the iovec from 00795 * @param vec The iovec to create 00796 * @param nvec The number of elements in the iovec. On return, it is the 00797 * number of iovec elements actually filled out. 00798 */ 00799 APR_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b, 00800 struct iovec *vec, int *nvec) 00801 __attribute__((nonnull(1,2,3))); 00802 00803 /** 00804 * This function writes a list of strings into a bucket brigade. 00805 * @param b The bucket brigade to add to 00806 * @param flush The flush function to use if the brigade is full 00807 * @param ctx The structure to pass to the flush function 00808 * @param va A list of strings to add 00809 * @return APR_SUCCESS or error code. 00810 */ 00811 APR_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b, 00812 apr_brigade_flush flush, 00813 void *ctx, 00814 va_list va) 00815 __attribute__((nonnull(1))); 00816 00817 /** 00818 * This function writes a string into a bucket brigade. 00819 * 00820 * The apr_brigade_write function attempts to be efficient with the 00821 * handling of heap buckets. Regardless of the amount of data stored 00822 * inside a heap bucket, heap buckets are a fixed size to promote their 00823 * reuse. 00824 * 00825 * If an attempt is made to write a string to a brigade that already 00826 * ends with a heap bucket, this function will attempt to pack the 00827 * string into the remaining space in the previous heap bucket, before 00828 * allocating a new heap bucket. 00829 * 00830 * This function always returns APR_SUCCESS, unless a flush function is 00831 * passed, in which case the return value of the flush function will be 00832 * returned if used. 00833 * @param b The bucket brigade to add to 00834 * @param flush The flush function to use if the brigade is full 00835 * @param ctx The structure to pass to the flush function 00836 * @param str The string to add 00837 * @param nbyte The number of bytes to write 00838 * @return APR_SUCCESS or error code 00839 */ 00840 APR_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b, 00841 apr_brigade_flush flush, void *ctx, 00842 const char *str, apr_size_t nbyte) 00843 __attribute__((nonnull(1,4))); 00844 00845 /** 00846 * This function writes multiple strings into a bucket brigade. 00847 * @param b The bucket brigade to add to 00848 * @param flush The flush function to use if the brigade is full 00849 * @param ctx The structure to pass to the flush function 00850 * @param vec The strings to add (address plus length for each) 00851 * @param nvec The number of entries in iovec 00852 * @return APR_SUCCESS or error code 00853 */ 00854 APR_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b, 00855 apr_brigade_flush flush, 00856 void *ctx, 00857 const struct iovec *vec, 00858 apr_size_t nvec) 00859 __attribute__((nonnull(1,4))); 00860 00861 /** 00862 * This function writes a string into a bucket brigade. 00863 * @param bb The bucket brigade to add to 00864 * @param flush The flush function to use if the brigade is full 00865 * @param ctx The structure to pass to the flush function 00866 * @param str The string to add 00867 * @return APR_SUCCESS or error code 00868 */ 00869 APR_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb, 00870 apr_brigade_flush flush, void *ctx, 00871 const char *str) 00872 __attribute__((nonnull(1,4))); 00873 00874 /** 00875 * This function writes a character into a bucket brigade. 00876 * @param b The bucket brigade to add to 00877 * @param flush The flush function to use if the brigade is full 00878 * @param ctx The structure to pass to the flush function 00879 * @param c The character to add 00880 * @return APR_SUCCESS or error code 00881 */ 00882 APR_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b, 00883 apr_brigade_flush flush, void *ctx, 00884 const char c) 00885 __attribute__((nonnull(1))); 00886 00887 /** 00888 * This function writes an unspecified number of strings into a bucket brigade. 00889 * @param b The bucket brigade to add to 00890 * @param flush The flush function to use if the brigade is full 00891 * @param ctx The structure to pass to the flush function 00892 * @param ... The strings to add. The final string must be NULL 00893 * @return APR_SUCCESS or error code 00894 */ 00895 APR_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b, 00896 apr_brigade_flush flush, 00897 void *ctx, ...) 00898 #if defined(__GNUC__) && __GNUC__ >= 4 00899 __attribute__((sentinel)) 00900 #endif 00901 __attribute__((nonnull(1))) 00902 ; 00903 00904 /** 00905 * Evaluate a printf and put the resulting string at the end 00906 * of the bucket brigade. 00907 * @param b The brigade to write to 00908 * @param flush The flush function to use if the brigade is full 00909 * @param ctx The structure to pass to the flush function 00910 * @param fmt The format of the string to write 00911 * @param ... The arguments to fill out the format 00912 * @return APR_SUCCESS or error code 00913 */ 00914 APR_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b, 00915 apr_brigade_flush flush, 00916 void *ctx, 00917 const char *fmt, ...) 00918 __attribute__((format(printf,4,5))) 00919 __attribute__((nonnull(1))); 00920 00921 /** 00922 * Evaluate a printf and put the resulting string at the end 00923 * of the bucket brigade. 00924 * @param b The brigade to write to 00925 * @param flush The flush function to use if the brigade is full 00926 * @param ctx The structure to pass to the flush function 00927 * @param fmt The format of the string to write 00928 * @param va The arguments to fill out the format 00929 * @return APR_SUCCESS or error code 00930 */ 00931 APR_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b, 00932 apr_brigade_flush flush, 00933 void *ctx, 00934 const char *fmt, va_list va) 00935 __attribute__((nonnull(1,4))); 00936 00937 /** 00938 * Utility function to insert a file (or a segment of a file) onto the 00939 * end of the brigade. The file is split into multiple buckets if it 00940 * is larger than the maximum size which can be represented by a 00941 * single bucket. 00942 * @param bb the brigade to insert into 00943 * @param f the file to insert 00944 * @param start the offset of the start of the segment 00945 * @param len the length of the segment of the file to insert 00946 * @param p pool from which file buckets are allocated 00947 * @return the last bucket inserted 00948 */ 00949 APR_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb, 00950 apr_file_t *f, 00951 apr_off_t start, 00952 apr_off_t len, 00953 apr_pool_t *p) 00954 __attribute__((nonnull(1,2,5))); 00955 00956 00957 00958 /* ***** Bucket freelist functions ***** */ 00959 /** 00960 * Create a bucket allocator. 00961 * @param p This pool's underlying apr_allocator_t is used to allocate memory 00962 * for the bucket allocator. When the pool is destroyed, the bucket 00963 * allocator's cleanup routine will free all memory that has been 00964 * allocated from it. 00965 * @remark The reason the allocator gets its memory from the pool's 00966 * apr_allocator_t rather than from the pool itself is because 00967 * the bucket allocator will free large memory blocks back to the 00968 * allocator when it's done with them, thereby preventing memory 00969 * footprint growth that would occur if we allocated from the pool. 00970 * @warning The allocator must never be used by more than one thread at a time. 00971 */ 00972 APR_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p); 00973 00974 /** 00975 * Create a bucket allocator. 00976 * @param allocator This apr_allocator_t is used to allocate both the bucket 00977 * allocator and all memory handed out by the bucket allocator. The 00978 * caller is responsible for destroying the bucket allocator and the 00979 * apr_allocator_t -- no automatic cleanups will happen. 00980 * @warning The allocator must never be used by more than one thread at a time. 00981 */ 00982 APR_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex( 00983 apr_allocator_t *allocator) 00984 __attribute__((nonnull(1))); 00985 00986 /** 00987 * Destroy a bucket allocator. 00988 * @param list The allocator to be destroyed 00989 */ 00990 APR_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list) 00991 __attribute__((nonnull(1))); 00992 00993 /** 00994 * Allocate memory for use by the buckets. 00995 * @param size The amount to allocate. 00996 * @param list The allocator from which to allocate the memory. 00997 */ 00998 APR_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, 00999 apr_bucket_alloc_t *list) 01000 __attribute__((nonnull(2))); 01001 01002 /** 01003 * Free memory previously allocated with apr_bucket_alloc(). 01004 * @param block The block of memory to be freed. 01005 */ 01006 APR_DECLARE_NONSTD(void) apr_bucket_free(void *block) 01007 __attribute__((nonnull(1))); 01008 01009 01010 /* ***** Bucket Functions ***** */ 01011 /** 01012 * Free the resources used by a bucket. If multiple buckets refer to 01013 * the same resource it is freed when the last one goes away. 01014 * @see apr_bucket_delete() 01015 * @param e The bucket to destroy 01016 */ 01017 #define apr_bucket_destroy(e) do { \ 01018 (e)->type->destroy((e)->data); \ 01019 (e)->free(e); \ 01020 } while (0) 01021 01022 /** 01023 * Delete a bucket by removing it from its brigade (if any) and then 01024 * destroying it. 01025 * @remark This mainly acts as an aid in avoiding code verbosity. It is 01026 * the preferred exact equivalent to: 01027 * <pre> 01028 * APR_BUCKET_REMOVE(e); 01029 * apr_bucket_destroy(e); 01030 * </pre> 01031 * @param e The bucket to delete 01032 */ 01033 #define apr_bucket_delete(e) do { \ 01034 APR_BUCKET_REMOVE(e); \ 01035 apr_bucket_destroy(e); \ 01036 } while (0) 01037 01038 /** 01039 * Read some data from the bucket. 01040 * 01041 * The apr_bucket_read function returns a convenient amount of data 01042 * from the bucket provided, writing the address and length of the 01043 * data to the pointers provided by the caller. The function tries 01044 * as hard as possible to avoid a memory copy. 01045 * 01046 * Buckets are expected to be a member of a brigade at the time they 01047 * are read. 01048 * 01049 * In typical application code, buckets are read in a loop, and after 01050 * each bucket is read and processed, it is moved or deleted from the 01051 * brigade and the next bucket read. 01052 * 01053 * The definition of "convenient" depends on the type of bucket that 01054 * is being read, and is decided by APR. In the case of memory based 01055 * buckets such as heap and immortal buckets, a pointer will be 01056 * returned to the location of the buffer containing the complete 01057 * contents of the bucket. 01058 * 01059 * Some buckets, such as the socket bucket, might have no concept 01060 * of length. If an attempt is made to read such a bucket, the 01061 * apr_bucket_read function will read a convenient amount of data 01062 * from the socket. The socket bucket is magically morphed into a 01063 * heap bucket containing the just-read data, and a new socket bucket 01064 * is inserted just after this heap bucket. 01065 * 01066 * To understand why apr_bucket_read might do this, consider the loop 01067 * described above to read and process buckets. The current bucket 01068 * is magically morphed into a heap bucket and returned to the caller. 01069 * The caller processes the data, and deletes the heap bucket, moving 01070 * onto the next bucket, the new socket bucket. This process repeats, 01071 * giving the illusion of a bucket brigade that contains potentially 01072 * infinite amounts of data. It is up to the caller to decide at what 01073 * point to stop reading buckets. 01074 * 01075 * Some buckets, such as the file bucket, might have a fixed size, 01076 * but be significantly larger than is practical to store in RAM in 01077 * one go. As with the socket bucket, if an attempt is made to read 01078 * from a file bucket, the file bucket is magically morphed into a 01079 * heap bucket containing a convenient amount of data read from the 01080 * current offset in the file. During the read, the offset will be 01081 * moved forward on the file, and a new file bucket will be inserted 01082 * directly after the current bucket representing the remainder of the 01083 * file. If the heap bucket was large enough to store the whole 01084 * remainder of the file, no more file buckets are inserted, and the 01085 * file bucket will disappear completely. 01086 * 01087 * The pattern for reading buckets described above does create the 01088 * illusion that the code is willing to swallow buckets that might be 01089 * too large for the system to handle in one go. This however is just 01090 * an illusion: APR will always ensure that large (file) or infinite 01091 * (socket) buckets are broken into convenient bite sized heap buckets 01092 * before data is returned to the caller. 01093 * 01094 * There is a potential gotcha to watch for: if buckets are read in a 01095 * loop, and aren't deleted after being processed, the potentially large 01096 * bucket will slowly be converted into RAM resident heap buckets. If 01097 * the file is larger than available RAM, an out of memory condition 01098 * could be caused if the application is not careful to manage this. 01099 * 01100 * @param e The bucket to read from 01101 * @param str The location to store a pointer to the data in 01102 * @param len The location to store the amount of data read 01103 * @param block Whether the read function blocks 01104 */ 01105 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block) 01106 01107 /** 01108 * Setaside data so that stack data is not destroyed on returning from 01109 * the function 01110 * @param e The bucket to setaside 01111 * @param p The pool to setaside into 01112 */ 01113 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p) 01114 01115 /** 01116 * Split one bucket in two at the point provided. 01117 * 01118 * Once split, the original bucket becomes the first of the two new buckets. 01119 * 01120 * (It is assumed that the bucket is a member of a brigade when this 01121 * function is called). 01122 * @param e The bucket to split 01123 * @param point The offset to split the bucket at 01124 */ 01125 #define apr_bucket_split(e,point) (e)->type->split(e, point) 01126 01127 /** 01128 * Copy a bucket. 01129 * @param e The bucket to copy 01130 * @param c Returns a pointer to the new bucket 01131 */ 01132 #define apr_bucket_copy(e,c) (e)->type->copy(e, c) 01133 01134 /* Bucket type handling */ 01135 01136 /** 01137 * This function simply returns APR_SUCCESS to denote that the bucket does 01138 * not require anything to happen for its setaside() function. This is 01139 * appropriate for buckets that have "immortal" data -- the data will live 01140 * at least as long as the bucket. 01141 * @param data The bucket to setaside 01142 * @param pool The pool defining the desired lifetime of the bucket data 01143 * @return APR_SUCCESS 01144 */ 01145 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data, 01146 apr_pool_t *pool); 01147 01148 /** 01149 * A place holder function that signifies that the setaside function was not 01150 * implemented for this bucket 01151 * @param data The bucket to setaside 01152 * @param pool The pool defining the desired lifetime of the bucket data 01153 * @return APR_ENOTIMPL 01154 */ 01155 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data, 01156 apr_pool_t *pool); 01157 01158 /** 01159 * A place holder function that signifies that the split function was not 01160 * implemented for this bucket 01161 * @param data The bucket to split 01162 * @param point The location to split the bucket 01163 * @return APR_ENOTIMPL 01164 */ 01165 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data, 01166 apr_size_t point); 01167 01168 /** 01169 * A place holder function that signifies that the copy function was not 01170 * implemented for this bucket 01171 * @param e The bucket to copy 01172 * @param c Returns a pointer to the new bucket 01173 * @return APR_ENOTIMPL 01174 */ 01175 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e, 01176 apr_bucket **c); 01177 01178 /** 01179 * A place holder function that signifies that this bucket does not need 01180 * to do anything special to be destroyed. That's only the case for buckets 01181 * that either have no data (metadata buckets) or buckets whose data pointer 01182 * points to something that's not a bucket-type-specific structure, as with 01183 * simple buckets where data points to a string and pipe buckets where data 01184 * points directly to the apr_file_t. 01185 * @param data The bucket data to destroy 01186 */ 01187 APR_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data); 01188 01189 /** 01190 * There is no apr_bucket_destroy_notimpl, because destruction is required 01191 * to be implemented (it could be a noop, but only if that makes sense for 01192 * the bucket type) 01193 */ 01194 01195 /* There is no apr_bucket_read_notimpl, because it is a required function 01196 */ 01197 01198 01199 /* All of the bucket types implemented by the core */ 01200 /** 01201 * The flush bucket type. This signifies that all data should be flushed to 01202 * the next filter. The flush bucket should be sent with the other buckets. 01203 */ 01204 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush; 01205 /** 01206 * The EOS bucket type. This signifies that there will be no more data, ever. 01207 * All filters MUST send all data to the next filter when they receive a 01208 * bucket of this type 01209 */ 01210 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos; 01211 /** 01212 * The FILE bucket type. This bucket represents a file on disk 01213 */ 01214 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file; 01215 /** 01216 * The HEAP bucket type. This bucket represents a data allocated from the 01217 * heap. 01218 */ 01219 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap; 01220 #if APR_HAS_MMAP 01221 /** 01222 * The MMAP bucket type. This bucket represents an MMAP'ed file 01223 */ 01224 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap; 01225 #endif 01226 /** 01227 * The POOL bucket type. This bucket represents a data that was allocated 01228 * from a pool. IF this bucket is still available when the pool is cleared, 01229 * the data is copied on to the heap. 01230 */ 01231 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool; 01232 /** 01233 * The PIPE bucket type. This bucket represents a pipe to another program. 01234 */ 01235 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe; 01236 /** 01237 * The IMMORTAL bucket type. This bucket represents a segment of data that 01238 * the creator is willing to take responsibility for. The core will do 01239 * nothing with the data in an immortal bucket 01240 */ 01241 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal; 01242 /** 01243 * The TRANSIENT bucket type. This bucket represents a data allocated off 01244 * the stack. When the setaside function is called, this data is copied on 01245 * to the heap 01246 */ 01247 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient; 01248 /** 01249 * The SOCKET bucket type. This bucket represents a socket to another machine 01250 */ 01251 APR_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket; 01252 01253 01254 /* ***** Simple buckets ***** */ 01255 01256 /** 01257 * Split a simple bucket into two at the given point. Most non-reference 01258 * counting buckets that allow multiple references to the same block of 01259 * data (eg transient and immortal) will use this as their split function 01260 * without any additional type-specific handling. 01261 * @param b The bucket to be split 01262 * @param point The offset of the first byte in the new bucket 01263 * @return APR_EINVAL if the point is not within the bucket; 01264 * APR_ENOMEM if allocation failed; 01265 * or APR_SUCCESS 01266 */ 01267 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b, 01268 apr_size_t point); 01269 01270 /** 01271 * Copy a simple bucket. Most non-reference-counting buckets that allow 01272 * multiple references to the same block of data (eg transient and immortal) 01273 * will use this as their copy function without any additional type-specific 01274 * handling. 01275 * @param a The bucket to copy 01276 * @param b Returns a pointer to the new bucket 01277 * @return APR_ENOMEM if allocation failed; 01278 * or APR_SUCCESS 01279 */ 01280 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a, 01281 apr_bucket **b); 01282 01283 01284 /* ***** Shared, reference-counted buckets ***** */ 01285 01286 /** 01287 * Initialize a bucket containing reference-counted data that may be 01288 * shared. The caller must allocate the bucket if necessary and 01289 * initialize its type-dependent fields, and allocate and initialize 01290 * its own private data structure. This function should only be called 01291 * by type-specific bucket creation functions. 01292 * @param b The bucket to initialize 01293 * @param data A pointer to the private data structure 01294 * with the reference count at the start 01295 * @param start The start of the data in the bucket 01296 * relative to the private base pointer 01297 * @param length The length of the data in the bucket 01298 * @return The new bucket, or NULL if allocation failed 01299 */ 01300 APR_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data, 01301 apr_off_t start, 01302 apr_size_t length); 01303 01304 /** 01305 * Decrement the refcount of the data in the bucket. This function 01306 * should only be called by type-specific bucket destruction functions. 01307 * @param data The private data pointer from the bucket to be destroyed 01308 * @return TRUE or FALSE; TRUE if the reference count is now 01309 * zero, indicating that the shared resource itself can 01310 * be destroyed by the caller. 01311 */ 01312 APR_DECLARE(int) apr_bucket_shared_destroy(void *data); 01313 01314 /** 01315 * Split a bucket into two at the given point, and adjust the refcount 01316 * to the underlying data. Most reference-counting bucket types will 01317 * be able to use this function as their split function without any 01318 * additional type-specific handling. 01319 * @param b The bucket to be split 01320 * @param point The offset of the first byte in the new bucket 01321 * @return APR_EINVAL if the point is not within the bucket; 01322 * APR_ENOMEM if allocation failed; 01323 * or APR_SUCCESS 01324 */ 01325 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b, 01326 apr_size_t point); 01327 01328 /** 01329 * Copy a refcounted bucket, incrementing the reference count. Most 01330 * reference-counting bucket types will be able to use this function 01331 * as their copy function without any additional type-specific handling. 01332 * @param a The bucket to copy 01333 * @param b Returns a pointer to the new bucket 01334 * @return APR_ENOMEM if allocation failed; 01335 or APR_SUCCESS 01336 */ 01337 APR_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a, 01338 apr_bucket **b); 01339 01340 01341 /* ***** Functions to Create Buckets of varying types ***** */ 01342 /* 01343 * Each bucket type foo has two initialization functions: 01344 * apr_bucket_foo_make which sets up some already-allocated memory as a 01345 * bucket of type foo; and apr_bucket_foo_create which allocates memory 01346 * for the bucket, calls apr_bucket_make_foo, and initializes the 01347 * bucket's list pointers. The apr_bucket_foo_make functions are used 01348 * inside the bucket code to change the type of buckets in place; 01349 * other code should call apr_bucket_foo_create. All the initialization 01350 * functions change nothing if they fail. 01351 */ 01352 01353 /** 01354 * Create an End of Stream bucket. This indicates that there is no more data 01355 * coming from down the filter stack. All filters should flush at this point. 01356 * @param list The freelist from which this bucket should be allocated 01357 * @return The new bucket, or NULL if allocation failed 01358 */ 01359 APR_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list); 01360 01361 /** 01362 * Make the bucket passed in an EOS bucket. This indicates that there is no 01363 * more data coming from down the filter stack. All filters should flush at 01364 * this point. 01365 * @param b The bucket to make into an EOS bucket 01366 * @return The new bucket, or NULL if allocation failed 01367 */ 01368 APR_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b) 01369 __attribute__((nonnull(1))); 01370 01371 /** 01372 * Create a flush bucket. This indicates that filters should flush their 01373 * data. There is no guarantee that they will flush it, but this is the 01374 * best we can do. 01375 * @param list The freelist from which this bucket should be allocated 01376 * @return The new bucket, or NULL if allocation failed 01377 */ 01378 APR_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list) 01379 __attribute__((nonnull(1))); 01380 01381 /** 01382 * Make the bucket passed in a FLUSH bucket. This indicates that filters 01383 * should flush their data. There is no guarantee that they will flush it, 01384 * but this is the best we can do. 01385 * @param b The bucket to make into a FLUSH bucket 01386 * @return The new bucket, or NULL if allocation failed 01387 */ 01388 APR_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b) 01389 __attribute__((nonnull(1))); 01390 01391 /** 01392 * Create a bucket referring to long-lived data. 01393 * @param buf The data to insert into the bucket 01394 * @param nbyte The size of the data to insert. 01395 * @param list The freelist from which this bucket should be allocated 01396 * @return The new bucket, or NULL if allocation failed 01397 */ 01398 APR_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf, 01399 apr_size_t nbyte, 01400 apr_bucket_alloc_t *list) 01401 __attribute__((nonnull(1,3))); 01402 01403 /** 01404 * Make the bucket passed in a bucket refer to long-lived data 01405 * @param b The bucket to make into a IMMORTAL bucket 01406 * @param buf The data to insert into the bucket 01407 * @param nbyte The size of the data to insert. 01408 * @return The new bucket, or NULL if allocation failed 01409 */ 01410 APR_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b, 01411 const char *buf, 01412 apr_size_t nbyte) 01413 __attribute__((nonnull(1,2))); 01414 01415 /** 01416 * Create a bucket referring to data on the stack. 01417 * @param buf The data to insert into the bucket 01418 * @param nbyte The size of the data to insert. 01419 * @param list The freelist from which this bucket should be allocated 01420 * @return The new bucket, or NULL if allocation failed 01421 */ 01422 APR_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf, 01423 apr_size_t nbyte, 01424 apr_bucket_alloc_t *list) 01425 __attribute__((nonnull(1,3))); 01426 01427 /** 01428 * Make the bucket passed in a bucket refer to stack data 01429 * @param b The bucket to make into a TRANSIENT bucket 01430 * @param buf The data to insert into the bucket 01431 * @param nbyte The size of the data to insert. 01432 * @return The new bucket, or NULL if allocation failed 01433 */ 01434 APR_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b, 01435 const char *buf, 01436 apr_size_t nbyte) 01437 __attribute__((nonnull(1,2))); 01438 01439 /** 01440 * Create a bucket referring to memory on the heap. If the caller asks 01441 * for the data to be copied, this function always allocates 4K of 01442 * memory so that more data can be added to the bucket without 01443 * requiring another allocation. Therefore not all the data may be put 01444 * into the bucket. If copying is not requested then the bucket takes 01445 * over responsibility for free()ing the memory. 01446 * @param buf The buffer to insert into the bucket 01447 * @param nbyte The size of the buffer to insert. 01448 * @param free_func Function to use to free the data; NULL indicates that the 01449 * bucket should make a copy of the data 01450 * @param list The freelist from which this bucket should be allocated 01451 * @return The new bucket, or NULL if allocation failed 01452 */ 01453 APR_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf, 01454 apr_size_t nbyte, 01455 void (*free_func)(void *data), 01456 apr_bucket_alloc_t *list) 01457 __attribute__((nonnull(1,4))); 01458 /** 01459 * Make the bucket passed in a bucket refer to heap data 01460 * @param b The bucket to make into a HEAP bucket 01461 * @param buf The buffer to insert into the bucket 01462 * @param nbyte The size of the buffer to insert. 01463 * @param free_func Function to use to free the data; NULL indicates that the 01464 * bucket should make a copy of the data 01465 * @return The new bucket, or NULL if allocation failed 01466 */ 01467 APR_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf, 01468 apr_size_t nbyte, 01469 void (*free_func)(void *data)) 01470 __attribute__((nonnull(1,2))); 01471 01472 /** 01473 * Create a bucket referring to memory allocated from a pool. 01474 * 01475 * @param buf The buffer to insert into the bucket 01476 * @param length The number of bytes referred to by this bucket 01477 * @param pool The pool the memory was allocated from 01478 * @param list The freelist from which this bucket should be allocated 01479 * @return The new bucket, or NULL if allocation failed 01480 */ 01481 APR_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf, 01482 apr_size_t length, 01483 apr_pool_t *pool, 01484 apr_bucket_alloc_t *list) 01485 __attribute__((nonnull(1,3,4))); 01486 01487 /** 01488 * Make the bucket passed in a bucket refer to pool data 01489 * @param b The bucket to make into a pool bucket 01490 * @param buf The buffer to insert into the bucket 01491 * @param length The number of bytes referred to by this bucket 01492 * @param pool The pool the memory was allocated from 01493 * @return The new bucket, or NULL if allocation failed 01494 */ 01495 APR_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf, 01496 apr_size_t length, 01497 apr_pool_t *pool) 01498 __attribute__((nonnull(1,2,4))); 01499 01500 #if APR_HAS_MMAP 01501 /** 01502 * Create a bucket referring to mmap()ed memory. 01503 * @param mm The mmap to insert into the bucket 01504 * @param start The offset of the first byte in the mmap 01505 * that this bucket refers to 01506 * @param length The number of bytes referred to by this bucket 01507 * @param list The freelist from which this bucket should be allocated 01508 * @return The new bucket, or NULL if allocation failed 01509 */ 01510 APR_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm, 01511 apr_off_t start, 01512 apr_size_t length, 01513 apr_bucket_alloc_t *list) 01514 __attribute__((nonnull(1,4))); 01515 01516 /** 01517 * Make the bucket passed in a bucket refer to an MMAP'ed file 01518 * @param b The bucket to make into a MMAP bucket 01519 * @param mm The mmap to insert into the bucket 01520 * @param start The offset of the first byte in the mmap 01521 * that this bucket refers to 01522 * @param length The number of bytes referred to by this bucket 01523 * @return The new bucket, or NULL if allocation failed 01524 */ 01525 APR_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm, 01526 apr_off_t start, 01527 apr_size_t length) 01528 __attribute__((nonnull(1,2))); 01529 #endif 01530 01531 /** 01532 * Create a bucket referring to a socket. 01533 * @param thissock The socket to put in the bucket 01534 * @param list The freelist from which this bucket should be allocated 01535 * @return The new bucket, or NULL if allocation failed 01536 */ 01537 APR_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock, 01538 apr_bucket_alloc_t *list) 01539 __attribute__((nonnull(1,2))); 01540 /** 01541 * Make the bucket passed in a bucket refer to a socket 01542 * @param b The bucket to make into a SOCKET bucket 01543 * @param thissock The socket to put in the bucket 01544 * @return The new bucket, or NULL if allocation failed 01545 */ 01546 APR_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b, 01547 apr_socket_t *thissock) 01548 __attribute__((nonnull(1,2))); 01549 01550 /** 01551 * Create a bucket referring to a pipe. 01552 * @param thispipe The pipe to put in the bucket 01553 * @param list The freelist from which this bucket should be allocated 01554 * @return The new bucket, or NULL if allocation failed 01555 */ 01556 APR_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe, 01557 apr_bucket_alloc_t *list) 01558 __attribute__((nonnull(1,2))); 01559 01560 /** 01561 * Make the bucket passed in a bucket refer to a pipe 01562 * @param b The bucket to make into a PIPE bucket 01563 * @param thispipe The pipe to put in the bucket 01564 * @return The new bucket, or NULL if allocation failed 01565 */ 01566 APR_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b, 01567 apr_file_t *thispipe) 01568 __attribute__((nonnull(1,2))); 01569 01570 /** 01571 * Create a bucket referring to a file. 01572 * @param fd The file to put in the bucket 01573 * @param offset The offset where the data of interest begins in the file 01574 * @param len The amount of data in the file we are interested in 01575 * @param p The pool into which any needed structures should be created 01576 * while reading from this file bucket 01577 * @param list The freelist from which this bucket should be allocated 01578 * @return The new bucket, or NULL if allocation failed 01579 * @remark If the file is truncated such that the segment of the file 01580 * referenced by the bucket no longer exists, an attempt to read 01581 * from the bucket will fail with APR_EOF. 01582 * @remark apr_brigade_insert_file() should generally be used to 01583 * insert files into brigades, since that function can correctly 01584 * handle large file issues. 01585 */ 01586 APR_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd, 01587 apr_off_t offset, 01588 apr_size_t len, 01589 apr_pool_t *p, 01590 apr_bucket_alloc_t *list) 01591 __attribute__((nonnull(1,4,5))); 01592 01593 /** 01594 * Make the bucket passed in a bucket refer to a file 01595 * @param b The bucket to make into a FILE bucket 01596 * @param fd The file to put in the bucket 01597 * @param offset The offset where the data of interest begins in the file 01598 * @param len The amount of data in the file we are interested in 01599 * @param p The pool into which any needed structures should be created 01600 * while reading from this file bucket 01601 * @return The new bucket, or NULL if allocation failed 01602 */ 01603 APR_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd, 01604 apr_off_t offset, 01605 apr_size_t len, apr_pool_t *p) 01606 __attribute__((nonnull(1,2,5))); 01607 01608 /** 01609 * Enable or disable memory-mapping for a FILE bucket (default is enabled) 01610 * @param b The bucket 01611 * @param enabled Whether memory-mapping should be enabled 01612 * @return APR_SUCCESS normally, or an error code if the operation fails 01613 */ 01614 APR_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b, 01615 int enabled) 01616 __attribute__((nonnull(1))); 01617 01618 /** @} */ 01619 #ifdef __cplusplus 01620 } 01621 #endif 01622 01623 #endif /* !APR_BUCKETS_H */
1.7.5