home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 December / PCpro_2006_12.ISO / ossdvd / server / Apache / include / ap_alloc.h next >
Encoding:
C/C++ Source or Header  |  2006-04-21  |  14.9 KB  |  375 lines

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