Apache Portable Runtime
apr_buckets.h
Go to the documentation of this file.
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_Util
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)                                \
00292         APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link)
00293 
00294 #define APR_BUCKET_CHECK_CONSISTENCY(e)                                 \
00295         APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
00296 
00297 #else
00298 /**
00299  * checks the ring pointers in a bucket brigade for consistency.  an
00300  * abort() will be triggered if any inconsistencies are found.
00301  *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
00302  * @param b The brigade
00303  */
00304 #define APR_BRIGADE_CHECK_CONSISTENCY(b)
00305 /**
00306  * checks the brigade a bucket is in for ring consistency.  an
00307  * abort() will be triggered if any inconsistencies are found.
00308  *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
00309  * @param e The bucket
00310  */
00311 #define APR_BUCKET_CHECK_CONSISTENCY(e)
00312 #endif
00313 
00314 
00315 /**
00316  * Wrappers around the RING macros to reduce the verbosity of the code
00317  * that handles bucket brigades.
00318  */
00319 /**
00320  * The magic pointer value that indicates the head of the brigade
00321  * @remark This is used to find the beginning and end of the brigade, eg:
00322  * <pre>
00323  *      while (e != APR_BRIGADE_SENTINEL(b)) {
00324  *          ...
00325  *          e = APR_BUCKET_NEXT(e);
00326  *      }
00327  * </pre>
00328  * @param  b The brigade
00329  * @return The magic pointer value
00330  */
00331 #define APR_BRIGADE_SENTINEL(b) APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
00332 
00333 /**
00334  * Determine if the bucket brigade is empty
00335  * @param b The brigade to check
00336  * @return true or false
00337  */
00338 #define APR_BRIGADE_EMPTY(b)    APR_RING_EMPTY(&(b)->list, apr_bucket, link)
00339 
00340 /**
00341  * Return the first bucket in a brigade
00342  * @param b The brigade to query
00343  * @return The first bucket in the brigade
00344  */
00345 #define APR_BRIGADE_FIRST(b)    APR_RING_FIRST(&(b)->list)
00346 /**
00347  * Return the last bucket in a brigade
00348  * @param b The brigade to query
00349  * @return The last bucket in the brigade
00350  */
00351 #define APR_BRIGADE_LAST(b)     APR_RING_LAST(&(b)->list)
00352 
00353 /**
00354  * Insert a list of buckets at the front of a brigade
00355  * @param b The brigade to add to
00356  * @param e The first bucket in a list of buckets to insert
00357  */
00358 #define APR_BRIGADE_INSERT_HEAD(b, e) do {                              \
00359         apr_bucket *ap__b = (e);                                        \
00360         APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link);      \
00361         APR_BRIGADE_CHECK_CONSISTENCY((b));                             \
00362     } while (0)
00363 
00364 /**
00365  * Insert a list of buckets at the end of a brigade
00366  * @param b The brigade to add to
00367  * @param e The first bucket in a list of buckets to insert
00368  */
00369 #define APR_BRIGADE_INSERT_TAIL(b, e) do {                              \
00370         apr_bucket *ap__b = (e);                                        \
00371         APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link);      \
00372         APR_BRIGADE_CHECK_CONSISTENCY((b));                             \
00373     } while (0)
00374 
00375 /**
00376  * Concatenate brigade b onto the end of brigade a, leaving brigade b empty
00377  * @param a The first brigade
00378  * @param b The second brigade
00379  */
00380 #define APR_BRIGADE_CONCAT(a, b) do {                                   \
00381         APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link);      \
00382         APR_BRIGADE_CHECK_CONSISTENCY((a));                             \
00383     } while (0)
00384 
00385 /**
00386  * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty
00387  * @param a The first brigade
00388  * @param b The second brigade
00389  */
00390 #define APR_BRIGADE_PREPEND(a, b) do {                                  \
00391         APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link);     \
00392         APR_BRIGADE_CHECK_CONSISTENCY((a));                             \
00393     } while (0)
00394 
00395 /**
00396  * Insert a list of buckets before a specified bucket
00397  * @param a The bucket to insert before
00398  * @param b The buckets to insert
00399  */
00400 #define APR_BUCKET_INSERT_BEFORE(a, b) do {                             \
00401         apr_bucket *ap__a = (a), *ap__b = (b);                          \
00402         APR_RING_INSERT_BEFORE(ap__a, ap__b, link);                     \
00403         APR_BUCKET_CHECK_CONSISTENCY(ap__a);                            \
00404     } while (0)
00405 
00406 /**
00407  * Insert a list of buckets after a specified bucket
00408  * @param a The bucket to insert after
00409  * @param b The buckets to insert
00410  */
00411 #define APR_BUCKET_INSERT_AFTER(a, b) do {                              \
00412         apr_bucket *ap__a = (a), *ap__b = (b);                          \
00413         APR_RING_INSERT_AFTER(ap__a, ap__b, link);                      \
00414         APR_BUCKET_CHECK_CONSISTENCY(ap__a);                            \
00415     } while (0)
00416 
00417 /**
00418  * Get the next bucket in the list
00419  * @param e The current bucket
00420  * @return The next bucket
00421  */
00422 #define APR_BUCKET_NEXT(e)      APR_RING_NEXT((e), link)
00423 /**
00424  * Get the previous bucket in the list
00425  * @param e The current bucket
00426  * @return The previous bucket
00427  */
00428 #define APR_BUCKET_PREV(e)      APR_RING_PREV((e), link)
00429 
00430 /**
00431  * Remove a bucket from its bucket brigade
00432  * @param e The bucket to remove
00433  */
00434 #define APR_BUCKET_REMOVE(e)    APR_RING_REMOVE((e), link)
00435 
00436 /**
00437  * Initialize a new bucket's prev/next pointers
00438  * @param e The bucket to initialize
00439  */
00440 #define APR_BUCKET_INIT(e)      APR_RING_ELEM_INIT((e), link)
00441 
00442 /**
00443  * Determine if a bucket contains metadata.  An empty bucket is
00444  * safe to arbitrarily remove if and only if this is false.
00445  * @param e The bucket to inspect
00446  * @return true or false
00447  */
00448 #define APR_BUCKET_IS_METADATA(e)    ((e)->type->is_metadata)
00449 
00450 /**
00451  * Determine if a bucket is a FLUSH bucket
00452  * @param e The bucket to inspect
00453  * @return true or false
00454  */
00455 #define APR_BUCKET_IS_FLUSH(e)       ((e)->type == &apr_bucket_type_flush)
00456 /**
00457  * Determine if a bucket is an EOS bucket
00458  * @param e The bucket to inspect
00459  * @return true or false
00460  */
00461 #define APR_BUCKET_IS_EOS(e)         ((e)->type == &apr_bucket_type_eos)
00462 /**
00463  * Determine if a bucket is a FILE bucket
00464  * @param e The bucket to inspect
00465  * @return true or false
00466  */
00467 #define APR_BUCKET_IS_FILE(e)        ((e)->type == &apr_bucket_type_file)
00468 /**
00469  * Determine if a bucket is a PIPE bucket
00470  * @param e The bucket to inspect
00471  * @return true or false
00472  */
00473 #define APR_BUCKET_IS_PIPE(e)        ((e)->type == &apr_bucket_type_pipe)
00474 /**
00475  * Determine if a bucket is a SOCKET bucket
00476  * @param e The bucket to inspect
00477  * @return true or false
00478  */
00479 #define APR_BUCKET_IS_SOCKET(e)      ((e)->type == &apr_bucket_type_socket)
00480 /**
00481  * Determine if a bucket is a HEAP bucket
00482  * @param e The bucket to inspect
00483  * @return true or false
00484  */
00485 #define APR_BUCKET_IS_HEAP(e)        ((e)->type == &apr_bucket_type_heap)
00486 /**
00487  * Determine if a bucket is a TRANSIENT bucket
00488  * @param e The bucket to inspect
00489  * @return true or false
00490  */
00491 #define APR_BUCKET_IS_TRANSIENT(e)   ((e)->type == &apr_bucket_type_transient)
00492 /**
00493  * Determine if a bucket is a IMMORTAL bucket
00494  * @param e The bucket to inspect
00495  * @return true or false
00496  */
00497 #define APR_BUCKET_IS_IMMORTAL(e)    ((e)->type == &apr_bucket_type_immortal)
00498 #if APR_HAS_MMAP
00499 /**
00500  * Determine if a bucket is a MMAP bucket
00501  * @param e The bucket to inspect
00502  * @return true or false
00503  */
00504 #define APR_BUCKET_IS_MMAP(e)        ((e)->type == &apr_bucket_type_mmap)
00505 #endif
00506 /**
00507  * Determine if a bucket is a POOL bucket
00508  * @param e The bucket to inspect
00509  * @return true or false
00510  */
00511 #define APR_BUCKET_IS_POOL(e)        ((e)->type == &apr_bucket_type_pool)
00512 
00513 /*
00514  * General-purpose reference counting for the various bucket types.
00515  *
00516  * Any bucket type that keeps track of the resources it uses (i.e.
00517  * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
00518  * attach a reference count to the resource so that it can be freed
00519  * when the last bucket that uses it goes away. Resource-sharing may
00520  * occur because of bucket splits or buckets that refer to globally
00521  * cached data. */
00522 
00523 /** @see apr_bucket_refcount */
00524 typedef struct apr_bucket_refcount apr_bucket_refcount;
00525 /**
00526  * The structure used to manage the shared resource must start with an
00527  * apr_bucket_refcount which is updated by the general-purpose refcount
00528  * code. A pointer to the bucket-type-dependent private data structure
00529  * can be cast to a pointer to an apr_bucket_refcount and vice versa.
00530  */
00531 struct apr_bucket_refcount {
00532     /** The number of references to this bucket */
00533     int          refcount;
00534 };
00535 
00536 /*  *****  Reference-counted bucket types  *****  */
00537 
00538 /** @see apr_bucket_heap */
00539 typedef struct apr_bucket_heap apr_bucket_heap;
00540 /**
00541  * A bucket referring to data allocated off the heap.
00542  */
00543 struct apr_bucket_heap {
00544     /** Number of buckets using this memory */
00545     apr_bucket_refcount  refcount;
00546     /** The start of the data actually allocated.  This should never be
00547      * modified, it is only used to free the bucket.
00548      */
00549     char    *base;
00550     /** how much memory was allocated */
00551     apr_size_t  alloc_len;
00552     /** function to use to delete the data */
00553     void (*free_func)(void *data);
00554 };
00555 
00556 /** @see apr_bucket_pool */
00557 typedef struct apr_bucket_pool apr_bucket_pool;
00558 /**
00559  * A bucket referring to data allocated from a pool
00560  */
00561 struct apr_bucket_pool {
00562     /** The pool bucket must be able to be easily morphed to a heap
00563      * bucket if the pool gets cleaned up before all references are
00564      * destroyed.  This apr_bucket_heap structure is populated automatically
00565      * when the pool gets cleaned up, and subsequent calls to pool_read()
00566      * will result in the apr_bucket in question being morphed into a
00567      * regular heap bucket.  (To avoid having to do many extra refcount
00568      * manipulations and b->data manipulations, the apr_bucket_pool
00569      * struct actually *contains* the apr_bucket_heap struct that it
00570      * will become as its first element; the two share their
00571      * apr_bucket_refcount members.)
00572      */
00573     apr_bucket_heap  heap;
00574     /** The block of data actually allocated from the pool.
00575      * Segments of this block are referenced by adjusting
00576      * the start and length of the apr_bucket accordingly.
00577      * This will be NULL after the pool gets cleaned up.
00578      */
00579     const char *base;
00580     /** The pool the data was allocated from.  When the pool
00581      * is cleaned up, this gets set to NULL as an indicator
00582      * to pool_read() that the data is now on the heap and
00583      * so it should morph the bucket into a regular heap
00584      * bucket before continuing.
00585      */
00586     apr_pool_t *pool;
00587     /** The freelist this structure was allocated from, which is
00588      * needed in the cleanup phase in order to allocate space on the heap
00589      */
00590     apr_bucket_alloc_t *list;
00591 };
00592 
00593 #if APR_HAS_MMAP
00594 /** @see apr_bucket_mmap */
00595 typedef struct apr_bucket_mmap apr_bucket_mmap;
00596 /**
00597  * A bucket referring to an mmap()ed file
00598  */
00599 struct apr_bucket_mmap {
00600     /** Number of buckets using this memory */
00601     apr_bucket_refcount  refcount;
00602     /** The mmap this sub_bucket refers to */
00603     apr_mmap_t *mmap;
00604 };
00605 #endif
00606 
00607 /** @see apr_bucket_file */
00608 typedef struct apr_bucket_file apr_bucket_file;
00609 /**
00610  * A bucket referring to an file
00611  */
00612 struct apr_bucket_file {
00613     /** Number of buckets using this memory */
00614     apr_bucket_refcount  refcount;
00615     /** The file this bucket refers to */
00616     apr_file_t *fd;
00617     /** The pool into which any needed structures should
00618      *  be created while reading from this file bucket */
00619     apr_pool_t *readpool;
00620 #if APR_HAS_MMAP
00621     /** Whether this bucket should be memory-mapped if
00622      *  a caller tries to read from it */
00623     int can_mmap;
00624 #endif /* APR_HAS_MMAP */
00625 };
00626 
00627 /** @see apr_bucket_structs */
00628 typedef union apr_bucket_structs apr_bucket_structs;
00629 /**
00630  * A union of all bucket structures so we know what
00631  * the max size is.
00632  */
00633 union apr_bucket_structs {
00634     apr_bucket      b;      /**< Bucket */
00635     apr_bucket_heap heap;   /**< Heap */
00636     apr_bucket_pool pool;   /**< Pool */
00637 #if APR_HAS_MMAP
00638     apr_bucket_mmap mmap;   /**< MMap */
00639 #endif
00640     apr_bucket_file file;   /**< File */
00641 };
00642 
00643 /**
00644  * The amount that apr_bucket_alloc() should allocate in the common case.
00645  * Note: this is twice as big as apr_bucket_structs to allow breathing
00646  * room for third-party bucket types.
00647  */
00648 #define APR_BUCKET_ALLOC_SIZE  APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
00649 
00650 /*  *****  Bucket Brigade Functions  *****  */
00651 /**
00652  * Create a new bucket brigade.  The bucket brigade is originally empty.
00653  * @param p The pool to associate with the brigade.  Data is not allocated out
00654  *          of the pool, but a cleanup is registered.
00655  * @param list The bucket allocator to use
00656  * @return The empty bucket brigade
00657  */
00658 APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
00659                                                      apr_bucket_alloc_t *list);
00660 
00661 /**
00662  * destroy an entire bucket brigade.  This includes destroying all of the
00663  * buckets within the bucket brigade's bucket list. 
00664  * @param b The bucket brigade to destroy
00665  */
00666 APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
00667 
00668 /**
00669  * empty out an entire bucket brigade.  This includes destroying all of the
00670  * buckets within the bucket brigade's bucket list.  This is similar to
00671  * apr_brigade_destroy(), except that it does not deregister the brigade's
00672  * pool cleanup function.
00673  * @param data The bucket brigade to clean up
00674  * @remark Generally, you should use apr_brigade_destroy().  This function
00675  *         can be useful in situations where you have a single brigade that
00676  *         you wish to reuse many times by destroying all of the buckets in
00677  *         the brigade and putting new buckets into it later.
00678  */
00679 APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
00680 
00681 /**
00682  * Move the buckets from the tail end of the existing brigade @a b into
00683  * the brigade @a a. If @a a is NULL a new brigade is created. Buckets
00684  * from @a e to the last bucket (inclusively) of brigade @a b are moved
00685  * from @a b to the returned brigade @a a.
00686  *
00687  * @param b The brigade to split
00688  * @param e The first bucket to move
00689  * @param a The brigade which should be used for the result or NULL if
00690  *          a new brigade should be created.
00691  * @return The brigade supplied in @param a or a new one if @param a was NULL.
00692  * @warning Note that this function allocates a new brigade if @param a is
00693  * NULL so memory consumption should be carefully considered.
00694  */
00695 APU_DECLARE(apr_bucket_brigade *) apr_brigade_split_ex(apr_bucket_brigade *b,
00696                                                        apr_bucket *e,
00697                                                        apr_bucket_brigade *a);
00698 
00699 /**
00700  * Create a new bucket brigade and move the buckets from the tail end
00701  * of an existing brigade into the new brigade.  Buckets from 
00702  * @param e to the last bucket (inclusively) of brigade @param b
00703  * are moved from @param b to the returned brigade.
00704  * @param b The brigade to split 
00705  * @param e The first bucket to move
00706  * @return The new brigade
00707  * @warning Note that this function always allocates a new brigade
00708  * so memory consumption should be carefully considered.
00709  */
00710 APU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
00711                                                     apr_bucket *e);
00712 
00713 /**
00714  * Partition a bucket brigade at a given offset (in bytes from the start of
00715  * the brigade).  This is useful whenever a filter wants to use known ranges
00716  * of bytes from the brigade; the ranges can even overlap.
00717  * @param b The brigade to partition
00718  * @param point The offset at which to partition the brigade
00719  * @param after_point Returns a pointer to the first bucket after the partition
00720  * @return APR_SUCCESS on success, APR_INCOMPLETE if the contents of the
00721  * brigade were shorter than @a point, or an error code.
00722  * @remark if APR_INCOMPLETE is returned, @a after_point will be set to
00723  * the brigade sentinel.
00724  */
00725 APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
00726                                                 apr_off_t point,
00727                                                 apr_bucket **after_point);
00728 
00729 /**
00730  * Return the total length of the brigade.
00731  * @param bb The brigade to compute the length of
00732  * @param read_all Read unknown-length buckets to force a size
00733  * @param length Returns the length of the brigade (up to the end, or up
00734  *               to a bucket read error), or -1 if the brigade has buckets
00735  *               of indeterminate length and read_all is 0.
00736  */
00737 APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
00738                                              int read_all,
00739                                              apr_off_t *length);
00740 
00741 /**
00742  * Take a bucket brigade and store the data in a flat char*
00743  * @param bb The bucket brigade to create the char* from
00744  * @param c The char* to write into
00745  * @param len The maximum length of the char array. On return, it is the
00746  *            actual length of the char array.
00747  */
00748 APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
00749                                               char *c,
00750                                               apr_size_t *len);
00751 
00752 /**
00753  * Creates a pool-allocated string representing a flat bucket brigade
00754  * @param bb The bucket brigade to create the char array from
00755  * @param c On return, the allocated char array
00756  * @param len On return, the length of the char array.
00757  * @param pool The pool to allocate the string from.
00758  */
00759 APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb, 
00760                                                char **c,
00761                                                apr_size_t *len,
00762                                                apr_pool_t *pool);
00763 
00764 /**
00765  * Split a brigade to represent one LF line.
00766  * @param bbOut The bucket brigade that will have the LF line appended to.
00767  * @param bbIn The input bucket brigade to search for a LF-line.
00768  * @param block The blocking mode to be used to split the line.
00769  * @param maxbytes The maximum bytes to read.  If this many bytes are seen
00770  *                 without a LF, the brigade will contain a partial line.
00771  */
00772 APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
00773                                                  apr_bucket_brigade *bbIn,
00774                                                  apr_read_type_e block,
00775                                                  apr_off_t maxbytes);
00776 
00777 /**
00778  * Create an iovec of the elements in a bucket_brigade... return number 
00779  * of elements used.  This is useful for writing to a file or to the
00780  * network efficiently.
00781  * @param b The bucket brigade to create the iovec from
00782  * @param vec The iovec to create
00783  * @param nvec The number of elements in the iovec. On return, it is the
00784  *             number of iovec elements actually filled out.
00785  */
00786 APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b, 
00787                                                struct iovec *vec, int *nvec);
00788 
00789 /**
00790  * This function writes a list of strings into a bucket brigade. 
00791  * @param b The bucket brigade to add to
00792  * @param flush The flush function to use if the brigade is full
00793  * @param ctx The structure to pass to the flush function
00794  * @param va A list of strings to add
00795  * @return APR_SUCCESS or error code.
00796  */
00797 APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
00798                                                apr_brigade_flush flush,
00799                                                void *ctx,
00800                                                va_list va);
00801 
00802 /**
00803  * This function writes a string into a bucket brigade.
00804  *
00805  * The apr_brigade_write function attempts to be efficient with the
00806  * handling of heap buckets. Regardless of the amount of data stored
00807  * inside a heap bucket, heap buckets are a fixed size to promote their
00808  * reuse.
00809  *
00810  * If an attempt is made to write a string to a brigade that already 
00811  * ends with a heap bucket, this function will attempt to pack the
00812  * string into the remaining space in the previous heap bucket, before
00813  * allocating a new heap bucket.
00814  *
00815  * This function always returns APR_SUCCESS, unless a flush function is
00816  * passed, in which case the return value of the flush function will be
00817  * returned if used.
00818  * @param b The bucket brigade to add to
00819  * @param flush The flush function to use if the brigade is full
00820  * @param ctx The structure to pass to the flush function
00821  * @param str The string to add
00822  * @param nbyte The number of bytes to write
00823  * @return APR_SUCCESS or error code
00824  */
00825 APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
00826                                             apr_brigade_flush flush, void *ctx,
00827                                             const char *str, apr_size_t nbyte);
00828 
00829 /**
00830  * This function writes multiple strings into a bucket brigade.
00831  * @param b The bucket brigade to add to
00832  * @param flush The flush function to use if the brigade is full
00833  * @param ctx The structure to pass to the flush function
00834  * @param vec The strings to add (address plus length for each)
00835  * @param nvec The number of entries in iovec
00836  * @return APR_SUCCESS or error code
00837  */
00838 APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
00839                                              apr_brigade_flush flush,
00840                                              void *ctx,
00841                                              const struct iovec *vec,
00842                                              apr_size_t nvec);
00843 
00844 /**
00845  * This function writes a string into a bucket brigade.
00846  * @param bb The bucket brigade to add to
00847  * @param flush The flush function to use if the brigade is full
00848  * @param ctx The structure to pass to the flush function
00849  * @param str The string to add
00850  * @return APR_SUCCESS or error code
00851  */
00852 APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
00853                                            apr_brigade_flush flush, void *ctx,
00854                                            const char *str);
00855 
00856 /**
00857  * This function writes a character into a bucket brigade.
00858  * @param b The bucket brigade to add to
00859  * @param flush The flush function to use if the brigade is full
00860  * @param ctx The structure to pass to the flush function
00861  * @param c The character to add
00862  * @return APR_SUCCESS or error code
00863  */
00864 APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
00865                                            apr_brigade_flush flush, void *ctx,
00866                                            const char c);
00867 
00868 /**
00869  * This function writes an unspecified number of strings into a bucket brigade.
00870  * @param b The bucket brigade to add to
00871  * @param flush The flush function to use if the brigade is full
00872  * @param ctx The structure to pass to the flush function
00873  * @param ... The strings to add
00874  * @return APR_SUCCESS or error code
00875  */
00876 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
00877                                                      apr_brigade_flush flush,
00878                                                      void *ctx, ...);
00879 
00880 /**
00881  * Evaluate a printf and put the resulting string at the end 
00882  * of the bucket brigade.
00883  * @param b The brigade to write to
00884  * @param flush The flush function to use if the brigade is full
00885  * @param ctx The structure to pass to the flush function
00886  * @param fmt The format of the string to write
00887  * @param ... The arguments to fill out the format
00888  * @return APR_SUCCESS or error code
00889  */
00890 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b, 
00891                                                     apr_brigade_flush flush,
00892                                                     void *ctx,
00893                                                     const char *fmt, ...)
00894         __attribute__((format(printf,4,5)));
00895 
00896 /**
00897  * Evaluate a printf and put the resulting string at the end 
00898  * of the bucket brigade.
00899  * @param b The brigade to write to
00900  * @param flush The flush function to use if the brigade is full
00901  * @param ctx The structure to pass to the flush function
00902  * @param fmt The format of the string to write
00903  * @param va The arguments to fill out the format
00904  * @return APR_SUCCESS or error code
00905  */
00906 APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b, 
00907                                               apr_brigade_flush flush,
00908                                               void *ctx,
00909                                               const char *fmt, va_list va);
00910 
00911 /**
00912  * Utility function to insert a file (or a segment of a file) onto the
00913  * end of the brigade.  The file is split into multiple buckets if it
00914  * is larger than the maximum size which can be represented by a
00915  * single bucket.
00916  * @param bb the brigade to insert into
00917  * @param f the file to insert
00918  * @param start the offset of the start of the segment
00919  * @param len the length of the segment of the file to insert
00920  * @param p pool from which file buckets are allocated
00921  * @return the last bucket inserted
00922  */
00923 APU_DECLARE(apr_bucket *) apr_brigade_insert_file(apr_bucket_brigade *bb,
00924                                                   apr_file_t *f,
00925                                                   apr_off_t start,
00926                                                   apr_off_t len,
00927                                                   apr_pool_t *p);
00928 
00929 
00930 
00931 /*  *****  Bucket freelist functions *****  */
00932 /**
00933  * Create a bucket allocator.
00934  * @param p This pool's underlying apr_allocator_t is used to allocate memory
00935  *          for the bucket allocator.  When the pool is destroyed, the bucket
00936  *          allocator's cleanup routine will free all memory that has been
00937  *          allocated from it.
00938  * @remark  The reason the allocator gets its memory from the pool's
00939  *          apr_allocator_t rather than from the pool itself is because
00940  *          the bucket allocator will free large memory blocks back to the
00941  *          allocator when it's done with them, thereby preventing memory
00942  *          footprint growth that would occur if we allocated from the pool.
00943  * @warning The allocator must never be used by more than one thread at a time.
00944  */
00945 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
00946 
00947 /**
00948  * Create a bucket allocator.
00949  * @param allocator This apr_allocator_t is used to allocate both the bucket
00950  *          allocator and all memory handed out by the bucket allocator.  The
00951  *          caller is responsible for destroying the bucket allocator and the
00952  *          apr_allocator_t -- no automatic cleanups will happen.
00953  * @warning The allocator must never be used by more than one thread at a time.
00954  */
00955 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
00956 
00957 /**
00958  * Destroy a bucket allocator.
00959  * @param list The allocator to be destroyed
00960  */
00961 APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
00962 
00963 /**
00964  * Allocate memory for use by the buckets.
00965  * @param size The amount to allocate.
00966  * @param list The allocator from which to allocate the memory.
00967  */
00968 APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
00969 
00970 /**
00971  * Free memory previously allocated with apr_bucket_alloc().
00972  * @param block The block of memory to be freed.
00973  */
00974 APU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
00975 
00976 
00977 /*  *****  Bucket Functions  *****  */
00978 /**
00979  * Free the resources used by a bucket. If multiple buckets refer to
00980  * the same resource it is freed when the last one goes away.
00981  * @see apr_bucket_delete()
00982  * @param e The bucket to destroy
00983  */
00984 #define apr_bucket_destroy(e) do {                                      \
00985         (e)->type->destroy((e)->data);                                  \
00986         (e)->free(e);                                                   \
00987     } while (0)
00988 
00989 /**
00990  * Delete a bucket by removing it from its brigade (if any) and then
00991  * destroying it.
00992  * @remark This mainly acts as an aid in avoiding code verbosity.  It is
00993  * the preferred exact equivalent to:
00994  * <pre>
00995  *      APR_BUCKET_REMOVE(e);
00996  *      apr_bucket_destroy(e);
00997  * </pre>
00998  * @param e The bucket to delete
00999  */
01000 #define apr_bucket_delete(e) do {                                       \
01001         APR_BUCKET_REMOVE(e);                                           \
01002         apr_bucket_destroy(e);                                          \
01003     } while (0)
01004 
01005 /**
01006  * Read some data from the bucket.
01007  *
01008  * The apr_bucket_read function returns a convenient amount of data
01009  * from the bucket provided, writing the address and length of the
01010  * data to the pointers provided by the caller. The function tries
01011  * as hard as possible to avoid a memory copy.
01012  *
01013  * Buckets are expected to be a member of a brigade at the time they
01014  * are read.
01015  *
01016  * In typical application code, buckets are read in a loop, and after
01017  * each bucket is read and processed, it is moved or deleted from the
01018  * brigade and the next bucket read.
01019  *
01020  * The definition of "convenient" depends on the type of bucket that
01021  * is being read, and is decided by APR. In the case of memory based
01022  * buckets such as heap and immortal buckets, a pointer will be
01023  * returned to the location of the buffer containing the complete
01024  * contents of the bucket.
01025  *
01026  * Some buckets, such as the socket bucket, might have no concept
01027  * of length. If an attempt is made to read such a bucket, the
01028  * apr_bucket_read function will read a convenient amount of data
01029  * from the socket. The socket bucket is magically morphed into a
01030  * heap bucket containing the just-read data, and a new socket bucket
01031  * is inserted just after this heap bucket.
01032  *
01033  * To understand why apr_bucket_read might do this, consider the loop
01034  * described above to read and process buckets. The current bucket
01035  * is magically morphed into a heap bucket and returned to the caller.
01036  * The caller processes the data, and deletes the heap bucket, moving
01037  * onto the next bucket, the new socket bucket. This process repeats,
01038  * giving the illusion of a bucket brigade that contains potentially
01039  * infinite amounts of data. It is up to the caller to decide at what
01040  * point to stop reading buckets.
01041  *
01042  * Some buckets, such as the file bucket, might have a fixed size,
01043  * but be significantly larger than is practical to store in RAM in
01044  * one go. As with the socket bucket, if an attempt is made to read
01045  * from a file bucket, the file bucket is magically morphed into a
01046  * heap bucket containing a convenient amount of data read from the
01047  * current offset in the file. During the read, the offset will be
01048  * moved forward on the file, and a new file bucket will be inserted
01049  * directly after the current bucket representing the remainder of the
01050  * file. If the heap bucket was large enough to store the whole
01051  * remainder of the file, no more file buckets are inserted, and the
01052  * file bucket will disappear completely.
01053  *
01054  * The pattern for reading buckets described above does create the
01055  * illusion that the code is willing to swallow buckets that might be
01056  * too large for the system to handle in one go. This however is just
01057  * an illusion: APR will always ensure that large (file) or infinite
01058  * (socket) buckets are broken into convenient bite sized heap buckets
01059  * before data is returned to the caller.
01060  *
01061  * There is a potential gotcha to watch for: if buckets are read in a
01062  * loop, and aren't deleted after being processed, the potentially large
01063  * bucket will slowly be converted into RAM resident heap buckets. If
01064  * the file is larger than available RAM, an out of memory condition
01065  * could be caused if the application is not careful to manage this.
01066  *
01067  * @param e The bucket to read from
01068  * @param str The location to store a pointer to the data in
01069  * @param len The location to store the amount of data read
01070  * @param block Whether the read function blocks
01071  */
01072 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
01073 
01074 /**
01075  * Setaside data so that stack data is not destroyed on returning from
01076  * the function
01077  * @param e The bucket to setaside
01078  * @param p The pool to setaside into
01079  */
01080 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
01081 
01082 /**
01083  * Split one bucket in two at the point provided.
01084  * 
01085  * Once split, the original bucket becomes the first of the two new buckets.
01086  * 
01087  * (It is assumed that the bucket is a member of a brigade when this
01088  * function is called).
01089  * @param e The bucket to split
01090  * @param point The offset to split the bucket at
01091  */
01092 #define apr_bucket_split(e,point) (e)->type->split(e, point)
01093 
01094 /**
01095  * Copy a bucket.
01096  * @param e The bucket to copy
01097  * @param c Returns a pointer to the new bucket
01098  */
01099 #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
01100 
01101 /* Bucket type handling */
01102 
01103 /**
01104  * This function simply returns APR_SUCCESS to denote that the bucket does
01105  * not require anything to happen for its setaside() function. This is
01106  * appropriate for buckets that have "immortal" data -- the data will live
01107  * at least as long as the bucket.
01108  * @param data The bucket to setaside
01109  * @param pool The pool defining the desired lifetime of the bucket data
01110  * @return APR_SUCCESS
01111  */ 
01112 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
01113                                                           apr_pool_t *pool);
01114 
01115 /**
01116  * A place holder function that signifies that the setaside function was not
01117  * implemented for this bucket
01118  * @param data The bucket to setaside
01119  * @param pool The pool defining the desired lifetime of the bucket data
01120  * @return APR_ENOTIMPL
01121  */ 
01122 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
01123                                                              apr_pool_t *pool);
01124 
01125 /**
01126  * A place holder function that signifies that the split function was not
01127  * implemented for this bucket
01128  * @param data The bucket to split
01129  * @param point The location to split the bucket
01130  * @return APR_ENOTIMPL
01131  */ 
01132 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
01133                                                           apr_size_t point);
01134 
01135 /**
01136  * A place holder function that signifies that the copy function was not
01137  * implemented for this bucket
01138  * @param e The bucket to copy
01139  * @param c Returns a pointer to the new bucket
01140  * @return APR_ENOTIMPL
01141  */
01142 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
01143                                                          apr_bucket **c);
01144 
01145 /**
01146  * A place holder function that signifies that this bucket does not need
01147  * to do anything special to be destroyed.  That's only the case for buckets
01148  * that either have no data (metadata buckets) or buckets whose data pointer
01149  * points to something that's not a bucket-type-specific structure, as with
01150  * simple buckets where data points to a string and pipe buckets where data
01151  * points directly to the apr_file_t.
01152  * @param data The bucket data to destroy
01153  */ 
01154 APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
01155 
01156 /**
01157  * There is no apr_bucket_destroy_notimpl, because destruction is required
01158  * to be implemented (it could be a noop, but only if that makes sense for
01159  * the bucket type)
01160  */
01161 
01162 /* There is no apr_bucket_read_notimpl, because it is a required function
01163  */
01164 
01165 
01166 /* All of the bucket types implemented by the core */
01167 /**
01168  * The flush bucket type.  This signifies that all data should be flushed to
01169  * the next filter.  The flush bucket should be sent with the other buckets.
01170  */
01171 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
01172 /**
01173  * The EOS bucket type.  This signifies that there will be no more data, ever.
01174  * All filters MUST send all data to the next filter when they receive a
01175  * bucket of this type
01176  */
01177 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
01178 /**
01179  * The FILE bucket type.  This bucket represents a file on disk
01180  */
01181 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
01182 /**
01183  * The HEAP bucket type.  This bucket represents a data allocated from the
01184  * heap.
01185  */
01186 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
01187 #if APR_HAS_MMAP
01188 /**
01189  * The MMAP bucket type.  This bucket represents an MMAP'ed file
01190  */
01191 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
01192 #endif
01193 /**
01194  * The POOL bucket type.  This bucket represents a data that was allocated
01195  * from a pool.  IF this bucket is still available when the pool is cleared,
01196  * the data is copied on to the heap.
01197  */
01198 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
01199 /**
01200  * The PIPE bucket type.  This bucket represents a pipe to another program.
01201  */
01202 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
01203 /**
01204  * The IMMORTAL bucket type.  This bucket represents a segment of data that
01205  * the creator is willing to take responsibility for.  The core will do
01206  * nothing with the data in an immortal bucket
01207  */
01208 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
01209 /**
01210  * The TRANSIENT bucket type.  This bucket represents a data allocated off
01211  * the stack.  When the setaside function is called, this data is copied on
01212  * to the heap
01213  */
01214 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
01215 /**
01216  * The SOCKET bucket type.  This bucket represents a socket to another machine
01217  */
01218 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
01219 
01220 
01221 /*  *****  Simple buckets  *****  */
01222 
01223 /**
01224  * Split a simple bucket into two at the given point.  Most non-reference
01225  * counting buckets that allow multiple references to the same block of
01226  * data (eg transient and immortal) will use this as their split function
01227  * without any additional type-specific handling.
01228  * @param b The bucket to be split
01229  * @param point The offset of the first byte in the new bucket
01230  * @return APR_EINVAL if the point is not within the bucket;
01231  *         APR_ENOMEM if allocation failed;
01232  *         or APR_SUCCESS
01233  */
01234 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
01235                                                          apr_size_t point);
01236 
01237 /**
01238  * Copy a simple bucket.  Most non-reference-counting buckets that allow
01239  * multiple references to the same block of data (eg transient and immortal)
01240  * will use this as their copy function without any additional type-specific
01241  * handling.
01242  * @param a The bucket to copy
01243  * @param b Returns a pointer to the new bucket
01244  * @return APR_ENOMEM if allocation failed;
01245  *         or APR_SUCCESS
01246  */
01247 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
01248                                                         apr_bucket **b);
01249 
01250 
01251 /*  *****  Shared, reference-counted buckets  *****  */
01252 
01253 /**
01254  * Initialize a bucket containing reference-counted data that may be
01255  * shared. The caller must allocate the bucket if necessary and
01256  * initialize its type-dependent fields, and allocate and initialize
01257  * its own private data structure. This function should only be called
01258  * by type-specific bucket creation functions.
01259  * @param b The bucket to initialize
01260  * @param data A pointer to the private data structure
01261  *             with the reference count at the start
01262  * @param start The start of the data in the bucket
01263  *              relative to the private base pointer
01264  * @param length The length of the data in the bucket
01265  * @return The new bucket, or NULL if allocation failed
01266  */
01267 APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
01268                                                  apr_off_t start, 
01269                                                  apr_size_t length);
01270 
01271 /**
01272  * Decrement the refcount of the data in the bucket. This function
01273  * should only be called by type-specific bucket destruction functions.
01274  * @param data The private data pointer from the bucket to be destroyed
01275  * @return TRUE or FALSE; TRUE if the reference count is now
01276  *         zero, indicating that the shared resource itself can
01277  *         be destroyed by the caller.
01278  */
01279 APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
01280 
01281 /**
01282  * Split a bucket into two at the given point, and adjust the refcount
01283  * to the underlying data. Most reference-counting bucket types will
01284  * be able to use this function as their split function without any
01285  * additional type-specific handling.
01286  * @param b The bucket to be split
01287  * @param point The offset of the first byte in the new bucket
01288  * @return APR_EINVAL if the point is not within the bucket;
01289  *         APR_ENOMEM if allocation failed;
01290  *         or APR_SUCCESS
01291  */
01292 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
01293                                                          apr_size_t point);
01294 
01295 /**
01296  * Copy a refcounted bucket, incrementing the reference count. Most
01297  * reference-counting bucket types will be able to use this function
01298  * as their copy function without any additional type-specific handling.
01299  * @param a The bucket to copy
01300  * @param b Returns a pointer to the new bucket
01301  * @return APR_ENOMEM if allocation failed;
01302            or APR_SUCCESS
01303  */
01304 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
01305                                                         apr_bucket **b);
01306 
01307 
01308 /*  *****  Functions to Create Buckets of varying types  *****  */
01309 /*
01310  * Each bucket type foo has two initialization functions:
01311  * apr_bucket_foo_make which sets up some already-allocated memory as a
01312  * bucket of type foo; and apr_bucket_foo_create which allocates memory
01313  * for the bucket, calls apr_bucket_make_foo, and initializes the
01314  * bucket's list pointers. The apr_bucket_foo_make functions are used
01315  * inside the bucket code to change the type of buckets in place;
01316  * other code should call apr_bucket_foo_create. All the initialization
01317  * functions change nothing if they fail.
01318  */
01319 
01320 /**
01321  * Create an End of Stream bucket.  This indicates that there is no more data
01322  * coming from down the filter stack.  All filters should flush at this point.
01323  * @param list The freelist from which this bucket should be allocated
01324  * @return The new bucket, or NULL if allocation failed
01325  */
01326 APU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
01327 
01328 /**
01329  * Make the bucket passed in an EOS bucket.  This indicates that there is no 
01330  * more data coming from down the filter stack.  All filters should flush at 
01331  * this point.
01332  * @param b The bucket to make into an EOS bucket
01333  * @return The new bucket, or NULL if allocation failed
01334  */
01335 APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
01336 
01337 /**
01338  * Create a flush  bucket.  This indicates that filters should flush their
01339  * data.  There is no guarantee that they will flush it, but this is the
01340  * best we can do.
01341  * @param list The freelist from which this bucket should be allocated
01342  * @return The new bucket, or NULL if allocation failed
01343  */
01344 APU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
01345 
01346 /**
01347  * Make the bucket passed in a FLUSH  bucket.  This indicates that filters 
01348  * should flush their data.  There is no guarantee that they will flush it, 
01349  * but this is the best we can do.
01350  * @param b The bucket to make into a FLUSH bucket
01351  * @return The new bucket, or NULL if allocation failed
01352  */
01353 APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
01354 
01355 /**
01356  * Create a bucket referring to long-lived data.
01357  * @param buf The data to insert into the bucket
01358  * @param nbyte The size of the data to insert.
01359  * @param list The freelist from which this bucket should be allocated
01360  * @return The new bucket, or NULL if allocation failed
01361  */
01362 APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf, 
01363                                                      apr_size_t nbyte,
01364                                                      apr_bucket_alloc_t *list);
01365 
01366 /**
01367  * Make the bucket passed in a bucket refer to long-lived data
01368  * @param b The bucket to make into a IMMORTAL bucket
01369  * @param buf The data to insert into the bucket
01370  * @param nbyte The size of the data to insert.
01371  * @return The new bucket, or NULL if allocation failed
01372  */
01373 APU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b, 
01374                                                    const char *buf, 
01375                                                    apr_size_t nbyte);
01376 
01377 /**
01378  * Create a bucket referring to data on the stack.
01379  * @param buf The data to insert into the bucket
01380  * @param nbyte The size of the data to insert.
01381  * @param list The freelist from which this bucket should be allocated
01382  * @return The new bucket, or NULL if allocation failed
01383  */
01384 APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf, 
01385                                                       apr_size_t nbyte,
01386                                                       apr_bucket_alloc_t *list);
01387 
01388 /**
01389  * Make the bucket passed in a bucket refer to stack data
01390  * @param b The bucket to make into a TRANSIENT bucket
01391  * @param buf The data to insert into the bucket
01392  * @param nbyte The size of the data to insert.
01393  * @return The new bucket, or NULL if allocation failed
01394  */
01395 APU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b, 
01396                                                     const char *buf,
01397                                                     apr_size_t nbyte);
01398 
01399 /**
01400  * Create a bucket referring to memory on the heap. If the caller asks
01401  * for the data to be copied, this function always allocates 4K of
01402  * memory so that more data can be added to the bucket without
01403  * requiring another allocation. Therefore not all the data may be put
01404  * into the bucket. If copying is not requested then the bucket takes
01405  * over responsibility for free()ing the memory.
01406  * @param buf The buffer to insert into the bucket
01407  * @param nbyte The size of the buffer to insert.
01408  * @param free_func Function to use to free the data; NULL indicates that the
01409  *                  bucket should make a copy of the data
01410  * @param list The freelist from which this bucket should be allocated
01411  * @return The new bucket, or NULL if allocation failed
01412  */
01413 APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf, 
01414                                                  apr_size_t nbyte,
01415                                                  void (*free_func)(void *data),
01416                                                  apr_bucket_alloc_t *list);
01417 /**
01418  * Make the bucket passed in a bucket refer to heap data
01419  * @param b The bucket to make into a HEAP bucket
01420  * @param buf The buffer to insert into the bucket
01421  * @param nbyte The size of the buffer to insert.
01422  * @param free_func Function to use to free the data; NULL indicates that the
01423  *                  bucket should make a copy of the data
01424  * @return The new bucket, or NULL if allocation failed
01425  */
01426 APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
01427                                                apr_size_t nbyte,
01428                                                void (*free_func)(void *data));
01429 
01430 /**
01431  * Create a bucket referring to memory allocated from a pool.
01432  *
01433  * @param buf The buffer to insert into the bucket
01434  * @param length The number of bytes referred to by this bucket
01435  * @param pool The pool the memory was allocated from
01436  * @param list The freelist from which this bucket should be allocated
01437  * @return The new bucket, or NULL if allocation failed
01438  */
01439 APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf, 
01440                                                  apr_size_t length,
01441                                                  apr_pool_t *pool,
01442                                                  apr_bucket_alloc_t *list);
01443 
01444 /**
01445  * Make the bucket passed in a bucket refer to pool data
01446  * @param b The bucket to make into a pool bucket
01447  * @param buf The buffer to insert into the bucket
01448  * @param length The number of bytes referred to by this bucket
01449  * @param pool The pool the memory was allocated from
01450  * @return The new bucket, or NULL if allocation failed
01451  */
01452 APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
01453                                                apr_size_t length, 
01454                                                apr_pool_t *pool);
01455 
01456 #if APR_HAS_MMAP
01457 /**
01458  * Create a bucket referring to mmap()ed memory.
01459  * @param mm The mmap to insert into the bucket
01460  * @param start The offset of the first byte in the mmap
01461  *              that this bucket refers to
01462  * @param length The number of bytes referred to by this bucket
01463  * @param list The freelist from which this bucket should be allocated
01464  * @return The new bucket, or NULL if allocation failed
01465  */
01466 APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm, 
01467                                                  apr_off_t start,
01468                                                  apr_size_t length,
01469                                                  apr_bucket_alloc_t *list);
01470 
01471 /**
01472  * Make the bucket passed in a bucket refer to an MMAP'ed file
01473  * @param b The bucket to make into a MMAP bucket
01474  * @param mm The mmap to insert into the bucket
01475  * @param start The offset of the first byte in the mmap
01476  *              that this bucket refers to
01477  * @param length The number of bytes referred to by this bucket
01478  * @return The new bucket, or NULL if allocation failed
01479  */
01480 APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
01481                                                apr_off_t start, 
01482                                                apr_size_t length);
01483 #endif
01484 
01485 /**
01486  * Create a bucket referring to a socket.
01487  * @param thissock The socket to put in the bucket
01488  * @param list The freelist from which this bucket should be allocated
01489  * @return The new bucket, or NULL if allocation failed
01490  */
01491 APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
01492                                                    apr_bucket_alloc_t *list);
01493 /**
01494  * Make the bucket passed in a bucket refer to a socket
01495  * @param b The bucket to make into a SOCKET bucket
01496  * @param thissock The socket to put in the bucket
01497  * @return The new bucket, or NULL if allocation failed
01498  */
01499 APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b, 
01500                                                  apr_socket_t *thissock);
01501 
01502 /**
01503  * Create a bucket referring to a pipe.
01504  * @param thispipe The pipe to put in the bucket
01505  * @param list The freelist from which this bucket should be allocated
01506  * @return The new bucket, or NULL if allocation failed
01507  */
01508 APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
01509                                                  apr_bucket_alloc_t *list);
01510 
01511 /**
01512  * Make the bucket passed in a bucket refer to a pipe
01513  * @param b The bucket to make into a PIPE bucket
01514  * @param thispipe The pipe to put in the bucket
01515  * @return The new bucket, or NULL if allocation failed
01516  */
01517 APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b, 
01518                                                apr_file_t *thispipe);
01519 
01520 /**
01521  * Create a bucket referring to a file.
01522  * @param fd The file to put in the bucket
01523  * @param offset The offset where the data of interest begins in the file
01524  * @param len The amount of data in the file we are interested in
01525  * @param p The pool into which any needed structures should be created
01526  *          while reading from this file bucket
01527  * @param list The freelist from which this bucket should be allocated
01528  * @return The new bucket, or NULL if allocation failed
01529  * @remark If the file is truncated such that the segment of the file
01530  * referenced by the bucket no longer exists, an attempt to read
01531  * from the bucket will fail with APR_EOF. 
01532  * @remark apr_brigade_insert_file() should generally be used to
01533  * insert files into brigades, since that function can correctly
01534  * handle large file issues.
01535  */
01536 APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
01537                                                  apr_off_t offset,
01538                                                  apr_size_t len, 
01539                                                  apr_pool_t *p,
01540                                                  apr_bucket_alloc_t *list);
01541 
01542 /**
01543  * Make the bucket passed in a bucket refer to a file
01544  * @param b The bucket to make into a FILE bucket
01545  * @param fd The file to put in the bucket
01546  * @param offset The offset where the data of interest begins in the file
01547  * @param len The amount of data in the file we are interested in
01548  * @param p The pool into which any needed structures should be created
01549  *          while reading from this file bucket
01550  * @return The new bucket, or NULL if allocation failed
01551  */
01552 APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
01553                                                apr_off_t offset,
01554                                                apr_size_t len, apr_pool_t *p);
01555 
01556 /**
01557  * Enable or disable memory-mapping for a FILE bucket (default is enabled)
01558  * @param b The bucket
01559  * @param enabled Whether memory-mapping should be enabled
01560  * @return APR_SUCCESS normally, or an error code if the operation fails
01561  */
01562 APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
01563                                                       int enabled);
01564 
01565 /** @} */
01566 #ifdef __cplusplus
01567 }
01568 #endif
01569 
01570 #endif /* !APR_BUCKETS_H */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines