1d514b0f3Smrg#ifndef _H_MSPACE
2d514b0f3Smrg#define _H_MSPACE
3d514b0f3Smrg
4d514b0f3Smrg#define NO_MALLINFO 0
5d514b0f3Smrg
6d514b0f3Smrg#ifdef __cplusplus
7d514b0f3Smrgextern "C" {
8d514b0f3Smrg#endif /* __cplusplus */
9d514b0f3Smrg
10d514b0f3Smrg//typedef unsigned long size_t;
11d514b0f3Smrgtypedef void (*mspace_abort_t)(void *user_data);
12d514b0f3Smrgtypedef void (*mspace_print_t)(void *user_data, const char *format, ...)  __attribute__((format(gnu_printf, 2, 3)));
13d514b0f3Smrg
14d514b0f3Smrgvoid mspace_set_abort_func(mspace_abort_t f);
15d514b0f3Smrgvoid mspace_set_print_func(mspace_print_t f);
16d514b0f3Smrg
17d514b0f3Smrgvoid default_abort_func(void *user_data);
18d514b0f3Smrgvoid default_print_func(void *user_data, const char *format, ...);
19d514b0f3Smrg
20d514b0f3Smrg/*
21d514b0f3Smrg  mspace is an opaque type representing an independent
22d514b0f3Smrg  region of space that supports mspace_malloc, etc.
23d514b0f3Smrg*/
24d514b0f3Smrgtypedef void* mspace;
25d514b0f3Smrg
26d514b0f3Smrg/*
27d514b0f3Smrg  create_mspace creates and returns a new independent space with the
28d514b0f3Smrg  given initial capacity, or, if 0, the default granularity size.  It
29d514b0f3Smrg  returns null if there is no system memory available to create the
30d514b0f3Smrg  space.  If argument locked is non-zero, the space uses a separate
31d514b0f3Smrg  lock to control access. The capacity of the space will grow
32d514b0f3Smrg  dynamically as needed to service mspace_malloc requests.  You can
33d514b0f3Smrg  control the sizes of incremental increases of this space by
34d514b0f3Smrg  compiling with a different DEFAULT_GRANULARITY or dynamically
35d514b0f3Smrg  setting with mallopt(M_GRANULARITY, value).
36d514b0f3Smrg*/
37d514b0f3Smrg//mspace create_mspace(size_t capacity, int locked);
38d514b0f3Smrg
39d514b0f3Smrg/*
40d514b0f3Smrg  destroy_mspace destroys the given space, and attempts to return all
41d514b0f3Smrg  of its memory back to the system, returning the total number of
42d514b0f3Smrg  bytes freed. After destruction, the results of access to all memory
43d514b0f3Smrg  used by the space become undefined.
44d514b0f3Smrg*/
45d514b0f3Smrg//size_t destroy_mspace(mspace msp);
46d514b0f3Smrg
47d514b0f3Smrg/*
48d514b0f3Smrg  create_mspace_with_base uses the memory supplied as the initial base
49d514b0f3Smrg  of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
50d514b0f3Smrg  space is used for bookkeeping, so the capacity must be at least this
51d514b0f3Smrg  large. (Otherwise 0 is returned.) When this initial space is
52d514b0f3Smrg  exhausted, additional memory will be obtained from the system.
53d514b0f3Smrg  Destroying this space will deallocate all additionally allocated
54d514b0f3Smrg  space (if possible) but not the initial base.
55d514b0f3Smrg*/
56d514b0f3Smrgmspace create_mspace_with_base(void* base, size_t capacity, int locked, void *user_data);
57d514b0f3Smrg
58d514b0f3Smrg/*
59d514b0f3Smrg  mspace_malloc behaves as malloc, but operates within
60d514b0f3Smrg  the given space.
61d514b0f3Smrg*/
62d514b0f3Smrgvoid* mspace_malloc(mspace msp, size_t bytes);
63d514b0f3Smrg
64d514b0f3Smrg/*
65d514b0f3Smrg  mspace_free behaves as free, but operates within
66d514b0f3Smrg  the given space.
67d514b0f3Smrg
68d514b0f3Smrg  If compiled with FOOTERS==1, mspace_free is not actually needed.
69d514b0f3Smrg  free may be called instead of mspace_free because freed chunks from
70d514b0f3Smrg  any space are handled by their originating spaces.
71d514b0f3Smrg*/
72d514b0f3Smrgvoid mspace_free(mspace msp, void* mem);
73d514b0f3Smrg
74d514b0f3Smrg/*
75d514b0f3Smrg  mspace_realloc behaves as realloc, but operates within
76d514b0f3Smrg  the given space.
77d514b0f3Smrg
78d514b0f3Smrg  If compiled with FOOTERS==1, mspace_realloc is not actually
79d514b0f3Smrg  needed.  realloc may be called instead of mspace_realloc because
80d514b0f3Smrg  realloced chunks from any space are handled by their originating
81d514b0f3Smrg  spaces.
82d514b0f3Smrg*/
83d514b0f3Smrgvoid* mspace_realloc(mspace msp, void* mem, size_t newsize);
84d514b0f3Smrg
85d514b0f3Smrg/*
86d514b0f3Smrg  mspace_calloc behaves as calloc, but operates within
87d514b0f3Smrg  the given space.
88d514b0f3Smrg*/
89d514b0f3Smrgvoid* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
90d514b0f3Smrg
91d514b0f3Smrg/*
92d514b0f3Smrg  mspace_memalign behaves as memalign, but operates within
93d514b0f3Smrg  the given space.
94d514b0f3Smrg*/
95d514b0f3Smrgvoid* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
96d514b0f3Smrg
97d514b0f3Smrg/*
98d514b0f3Smrg  mspace_independent_calloc behaves as independent_calloc, but
99d514b0f3Smrg  operates within the given space.
100d514b0f3Smrg*/
101d514b0f3Smrg//void** mspace_independent_calloc(mspace msp, size_t n_elements,
102d514b0f3Smrg//                                 size_t elem_size, void* chunks[]);
103d514b0f3Smrg
104d514b0f3Smrg/*
105d514b0f3Smrg  mspace_independent_comalloc behaves as independent_comalloc, but
106d514b0f3Smrg  operates within the given space.
107d514b0f3Smrg*/
108d514b0f3Smrg//void** mspace_independent_comalloc(mspace msp, size_t n_elements,
109d514b0f3Smrg//                                   size_t sizes[], void* chunks[]);
110d514b0f3Smrg
111d514b0f3Smrg/*
112d514b0f3Smrg  mspace_footprint() returns the number of bytes obtained from the
113d514b0f3Smrg  system for this space.
114d514b0f3Smrg*/
115d514b0f3Smrgsize_t mspace_footprint(mspace msp);
116d514b0f3Smrg
117d514b0f3Smrg/*
118d514b0f3Smrg  mspace_max_footprint() returns the peak number of bytes obtained from the
119d514b0f3Smrg  system for this space.
120d514b0f3Smrg*/
121d514b0f3Smrgsize_t mspace_max_footprint(mspace msp);
122d514b0f3Smrg
123d514b0f3Smrg
124d514b0f3Smrg#if !NO_MALLINFO
125d514b0f3Smrg/*
126d514b0f3Smrg  mspace_mallinfo behaves as mallinfo, but reports properties of
127d514b0f3Smrg  the given space.
128d514b0f3Smrg*/
129d514b0f3Smrgstruct mallinfo mspace_mallinfo(mspace msp);
130d514b0f3Smrg#endif /* NO_MALLINFO */
131d514b0f3Smrg
132d514b0f3Smrg/*
133d514b0f3Smrg  mspace_malloc_stats behaves as malloc_stats, but reports
134d514b0f3Smrg  properties of the given space. The return variant returns instead of
135d514b0f3Smrg  printing the three quantities, maxfp, fp, and used.
136d514b0f3Smrg*/
137d514b0f3Smrgvoid mspace_malloc_stats(mspace msp);
138d514b0f3Smrgvoid mspace_malloc_stats_return(mspace msp, size_t *ret_maxfp, size_t *ret_fp,
139d514b0f3Smrg                                size_t *ret_used);
140d514b0f3Smrg
141d514b0f3Smrg/*
142d514b0f3Smrg  mspace_trim behaves as malloc_trim, but
143d514b0f3Smrg  operates within the given space.
144d514b0f3Smrg*/
145d514b0f3Smrg//int mspace_trim(mspace msp, size_t pad);
146d514b0f3Smrg
147d514b0f3Smrg/*
148d514b0f3Smrg  An alias for mallopt.
149d514b0f3Smrg*/
150d514b0f3Smrgint mspace_mallopt(int, int);
151d514b0f3Smrg
152d514b0f3Smrg#ifdef __cplusplus
153d514b0f3Smrg};  /* end of extern "C" */
154d514b0f3Smrg#endif /* __cplusplus */
155d514b0f3Smrg
156d514b0f3Smrg#endif
157