Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fa9b62afec4b3810511a9bf867045627f63977ec
[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 #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 const char *xbt_thread_self_name(void);
83
84 /* Data structure giving per-block information.  */
85 typedef union {
86   /* Heap information for a busy block.  */
87   struct {
88     /* Zero for a large block, or positive giving the
89        logarithm to the base two of the fragment size.  */
90     int type;
91     union {
92       struct {
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     size_t size;                /* Size (in blocks) of a free cluster.  */
104     size_t next;                /* Index of next free cluster.  */
105     size_t prev;                /* Index of previous free cluster.  */
106   } free;
107 } malloc_info;
108
109 /* List of blocks allocated with `mmemalign' (or `mvalloc').  */
110
111 struct alignlist {
112   struct alignlist *next;
113   void *aligned;                /* The address that mmemaligned returned.  */
114   void *exact;                  /* The address that malloc returned.  */
115 };
116
117 /* Doubly linked lists of free fragments.  */
118
119 struct list {
120   struct list *next;
121   struct list *prev;
122 };
123
124 /* Statistics available to the user.
125    FIXME:  By design, the internals of the malloc package are no longer
126    exported to the user via an include file, so access to this data needs
127    to be via some other mechanism, such as mmstat_<something> where the
128    return value is the <something> the user is interested in. */
129
130 struct mstats {
131   size_t bytes_total;           /* Total size of the heap. */
132   size_t chunks_used;           /* Chunks allocated by the user. */
133   size_t bytes_used;            /* Byte total of user-allocated chunks. */
134   size_t chunks_free;           /* Chunks in the free list. */
135   size_t bytes_free;            /* Byte total of chunks in the free list. */
136 };
137
138 /* Internal structure that defines the format of the malloc-descriptor.
139    This gets written to the base address of the region that mmalloc is
140    managing, and thus also becomes the file header for the mapped file,
141    if such a file exists. */
142
143 struct mdesc {
144
145   /* Semaphore locking the access to the heap */
146   sem_t sem;
147
148   /* Number of processes that attached the heap */
149   unsigned int refcount;
150
151   /* Chained lists of mdescs */
152   struct mdesc *next_mdesc;
153   
154   /* The "magic number" for an mmalloc file. */
155   char magic[MMALLOC_MAGIC_SIZE];
156
157   /* The size in bytes of this structure, used as a sanity check when reusing
158      a previously created mapped file. */
159   unsigned int headersize;
160
161   /* The version number of the mmalloc package that created this file. */
162   unsigned char version;
163
164   /* Some flag bits to keep track of various internal things. */
165   unsigned int flags;
166
167   /* If a system call made by the mmalloc package fails, the errno is
168      preserved for future examination. */
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   void *(*morecore) (struct mdesc * mdp, int size);
181
182   /* Pointer to the function that causes an abort when the memory checking
183      features are activated.  By default this is set to abort(), but can
184      be set to another function by the application using mmalloc().
185
186      FIXME:  For mapped regions shared by more than one process, this
187      needs to be maintained on a per-process basis. */
188   void (*abortfunc) (void);
189
190   /* Debugging hook for free.
191
192      FIXME:  For mapped regions shared by more than one process, this
193      needs to be maintained on a per-process basis. */
194   void (*mfree_hook) (void *mdp, void *ptr);
195
196   /* Debugging hook for `malloc'.
197
198      FIXME:  For mapped regions shared by more than one process, this
199      needs to be maintained on a per-process basis. */
200   void *(*mmalloc_hook) (void *mdp, size_t size);
201
202   /* Debugging hook for realloc.
203
204      FIXME:  For mapped regions shared by more than one process, this
205      needs to be maintained on a per-process basis. */
206   void *(*mrealloc_hook) (void *mdp, void *ptr, size_t size);
207
208   /* Number of info entries.  */
209   size_t heapsize;
210
211   /* Pointer to first block of the heap (base of the first block).  */
212   void *heapbase;
213
214   /* Current search index for the heap table.  */
215   /* Search index in the info table.  */
216   size_t heapindex;
217
218   /* Limit of valid info table indices.  */
219   size_t heaplimit;
220
221   /* Block information table.
222      Allocated with malign/__mmalloc_free (not mmalloc/mfree).  */
223   /* Table indexed by block number giving per-block information.  */
224
225   malloc_info *heapinfo;
226
227   /* Instrumentation.  */
228
229   struct mstats heapstats;
230
231   /* Free list headers for each fragment size.  */
232   /* Free lists for each fragment size.  */
233
234   struct list fraghead[BLOCKLOG];
235
236   /* List of blocks allocated by memalign.  */
237
238   struct alignlist *aligned_blocks;
239
240   /* The base address of the memory region for this malloc heap.  This
241      is the location where the bookkeeping data for mmap and for malloc
242      begins. */
243
244   void *base;
245
246   /* The current location in the memory region for this malloc heap which
247      represents the end of memory in use. */
248
249   void *breakval;
250
251   /* The end of the current memory region for this malloc heap.  This is
252      the first location past the end of mapped memory. */
253
254   void *top;
255
256   /* Open file descriptor for the file to which this malloc heap is mapped.
257      This will always be a valid file descriptor, since /dev/zero is used
258      by default if no open file is supplied by the client.  Also note that
259      it may change each time the region is mapped and unmapped. */
260
261   int fd;
262
263   /* An array of keys to data within the mapped region, for use by the
264      application.  */
265
266   void *keys[MMALLOC_KEYS];
267
268 };
269
270 int mmalloc_compare_heap(void *h1, void *h2);
271
272 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2);
273
274 void mmalloc_display_info(void *h);
275
276 /* Bits to look at in the malloc descriptor flags word */
277
278 #define MMALLOC_DEVZERO         (1 << 0)        /* Have mapped to /dev/zero */
279 #define MMALLOC_ANONYMOUS (1 << 1)      /* Use anonymous mapping */
280 #define MMALLOC_INITIALIZED     (1 << 2)        /* Initialized mmalloc */
281 #define MMALLOC_MMCHECK_USED    (1 << 3)        /* mmcheckf() called already */
282
283 /* Internal version of `mfree' used in `morecore'. */
284
285 extern void __mmalloc_free(struct mdesc *mdp, void *ptr);
286
287 /* A default malloc descriptor for the single sbrk() managed region. */
288
289 extern struct mdesc *__mmalloc_default_mdp;
290
291 /* Initialize the first use of the default malloc descriptor, which uses
292    an sbrk() region. */
293
294 extern struct mdesc *__mmalloc_create_default_mdp(void);
295
296 /* Grow or shrink a contiguous mapped region using mmap().
297    Works much like sbrk(), only faster */
298
299 extern void *__mmalloc_mmap_morecore(struct mdesc *mdp, int size);
300
301
302 /* Remap a mmalloc region that was previously mapped. */
303
304 extern void *__mmalloc_remap_core(struct mdesc *mdp);
305
306 /* Macro to convert from a user supplied malloc descriptor to pointer to the
307    internal malloc descriptor.  If the user supplied descriptor is NULL, then
308    use the default internal version, initializing it if necessary.  Otherwise
309    just cast the user supplied version (which is void *) to the proper type
310    (struct mdesc *). */
311
312 #define MD_TO_MDP(md) \
313   ((md) == NULL \
314    ? __mmalloc_default_mdp  \
315    : (struct mdesc *) (md))
316
317 /* Thread-safety (if the sem is already created)*/
318 #define LOCK(md)                                        \
319   do {\
320     struct mdesc *lock_local_mdp = MD_TO_MDP(md);       \
321     sem_wait(&lock_local_mdp->sem);     \
322   } while (0)
323
324 #define UNLOCK(md)                                        \
325   do {                                                  \
326     struct mdesc *unlock_local_mdp = MD_TO_MDP(md);       \
327     sem_post(&unlock_local_mdp->sem);     \
328   } while (0)
329
330 #endif                          /* __MMPRIVATE_H */