Logo AND Algorithmique Numérique Distribuée

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