Logo AND Algorithmique Numérique Distribuée

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