ap_alloc.inc 17 KB

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