home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / alloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  9.1 KB  |  246 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 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. /*
  56.  * Resource allocation routines...
  57.  *
  58.  * designed so that we don't have to keep track of EVERYTHING so that
  59.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  60.  * particularly in the presence of die()).
  61.  *
  62.  * Instead, we maintain pools, and allocate items (both memory and I/O
  63.  * handlers) from the pools --- currently there are two, one for per
  64.  * transaction info, and one for config info.  When a transaction is over,
  65.  * we can delete everything in the per-transaction pool without fear, and
  66.  * without thinking too hard about it either.
  67.  *
  68.  * rst
  69.  */
  70.  
  71. /* Arenas for configuration info and transaction info
  72.  * --- actual layout of the pool structure is private to
  73.  * alloc.c.
  74.  */
  75.  
  76. typedef struct pool pool;
  77.  
  78. extern pool *permanent_pool;
  79. void init_alloc();      /* Set up everything */
  80. pool *make_sub_pool (pool *);   /* All pools are subpools of permanent_pool */
  81. void destroy_pool (pool *);
  82.  
  83. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  84.  
  85. void clear_pool (struct pool *);
  86.  
  87. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  88.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  89.  */
  90.  
  91. void cleanup_for_exec ();
  92.  
  93. /* routines to allocate memory from an pool... */
  94.  
  95. void *palloc(struct pool *, int nbytes);
  96. void *pcalloc(struct pool *, int nbytes);
  97. extern char *pstrdup(struct pool *, const char *s);
  98. extern char *pstrndup(struct pool *, const char *s, int n);
  99. char *pstrcat(struct pool *, ...); /* all '...' must be char* */
  100.  
  101. /* array and alist management... keeping lists of things.
  102.  * Common enough to want common support code ...
  103.  */
  104.  
  105. typedef struct {
  106.     pool *pool;
  107.     int elt_size;
  108.     int nelts;
  109.     int nalloc;
  110.     char *elts;
  111. } array_header;
  112.  
  113. array_header *make_array (pool *p, int nelts, int elt_size);
  114. void *push_array (array_header *);
  115. void array_cat (array_header *dst, array_header *src);
  116. array_header *append_arrays (pool *, array_header *, 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, array_header *src);
  124. array_header *copy_array_hdr (pool *p, 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, table *);
  147. char *table_get (table *, char *);
  148. void table_set (table *, const char *name, const char *val);
  149. void table_merge (table *, char *name, char *more_val);
  150. void table_unset (table *, char *key);
  151. void table_add (table *, char *name, char *val);
  152.  
  153. table *overlay_tables (pool *p, table *overlay, table *base);
  154.  
  155. array_header *table_elts (table *);
  156.  
  157. /* routines to remember allocation of other sorts of things...
  158.  * generic interface first.  Note that we want to have two separate
  159.  * cleanup functions in the general case, one for exec() preparation,
  160.  * to keep CGI scripts and the like from inheriting access to things
  161.  * they shouldn't be able to touch, and one for actually cleaning up,
  162.  * when the actual server process wants to get rid of the thing,
  163.  * whatever it is.
  164.  *
  165.  * kill_cleanup disarms a cleanup, presumably because the resource in
  166.  * question has been closed, freed, or whatever, and it's scarce
  167.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  168.  * resource not to be cleaned up a second time (it might have been
  169.  * reallocated).  run_cleanup does the same, but runs it first.
  170.  *
  171.  * Cleanups are identified for purposes of finding & running them off by the
  172.  * plain_cleanup and data, which should presumably be unique.
  173.  *
  174.  * NB any code which invokes register_cleanup or kill_cleanup directly
  175.  * is a critical section which should be guarded by block_alarms() and
  176.  * unblock_alarms() below...
  177.  */
  178.  
  179. void register_cleanup (pool *p, void *data,
  180.                void (*plain_cleanup)(void *),
  181.                void (*child_cleanup)(void *));
  182.  
  183. void kill_cleanup (pool *p, void *data, void (*plain_cleanup)(void *));
  184.  
  185. /* The time between when a resource is actually allocated, and when it
  186.  * its cleanup is registered is a critical section, during which the
  187.  * resource could leak if we got interrupted or timed out.  So, anything
  188.  * which registers cleanups should bracket resource allocation and the
  189.  * cleanup registry with these.  (This is done internally by run_cleanup).
  190.  *
  191.  * NB they are actually implemented in http_main.c, since they are bound
  192.  * up with timeout handling in general...
  193.  */
  194.  
  195. extern void block_alarms();
  196. extern void unblock_alarms();
  197.  
  198. /* Common cases which want utility support..
  199.  * the note_cleanups_for_foo routines are for
  200.  */
  201.  
  202. FILE *pfopen(struct pool *, char *name, char *fmode);
  203. FILE *pfdopen(struct pool *, int fd, char *fmode);
  204. int popenf(struct pool *, char *name, int flg, int mode);
  205.  
  206. void note_cleanups_for_file (pool *, FILE *);
  207. void note_cleanups_for_fd (pool *, int);
  208.  
  209. /* routines to note closes... file descriptors are constrained enough
  210.  * on some systems that we want to support this.
  211.  */
  212.  
  213. int pfclose(struct pool *, FILE *);
  214. int pclosef(struct pool *, int fd);
  215.  
  216. /* ... even child processes (which we may want to wait for,
  217.  * or to kill outright, on unexpected termination).
  218.  *
  219.  * spawn_child is a utility routine which handles an awful lot of
  220.  * the rigamarole associated with spawning a child --- it arranges
  221.  * for pipes to the child's stdin and stdout, if desired (if not,
  222.  * set the associated args to NULL).  It takes as args a function
  223.  * to call in the child, and an argument to be passed to the function.
  224.  */
  225.  
  226. #ifndef __EMX__
  227. enum kill_conditions { kill_never, kill_always, kill_after_timeout, just_wait};
  228. #endif
  229.  
  230. int spawn_child (pool *, void (*)(void *), void *,
  231.          enum kill_conditions, FILE **pipe_in, FILE **pipe_out);
  232. #ifdef __EMX__
  233. int spawn_child_os2 (pool *, void (*)(void *), void *,
  234.          enum kill_conditions, FILE **pipe_in, FILE **pipe_out, request_rec *r);
  235.          //enum kill_conditions, FILE **pipe_in, FILE **pipe_out, char *buffer, int lenp);
  236. #endif
  237.  
  238. /* magic numbers --- only one so far, min free bytes in a new pool block */
  239.  
  240. #define BLOCK_MINFREE 8192
  241.  
  242. /* Finally, some accounting */
  243.  
  244. long bytes_in_pool(pool *p);
  245. long bytes_in_free_blocks();
  246.