xref: /plan9/sys/src/cmd/gs/src/gsmemret.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gsmemret.h,v 1.5 2002/06/16 08:45:42 lpd Exp $ */
18 /* Interface to retrying memory allocator */
19 
20 #if !defined(gsmemret_INCLUDED)
21 #  define gsmemret_INCLUDED
22 
23 #include "gsmemory.h"
24 
25 /*
26  * This allocator encapsulates another allocator with a closure that is
27  * called to attempt to free up memory if an allocation fails.
28  * Note that it does not keep track of memory that it acquires:
29  * thus free_all with FREE_ALL_DATA is a no-op.
30  */
31 typedef struct gs_memory_retrying_s gs_memory_retrying_t;
32 
33 /*
34  * Define the procedure type for the recovery closure.
35  */
36 typedef enum {
37     RECOVER_STATUS_NO_RETRY,
38     RECOVER_STATUS_RETRY_OK
39 } gs_memory_recover_status_t;
40 typedef gs_memory_recover_status_t (*gs_memory_recover_proc_t)
41      (gs_memory_retrying_t *rmem, void *proc_data);
42 
43 struct gs_memory_retrying_s {
44     gs_memory_common;		/* interface outside world sees */
45     gs_memory_t *target;	/* allocator to front */
46     gs_memory_recover_proc_t recover_proc;
47     void *recover_proc_data;
48 };
49 
50 /* ---------- Public constructors/destructors ---------- */
51 
52 /* Initialize a retrying memory manager. */
53 int gs_memory_retrying_init(
54 			    gs_memory_retrying_t * rmem, /* allocator to init */
55 			    gs_memory_t * target	/* allocator to wrap */
56 			    );
57 
58 /* Release a retrying memory manager. */
59 /* Note that this has no effect on the target. */
60 void gs_memory_retrying_release(gs_memory_retrying_t *rmem);
61 
62 /* Set the recovery closure of a retrying memory manager. */
63 void gs_memory_retrying_set_recover(gs_memory_retrying_t *rmem,
64 				    gs_memory_recover_proc_t recover_proc,
65 				    void *recover_proc_data);
66 
67 /* Get the target of a retrying memory manager. */
68 gs_memory_t * gs_memory_retrying_target(const gs_memory_retrying_t *rmem);
69 
70 #endif /*!defined(gsmemret_INCLUDED) */
71