home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Servers / apache-1.2.4-MIHS / original-source / src / alloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-27  |  9.6 KB  |  253 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54. /*
  55.  * Resource allocation routines...
  56.  *
  57.  * designed so that we don't have to keep track of EVERYTHING so that
  58.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  59.  * particularly in the presence of die()).
  60.  *
  61.  * Instead, we maintain pools, and allocate items (both memory and I/O
  62.  * handlers) from the pools --- currently there are two, one for per
  63.  * transaction info, and one for config info.  When a transaction is over,
  64.  * we can delete everything in the per-transaction pool without fear, and
  65.  * without thinking too hard about it either.
  66.  *
  67.  * rst
  68.  */
  69.  
  70. /* Arenas for configuration info and transaction info
  71.  * --- actual layout of the pool structure is private to 
  72.  * alloc.c.  
  73.  */
  74.  
  75. typedef struct pool pool;
  76.  
  77. extern pool *permanent_pool;
  78. void init_alloc();        /* Set up everything */
  79. pool *make_sub_pool (pool *);    /* All pools are subpools of permanent_pool */
  80. void destroy_pool (pool *);
  81.  
  82. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  83.  
  84. void clear_pool (struct pool *);
  85.  
  86. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  87.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  88.  */
  89.  
  90. void cleanup_for_exec ();
  91.  
  92. /* routines to allocate memory from an pool... */
  93.  
  94. void *palloc(struct pool *, int nbytes);
  95. void *pcalloc(struct pool *, int nbytes);
  96. extern char *pstrdup(struct pool *, const char *s);
  97. extern char *pstrndup(struct pool *, const char *s, int n);
  98. char *pstrcat(struct pool *, ...); /* all '...' must be char* */
  99.  
  100. /* array and alist management... keeping lists of things.
  101.  * Common enough to want common support code ...
  102.  */
  103.  
  104. typedef struct {
  105.     pool *pool;
  106.     int elt_size;
  107.     int nelts;
  108.     int nalloc;
  109.     char *elts;
  110. } array_header;
  111.  
  112. array_header *make_array (pool *p, int nelts, int elt_size);
  113. void *push_array (array_header *);
  114. void array_cat (array_header *dst, const array_header *src);
  115. array_header *append_arrays (pool *, const array_header *,
  116.                  const array_header *);
  117.  
  118. /* copy_array copies the *entire* array.  copy_array_hdr just copies
  119.  * the header, and arranges for the elements to be copied if (and only
  120.  * if) the code subsequently does a push or arraycat.
  121.  */
  122.      
  123. array_header *copy_array (pool *p, const array_header *src);
  124. array_header *copy_array_hdr (pool *p, const array_header *src);
  125.                
  126.  
  127. /* Tables.  Implemented alist style, for now, though we try to keep
  128.  * it so that imposing a hash table structure on top in the future
  129.  * wouldn't be *too* hard...
  130.  *
  131.  * Note that key comparisons for these are case-insensitive, largely
  132.  * because that's what's appropriate and convenient everywhere they're
  133.  * currently being used...
  134.  */
  135.  
  136. typedef array_header table;     
  137.      
  138. typedef struct {
  139.     char *key;            /* maybe NULL in future;
  140.                  * check when iterating thru table_elts
  141.                  */
  142.     char *val;
  143. } table_entry;
  144.  
  145. table *make_table (pool *p, int nelts);
  146. table *copy_table (pool *p, const table *);     
  147. void clear_table (table *);     
  148. char *table_get (const table *, const char *);
  149. void table_set (table *, const char *name, const char *val);
  150. void table_merge (table *, const char *name, const char *more_val);
  151. void table_unset (table *, const char *key);
  152. void table_add (table *, const char *name, const char *val);
  153. void table_do (int (*comp)(void *, const char *, const char *), void *rec,
  154.                const table *t, ...);
  155.  
  156. table *overlay_tables (pool *p, const table *overlay, const table *base);     
  157.  
  158. array_header *table_elts (table *);     
  159.  
  160. #define is_empty_table(t) (((t) == NULL)||((t)->nelts == 0))
  161.  
  162. /* routines to remember allocation of other sorts of things...
  163.  * generic interface first.  Note that we want to have two separate
  164.  * cleanup functions in the general case, one for exec() preparation,
  165.  * to keep CGI scripts and the like from inheriting access to things
  166.  * they shouldn't be able to touch, and one for actually cleaning up,
  167.  * when the actual server process wants to get rid of the thing,
  168.  * whatever it is.  
  169.  *
  170.  * kill_cleanup disarms a cleanup, presumably because the resource in
  171.  * question has been closed, freed, or whatever, and it's scarce
  172.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  173.  * resource not to be cleaned up a second time (it might have been
  174.  * reallocated).  run_cleanup does the same, but runs it first.
  175.  *
  176.  * Cleanups are identified for purposes of finding & running them off by the
  177.  * plain_cleanup and data, which should presumably be unique.
  178.  *
  179.  * NB any code which invokes register_cleanup or kill_cleanup directly
  180.  * is a critical section which should be guarded by block_alarms() and
  181.  * unblock_alarms() below...
  182.  */
  183.  
  184. void register_cleanup (pool *p, void *data,
  185.                void (*plain_cleanup)(void *),
  186.                void (*child_cleanup)(void *));
  187.  
  188. void kill_cleanup (pool *p, void *data, void (*plain_cleanup)(void *));
  189. void run_cleanup (pool *p, void *data, void (*cleanup)(void *));
  190.  
  191. /* The time between when a resource is actually allocated, and when it
  192.  * its cleanup is registered is a critical section, during which the
  193.  * resource could leak if we got interrupted or timed out.  So, anything
  194.  * which registers cleanups should bracket resource allocation and the
  195.  * cleanup registry with these.  (This is done internally by run_cleanup).
  196.  *
  197.  * NB they are actually implemented in http_main.c, since they are bound
  198.  * up with timeout handling in general...
  199.  */
  200.  
  201. extern void block_alarms();
  202. extern void unblock_alarms();
  203.  
  204. /* Common cases which want utility support..
  205.  * the note_cleanups_for_foo routines are for 
  206.  */
  207.  
  208. FILE *pfopen(struct pool *, const char *name, const char *fmode);
  209. FILE *pfdopen(struct pool *, int fd, const char *fmode);
  210. int popenf(struct pool *, const char *name, int flg, int mode); 
  211.  
  212. void note_cleanups_for_file (pool *, FILE *);
  213. void note_cleanups_for_fd (pool *, int);
  214. void kill_cleanups_for_fd (pool *p, int fd);
  215.  
  216. regex_t *pregcomp (pool *p, const char *pattern, int cflags);
  217. void pregfree (pool *p, regex_t *reg);
  218.  
  219. /* routines to note closes... file descriptors are constrained enough
  220.  * on some systems that we want to support this.
  221.  */
  222.  
  223. int pfclose(struct pool *, FILE *);
  224. int pclosef(struct pool *, int fd);
  225.  
  226. /* ... even child processes (which we may want to wait for,
  227.  * or to kill outright, on unexpected termination).
  228.  *
  229.  * spawn_child is a utility routine which handles an awful lot of
  230.  * the rigamarole associated with spawning a child --- it arranges
  231.  * for pipes to the child's stdin and stdout, if desired (if not,
  232.  * set the associated args to NULL).  It takes as args a function
  233.  * to call in the child, and an argument to be passed to the function.
  234.  */
  235.      
  236. enum kill_conditions { kill_never, kill_always, kill_after_timeout, just_wait};
  237.  
  238. int spawn_child_err (pool *, void (*)(void *), void *,
  239.          enum kill_conditions, FILE **pipe_in, FILE **pipe_out,
  240.                  FILE **pipe_err);
  241. #define spawn_child(p,f,v,k,in,out) spawn_child_err(p,f,v,k,in,out,NULL)
  242.  
  243. /* magic numbers --- min free bytes to consider a free pool block useable,
  244.  * and the min amount to allocate if we have to go to malloc() */
  245.  
  246. #define BLOCK_MINFREE 4096
  247. #define BLOCK_MINALLOC 8192
  248.  
  249. /* Finally, some accounting */
  250.  
  251. long bytes_in_pool(pool *p);
  252. long bytes_in_free_blocks();
  253.