home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 October / PCW1001.iso / Linux / apache / apache_1.3.20-win32-no_src-r2.msi / Data.Cab / F160720_ap_alloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-15  |  16.4 KB  |  408 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * Portions of this software are based upon public domain software
  55.  * originally written at the National Center for Supercomputing Applications,
  56.  * University of Illinois, Urbana-Champaign.
  57.  */
  58.  
  59. #ifndef APACHE_ALLOC_H
  60. #define APACHE_ALLOC_H
  61.  
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65.  
  66. /*
  67.  * Resource allocation routines...
  68.  *
  69.  * designed so that we don't have to keep track of EVERYTHING so that
  70.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  71.  * particularly in the presence of die()).
  72.  *
  73.  * Instead, we maintain pools, and allocate items (both memory and I/O
  74.  * handlers) from the pools --- currently there are two, one for per
  75.  * transaction info, and one for config info.  When a transaction is over,
  76.  * we can delete everything in the per-transaction pool without fear, and
  77.  * without thinking too hard about it either.
  78.  *
  79.  * rst
  80.  */
  81.  
  82. /* Arenas for configuration info and transaction info
  83.  * --- actual layout of the pool structure is private to 
  84.  * alloc.c.  
  85.  */
  86.  
  87.  /* Need declaration of DIR on Win32 */
  88. #ifdef WIN32
  89. #include "readdir.h"
  90. #endif
  91.  
  92. typedef struct pool pool;
  93. typedef struct pool ap_pool;
  94.  
  95. pool * ap_init_alloc(void);        /* Set up everything */
  96. void ap_cleanup_alloc(void);
  97. API_EXPORT(pool *) ap_make_sub_pool(pool *);    /* All pools are subpools of permanent_pool */
  98. API_EXPORT(void) ap_destroy_pool(pool *);
  99.  
  100. /* pools have nested lifetimes -- sub_pools are destroyed when the
  101.  * parent pool is cleared.  We allow certain liberties with operations
  102.  * on things such as tables (and on other structures in a more general
  103.  * sense) where we allow the caller to insert values into a table which
  104.  * were not allocated from the table's pool.  The table's data will
  105.  * remain valid as long as all the pools from which its values are
  106.  * allocated remain valid.
  107.  *
  108.  * For example, if B is a sub pool of A, and you build a table T in
  109.  * pool B, then it's safe to insert data allocated in A or B into T
  110.  * (because B lives at most as long as A does, and T is destroyed when
  111.  * B is cleared/destroyed).  On the other hand, if S is a table in
  112.  * pool A, it is safe to insert data allocated in A into S, but it
  113.  * is *not safe* to insert data allocated from B into S... because
  114.  * B can be cleared/destroyed before A is (which would leave dangling
  115.  * pointers in T's data structures).
  116.  *
  117.  * In general we say that it is safe to insert data into a table T
  118.  * if the data is allocated in any ancestor of T's pool.  This is the
  119.  * basis on which the POOL_DEBUG code works -- it tests these ancestor
  120.  * relationships for all data inserted into tables.  POOL_DEBUG also
  121.  * provides tools (ap_find_pool, and ap_pool_is_ancestor) for other
  122.  * folks to implement similar restrictions for their own data
  123.  * structures.
  124.  *
  125.  * However, sometimes this ancestor requirement is inconvenient --
  126.  * sometimes we're forced to create a sub pool (such as through
  127.  * ap_sub_req_lookup_uri), and the sub pool is guaranteed to have
  128.  * the same lifetime as the parent pool.  This is a guarantee implemented
  129.  * by the *caller*, not by the pool code.  That is, the caller guarantees
  130.  * they won't destroy the sub pool individually prior to destroying the
  131.  * parent pool.
  132.  *
  133.  * In this case the caller must call ap_pool_join() to indicate this
  134.  * guarantee to the POOL_DEBUG code.  There are a few examples spread
  135.  * through the standard modules.
  136.  */
  137. #ifndef POOL_DEBUG
  138. #ifdef ap_pool_join
  139. #undef ap_pool_join
  140. #endif
  141. #define ap_pool_join(a,b)
  142. #else
  143. API_EXPORT(void) ap_pool_join(pool *p, pool *sub);
  144. API_EXPORT(pool *) ap_find_pool(const void *ts);
  145. API_EXPORT(int) ap_pool_is_ancestor(pool *a, pool *b);
  146. #endif
  147.  
  148. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  149.  
  150. API_EXPORT(void) ap_clear_pool(struct pool *);
  151.  
  152. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  153.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  154.  */
  155.  
  156. API_EXPORT(void) ap_cleanup_for_exec(void);
  157.  
  158. /* routines to allocate memory from an pool... */
  159.  
  160. API_EXPORT(void *) ap_palloc(struct pool *, int nbytes);
  161. API_EXPORT(void *) ap_pcalloc(struct pool *, int nbytes);
  162. API_EXPORT(char *) ap_pstrdup(struct pool *, const char *s);
  163. /* make a nul terminated copy of the n characters starting with s */
  164. API_EXPORT(char *) ap_pstrndup(struct pool *, const char *s, int n);
  165. API_EXPORT_NONSTD(char *) ap_pstrcat(struct pool *,...);    /* all '...' must be char* */
  166. API_EXPORT_NONSTD(char *) ap_psprintf(struct pool *, const char *fmt, ...)
  167.     __attribute__((format(printf,2,3)));
  168. API_EXPORT(char *) ap_pvsprintf(struct pool *, const char *fmt, va_list);
  169.  
  170. /* array and alist management... keeping lists of things.
  171.  * Common enough to want common support code ...
  172.  */
  173.  
  174. typedef struct {
  175.     ap_pool *pool;
  176.     int elt_size;
  177.     int nelts;
  178.     int nalloc;
  179.     char *elts;
  180. } array_header;
  181.  
  182. API_EXPORT(array_header *) ap_make_array(pool *p, int nelts, int elt_size);
  183. API_EXPORT(void *) ap_push_array(array_header *);
  184. API_EXPORT(void) ap_array_cat(array_header *dst, const array_header *src);
  185. API_EXPORT(array_header *) ap_append_arrays(pool *, const array_header *,
  186.                      const array_header *);
  187.  
  188. /* ap_array_pstrcat generates a new string from the pool containing
  189.  * the concatenated sequence of substrings referenced as elements within
  190.  * the array.  The string will be empty if all substrings are empty or null,
  191.  * or if there are no elements in the array.
  192.  * If sep is non-NUL, it will be inserted between elements as a separator.
  193.  */
  194. API_EXPORT(char *) ap_array_pstrcat(pool *p, const array_header *arr,
  195.                                     const char sep);
  196.  
  197. /* copy_array copies the *entire* array.  copy_array_hdr just copies
  198.  * the header, and arranges for the elements to be copied if (and only
  199.  * if) the code subsequently does a push or arraycat.
  200.  */
  201.  
  202. API_EXPORT(array_header *) ap_copy_array(pool *p, const array_header *src);
  203. API_EXPORT(array_header *) ap_copy_array_hdr(pool *p, const array_header *src);
  204.  
  205.  
  206. /* Tables.  Implemented alist style, for now, though we try to keep
  207.  * it so that imposing a hash table structure on top in the future
  208.  * wouldn't be *too* hard...
  209.  *
  210.  * Note that key comparisons for these are case-insensitive, largely
  211.  * because that's what's appropriate and convenient everywhere they're
  212.  * currently being used...
  213.  */
  214.  
  215. typedef struct table table;
  216.  
  217. typedef struct {
  218.     char *key;        /* maybe NULL in future;
  219.              * check when iterating thru table_elts
  220.              */
  221.     char *val;
  222. } table_entry;
  223.  
  224. API_EXPORT(table *) ap_make_table(pool *p, int nelts);
  225. API_EXPORT(table *) ap_copy_table(pool *p, const table *);
  226. API_EXPORT(void) ap_clear_table(table *);
  227. API_EXPORT(const char *) ap_table_get(const table *, const char *);
  228. API_EXPORT(void) ap_table_set(table *, const char *name, const char *val);
  229. API_EXPORT(void) ap_table_setn(table *, const char *name, const char *val);
  230. API_EXPORT(void) ap_table_merge(table *, const char *name, const char *more_val);
  231. API_EXPORT(void) ap_table_mergen(table *, const char *name, const char *more_val);
  232. API_EXPORT(void) ap_table_unset(table *, const char *key);
  233. API_EXPORT(void) ap_table_add(table *, const char *name, const char *val);
  234. API_EXPORT(void) ap_table_addn(table *, const char *name, const char *val);
  235. API_EXPORT(void) ap_table_do(int (*comp) (void *, const char *, const char *), void *rec,
  236.               const table *t,...);
  237.  
  238. API_EXPORT(table *) ap_overlay_tables(pool *p, const table *overlay, const table *base);
  239.  
  240. /* Conceptually, ap_overlap_tables does this:
  241.  
  242.     array_header *barr = ap_table_elts(b);
  243.     table_entry *belt = (table_entry *)barr->elts;
  244.     int i;
  245.  
  246.     for (i = 0; i < barr->nelts; ++i) {
  247.     if (flags & AP_OVERLAP_TABLES_MERGE) {
  248.         ap_table_mergen(a, belt[i].key, belt[i].val);
  249.     }
  250.     else {
  251.         ap_table_setn(a, belt[i].key, belt[i].val);
  252.     }
  253.     }
  254.  
  255.     Except that it is more efficient (less space and cpu-time) especially
  256.     when b has many elements.
  257.  
  258.     Notice the assumptions on the keys and values in b -- they must be
  259.     in an ancestor of a's pool.  In practice b and a are usually from
  260.     the same pool.
  261. */
  262. #define AP_OVERLAP_TABLES_SET    (0)
  263. #define AP_OVERLAP_TABLES_MERGE    (1)
  264. API_EXPORT(void) ap_overlap_tables(table *a, const table *b, unsigned flags);
  265.  
  266. /* XXX: these know about the definition of struct table in alloc.c.  That
  267.  * definition is not here because it is supposed to be private, and by not
  268.  * placing it here we are able to get compile-time diagnostics from modules
  269.  * written which assume that a table is the same as an array_header. -djg
  270.  */
  271. #define ap_table_elts(t) ((array_header *)(t))
  272. #define ap_is_empty_table(t) (((t) == NULL)||(((array_header *)(t))->nelts == 0))
  273.  
  274. /* routines to remember allocation of other sorts of things...
  275.  * generic interface first.  Note that we want to have two separate
  276.  * cleanup functions in the general case, one for exec() preparation,
  277.  * to keep CGI scripts and the like from inheriting access to things
  278.  * they shouldn't be able to touch, and one for actually cleaning up,
  279.  * when the actual server process wants to get rid of the thing,
  280.  * whatever it is.  
  281.  *
  282.  * kill_cleanup disarms a cleanup, presumably because the resource in
  283.  * question has been closed, freed, or whatever, and it's scarce
  284.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  285.  * resource not to be cleaned up a second time (it might have been
  286.  * reallocated).  run_cleanup does the same, but runs it first.
  287.  *
  288.  * Cleanups are identified for purposes of finding & running them off by the
  289.  * plain_cleanup and data, which should presumably be unique.
  290.  *
  291.  * NB any code which invokes register_cleanup or kill_cleanup directly
  292.  * is a critical section which should be guarded by block_alarms() and
  293.  * unblock_alarms() below...
  294.  */
  295.  
  296. API_EXPORT(void) ap_register_cleanup(pool *p, void *data,
  297.                   void (*plain_cleanup) (void *),
  298.                   void (*child_cleanup) (void *));
  299.  
  300. API_EXPORT(void) ap_kill_cleanup(pool *p, void *data, void (*plain_cleanup) (void *));
  301. API_EXPORT(void) ap_run_cleanup(pool *p, void *data, void (*cleanup) (void *));
  302.  
  303. /* A "do-nothing" cleanup, for register_cleanup; it's faster to do
  304.  * things this way than to test for NULL. */
  305. API_EXPORT_NONSTD(void) ap_null_cleanup(void *data);
  306.  
  307. /* The time between when a resource is actually allocated, and when it
  308.  * its cleanup is registered is a critical section, during which the
  309.  * resource could leak if we got interrupted or timed out.  So, anything
  310.  * which registers cleanups should bracket resource allocation and the
  311.  * cleanup registry with these.  (This is done internally by run_cleanup).
  312.  *
  313.  * NB they are actually implemented in http_main.c, since they are bound
  314.  * up with timeout handling in general...
  315.  */
  316.  
  317. #ifdef TPF
  318. #define ap_block_alarms() (0)
  319. #define ap_unblock_alarms() (0)
  320. #else
  321. API_EXPORT(void) ap_block_alarms(void);
  322. API_EXPORT(void) ap_unblock_alarms(void);
  323. #endif /* TPF */
  324.  
  325. /* Common cases which want utility support..
  326.  * the note_cleanups_for_foo routines are for 
  327.  */
  328.  
  329. API_EXPORT(FILE *) ap_pfopen(struct pool *, const char *name, const char *fmode);
  330. API_EXPORT(FILE *) ap_pfdopen(struct pool *, int fd, const char *fmode);
  331. API_EXPORT(int) ap_popenf(struct pool *, const char *name, int flg, int mode);
  332.  
  333. API_EXPORT(void) ap_note_cleanups_for_file(pool *, FILE *);
  334. API_EXPORT(void) ap_note_cleanups_for_fd(pool *, int);
  335. #ifdef WIN32
  336. API_EXPORT(void) ap_note_cleanups_for_h(pool *, HANDLE);
  337. #endif
  338. API_EXPORT(void) ap_kill_cleanups_for_fd(pool *p, int fd);
  339.  
  340. API_EXPORT(void) ap_note_cleanups_for_socket(pool *, int);
  341. API_EXPORT(void) ap_kill_cleanups_for_socket(pool *p, int sock);
  342. API_EXPORT(int) ap_psocket(pool *p, int, int, int);
  343. API_EXPORT(int) ap_pclosesocket(pool *a, int sock);
  344.  
  345. API_EXPORT(regex_t *) ap_pregcomp(pool *p, const char *pattern, int cflags);
  346. API_EXPORT(void) ap_pregfree(pool *p, regex_t * reg);
  347.  
  348. /* routines to note closes... file descriptors are constrained enough
  349.  * on some systems that we want to support this.
  350.  */
  351.  
  352. API_EXPORT(int) ap_pfclose(struct pool *, FILE *);
  353. API_EXPORT(int) ap_pclosef(struct pool *, int fd);
  354. #ifdef WIN32
  355. API_EXPORT(int) ap_pcloseh(struct pool *, HANDLE hDevice);
  356. #endif
  357.  
  358. /* routines to deal with directories */
  359. API_EXPORT(DIR *) ap_popendir(pool *p, const char *name);
  360. API_EXPORT(void) ap_pclosedir(pool *p, DIR * d);
  361.  
  362. /* ... even child processes (which we may want to wait for,
  363.  * or to kill outright, on unexpected termination).
  364.  *
  365.  * ap_spawn_child is a utility routine which handles an awful lot of
  366.  * the rigamarole associated with spawning a child --- it arranges
  367.  * for pipes to the child's stdin and stdout, if desired (if not,
  368.  * set the associated args to NULL).  It takes as args a function
  369.  * to call in the child, and an argument to be passed to the function.
  370.  */
  371.  
  372. enum kill_conditions {
  373.     kill_never,            /* process is never sent any signals */
  374.     kill_always,        /* process is sent SIGKILL on pool cleanup */
  375.     kill_after_timeout,        /* SIGTERM, wait 3 seconds, SIGKILL */
  376.     just_wait,            /* wait forever for the process to complete */
  377.     kill_only_once        /* send SIGTERM and then wait */
  378. };
  379.  
  380. typedef struct child_info child_info;
  381. API_EXPORT(void) ap_note_subprocess(pool *a, pid_t pid,
  382.                     enum kill_conditions how);
  383. API_EXPORT(int) ap_spawn_child(pool *, int (*)(void *, child_info *),
  384.                    void *, enum kill_conditions,
  385.                    FILE **pipe_in, FILE **pipe_out,
  386.                    FILE **pipe_err);
  387.  
  388. /* magic numbers --- min free bytes to consider a free pool block useable,
  389.  * and the min amount to allocate if we have to go to malloc() */
  390.  
  391. #ifndef BLOCK_MINFREE
  392. #define BLOCK_MINFREE 4096
  393. #endif
  394. #ifndef BLOCK_MINALLOC
  395. #define BLOCK_MINALLOC 8192
  396. #endif
  397.  
  398. /* Finally, some accounting */
  399.  
  400. API_EXPORT(long) ap_bytes_in_pool(pool *p);
  401. API_EXPORT(long) ap_bytes_in_free_blocks(void);
  402.  
  403. #ifdef __cplusplus
  404. }
  405. #endif
  406.  
  407. #endif    /* !APACHE_ALLOC_H */
  408.