Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3c83a4b6cf85ec855d454b7b170338c473e6d4e3
[simgrid.git] / src / xbt / mmalloc / mmprivate.h
1 /* Declarations for `mmalloc' and friends.
2    Copyright 1990, 1991, 1992 Free Software Foundation
3
4    Written May 1989 by Mike Haertel.
5    Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
6
7 /* Copyright (c) 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #ifndef __MMPRIVATE_H
14 #define __MMPRIVATE_H 1
15
16 #include "xbt/mmalloc.h"
17
18 #ifdef HAVE_LIMITS_H
19 #  include <limits.h>
20 #else
21 #  ifndef CHAR_BIT
22 #    define CHAR_BIT 8
23 #  endif
24 #endif
25
26 #ifndef MIN
27 #  define MIN(A, B) ((A) < (B) ? (A) : (B))
28 #endif
29
30 #define MMALLOC_MAGIC           "mmalloc"       /* Mapped file magic number */
31 #define MMALLOC_MAGIC_SIZE      8               /* Size of magic number buf */
32 #define MMALLOC_VERSION         1               /* Current mmalloc version */
33 #define MMALLOC_KEYS            16              /* Keys for application use */
34
35 /* The allocator divides the heap into blocks of fixed size; large
36    requests receive one or more whole blocks, and small requests
37    receive a fragment of a block.  Fragment sizes are powers of two,
38    and all fragments of a block are the same size.  When all the
39    fragments in a block have been freed, the block itself is freed.  */
40
41 #define INT_BIT         (CHAR_BIT * sizeof(int))
42 #define BLOCKLOG        (INT_BIT > 16 ? 12 : 9)
43 #define BLOCKSIZE       ((unsigned int) 1 << BLOCKLOG)
44 #define BLOCKIFY(SIZE)  (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
45
46 /* The difference between two pointers is a signed int.  On machines where
47    the data addresses have the high bit set, we need to ensure that the
48    difference becomes an unsigned int when we are using the address as an
49    integral value.  In addition, when using with the '%' operator, the
50    sign of the result is machine dependent for negative values, so force
51    it to be treated as an unsigned int. */
52
53 #define ADDR2UINT(addr) ((unsigned int) ((char*) (addr) - (char*) NULL))
54 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
55
56 /* Determine the amount of memory spanned by the initial heap table
57    (not an absolute limit).  */
58
59 #define HEAP            (INT_BIT > 16 ? 4194304 : 65536)
60
61 /* Number of contiguous free blocks allowed to build up at the end of
62    memory before they will be returned to the system.  */
63
64 #define FINAL_FREE_BLOCKS       8
65
66 /* Where to start searching the free list when looking for new memory.
67    The two possible values are 0 and heapindex.  Starting at 0 seems
68    to reduce total memory usage, while starting at heapindex seems to
69    run faster.  */
70
71 #define MALLOC_SEARCH_START     mdp -> heapindex
72
73 /* Address to block number and vice versa.  */
74
75 #define BLOCK(A) (((char*) (A) - (char*) mdp -> heapbase) / BLOCKSIZE + 1)
76
77 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
78
79 /* Data structure giving per-block information.  */
80
81 typedef union
82   {
83     /* Heap information for a busy block.  */
84     struct
85       {
86         /* Zero for a large block, or positive giving the
87            logarithm to the base two of the fragment size.  */
88         int type;
89         union
90           {
91             struct
92               {
93                 size_t nfree;   /* Free fragments in a fragmented block.  */
94                 size_t first;   /* First free fragment of the block.  */
95               } frag;
96             /* Size (in blocks) of a large cluster.  */
97             size_t size;
98           } info;
99       } busy;
100     /* Heap information for a free block (that may be the first of
101        a free cluster).  */
102     struct
103       {
104         size_t size;            /* Size (in blocks) of a free cluster.  */
105         size_t next;            /* Index of next free cluster.  */
106         size_t prev;            /* Index of previous free cluster.  */
107       } free;
108   } malloc_info;
109
110 /* List of blocks allocated with `mmemalign' (or `mvalloc').  */
111
112 struct alignlist
113   {
114     struct alignlist *next;
115     void* aligned;              /* The address that mmemaligned returned.  */
116     void* exact;                        /* The address that malloc returned.  */
117   };
118
119 /* Doubly linked lists of free fragments.  */
120
121 struct list
122   {
123     struct list *next;
124     struct list *prev;
125   };
126
127 /* Statistics available to the user.
128    FIXME:  By design, the internals of the malloc package are no longer
129    exported to the user via an include file, so access to this data needs
130    to be via some other mechanism, such as mmstat_<something> where the
131    return value is the <something> the user is interested in. */
132
133 struct mstats
134   {
135     size_t bytes_total;         /* Total size of the heap. */
136     size_t chunks_used;         /* Chunks allocated by the user. */
137     size_t bytes_used;          /* Byte total of user-allocated chunks. */
138     size_t chunks_free;         /* Chunks in the free list. */
139     size_t bytes_free;          /* Byte total of chunks in the free list. */
140   };
141
142 /* Internal structure that defines the format of the malloc-descriptor.
143    This gets written to the base address of the region that mmalloc is
144    managing, and thus also becomes the file header for the mapped file,
145    if such a file exists. */
146
147 struct mdesc
148 {
149   /* The "magic number" for an mmalloc file. */
150
151   char magic[MMALLOC_MAGIC_SIZE];
152
153   /* The size in bytes of this structure, used as a sanity check when reusing
154      a previously created mapped file. */
155
156   unsigned int headersize;
157
158   /* The version number of the mmalloc package that created this file. */
159
160   unsigned char version;
161
162   /* Some flag bits to keep track of various internal things. */
163
164   unsigned int flags;
165
166   /* If a system call made by the mmalloc package fails, the errno is
167      preserved for future examination. */
168
169   int saved_errno;
170
171   /* Pointer to the function that is used to get more core, or return core
172      to the system, for requests using this malloc descriptor.  For memory
173      mapped regions, this is the mmap() based routine.  There may also be
174      a single malloc descriptor that points to an sbrk() based routine
175      for systems without mmap() or for applications that call the mmalloc()
176      package with a NULL malloc descriptor.
177
178      FIXME:  For mapped regions shared by more than one process, this
179      needs to be maintained on a per-process basis. */
180
181   void* (*morecore) (struct mdesc *mdp, int size);
182      
183   /* Pointer to the function that causes an abort when the memory checking
184      features are activated.  By default this is set to abort(), but can
185      be set to another function by the application using mmalloc().
186
187      FIXME:  For mapped regions shared by more than one process, this
188      needs to be maintained on a per-process basis. */
189
190   void (*abortfunc) (void);
191
192   /* Debugging hook for free.
193
194      FIXME:  For mapped regions shared by more than one process, this
195      needs to be maintained on a per-process basis. */
196
197   void (*mfree_hook) (void* mdp, void* ptr);
198
199   /* Debugging hook for `malloc'.
200
201      FIXME:  For mapped regions shared by more than one process, this
202      needs to be maintained on a per-process basis. */
203
204   void* (*mmalloc_hook) (void* mdp, size_t size);
205
206   /* Debugging hook for realloc.
207
208      FIXME:  For mapped regions shared by more than one process, this
209      needs to be maintained on a per-process basis. */
210
211   void* (*mrealloc_hook) (void* mdp, void* ptr, size_t size);
212
213   /* Number of info entries.  */
214
215   size_t heapsize;
216
217   /* Pointer to first block of the heap (base of the first block).  */
218
219   void* heapbase;
220
221   /* Current search index for the heap table.  */
222   /* Search index in the info table.  */
223
224   size_t heapindex;
225
226   /* Limit of valid info table indices.  */
227
228   size_t heaplimit;
229
230   /* Block information table.
231      Allocated with malign/__mmalloc_free (not mmalloc/mfree).  */
232   /* Table indexed by block number giving per-block information.  */
233
234   malloc_info *heapinfo;
235
236   /* Instrumentation.  */
237
238   struct mstats heapstats;
239
240   /* Free list headers for each fragment size.  */
241   /* Free lists for each fragment size.  */
242
243   struct list fraghead[BLOCKLOG];
244
245   /* List of blocks allocated by memalign.  */
246
247   struct alignlist *aligned_blocks;
248
249   /* The base address of the memory region for this malloc heap.  This
250      is the location where the bookkeeping data for mmap and for malloc
251      begins. */
252
253   void* base;
254
255   /* The current location in the memory region for this malloc heap which
256      represents the end of memory in use. */
257
258   void* breakval;
259
260   /* The end of the current memory region for this malloc heap.  This is
261      the first location past the end of mapped memory. */
262
263   void* top;
264
265   /* Open file descriptor for the file to which this malloc heap is mapped.
266      This will always be a valid file descriptor, since /dev/zero is used
267      by default if no open file is supplied by the client.  Also note that
268      it may change each time the region is mapped and unmapped. */
269
270   int fd;
271
272   /* An array of keys to data within the mapped region, for use by the
273      application.  */
274
275   void* keys[MMALLOC_KEYS];
276
277 };
278
279 /* Bits to look at in the malloc descriptor flags word */
280
281 #define MMALLOC_DEVZERO         (1 << 0)        /* Have mapped to /dev/zero */
282 #define MMALLOC_ANONYMOUS (1 << 1)  /* Use anonymous mapping */
283 #define MMALLOC_INITIALIZED     (1 << 2)        /* Initialized mmalloc */
284 #define MMALLOC_MMCHECK_USED    (1 << 3)        /* mmcheckf() called already */
285
286 /* Internal version of `mfree' used in `morecore'. */
287
288 extern void __mmalloc_free (struct mdesc *mdp, void* ptr);
289
290 /* A default malloc descriptor for the single sbrk() managed region. */
291
292 extern struct mdesc *__mmalloc_default_mdp;
293
294 /* Initialize the first use of the default malloc descriptor, which uses
295    an sbrk() region. */
296
297 extern struct mdesc *__mmalloc_sbrk_init (void);
298
299 /* Grow or shrink a contiguous mapped region using mmap().
300    Works much like sbrk() */
301
302 #if defined(HAVE_MMAP)
303
304 extern void* __mmalloc_mmap_morecore (struct mdesc *mdp, int size);
305
306 #endif
307
308 /* Remap a mmalloc region that was previously mapped. */
309
310 extern void* __mmalloc_remap_core (struct mdesc *mdp);
311
312 /* Macro to convert from a user supplied malloc descriptor to pointer to the
313    internal malloc descriptor.  If the user supplied descriptor is NULL, then
314    use the default internal version, initializing it if necessary.  Otherwise
315    just cast the user supplied version (which is void *) to the proper type
316    (struct mdesc *). */
317
318 #define MD_TO_MDP(md) \
319   ((md) == NULL \
320    ? (__mmalloc_default_mdp == NULL \
321       ? __mmalloc_sbrk_init () \
322       : __mmalloc_default_mdp) \
323    : (struct mdesc *) (md))
324
325 #endif  /* __MMPRIVATE_H */