upload apache
[bottlenecks.git] / rubbos / app / apache2 / include / apr_buckets.h
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /**
17  * @file apr_buckets.h
18  * @brief APR-UTIL Buckets/Bucket Brigades
19  */
20
21 #ifndef APR_BUCKETS_H
22 #define APR_BUCKETS_H
23
24 #if defined(APR_BUCKET_DEBUG) && !defined(APR_RING_DEBUG)
25 #define APR_RING_DEBUG
26 #endif
27
28 #include "apu.h"
29 #include "apr_network_io.h"
30 #include "apr_file_io.h"
31 #include "apr_general.h"
32 #include "apr_mmap.h"
33 #include "apr_errno.h"
34 #include "apr_ring.h"
35 #include "apr.h"
36 #if APR_HAVE_SYS_UIO_H
37 #include <sys/uio.h>    /* for struct iovec */
38 #endif
39 #if APR_HAVE_STDARG_H
40 #include <stdarg.h>
41 #endif
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /**
48  * @defgroup APR_Util_Bucket_Brigades Bucket Brigades
49  * @ingroup APR_Util
50  * @{ 
51  */
52
53 /** default bucket buffer size - 8KB minus room for memory allocator headers */
54 #define APR_BUCKET_BUFF_SIZE 8000
55
56 /** Determines how a bucket or brigade should be read */
57 typedef enum {
58     APR_BLOCK_READ,   /**< block until data becomes available */
59     APR_NONBLOCK_READ /**< return immediately if no data is available */
60 } apr_read_type_e;
61
62 /**
63  * The one-sentence buzzword-laden overview: Bucket brigades represent
64  * a complex data stream that can be passed through a layered IO
65  * system without unnecessary copying. A longer overview follows...
66  *
67  * A bucket brigade is a doubly linked list (ring) of buckets, so we
68  * aren't limited to inserting at the front and removing at the end.
69  * Buckets are only passed around as members of a brigade, although
70  * singleton buckets can occur for short periods of time.
71  *
72  * Buckets are data stores of various types. They can refer to data in
73  * memory, or part of a file or mmap area, or the output of a process,
74  * etc. Buckets also have some type-dependent accessor functions:
75  * read, split, copy, setaside, and destroy.
76  *
77  * read returns the address and size of the data in the bucket. If the
78  * data isn't in memory then it is read in and the bucket changes type
79  * so that it can refer to the new location of the data. If all the
80  * data doesn't fit in the bucket then a new bucket is inserted into
81  * the brigade to hold the rest of it.
82  *
83  * split divides the data in a bucket into two regions. After a split
84  * the original bucket refers to the first part of the data and a new
85  * bucket inserted into the brigade after the original bucket refers
86  * to the second part of the data. Reference counts are maintained as
87  * necessary.
88  *
89  * setaside ensures that the data in the bucket has a long enough
90  * lifetime. Sometimes it is convenient to create a bucket referring
91  * to data on the stack in the expectation that it will be consumed
92  * (output to the network) before the stack is unwound. If that
93  * expectation turns out not to be valid, the setaside function is
94  * called to move the data somewhere safer.
95  *
96  * copy makes a duplicate of the bucket structure as long as it's
97  * possible to have multiple references to a single copy of the
98  * data itself.  Not all bucket types can be copied.
99  *
100  * destroy maintains the reference counts on the resources used by a
101  * bucket and frees them if necessary.
102  *
103  * Note: all of the above functions have wrapper macros (apr_bucket_read(),
104  * apr_bucket_destroy(), etc), and those macros should be used rather
105  * than using the function pointers directly.
106  *
107  * To write a bucket brigade, they are first made into an iovec, so that we
108  * don't write too little data at one time.  Currently we ignore compacting the
109  * buckets into as few buckets as possible, but if we really want good
110  * performance, then we need to compact the buckets before we convert to an
111  * iovec, or possibly while we are converting to an iovec.
112  */
113
114 /*
115  * Forward declaration of the main types.
116  */
117
118 /** @see apr_bucket_brigade */
119 typedef struct apr_bucket_brigade apr_bucket_brigade;
120 /** @see apr_bucket */
121 typedef struct apr_bucket apr_bucket;
122 /** @see apr_bucket_alloc_t */
123 typedef struct apr_bucket_alloc_t apr_bucket_alloc_t;
124
125 /** @see apr_bucket_type_t */
126 typedef struct apr_bucket_type_t apr_bucket_type_t;
127
128 /**
129  * Basic bucket type
130  */
131 struct apr_bucket_type_t {
132     /**
133      * The name of the bucket type
134      */
135     const char *name;
136     /** 
137      * The number of functions this bucket understands.  Can not be less than
138      * five.
139      */
140     int num_func;
141     /**
142      * Whether the bucket contains metadata (ie, information that
143      * describes the regular contents of the brigade).  The metadata
144      * is not returned by apr_bucket_read() and is not indicated by
145      * the ->length of the apr_bucket itself.  In other words, an
146      * empty bucket is safe to arbitrarily remove if and only if it
147      * contains no metadata.  In this sense, "data" is just raw bytes
148      * that are the "content" of the brigade and "metadata" describes
149      * that data but is not a proper part of it.
150      */
151     enum {
152         /** This bucket type represents actual data to send to the client. */
153         APR_BUCKET_DATA = 0,
154         /** This bucket type represents metadata. */
155         APR_BUCKET_METADATA = 1
156     } is_metadata;
157     /**
158      * Free the private data and any resources used by the bucket (if they
159      *  aren't shared with another bucket).  This function is required to be
160      *  implemented for all bucket types, though it might be a no-op on some
161      *  of them (namely ones that never allocate any private data structures).
162      * @param data The private data pointer from the bucket to be destroyed
163      */
164     void (*destroy)(void *data);
165
166     /**
167      * Read the data from the bucket. This is required to be implemented
168      *  for all bucket types.
169      * @param b The bucket to read from
170      * @param str A place to store the data read.  Allocation should only be
171      *            done if absolutely necessary. 
172      * @param len The amount of data read.
173      * @param block Should this read function block if there is more data that
174      *              cannot be read immediately.
175      */
176     apr_status_t (*read)(apr_bucket *b, const char **str, apr_size_t *len, 
177                          apr_read_type_e block);
178     
179     /**
180      * Make it possible to set aside the data for at least as long as the
181      *  given pool. Buckets containing data that could potentially die before
182      *  this pool (e.g. the data resides on the stack, in a child pool of
183      *  the given pool, or in a disjoint pool) must somehow copy, shift, or
184      *  transform the data to have the proper lifetime.
185      * @param e The bucket to convert
186      * @remark Some bucket types contain data that will always outlive the
187      *         bucket itself. For example no data (EOS and FLUSH), or the data
188      *         resides in global, constant memory (IMMORTAL), or the data is on
189      *      the heap (HEAP). For these buckets, apr_bucket_setaside_noop can
190      *      be used.
191      */
192     apr_status_t (*setaside)(apr_bucket *e, apr_pool_t *pool);
193
194     /**
195      * Split one bucket in two at the specified position by duplicating
196      *  the bucket structure (not the data) and modifying any necessary
197      *  start/end/offset information.  If it's not possible to do this
198      *  for the bucket type (perhaps the length of the data is indeterminate,
199      *  as with pipe and socket buckets), then APR_ENOTIMPL is returned.
200      * @param e The bucket to split
201      * @param point The offset of the first byte in the new bucket
202      */
203     apr_status_t (*split)(apr_bucket *e, apr_size_t point);
204
205     /**
206      * Copy the bucket structure (not the data), assuming that this is
207      *  possible for the bucket type. If it's not, APR_ENOTIMPL is returned.
208      * @param e The bucket to copy
209      * @param c Returns a pointer to the new bucket
210      */
211     apr_status_t (*copy)(apr_bucket *e, apr_bucket **c);
212
213 };
214
215 /**
216  * apr_bucket structures are allocated on the malloc() heap and
217  * their lifetime is controlled by the parent apr_bucket_brigade
218  * structure. Buckets can move from one brigade to another e.g. by
219  * calling APR_BRIGADE_CONCAT(). In general the data in a bucket has
220  * the same lifetime as the bucket and is freed when the bucket is
221  * destroyed; if the data is shared by more than one bucket (e.g.
222  * after a split) the data is freed when the last bucket goes away.
223  */
224 struct apr_bucket {
225     /** Links to the rest of the brigade */
226     APR_RING_ENTRY(apr_bucket) link;
227     /** The type of bucket.  */
228     const apr_bucket_type_t *type;
229     /** The length of the data in the bucket.  This could have been implemented
230      *  with a function, but this is an optimization, because the most
231      *  common thing to do will be to get the length.  If the length is unknown,
232      *  the value of this field will be (apr_size_t)(-1).
233      */
234     apr_size_t length;
235     /** The start of the data in the bucket relative to the private base
236      *  pointer.  The vast majority of bucket types allow a fixed block of
237      *  data to be referenced by multiple buckets, each bucket pointing to
238      *  a different segment of the data.  That segment starts at base+start
239      *  and ends at base+start+length.  
240      *  If the length == (apr_size_t)(-1), then start == -1.
241      */
242     apr_off_t start;
243     /** type-dependent data hangs off this pointer */
244     void *data; 
245     /**
246      * Pointer to function used to free the bucket. This function should
247      * always be defined and it should be consistent with the memory
248      * function used to allocate the bucket. For example, if malloc() is 
249      * used to allocate the bucket, this pointer should point to free().
250      * @param e Pointer to the bucket being freed
251      */
252     void (*free)(void *e);
253     /** The freelist from which this bucket was allocated */
254     apr_bucket_alloc_t *list;
255 };
256
257 /** A list of buckets */
258 struct apr_bucket_brigade {
259     /** The pool to associate the brigade with.  The data is not allocated out
260      *  of the pool, but a cleanup is registered with this pool.  If the 
261      *  brigade is destroyed by some mechanism other than pool destruction,
262      *  the destroying function is responsible for killing the cleanup.
263      */
264     apr_pool_t *p;
265     /** The buckets in the brigade are on this list. */
266     /*
267      * The apr_bucket_list structure doesn't actually need a name tag
268      * because it has no existence independent of struct apr_bucket_brigade;
269      * the ring macros are designed so that you can leave the name tag
270      * argument empty in this situation but apparently the Windows compiler
271      * doesn't like that.
272      */
273     APR_RING_HEAD(apr_bucket_list, apr_bucket) list;
274     /** The freelist from which this bucket was allocated */
275     apr_bucket_alloc_t *bucket_alloc;
276 };
277
278
279 /**
280  * Function called when a brigade should be flushed
281  */
282 typedef apr_status_t (*apr_brigade_flush)(apr_bucket_brigade *bb, void *ctx);
283
284 /*
285  * define APR_BUCKET_DEBUG if you want your brigades to be checked for
286  * validity at every possible instant.  this will slow your code down
287  * substantially but is a very useful debugging tool.
288  */
289 #ifdef APR_BUCKET_DEBUG
290
291 #define APR_BRIGADE_CHECK_CONSISTENCY(b)                                \
292         APR_RING_CHECK_CONSISTENCY(&(b)->list, apr_bucket, link)
293
294 #define APR_BUCKET_CHECK_CONSISTENCY(e)                                 \
295         APR_RING_CHECK_ELEM_CONSISTENCY((e), apr_bucket, link)
296
297 #else
298 /**
299  * checks the ring pointers in a bucket brigade for consistency.  an
300  * abort() will be triggered if any inconsistencies are found.
301  *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
302  * @param b The brigade
303  */
304 #define APR_BRIGADE_CHECK_CONSISTENCY(b)
305 /**
306  * checks the brigade a bucket is in for ring consistency.  an
307  * abort() will be triggered if any inconsistencies are found.
308  *   note: this is a no-op unless APR_BUCKET_DEBUG is defined.
309  * @param e The bucket
310  */
311 #define APR_BUCKET_CHECK_CONSISTENCY(e)
312 #endif
313
314
315 /**
316  * Wrappers around the RING macros to reduce the verbosity of the code
317  * that handles bucket brigades.
318  */
319 /**
320  * The magic pointer value that indicates the head of the brigade
321  * @remark This is used to find the beginning and end of the brigade, eg:
322  * <pre>
323  *      while (e != APR_BRIGADE_SENTINEL(b)) {
324  *          ...
325  *          e = APR_BUCKET_NEXT(e);
326  *      }
327  * </pre>
328  * @param  b The brigade
329  * @return The magic pointer value
330  */
331 #define APR_BRIGADE_SENTINEL(b) APR_RING_SENTINEL(&(b)->list, apr_bucket, link)
332
333 /**
334  * Determine if the bucket brigade is empty
335  * @param b The brigade to check
336  * @return true or false
337  */
338 #define APR_BRIGADE_EMPTY(b)    APR_RING_EMPTY(&(b)->list, apr_bucket, link)
339
340 /**
341  * Return the first bucket in a brigade
342  * @param b The brigade to query
343  * @return The first bucket in the brigade
344  */
345 #define APR_BRIGADE_FIRST(b)    APR_RING_FIRST(&(b)->list)
346 /**
347  * Return the last bucket in a brigade
348  * @param b The brigade to query
349  * @return The last bucket in the brigade
350  */
351 #define APR_BRIGADE_LAST(b)     APR_RING_LAST(&(b)->list)
352
353 /**
354  * Iterate through a bucket brigade
355  * @param e The current bucket
356  * @param b The brigade to iterate over
357  * @remark This is the same as either:
358  * <pre>
359  *      e = APR_BRIGADE_FIRST(b);
360  *      while (e != APR_BRIGADE_SENTINEL(b)) {
361  *          ...
362  *          e = APR_BUCKET_NEXT(e);
363  *      }
364  *  OR
365  *      for (e = APR_BRIGADE_FIRST(b);
366  *           e != APR_BRIGADE_SENTINEL(b);
367  *           e = APR_BUCKET_NEXT(e)) {
368  *          ...
369  *      }
370  * </pre>
371  * @warning Be aware that you cannot change the value of e within
372  * the foreach loop, nor can you destroy the bucket it points to.
373  * Modifying the prev and next pointers of the bucket is dangerous
374  * but can be done if you're careful.  If you change e's value or
375  * destroy the bucket it points to, then APR_BRIGADE_FOREACH
376  * will have no way to find out what bucket to use for its next
377  * iteration.  The reason for this can be seen by looking closely
378  * at the equivalent loops given in the tip above.  So, for example,
379  * if you are writing a loop that empties out a brigade one bucket
380  * at a time, APR_BRIGADE_FOREACH just won't work for you.  Do it
381  * by hand, like so:
382  * <pre>
383  *      while (!APR_BRIGADE_EMPTY(b)) {
384  *          e = APR_BRIGADE_FIRST(b);
385  *          ...
386  *          apr_bucket_delete(e);
387  *      }
388  * </pre>
389  * @deprecated This macro causes more headaches than it's worth.  Use
390  * one of the alternatives documented here instead; the clarity gained
391  * in what's really going on is well worth the extra line or two of code.
392  * This macro will be removed at some point in the future.
393  */
394 #define APR_BRIGADE_FOREACH(e, b)                                       \
395         APR_RING_FOREACH((e), &(b)->list, apr_bucket, link)
396
397 /**
398  * Insert a list of buckets at the front of a brigade
399  * @param b The brigade to add to
400  * @param e The first bucket in a list of buckets to insert
401  */
402 #define APR_BRIGADE_INSERT_HEAD(b, e) do {                              \
403         apr_bucket *ap__b = (e);                                        \
404         APR_RING_INSERT_HEAD(&(b)->list, ap__b, apr_bucket, link);      \
405         APR_BRIGADE_CHECK_CONSISTENCY((b));                             \
406     } while (0)
407
408 /**
409  * Insert a list of buckets at the end of a brigade
410  * @param b The brigade to add to
411  * @param e The first bucket in a list of buckets to insert
412  */
413 #define APR_BRIGADE_INSERT_TAIL(b, e) do {                              \
414         apr_bucket *ap__b = (e);                                        \
415         APR_RING_INSERT_TAIL(&(b)->list, ap__b, apr_bucket, link);      \
416         APR_BRIGADE_CHECK_CONSISTENCY((b));                             \
417     } while (0)
418
419 /**
420  * Concatenate brigade b onto the end of brigade a, leaving brigade b empty
421  * @param a The first brigade
422  * @param b The second brigade
423  */
424 #define APR_BRIGADE_CONCAT(a, b) do {                                   \
425         APR_RING_CONCAT(&(a)->list, &(b)->list, apr_bucket, link);      \
426         APR_BRIGADE_CHECK_CONSISTENCY((a));                             \
427     } while (0)
428
429 /**
430  * Prepend brigade b onto the beginning of brigade a, leaving brigade b empty
431  * @param a The first brigade
432  * @param b The second brigade
433  */
434 #define APR_BRIGADE_PREPEND(a, b) do {                                  \
435         APR_RING_PREPEND(&(a)->list, &(b)->list, apr_bucket, link);     \
436         APR_BRIGADE_CHECK_CONSISTENCY((a));                             \
437     } while (0)
438
439 /**
440  * Insert a list of buckets before a specified bucket
441  * @param a The bucket to insert before
442  * @param b The buckets to insert
443  */
444 #define APR_BUCKET_INSERT_BEFORE(a, b) do {                             \
445         apr_bucket *ap__a = (a), *ap__b = (b);                          \
446         APR_RING_INSERT_BEFORE(ap__a, ap__b, link);                     \
447         APR_BUCKET_CHECK_CONSISTENCY(ap__a);                            \
448     } while (0)
449
450 /**
451  * Insert a list of buckets after a specified bucket
452  * @param a The bucket to insert after
453  * @param b The buckets to insert
454  */
455 #define APR_BUCKET_INSERT_AFTER(a, b) do {                              \
456         apr_bucket *ap__a = (a), *ap__b = (b);                          \
457         APR_RING_INSERT_AFTER(ap__a, ap__b, link);                      \
458         APR_BUCKET_CHECK_CONSISTENCY(ap__a);                            \
459     } while (0)
460
461 /**
462  * Get the next bucket in the list
463  * @param e The current bucket
464  * @return The next bucket
465  */
466 #define APR_BUCKET_NEXT(e)      APR_RING_NEXT((e), link)
467 /**
468  * Get the previous bucket in the list
469  * @param e The current bucket
470  * @return The previous bucket
471  */
472 #define APR_BUCKET_PREV(e)      APR_RING_PREV((e), link)
473
474 /**
475  * Remove a bucket from its bucket brigade
476  * @param e The bucket to remove
477  */
478 #define APR_BUCKET_REMOVE(e)    APR_RING_REMOVE((e), link)
479
480 /**
481  * Initialize a new bucket's prev/next pointers
482  * @param e The bucket to initialize
483  */
484 #define APR_BUCKET_INIT(e)      APR_RING_ELEM_INIT((e), link)
485
486 /**
487  * Determine if a bucket contains metadata.  An empty bucket is
488  * safe to arbitrarily remove if and only if this is false.
489  * @param e The bucket to inspect
490  * @return true or false
491  */
492 #define APR_BUCKET_IS_METADATA(e)    ((e)->type->is_metadata)
493
494 /**
495  * Determine if a bucket is a FLUSH bucket
496  * @param e The bucket to inspect
497  * @return true or false
498  */
499 #define APR_BUCKET_IS_FLUSH(e)       ((e)->type == &apr_bucket_type_flush)
500 /**
501  * Determine if a bucket is an EOS bucket
502  * @param e The bucket to inspect
503  * @return true or false
504  */
505 #define APR_BUCKET_IS_EOS(e)         ((e)->type == &apr_bucket_type_eos)
506 /**
507  * Determine if a bucket is a FILE bucket
508  * @param e The bucket to inspect
509  * @return true or false
510  */
511 #define APR_BUCKET_IS_FILE(e)        ((e)->type == &apr_bucket_type_file)
512 /**
513  * Determine if a bucket is a PIPE bucket
514  * @param e The bucket to inspect
515  * @return true or false
516  */
517 #define APR_BUCKET_IS_PIPE(e)        ((e)->type == &apr_bucket_type_pipe)
518 /**
519  * Determine if a bucket is a SOCKET bucket
520  * @param e The bucket to inspect
521  * @return true or false
522  */
523 #define APR_BUCKET_IS_SOCKET(e)      ((e)->type == &apr_bucket_type_socket)
524 /**
525  * Determine if a bucket is a HEAP bucket
526  * @param e The bucket to inspect
527  * @return true or false
528  */
529 #define APR_BUCKET_IS_HEAP(e)        ((e)->type == &apr_bucket_type_heap)
530 /**
531  * Determine if a bucket is a TRANSIENT bucket
532  * @param e The bucket to inspect
533  * @return true or false
534  */
535 #define APR_BUCKET_IS_TRANSIENT(e)   ((e)->type == &apr_bucket_type_transient)
536 /**
537  * Determine if a bucket is a IMMORTAL bucket
538  * @param e The bucket to inspect
539  * @return true or false
540  */
541 #define APR_BUCKET_IS_IMMORTAL(e)    ((e)->type == &apr_bucket_type_immortal)
542 #if APR_HAS_MMAP
543 /**
544  * Determine if a bucket is a MMAP bucket
545  * @param e The bucket to inspect
546  * @return true or false
547  */
548 #define APR_BUCKET_IS_MMAP(e)        ((e)->type == &apr_bucket_type_mmap)
549 #endif
550 /**
551  * Determine if a bucket is a POOL bucket
552  * @param e The bucket to inspect
553  * @return true or false
554  */
555 #define APR_BUCKET_IS_POOL(e)        ((e)->type == &apr_bucket_type_pool)
556
557 /*
558  * General-purpose reference counting for the various bucket types.
559  *
560  * Any bucket type that keeps track of the resources it uses (i.e.
561  * most of them except for IMMORTAL, TRANSIENT, and EOS) needs to
562  * attach a reference count to the resource so that it can be freed
563  * when the last bucket that uses it goes away. Resource-sharing may
564  * occur because of bucket splits or buckets that refer to globally
565  * cached data. */
566
567 /** @see apr_bucket_refcount */
568 typedef struct apr_bucket_refcount apr_bucket_refcount;
569 /**
570  * The structure used to manage the shared resource must start with an
571  * apr_bucket_refcount which is updated by the general-purpose refcount
572  * code. A pointer to the bucket-type-dependent private data structure
573  * can be cast to a pointer to an apr_bucket_refcount and vice versa.
574  */
575 struct apr_bucket_refcount {
576     /** The number of references to this bucket */
577     int          refcount;
578 };
579
580 /*  *****  Reference-counted bucket types  *****  */
581
582 /** @see apr_bucket_heap */
583 typedef struct apr_bucket_heap apr_bucket_heap;
584 /**
585  * A bucket referring to data allocated off the heap.
586  */
587 struct apr_bucket_heap {
588     /** Number of buckets using this memory */
589     apr_bucket_refcount  refcount;
590     /** The start of the data actually allocated.  This should never be
591      * modified, it is only used to free the bucket.
592      */
593     char    *base;
594     /** how much memory was allocated */
595     apr_size_t  alloc_len;
596     /** function to use to delete the data */
597     void (*free_func)(void *data);
598 };
599
600 /** @see apr_bucket_pool */
601 typedef struct apr_bucket_pool apr_bucket_pool;
602 /**
603  * A bucket referring to data allocated from a pool
604  */
605 struct apr_bucket_pool {
606     /** The pool bucket must be able to be easily morphed to a heap
607      * bucket if the pool gets cleaned up before all references are
608      * destroyed.  This apr_bucket_heap structure is populated automatically
609      * when the pool gets cleaned up, and subsequent calls to pool_read()
610      * will result in the apr_bucket in question being morphed into a
611      * regular heap bucket.  (To avoid having to do many extra refcount
612      * manipulations and b->data manipulations, the apr_bucket_pool
613      * struct actually *contains* the apr_bucket_heap struct that it
614      * will become as its first element; the two share their
615      * apr_bucket_refcount members.)
616      */
617     apr_bucket_heap  heap;
618     /** The block of data actually allocated from the pool.
619      * Segments of this block are referenced by adjusting
620      * the start and length of the apr_bucket accordingly.
621      * This will be NULL after the pool gets cleaned up.
622      */
623     const char *base;
624     /** The pool the data was allocated from.  When the pool
625      * is cleaned up, this gets set to NULL as an indicator
626      * to pool_read() that the data is now on the heap and
627      * so it should morph the bucket into a regular heap
628      * bucket before continuing.
629      */
630     apr_pool_t *pool;
631     /** The freelist this structure was allocated from, which is
632      * needed in the cleanup phase in order to allocate space on the heap
633      */
634     apr_bucket_alloc_t *list;
635 };
636
637 #if APR_HAS_MMAP
638 /** @see apr_bucket_mmap */
639 typedef struct apr_bucket_mmap apr_bucket_mmap;
640 /**
641  * A bucket referring to an mmap()ed file
642  */
643 struct apr_bucket_mmap {
644     /** Number of buckets using this memory */
645     apr_bucket_refcount  refcount;
646     /** The mmap this sub_bucket refers to */
647     apr_mmap_t *mmap;
648 };
649 #endif
650
651 /** @see apr_bucket_file */
652 typedef struct apr_bucket_file apr_bucket_file;
653 /**
654  * A bucket referring to an file
655  */
656 struct apr_bucket_file {
657     /** Number of buckets using this memory */
658     apr_bucket_refcount  refcount;
659     /** The file this bucket refers to */
660     apr_file_t *fd;
661     /** The pool into which any needed structures should
662      *  be created while reading from this file bucket */
663     apr_pool_t *readpool;
664 #if APR_HAS_MMAP
665     /** Whether this bucket should be memory-mapped if
666      *  a caller tries to read from it */
667     int can_mmap;
668 #endif /* APR_HAS_MMAP */
669 };
670
671 /** @see apr_bucket_structs */
672 typedef union apr_bucket_structs apr_bucket_structs;
673 /**
674  * A union of all bucket structures so we know what
675  * the max size is.
676  */
677 union apr_bucket_structs {
678     apr_bucket      b;      /**< Bucket */
679     apr_bucket_heap heap;   /**< Heap */
680     apr_bucket_pool pool;   /**< Pool */
681 #if APR_HAS_MMAP
682     apr_bucket_mmap mmap;   /**< MMap */
683 #endif
684     apr_bucket_file file;   /**< File */
685 };
686
687 /**
688  * The amount that apr_bucket_alloc() should allocate in the common case.
689  * Note: this is twice as big as apr_bucket_structs to allow breathing
690  * room for third-party bucket types.
691  */
692 #define APR_BUCKET_ALLOC_SIZE  APR_ALIGN_DEFAULT(2*sizeof(apr_bucket_structs))
693
694 /*  *****  Bucket Brigade Functions  *****  */
695 /**
696  * Create a new bucket brigade.  The bucket brigade is originally empty.
697  * @param p The pool to associate with the brigade.  Data is not allocated out
698  *          of the pool, but a cleanup is registered.
699  * @param list The bucket allocator to use
700  * @return The empty bucket brigade
701  */
702 APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p,
703                                                      apr_bucket_alloc_t *list);
704
705 /**
706  * destroy an entire bucket brigade.  This includes destroying all of the
707  * buckets within the bucket brigade's bucket list. 
708  * @param b The bucket brigade to destroy
709  */
710 APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b);
711
712 /**
713  * empty out an entire bucket brigade.  This includes destroying all of the
714  * buckets within the bucket brigade's bucket list.  This is similar to
715  * apr_brigade_destroy(), except that it does not deregister the brigade's
716  * pool cleanup function.
717  * @param data The bucket brigade to clean up
718  * @remark Generally, you should use apr_brigade_destroy().  This function
719  *         can be useful in situations where you have a single brigade that
720  *         you wish to reuse many times by destroying all of the buckets in
721  *         the brigade and putting new buckets into it later.
722  */
723 APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data);
724
725 /**
726  * Split a bucket brigade into two, such that the given bucket is the
727  * first in the new bucket brigade. This function is useful when a
728  * filter wants to pass only the initial part of a brigade to the next
729  * filter.
730  * @param b The brigade to split
731  * @param e The first element of the new brigade
732  * @return The new brigade
733  */
734 APU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b,
735                                                     apr_bucket *e);
736
737 /**
738  * Partition a bucket brigade at a given offset (in bytes from the start of
739  * the brigade).  This is useful whenever a filter wants to use known ranges
740  * of bytes from the brigade; the ranges can even overlap.
741  * @param b The brigade to partition
742  * @param point The offset at which to partition the brigade
743  * @param after_point Returns a pointer to the first bucket after the partition
744  */
745 APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b,
746                                                 apr_off_t point,
747                                                 apr_bucket **after_point);
748
749 #if APR_NOT_DONE_YET
750 /**
751  * consume nbytes from beginning of b -- call apr_bucket_destroy as
752  * appropriate, and/or modify start on last element 
753  * @param b The brigade to consume data from
754  * @param nbytes The number of bytes to consume
755  */
756 APU_DECLARE(void) apr_brigade_consume(apr_bucket_brigade *b,
757                                       apr_off_t nbytes);
758 #endif
759
760 /**
761  * Return the total length of the brigade.
762  * @param bb The brigade to compute the length of
763  * @param read_all Read unknown-length buckets to force a size
764  * @param length Returns the length of the brigade, or -1 if the brigade has
765  *               buckets of indeterminate length and read_all is 0.
766  */
767 APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb,
768                                              int read_all,
769                                              apr_off_t *length);
770
771 /**
772  * Take a bucket brigade and store the data in a flat char*
773  * @param bb The bucket brigade to create the char* from
774  * @param c The char* to write into
775  * @param len The maximum length of the char array. On return, it is the
776  *            actual length of the char array.
777  */
778 APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb,
779                                               char *c,
780                                               apr_size_t *len);
781
782 /**
783  * Creates a pool-allocated string representing a flat bucket brigade
784  * @param bb The bucket brigade to create the char array from
785  * @param c On return, the allocated char array
786  * @param len On return, the length of the char array.
787  * @param pool The pool to allocate the string from.
788  */
789 APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb, 
790                                                char **c,
791                                                apr_size_t *len,
792                                                apr_pool_t *pool);
793
794 /**
795  * Split a brigade to represent one LF line.
796  * @param bbOut The bucket brigade that will have the LF line appended to.
797  * @param bbIn The input bucket brigade to search for a LF-line.
798  * @param block The blocking mode to be used to split the line.
799  * @param maxbytes The maximum bytes to read.  If this many bytes are seen
800  *                 without a LF, the brigade will contain a partial line.
801  */
802 APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut,
803                                                  apr_bucket_brigade *bbIn,
804                                                  apr_read_type_e block,
805                                                  apr_off_t maxbytes);
806
807 /**
808  * create an iovec of the elements in a bucket_brigade... return number 
809  * of elements used.  This is useful for writing to a file or to the
810  * network efficiently.
811  * @param b The bucket brigade to create the iovec from
812  * @param vec The iovec to create
813  * @param nvec The number of elements in the iovec. On return, it is the
814  *             number of iovec elements actually filled out.
815  */
816 APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b, 
817                                                struct iovec *vec, int *nvec);
818
819 /**
820  * This function writes a list of strings into a bucket brigade. 
821  * @param b The bucket brigade to add to
822  * @param flush The flush function to use if the brigade is full
823  * @param ctx The structure to pass to the flush function
824  * @param va A list of strings to add
825  * @return APR_SUCCESS or error code.
826  */
827 APU_DECLARE(apr_status_t) apr_brigade_vputstrs(apr_bucket_brigade *b,
828                                                apr_brigade_flush flush,
829                                                void *ctx,
830                                                va_list va);
831
832 /**
833  * This function writes a string into a bucket brigade.
834  * @param b The bucket brigade to add to
835  * @param flush The flush function to use if the brigade is full
836  * @param ctx The structure to pass to the flush function
837  * @param str The string to add
838  * @param nbyte The number of bytes to write
839  * @return APR_SUCCESS or error code
840  */
841 APU_DECLARE(apr_status_t) apr_brigade_write(apr_bucket_brigade *b,
842                                             apr_brigade_flush flush, void *ctx,
843                                             const char *str, apr_size_t nbyte);
844
845 /**
846  * This function writes multiple strings into a bucket brigade.
847  * @param b The bucket brigade to add to
848  * @param flush The flush function to use if the brigade is full
849  * @param ctx The structure to pass to the flush function
850  * @param vec The strings to add (address plus length for each)
851  * @param nvec The number of entries in iovec
852  * @return APR_SUCCESS or error code
853  */
854 APU_DECLARE(apr_status_t) apr_brigade_writev(apr_bucket_brigade *b,
855                                              apr_brigade_flush flush,
856                                              void *ctx,
857                                              const struct iovec *vec,
858                                              apr_size_t nvec);
859
860 /**
861  * This function writes a string into a bucket brigade.
862  * @param bb The bucket brigade to add to
863  * @param flush The flush function to use if the brigade is full
864  * @param ctx The structure to pass to the flush function
865  * @param str The string to add
866  * @return APR_SUCCESS or error code
867  */
868 APU_DECLARE(apr_status_t) apr_brigade_puts(apr_bucket_brigade *bb,
869                                            apr_brigade_flush flush, void *ctx,
870                                            const char *str);
871
872 /**
873  * This function writes a character into a bucket brigade.
874  * @param b The bucket brigade to add to
875  * @param flush The flush function to use if the brigade is full
876  * @param ctx The structure to pass to the flush function
877  * @param c The character to add
878  * @return APR_SUCCESS or error code
879  */
880 APU_DECLARE(apr_status_t) apr_brigade_putc(apr_bucket_brigade *b,
881                                            apr_brigade_flush flush, void *ctx,
882                                            const char c);
883
884 /**
885  * This function writes an unspecified number of strings into a bucket brigade.
886  * @param b The bucket brigade to add to
887  * @param flush The flush function to use if the brigade is full
888  * @param ctx The structure to pass to the flush function
889  * @param ... The strings to add
890  * @return APR_SUCCESS or error code
891  */
892 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_putstrs(apr_bucket_brigade *b,
893                                                      apr_brigade_flush flush,
894                                                      void *ctx, ...);
895
896 /**
897  * Evaluate a printf and put the resulting string at the end 
898  * of the bucket brigade.
899  * @param b The brigade to write to
900  * @param flush The flush function to use if the brigade is full
901  * @param ctx The structure to pass to the flush function
902  * @param fmt The format of the string to write
903  * @param ... The arguments to fill out the format
904  * @return APR_SUCCESS or error code
905  */
906 APU_DECLARE_NONSTD(apr_status_t) apr_brigade_printf(apr_bucket_brigade *b, 
907                                                     apr_brigade_flush flush,
908                                                     void *ctx,
909                                                     const char *fmt, ...)
910         __attribute__((format(printf,4,5)));
911
912 /**
913  * Evaluate a printf and put the resulting string at the end 
914  * of the bucket brigade.
915  * @param b The brigade to write to
916  * @param flush The flush function to use if the brigade is full
917  * @param ctx The structure to pass to the flush function
918  * @param fmt The format of the string to write
919  * @param va The arguments to fill out the format
920  * @return APR_SUCCESS or error code
921  */
922 APU_DECLARE(apr_status_t) apr_brigade_vprintf(apr_bucket_brigade *b, 
923                                               apr_brigade_flush flush,
924                                               void *ctx,
925                                               const char *fmt, va_list va);
926
927 /*  *****  Bucket freelist functions *****  */
928 /**
929  * Create a bucket allocator.
930  * @param p This pool's underlying apr_allocator_t is used to allocate memory
931  *          for the bucket allocator.  When the pool is destroyed, the bucket
932  *          allocator's cleanup routine will free all memory that has been
933  *          allocated from it.
934  * @remark  The reason the allocator gets its memory from the pool's
935  *          apr_allocator_t rather than from the pool itself is because
936  *          the bucket allocator will free large memory blocks back to the
937  *          allocator when it's done with them, thereby preventing memory
938  *          footprint growth that would occur if we allocated from the pool.
939  * @warning The allocator must never be used by more than one thread at a time.
940  */
941 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create(apr_pool_t *p);
942
943 /**
944  * Create a bucket allocator.
945  * @param allocator This apr_allocator_t is used to allocate both the bucket
946  *          allocator and all memory handed out by the bucket allocator.  The
947  *          caller is responsible for destroying the bucket allocator and the
948  *          apr_allocator_t -- no automatic cleanups will happen.
949  * @warning The allocator must never be used by more than one thread at a time.
950  */
951 APU_DECLARE_NONSTD(apr_bucket_alloc_t *) apr_bucket_alloc_create_ex(apr_allocator_t *allocator);
952
953 /**
954  * Destroy a bucket allocator.
955  * @param list The allocator to be destroyed
956  */
957 APU_DECLARE_NONSTD(void) apr_bucket_alloc_destroy(apr_bucket_alloc_t *list);
958
959 /**
960  * Allocate memory for use by the buckets.
961  * @param size The amount to allocate.
962  * @param list The allocator from which to allocate the memory.
963  */
964 APU_DECLARE_NONSTD(void *) apr_bucket_alloc(apr_size_t size, apr_bucket_alloc_t *list);
965
966 /**
967  * Free memory previously allocated with apr_bucket_alloc().
968  * @param block The block of memory to be freed.
969  */
970 APU_DECLARE_NONSTD(void) apr_bucket_free(void *block);
971
972
973 /*  *****  Bucket Functions  *****  */
974 /**
975  * Free the resources used by a bucket. If multiple buckets refer to
976  * the same resource it is freed when the last one goes away.
977  * @see apr_bucket_delete()
978  * @param e The bucket to destroy
979  */
980 #define apr_bucket_destroy(e) do {                                      \
981         (e)->type->destroy((e)->data);                                  \
982         (e)->free(e);                                                   \
983     } while (0)
984
985 /**
986  * Delete a bucket by removing it from its brigade (if any) and then
987  * destroying it.
988  * @remark This mainly acts as an aid in avoiding code verbosity.  It is
989  * the preferred exact equivalent to:
990  * <pre>
991  *      APR_BUCKET_REMOVE(e);
992  *      apr_bucket_destroy(e);
993  * </pre>
994  * @param e The bucket to delete
995  */
996 #define apr_bucket_delete(e) do {                                       \
997         APR_BUCKET_REMOVE(e);                                           \
998         apr_bucket_destroy(e);                                          \
999     } while (0)
1000
1001 /**
1002  * read the data from the bucket
1003  * @param e The bucket to read from
1004  * @param str The location to store the data in
1005  * @param len The amount of data read
1006  * @param block Whether the read function blocks
1007  */
1008 #define apr_bucket_read(e,str,len,block) (e)->type->read(e, str, len, block)
1009
1010 /**
1011  * Setaside data so that stack data is not destroyed on returning from
1012  * the function
1013  * @param e The bucket to setaside
1014  * @param p The pool to setaside into
1015  */
1016 #define apr_bucket_setaside(e,p) (e)->type->setaside(e,p)
1017
1018 /**
1019  * Split one bucket in two.
1020  * @param e The bucket to split
1021  * @param point The offset to split the bucket at
1022  */
1023 #define apr_bucket_split(e,point) (e)->type->split(e, point)
1024
1025 /**
1026  * Copy a bucket.
1027  * @param e The bucket to copy
1028  * @param c Returns a pointer to the new bucket
1029  */
1030 #define apr_bucket_copy(e,c) (e)->type->copy(e, c)
1031
1032 /* Bucket type handling */
1033
1034 /**
1035  * This function simply returns APR_SUCCESS to denote that the bucket does
1036  * not require anything to happen for its setaside() function. This is
1037  * appropriate for buckets that have "immortal" data -- the data will live
1038  * at least as long as the bucket.
1039  * @param data The bucket to setaside
1040  * @param pool The pool defining the desired lifetime of the bucket data
1041  * @return APR_SUCCESS
1042  */ 
1043 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_noop(apr_bucket *data,
1044                                                           apr_pool_t *pool);
1045
1046 /**
1047  * A place holder function that signifies that the setaside function was not
1048  * implemented for this bucket
1049  * @param data The bucket to setaside
1050  * @param pool The pool defining the desired lifetime of the bucket data
1051  * @return APR_ENOTIMPL
1052  */ 
1053 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_setaside_notimpl(apr_bucket *data,
1054                                                              apr_pool_t *pool);
1055
1056 /**
1057  * A place holder function that signifies that the split function was not
1058  * implemented for this bucket
1059  * @param data The bucket to split
1060  * @param point The location to split the bucket
1061  * @return APR_ENOTIMPL
1062  */ 
1063 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_split_notimpl(apr_bucket *data,
1064                                                           apr_size_t point);
1065
1066 /**
1067  * A place holder function that signifies that the copy function was not
1068  * implemented for this bucket
1069  * @param e The bucket to copy
1070  * @param c Returns a pointer to the new bucket
1071  * @return APR_ENOTIMPL
1072  */
1073 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_copy_notimpl(apr_bucket *e,
1074                                                          apr_bucket **c);
1075
1076 /**
1077  * A place holder function that signifies that this bucket does not need
1078  * to do anything special to be destroyed.  That's only the case for buckets
1079  * that either have no data (metadata buckets) or buckets whose data pointer
1080  * points to something that's not a bucket-type-specific structure, as with
1081  * simple buckets where data points to a string and pipe buckets where data
1082  * points directly to the apr_file_t.
1083  * @param data The bucket data to destroy
1084  */ 
1085 APU_DECLARE_NONSTD(void) apr_bucket_destroy_noop(void *data);
1086
1087 /**
1088  * There is no apr_bucket_destroy_notimpl, because destruction is required
1089  * to be implemented (it could be a noop, but only if that makes sense for
1090  * the bucket type)
1091  */
1092
1093 /* There is no apr_bucket_read_notimpl, because it is a required function
1094  */
1095
1096
1097 /* All of the bucket types implemented by the core */
1098 /**
1099  * The flush bucket type.  This signifies that all data should be flushed to
1100  * the next filter.  The flush bucket should be sent with the other buckets.
1101  */
1102 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_flush;
1103 /**
1104  * The EOS bucket type.  This signifies that there will be no more data, ever.
1105  * All filters MUST send all data to the next filter when they receive a
1106  * bucket of this type
1107  */
1108 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_eos;
1109 /**
1110  * The FILE bucket type.  This bucket represents a file on disk
1111  */
1112 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_file;
1113 /**
1114  * The HEAP bucket type.  This bucket represents a data allocated from the
1115  * heap.
1116  */
1117 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_heap;
1118 #if APR_HAS_MMAP
1119 /**
1120  * The MMAP bucket type.  This bucket represents an MMAP'ed file
1121  */
1122 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_mmap;
1123 #endif
1124 /**
1125  * The POOL bucket type.  This bucket represents a data that was allocated
1126  * from a pool.  IF this bucket is still available when the pool is cleared,
1127  * the data is copied on to the heap.
1128  */
1129 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pool;
1130 /**
1131  * The PIPE bucket type.  This bucket represents a pipe to another program.
1132  */
1133 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_pipe;
1134 /**
1135  * The IMMORTAL bucket type.  This bucket represents a segment of data that
1136  * the creator is willing to take responsibility for.  The core will do
1137  * nothing with the data in an immortal bucket
1138  */
1139 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_immortal;
1140 /**
1141  * The TRANSIENT bucket type.  This bucket represents a data allocated off
1142  * the stack.  When the setaside function is called, this data is copied on
1143  * to the heap
1144  */
1145 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_transient;
1146 /**
1147  * The SOCKET bucket type.  This bucket represents a socket to another machine
1148  */
1149 APU_DECLARE_DATA extern const apr_bucket_type_t apr_bucket_type_socket;
1150
1151
1152 /*  *****  Simple buckets  *****  */
1153
1154 /**
1155  * Split a simple bucket into two at the given point.  Most non-reference
1156  * counting buckets that allow multiple references to the same block of
1157  * data (eg transient and immortal) will use this as their split function
1158  * without any additional type-specific handling.
1159  * @param b The bucket to be split
1160  * @param point The offset of the first byte in the new bucket
1161  * @return APR_EINVAL if the point is not within the bucket;
1162  *         APR_ENOMEM if allocation failed;
1163  *         or APR_SUCCESS
1164  */
1165 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_split(apr_bucket *b,
1166                                                          apr_size_t point);
1167
1168 /**
1169  * Copy a simple bucket.  Most non-reference-counting buckets that allow
1170  * multiple references to the same block of data (eg transient and immortal)
1171  * will use this as their copy function without any additional type-specific
1172  * handling.
1173  * @param a The bucket to copy
1174  * @param b Returns a pointer to the new bucket
1175  * @return APR_ENOMEM if allocation failed;
1176  *         or APR_SUCCESS
1177  */
1178 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_simple_copy(apr_bucket *a,
1179                                                         apr_bucket **b);
1180
1181
1182 /*  *****  Shared, reference-counted buckets  *****  */
1183
1184 /**
1185  * Initialize a bucket containing reference-counted data that may be
1186  * shared. The caller must allocate the bucket if necessary and
1187  * initialize its type-dependent fields, and allocate and initialize
1188  * its own private data structure. This function should only be called
1189  * by type-specific bucket creation functions.
1190  * @param b The bucket to initialize
1191  * @param data A pointer to the private data structure
1192  *             with the reference count at the start
1193  * @param start The start of the data in the bucket
1194  *              relative to the private base pointer
1195  * @param length The length of the data in the bucket
1196  * @return The new bucket, or NULL if allocation failed
1197  */
1198 APU_DECLARE(apr_bucket *) apr_bucket_shared_make(apr_bucket *b, void *data,
1199                                                  apr_off_t start, 
1200                                                  apr_size_t length);
1201
1202 /**
1203  * Decrement the refcount of the data in the bucket. This function
1204  * should only be called by type-specific bucket destruction functions.
1205  * @param data The private data pointer from the bucket to be destroyed
1206  * @return TRUE or FALSE; TRUE if the reference count is now
1207  *         zero, indicating that the shared resource itself can
1208  *         be destroyed by the caller.
1209  */
1210 APU_DECLARE(int) apr_bucket_shared_destroy(void *data);
1211
1212 /**
1213  * Split a bucket into two at the given point, and adjust the refcount
1214  * to the underlying data. Most reference-counting bucket types will
1215  * be able to use this function as their split function without any
1216  * additional type-specific handling.
1217  * @param b The bucket to be split
1218  * @param point The offset of the first byte in the new bucket
1219  * @return APR_EINVAL if the point is not within the bucket;
1220  *         APR_ENOMEM if allocation failed;
1221  *         or APR_SUCCESS
1222  */
1223 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_split(apr_bucket *b,
1224                                                          apr_size_t point);
1225
1226 /**
1227  * Copy a refcounted bucket, incrementing the reference count. Most
1228  * reference-counting bucket types will be able to use this function
1229  * as their copy function without any additional type-specific handling.
1230  * @param a The bucket to copy
1231  * @param b Returns a pointer to the new bucket
1232  * @return APR_ENOMEM if allocation failed;
1233            or APR_SUCCESS
1234  */
1235 APU_DECLARE_NONSTD(apr_status_t) apr_bucket_shared_copy(apr_bucket *a,
1236                                                         apr_bucket **b);
1237
1238
1239 /*  *****  Functions to Create Buckets of varying types  *****  */
1240 /*
1241  * Each bucket type foo has two initialization functions:
1242  * apr_bucket_foo_make which sets up some already-allocated memory as a
1243  * bucket of type foo; and apr_bucket_foo_create which allocates memory
1244  * for the bucket, calls apr_bucket_make_foo, and initializes the
1245  * bucket's list pointers. The apr_bucket_foo_make functions are used
1246  * inside the bucket code to change the type of buckets in place;
1247  * other code should call apr_bucket_foo_create. All the initialization
1248  * functions change nothing if they fail.
1249  */
1250
1251 /**
1252  * Create an End of Stream bucket.  This indicates that there is no more data
1253  * coming from down the filter stack.  All filters should flush at this point.
1254  * @param list The freelist from which this bucket should be allocated
1255  * @return The new bucket, or NULL if allocation failed
1256  */
1257 APU_DECLARE(apr_bucket *) apr_bucket_eos_create(apr_bucket_alloc_t *list);
1258
1259 /**
1260  * Make the bucket passed in an EOS bucket.  This indicates that there is no 
1261  * more data coming from down the filter stack.  All filters should flush at 
1262  * this point.
1263  * @param b The bucket to make into an EOS bucket
1264  * @return The new bucket, or NULL if allocation failed
1265  */
1266 APU_DECLARE(apr_bucket *) apr_bucket_eos_make(apr_bucket *b);
1267
1268 /**
1269  * Create a flush  bucket.  This indicates that filters should flush their
1270  * data.  There is no guarantee that they will flush it, but this is the
1271  * best we can do.
1272  * @param list The freelist from which this bucket should be allocated
1273  * @return The new bucket, or NULL if allocation failed
1274  */
1275 APU_DECLARE(apr_bucket *) apr_bucket_flush_create(apr_bucket_alloc_t *list);
1276
1277 /**
1278  * Make the bucket passed in a FLUSH  bucket.  This indicates that filters 
1279  * should flush their data.  There is no guarantee that they will flush it, 
1280  * but this is the best we can do.
1281  * @param b The bucket to make into a FLUSH bucket
1282  * @return The new bucket, or NULL if allocation failed
1283  */
1284 APU_DECLARE(apr_bucket *) apr_bucket_flush_make(apr_bucket *b);
1285
1286 /**
1287  * Create a bucket referring to long-lived data.
1288  * @param buf The data to insert into the bucket
1289  * @param nbyte The size of the data to insert.
1290  * @param list The freelist from which this bucket should be allocated
1291  * @return The new bucket, or NULL if allocation failed
1292  */
1293 APU_DECLARE(apr_bucket *) apr_bucket_immortal_create(const char *buf, 
1294                                                      apr_size_t nbyte,
1295                                                      apr_bucket_alloc_t *list);
1296
1297 /**
1298  * Make the bucket passed in a bucket refer to long-lived data
1299  * @param b The bucket to make into a IMMORTAL bucket
1300  * @param buf The data to insert into the bucket
1301  * @param nbyte The size of the data to insert.
1302  * @return The new bucket, or NULL if allocation failed
1303  */
1304 APU_DECLARE(apr_bucket *) apr_bucket_immortal_make(apr_bucket *b, 
1305                                                    const char *buf, 
1306                                                    apr_size_t nbyte);
1307
1308 /**
1309  * Create a bucket referring to data on the stack.
1310  * @param buf The data to insert into the bucket
1311  * @param nbyte The size of the data to insert.
1312  * @param list The freelist from which this bucket should be allocated
1313  * @return The new bucket, or NULL if allocation failed
1314  */
1315 APU_DECLARE(apr_bucket *) apr_bucket_transient_create(const char *buf, 
1316                                                       apr_size_t nbyte,
1317                                                       apr_bucket_alloc_t *list);
1318
1319 /**
1320  * Make the bucket passed in a bucket refer to stack data
1321  * @param b The bucket to make into a TRANSIENT bucket
1322  * @param buf The data to insert into the bucket
1323  * @param nbyte The size of the data to insert.
1324  * @return The new bucket, or NULL if allocation failed
1325  */
1326 APU_DECLARE(apr_bucket *) apr_bucket_transient_make(apr_bucket *b, 
1327                                                     const char *buf,
1328                                                     apr_size_t nbyte);
1329
1330 /**
1331  * Create a bucket referring to memory on the heap. If the caller asks
1332  * for the data to be copied, this function always allocates 4K of
1333  * memory so that more data can be added to the bucket without
1334  * requiring another allocation. Therefore not all the data may be put
1335  * into the bucket. If copying is not requested then the bucket takes
1336  * over responsibility for free()ing the memory.
1337  * @param buf The buffer to insert into the bucket
1338  * @param nbyte The size of the buffer to insert.
1339  * @param free_func Function to use to free the data; NULL indicates that the
1340  *                  bucket should make a copy of the data
1341  * @param list The freelist from which this bucket should be allocated
1342  * @return The new bucket, or NULL if allocation failed
1343  */
1344 APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf, 
1345                                                  apr_size_t nbyte,
1346                                                  void (*free_func)(void *data),
1347                                                  apr_bucket_alloc_t *list);
1348 /**
1349  * Make the bucket passed in a bucket refer to heap data
1350  * @param b The bucket to make into a HEAP bucket
1351  * @param buf The buffer to insert into the bucket
1352  * @param nbyte The size of the buffer to insert.
1353  * @param free_func Function to use to free the data; NULL indicates that the
1354  *                  bucket should make a copy of the data
1355  * @return The new bucket, or NULL if allocation failed
1356  */
1357 APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
1358                                                apr_size_t nbyte,
1359                                                void (*free_func)(void *data));
1360
1361 /**
1362  * Create a bucket referring to memory allocated from a pool.
1363  *
1364  * @param buf The buffer to insert into the bucket
1365  * @param length The number of bytes referred to by this bucket
1366  * @param pool The pool the memory was allocated from
1367  * @param list The freelist from which this bucket should be allocated
1368  * @return The new bucket, or NULL if allocation failed
1369  */
1370 APU_DECLARE(apr_bucket *) apr_bucket_pool_create(const char *buf, 
1371                                                  apr_size_t length,
1372                                                  apr_pool_t *pool,
1373                                                  apr_bucket_alloc_t *list);
1374
1375 /**
1376  * Make the bucket passed in a bucket refer to pool data
1377  * @param b The bucket to make into a pool bucket
1378  * @param buf The buffer to insert into the bucket
1379  * @param length The number of bytes referred to by this bucket
1380  * @param pool The pool the memory was allocated from
1381  * @return The new bucket, or NULL if allocation failed
1382  */
1383 APU_DECLARE(apr_bucket *) apr_bucket_pool_make(apr_bucket *b, const char *buf,
1384                                                apr_size_t length, 
1385                                                apr_pool_t *pool);
1386
1387 #if APR_HAS_MMAP
1388 /**
1389  * Create a bucket referring to mmap()ed memory.
1390  * @param mm The mmap to insert into the bucket
1391  * @param start The offset of the first byte in the mmap
1392  *              that this bucket refers to
1393  * @param length The number of bytes referred to by this bucket
1394  * @param list The freelist from which this bucket should be allocated
1395  * @return The new bucket, or NULL if allocation failed
1396  */
1397 APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm, 
1398                                                  apr_off_t start,
1399                                                  apr_size_t length,
1400                                                  apr_bucket_alloc_t *list);
1401
1402 /**
1403  * Make the bucket passed in a bucket refer to an MMAP'ed file
1404  * @param b The bucket to make into a MMAP bucket
1405  * @param mm The mmap to insert into the bucket
1406  * @param start The offset of the first byte in the mmap
1407  *              that this bucket refers to
1408  * @param length The number of bytes referred to by this bucket
1409  * @return The new bucket, or NULL if allocation failed
1410  */
1411 APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
1412                                                apr_off_t start, 
1413                                                apr_size_t length);
1414 #endif
1415
1416 /**
1417  * Create a bucket referring to a socket.
1418  * @param thissock The socket to put in the bucket
1419  * @param list The freelist from which this bucket should be allocated
1420  * @return The new bucket, or NULL if allocation failed
1421  */
1422 APU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *thissock,
1423                                                    apr_bucket_alloc_t *list);
1424 /**
1425  * Make the bucket passed in a bucket refer to a socket
1426  * @param b The bucket to make into a SOCKET bucket
1427  * @param thissock The socket to put in the bucket
1428  * @return The new bucket, or NULL if allocation failed
1429  */
1430 APU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b, 
1431                                                  apr_socket_t *thissock);
1432
1433 /**
1434  * Create a bucket referring to a pipe.
1435  * @param thispipe The pipe to put in the bucket
1436  * @param list The freelist from which this bucket should be allocated
1437  * @return The new bucket, or NULL if allocation failed
1438  */
1439 APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *thispipe,
1440                                                  apr_bucket_alloc_t *list);
1441
1442 /**
1443  * Make the bucket passed in a bucket refer to a pipe
1444  * @param b The bucket to make into a PIPE bucket
1445  * @param thispipe The pipe to put in the bucket
1446  * @return The new bucket, or NULL if allocation failed
1447  */
1448 APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b, 
1449                                                apr_file_t *thispipe);
1450
1451 /**
1452  * Create a bucket referring to a file.
1453  * @param fd The file to put in the bucket
1454  * @param offset The offset where the data of interest begins in the file
1455  * @param len The amount of data in the file we are interested in
1456  * @param p The pool into which any needed structures should be created
1457  *          while reading from this file bucket
1458  * @param list The freelist from which this bucket should be allocated
1459  * @return The new bucket, or NULL if allocation failed
1460  */
1461 APU_DECLARE(apr_bucket *) apr_bucket_file_create(apr_file_t *fd,
1462                                                  apr_off_t offset,
1463                                                  apr_size_t len, 
1464                                                  apr_pool_t *p,
1465                                                  apr_bucket_alloc_t *list);
1466
1467 /**
1468  * Make the bucket passed in a bucket refer to a file
1469  * @param b The bucket to make into a FILE bucket
1470  * @param fd The file to put in the bucket
1471  * @param offset The offset where the data of interest begins in the file
1472  * @param len The amount of data in the file we are interested in
1473  * @param p The pool into which any needed structures should be created
1474  *          while reading from this file bucket
1475  * @return The new bucket, or NULL if allocation failed
1476  */
1477 APU_DECLARE(apr_bucket *) apr_bucket_file_make(apr_bucket *b, apr_file_t *fd,
1478                                                apr_off_t offset,
1479                                                apr_size_t len, apr_pool_t *p);
1480
1481 /**
1482  * Enable or disable memory-mapping for a FILE bucket (default is enabled)
1483  * @param b The bucket
1484  * @param enabled Whether memory-mapping should be enabled
1485  * @return APR_SUCCESS normally, or an error code if the operation fails
1486  */
1487 APU_DECLARE(apr_status_t) apr_bucket_file_enable_mmap(apr_bucket *b,
1488                                                       int enabled);
1489
1490 /** @} */
1491 #ifdef __cplusplus
1492 }
1493 #endif
1494
1495 #endif /* !APR_BUCKETS_H */