1 /* Declarations for `mmalloc' and friends.
2 Copyright 1990, 1991, 1992 Free Software Foundation
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com) */
7 /* Copyright (c) 2010. The SimGrid Team.
8 * All rights reserved. */
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. */
14 #define __MMPRIVATE_H 1
17 #include "xbt/xbt_os_thread.h"
18 #include "xbt/mmalloc.h"
19 #include <semaphore.h>
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 */
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. */
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)
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. */
51 #define ADDR2UINT(addr) ((unsigned int) ((char*) (addr) - (char*) NULL))
52 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
54 /* Determine the amount of memory spanned by the initial heap table
55 (not an absolute limit). */
57 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
59 /* Number of contiguous free blocks allowed to build up at the end of
60 memory before they will be returned to the system. */
62 #define FINAL_FREE_BLOCKS 8
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
69 #define MALLOC_SEARCH_START mdp -> heapindex
71 /* Address to block number and vice versa. */
73 #define BLOCK(A) (((char*) (A) - (char*) mdp -> heapbase) / BLOCKSIZE + 1)
75 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
77 const char *xbt_thread_self_name(void);
79 /* Data structure giving per-block information. */
81 /* Heap information for a busy block. */
83 /* Zero for a large block, or positive giving the
84 logarithm to the base two of the fragment size. */
88 size_t nfree; /* Free fragments in a fragmented block. */
89 size_t first; /* First free fragment of the block. */
92 size_t size; /* Size (in blocks) of a large cluster. */
97 /* Heap information for a free block (that may be the first of
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. */
106 /* List of blocks allocated with `mmemalign' (or `mvalloc'). */
109 struct alignlist *next;
110 void *aligned; /* The address that mmemaligned returned. */
111 void *exact; /* The address that malloc returned. */
114 /* Doubly linked lists of free fragments. */
121 /* Internal structure that defines the format of the malloc-descriptor.
122 This gets written to the base address of the region that mmalloc is
123 managing, and thus also becomes the file header for the mapped file,
124 if such a file exists. */
128 /* Semaphore locking the access to the heap */
131 /* Number of processes that attached the heap */
132 unsigned int refcount;
134 /* Chained lists of mdescs */
135 struct mdesc *next_mdesc;
137 /* The "magic number" for an mmalloc file. */
138 char magic[MMALLOC_MAGIC_SIZE];
140 /* The size in bytes of this structure, used as a sanity check when reusing
141 a previously created mapped file. */
142 unsigned int headersize;
144 /* The version number of the mmalloc package that created this file. */
145 unsigned char version;
147 /* Some flag bits to keep track of various internal things. */
150 /* Number of info entries. */
153 /* Pointer to first block of the heap (base of the first block). */
156 /* Current search index for the heap table. */
157 /* Search index in the info table. */
160 /* Limit of valid info table indices. */
163 /* Block information table.
164 Allocated with malign/__mmalloc_free (not mmalloc/mfree). */
165 /* Table indexed by block number giving per-block information. */
167 malloc_info *heapinfo;
169 /* Free list headers for each fragment size. */
170 /* Free lists for each fragment size. */
172 struct list fraghead[BLOCKLOG];
174 /* List of blocks allocated by memalign. */
176 struct alignlist *aligned_blocks;
178 /* The base address of the memory region for this malloc heap. This
179 is the location where the bookkeeping data for mmap and for malloc
184 /* The current location in the memory region for this malloc heap which
185 represents the end of memory in use. */
189 /* The end of the current memory region for this malloc heap. This is
190 the first location past the end of mapped memory. */
194 /* Open file descriptor for the file to which this malloc heap is mapped.
195 This will always be a valid file descriptor, since /dev/zero is used
196 by default if no open file is supplied by the client. Also note that
197 it may change each time the region is mapped and unmapped. */
203 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr);
205 void mmalloc_display_info(void *h);
207 /* Bits to look at in the malloc descriptor flags word */
209 #define MMALLOC_DEVZERO (1 << 0) /* Have mapped to /dev/zero */
210 #define MMALLOC_ANONYMOUS (1 << 1) /* Use anonymous mapping */
211 #define MMALLOC_INITIALIZED (1 << 2) /* Initialized mmalloc */
213 /* Internal version of `mfree' used in `morecore'. */
215 extern void __mmalloc_free(struct mdesc *mdp, void *ptr);
217 /* A default malloc descriptor for the single sbrk() managed region. */
219 extern struct mdesc *__mmalloc_default_mdp;
221 /* Initialize the first use of the default malloc descriptor, which uses
224 extern struct mdesc *__mmalloc_create_default_mdp(void);
226 /* Remap a mmalloc region that was previously mapped. */
228 extern void *__mmalloc_remap_core(struct mdesc *mdp);
230 /* Get core for the memory region specified by MDP, using SIZE as the
231 amount to either add to or subtract from the existing region. Works
232 like sbrk(), but using mmap(). */
233 extern void *mmorecore(struct mdesc *mdp, int size);
235 /* Thread-safety (if the sem is already created) FIXME: KILLIT*/
239 #define UNLOCK(mdp) \
242 #endif /* __MMPRIVATE_H */