Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a9018c5d98be998bc9fa1cffb57c929bc807e1c8
[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 <semaphore.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
37 /* The allocator divides the heap into blocks of fixed size; large
38    requests receive one or more whole blocks, and small requests
39    receive a fragment of a block.  Fragment sizes are powers of two,
40    and all fragments of a block are the same size.  When all the
41    fragments in a block have been freed, the block itself is freed.  */
42
43 #define INT_BIT         (CHAR_BIT * sizeof(int))
44 #define BLOCKLOG        (INT_BIT > 16 ? 12 : 9)
45 #define BLOCKSIZE       ((unsigned int) 1 << BLOCKLOG)
46 #define BLOCKIFY(SIZE)  (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
47
48 /* The difference between two pointers is a signed int.  On machines where
49    the data addresses have the high bit set, we need to ensure that the
50    difference becomes an unsigned int when we are using the address as an
51    integral value.  In addition, when using with the '%' operator, the
52    sign of the result is machine dependent for negative values, so force
53    it to be treated as an unsigned int. */
54
55 #define ADDR2UINT(addr) ((unsigned int) ((char*) (addr) - (char*) NULL))
56 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
57
58 /* Determine the amount of memory spanned by the initial heap table
59    (not an absolute limit).  */
60
61 #define HEAP            (INT_BIT > 16 ? 4194304 : 65536)
62
63 /* Number of contiguous free blocks allowed to build up at the end of
64    memory before they will be returned to the system.  */
65
66 #define FINAL_FREE_BLOCKS       8
67
68 /* Where to start searching the free list when looking for new memory.
69    The two possible values are 0 and heapindex.  Starting at 0 seems
70    to reduce total memory usage, while starting at heapindex seems to
71    run faster.  */
72
73 #define MALLOC_SEARCH_START     mdp -> heapindex
74
75 /* Address to block number and vice versa.  */
76
77 #define BLOCK(A) (((char*) (A) - (char*) mdp -> heapbase) / BLOCKSIZE + 1)
78
79 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
80
81 const char *xbt_thread_self_name(void);
82
83 /* Data structure giving per-block information.  */
84 typedef union {
85   /* Heap information for a busy block.  */
86   struct {
87     /* Zero for a large block, or positive giving the
88        logarithm to the base two of the fragment size.  */
89     int type;
90     union {
91       struct {
92         size_t nfree;           /* Free fragments in a fragmented block.  */
93         size_t first;           /* First free fragment of the block.  */
94       } frag;
95       struct {
96         size_t size; /* Size (in blocks) of a large cluster.  */
97         size_t busy_size; 
98       } block;
99     } info;
100   } busy;
101   /* Heap information for a free block (that may be the first of
102      a free cluster).  */
103   struct {
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   struct alignlist *next;
114   void *aligned;                /* The address that mmemaligned returned.  */
115   void *exact;                  /* The address that malloc returned.  */
116 };
117
118 /* Doubly linked lists of free fragments.  */
119
120 struct list {
121   struct list *next;
122   struct list *prev;
123 };
124
125 /* Statistics available to the user.
126    FIXME:  By design, the internals of the malloc package are no longer
127    exported to the user via an include file, so access to this data needs
128    to be via some other mechanism, such as mmstat_<something> where the
129    return value is the <something> the user is interested in. */
130
131 struct mstats {
132   size_t bytes_total;           /* Total size of the heap. */
133   size_t chunks_used;           /* Chunks allocated by the user. */
134   size_t bytes_used;            /* Byte total of user-allocated chunks. */
135   size_t chunks_free;           /* Chunks in the free list. */
136   size_t bytes_free;            /* Byte total of chunks in the free list. */
137 };
138
139 /* Internal structure that defines the format of the malloc-descriptor.
140    This gets written to the base address of the region that mmalloc is
141    managing, and thus also becomes the file header for the mapped file,
142    if such a file exists. */
143
144 struct mdesc {
145
146   /* Semaphore locking the access to the heap */
147   sem_t sem;
148
149   /* Number of processes that attached the heap */
150   unsigned int refcount;
151
152   /* Chained lists of mdescs */
153   struct mdesc *next_mdesc;
154   
155   /* The "magic number" for an mmalloc file. */
156   char magic[MMALLOC_MAGIC_SIZE];
157
158   /* The size in bytes of this structure, used as a sanity check when reusing
159      a previously created mapped file. */
160   unsigned int headersize;
161
162   /* The version number of the mmalloc package that created this file. */
163   unsigned char version;
164
165   /* Some flag bits to keep track of various internal things. */
166   unsigned int flags;
167
168   /* If a system call made by the mmalloc package fails, the errno is
169      preserved for future examination. */
170   int saved_errno;
171
172   /* Pointer to the function that is used to get more core, or return core
173      to the system, for requests using this malloc descriptor.  For memory
174      mapped regions, this is the mmap() based routine.  There may also be
175      a single malloc descriptor that points to an sbrk() based routine
176      for systems without mmap() or for applications that call the mmalloc()
177      package with a NULL malloc descriptor.
178
179      FIXME:  For mapped regions shared by more than one process, this
180      needs to be maintained on a per-process basis. */
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   void (*abortfunc) (void);
190
191   /* Debugging hook for free.
192
193      FIXME:  For mapped regions shared by more than one process, this
194      needs to be maintained on a per-process basis. */
195   void (*mfree_hook) (void *mdp, void *ptr);
196
197   /* Debugging hook for `malloc'.
198
199      FIXME:  For mapped regions shared by more than one process, this
200      needs to be maintained on a per-process basis. */
201   void *(*mmalloc_hook) (void *mdp, size_t size);
202
203   /* Debugging hook for realloc.
204
205      FIXME:  For mapped regions shared by more than one process, this
206      needs to be maintained on a per-process basis. */
207   void *(*mrealloc_hook) (void *mdp, void *ptr, size_t size);
208
209   /* Number of info entries.  */
210   size_t heapsize;
211
212   /* Pointer to first block of the heap (base of the first block).  */
213   void *heapbase;
214
215   /* Current search index for the heap table.  */
216   /* Search index in the info table.  */
217   size_t heapindex;
218
219   /* Limit of valid info table indices.  */
220   size_t heaplimit;
221
222   /* Block information table.
223      Allocated with malign/__mmalloc_free (not mmalloc/mfree).  */
224   /* Table indexed by block number giving per-block information.  */
225
226   malloc_info *heapinfo;
227
228   /* Instrumentation.  */
229
230   struct mstats heapstats;
231
232   /* Free list headers for each fragment size.  */
233   /* Free lists for each fragment size.  */
234
235   struct list fraghead[BLOCKLOG];
236
237   /* List of blocks allocated by memalign.  */
238
239   struct alignlist *aligned_blocks;
240
241   /* The base address of the memory region for this malloc heap.  This
242      is the location where the bookkeeping data for mmap and for malloc
243      begins. */
244
245   void *base;
246
247   /* The current location in the memory region for this malloc heap which
248      represents the end of memory in use. */
249
250   void *breakval;
251
252   /* The end of the current memory region for this malloc heap.  This is
253      the first location past the end of mapped memory. */
254
255   void *top;
256
257   /* Open file descriptor for the file to which this malloc heap is mapped.
258      This will always be a valid file descriptor, since /dev/zero is used
259      by default if no open file is supplied by the client.  Also note that
260      it may change each time the region is mapped and unmapped. */
261
262   int fd;
263
264 };
265
266 int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr);
267
268 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr);
269
270 void mmalloc_display_info(void *h);
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 /* Thread-safety (if the sem is already created)*/
314 #define LOCK(md)                                        \
315   do {\
316     struct mdesc *lock_local_mdp = MD_TO_MDP(md);       \
317     sem_wait(&lock_local_mdp->sem);     \
318   } while (0)
319
320 #define UNLOCK(md)                                        \
321   do {                                                  \
322     struct mdesc *unlock_local_mdp = MD_TO_MDP(md);       \
323     sem_post(&unlock_local_mdp->sem);     \
324   } while (0)
325
326 #endif                          /* __MMPRIVATE_H */