Logo AND Algorithmique Numérique Distribuée

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