xref: /plan9/sys/src/cmd/gs/src/gsmalloc.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997, 1998, 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: gsmalloc.h,v 1.6 2004/08/04 19:36:12 stefan Exp $ */
18 /* Client interface to default (C heap) allocator */
19 /* Requires gsmemory.h */
20 
21 #ifndef gsmalloc_INCLUDED
22 #  define gsmalloc_INCLUDED
23 
24 /* Define a memory manager that allocates directly from the C heap. */
25 typedef struct gs_malloc_block_s gs_malloc_block_t;
26 typedef struct gs_malloc_memory_s {
27     gs_memory_common;
28     gs_malloc_block_t *allocated;
29     long limit;
30     long used;
31     long max_used;
32 } gs_malloc_memory_t;
33 
34 /* Allocate and initialize a malloc memory manager. */
35 gs_malloc_memory_t *gs_malloc_memory_init(void);
36 
37 /* Release all the allocated blocks, and free the memory manager. */
38 /* The cast is unfortunate, but unavoidable. */
39 #define gs_malloc_memory_release(mem)\
40   gs_memory_free_all((gs_memory_t *)mem, FREE_ALL_EVERYTHING,\
41 		     "gs_malloc_memory_release")
42 
43 gs_memory_t * gs_malloc_init(const gs_memory_t *parent);
44 void gs_malloc_release(gs_memory_t *mem);
45 
46 #define gs_malloc(mem, nelts, esize, cname)\
47   (void *)gs_alloc_byte_array(mem->non_gc_memory, nelts, esize, cname)
48 #define gs_free(mem, data, nelts, esize, cname)\
49   gs_free_object(mem->non_gc_memory, data, cname)
50 
51 /* ---------------- Locking ---------------- */
52 
53 /* Create a locked wrapper for a heap allocator. */
54 int gs_malloc_wrap(gs_memory_t **wrapped, gs_malloc_memory_t *contents);
55 
56 /* Get the wrapped contents. */
57 gs_malloc_memory_t *gs_malloc_wrapped_contents(gs_memory_t *wrapped);
58 
59 /* Free the wrapper, and return the wrapped contents. */
60 gs_malloc_memory_t *gs_malloc_unwrap(gs_memory_t *wrapped);
61 
62 #endif /* gsmalloc_INCLUDED */
63