Logo ROOT  
Reference Guide
mmprivate.h
Go to the documentation of this file.
1/* @(#)root/clib:$Id$ */
2
3/*************************************************************************
4 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11/* Declarations for `mmalloc' and friends.
12 Copyright 1990, 1991, 1992 Free Software Foundation
13
14 Written May 1989 by Mike Haertel.
15 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com)
16
17 The GNU C Library is free software; you can redistribute it and/or
18 modify it under the terms of the GNU Library General Public License as
19 published by the Free Software Foundation; either version 2 of the
20 License, or (at your option) any later version.
21
22 The GNU C Library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Library General Public License for more details.
26
27 You should have received a copy of the GNU Library General Public
28 License along with the GNU C Library; see the file COPYING.LIB. If
29 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA.
31
32 The author may be reached (Email) at the address mike@ai.mit.edu,
33 or (US mail) as Mike Haertel c/o Free Software Foundation. */
34
35
36#ifndef __MMPRIVATE_H
37#define __MMPRIVATE_H 1
38
39#include "mmalloc.h"
40
41#ifdef R__HAVE_LIMITS_H
42# include <limits.h>
43#else
44# ifndef CHAR_BIT
45# define CHAR_BIT 8
46# endif
47#endif
48
49#ifdef R__HAVE_STDDEF_H
50# include <stddef.h>
51#else
52# include <sys/types.h> /* hope for the best -- ANSI C is your friend */
53#endif
54
55#ifdef R__HAVE_UNISTD_H
56# include <unistd.h>
57#endif
58#ifdef R__HAVE_STDLIB_H
59# include <stdlib.h>
60#endif
61
62#include <stdint.h>
63
64#ifndef MIN
65# define MIN(A, B) ((A) < (B) ? (A) : (B))
66#endif
67
68#define MMALLOC_MAGIC "mmalloc" /* Mapped file magic number */
69#define MMALLOC_MAGIC_SIZE 8 /* Size of magic number buf */
70#define MMALLOC_VERSION 1 /* Current mmalloc version */
71#define MMALLOC_KEYS 16 /* Keys for application use */
72
73/* The allocator divides the heap into blocks of fixed size; large
74 requests receive one or more whole blocks, and small requests
75 receive a fragment of a block. Fragment sizes are powers of two,
76 and all fragments of a block are the same size. When all the
77 fragments in a block have been freed, the block itself is freed. */
78
79#define INT_BIT (CHAR_BIT * sizeof(int))
80#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
81#define BLOCKSIZE ((unsigned int) 1 << BLOCKLOG)
82#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
83
84#define ADDR2UINT(addr) ((uintptr_t)(addr))
85#define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
86
87/* Determine the amount of memory spanned by the initial heap table
88 (not an absolute limit). */
89
90#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
91
92/* Number of contiguous free blocks allowed to build up at the end of
93 memory before they will be returned to the system. */
94
95#define FINAL_FREE_BLOCKS 8
96
97/* Where to start searching the free list when looking for new memory.
98 The two possible values are 0 and heapindex. Starting at 0 seems
99 to reduce total memory usage, while starting at heapindex seems to
100 run faster. */
101
102#define MALLOC_SEARCH_START mdp -> heapindex
103
104/* Address to block number and vice versa. */
105
106#define BLOCK(A) (((char *) (A) - mdp -> heapbase) / BLOCKSIZE + 1)
107
108#define ADDRESS(B) ((PTR) (((B) - 1) * BLOCKSIZE + mdp -> heapbase))
109
110/* Data structure giving per-block information. */
111
112typedef union
113{
114 /* Heap information for a busy block. */
115 struct
116 {
117 /* Zero for a large block, or positive giving the
118 logarithm to the base two of the fragment size. */
119 int type;
120 union
121 {
122 struct
123 {
124 size_t nfree; /* Free fragments in a fragmented block. */
125 size_t first; /* First free fragment of the block. */
126 } frag;
127 /* Size (in blocks) of a large cluster. */
128 size_t size;
129 } info;
130 } busy;
131 /* Heap information for a free block (that may be the first of
132 a free cluster). */
133 struct
134 {
135 size_t size; /* Size (in blocks) of a free cluster. */
136 size_t next; /* Index of next free cluster. */
137 size_t prev; /* Index of previous free cluster. */
140
141/* List of blocks allocated with `mmemalign' (or `mvalloc'). */
142
144{
146 PTR aligned; /* The address that mmemaligned returned. */
147 PTR exact; /* The address that malloc returned. */
148};
149
150/* Doubly linked lists of free fragments. */
151
152struct mmlist
153{
154 struct mmlist *next;
155 struct mmlist *prev;
156};
157
158/* Statistics available to the user.
159 FIXME: By design, the internals of the malloc package are no longer
160 exported to the user via an include file, so access to this data needs
161 to be via some other mechanism, such as mmstat_<something> where the
162 return value is the <something> the user is interested in. */
163
165{
166 size_t bytes_total; /* Total size of the heap. */
167 size_t chunks_used; /* Chunks allocated by the user. */
168 size_t bytes_used; /* Byte total of user-allocated chunks. */
169 size_t chunks_free; /* Chunks in the free list. */
170 size_t bytes_free; /* Byte total of chunks in the free list. */
171};
172
173/* Internal structure that defines the format of the malloc-descriptor.
174 This gets written to the base address of the region that mmalloc is
175 managing, and thus also becomes the file header for the mapped file,
176 if such a file exists. */
177
178struct mdesc
179{
180 /* The "magic number" for an mmalloc file. */
181
183
184 /* The size in bytes of this structure, used as a sanity check when reusing
185 a previously created mapped file. */
186
187 unsigned int headersize;
188
189 /* The version number of the mmalloc package that created this file. */
190
191 unsigned char version;
192
193 /* Some flag bits to keep track of various internal things. */
194
195 unsigned int flags;
196
197 /* If a system call made by the mmalloc package fails, the errno is
198 preserved for future examination. */
199
201
202 /* Pointer to the function that is used to get more core, or return core
203 to the system, for requests using this malloc descriptor. For memory
204 mapped regions, this is the mmap() based routine. There may also be
205 a single malloc descriptor that points to an sbrk() based routine
206 for systems without mmap() or for applications that call the mmalloc()
207 package with a NULL malloc descriptor.
208
209 FIXME: For mapped regions shared by more than one process, this
210 needs to be maintained on a per-process basis. */
211
212 PTR (*morecore) PARAMS ((struct mdesc *, int));
213
214 /* Pointer to the function that causes an abort when the memory checking
215 features are activated. By default this is set to abort(), but can
216 be set to another function by the application using mmalloc().
217
218 FIXME: For mapped regions shared by more than one process, this
219 needs to be maintained on a per-process basis. */
220
221 void (*abortfunc) PARAMS ((void));
222
223 /* Debugging hook for free.
224
225 FIXME: For mapped regions shared by more than one process, this
226 needs to be maintained on a per-process basis. */
227
228 void (*mfree_hook) PARAMS ((PTR, PTR));
229
230 /* Debugging hook for `malloc'.
231
232 FIXME: For mapped regions shared by more than one process, this
233 needs to be maintained on a per-process basis. */
234
235 PTR (*mmalloc_hook) PARAMS ((PTR, size_t));
236
237 /* Debugging hook for realloc.
238
239 FIXME: For mapped regions shared by more than one process, this
240 needs to be maintained on a per-process basis. */
241
242 PTR (*mrealloc_hook) PARAMS ((PTR, PTR, size_t));
243
244 /* Number of info entries. */
245
246 size_t heapsize;
247
248 /* Pointer to first block of the heap (base of the first block). */
249
250 char *heapbase;
251
252 /* Current search index for the heap table. */
253 /* Search index in the info table. */
254
255 size_t heapindex;
256
257 /* Limit of valid info table indices. */
258
259 size_t heaplimit;
260
261 /* Block information table.
262 Allocated with malign/__mmalloc_free (not mmalloc/mfree). */
263 /* Table indexed by block number giving per-block information. */
264
266
267 /* Instrumentation. */
268
270
271 /* Free list headers for each fragment size. */
272 /* Free lists for each fragment size. */
273
275
276 /* List of blocks allocated by memalign. */
277
279
280 /* The base address of the memory region for this malloc heap. This
281 is the location where the bookkeeping data for mmap and for malloc
282 begins. */
283
284 char *base;
285
286 /* The current location in the memory region for this malloc heap which
287 represents the end of memory in use. */
288
289 char *breakval;
290
291 /* The end of the current memory region for this malloc heap. This is
292 the first location past the end of mapped memory. */
293
294 char *top;
295
296 /* Offset between base (as stored by the writer) and address where
297 mapped by the reader. */
298
299 long offset;
300
301 /* Open file descriptor for the file to which this malloc heap is mapped.
302 This will always be a valid file descriptor, since /dev/zero is used
303 by default if no open file is supplied by the client. Also note that
304 it may change each time the region is mapped and unmapped. */
305
306#ifndef WIN32
307 int fd;
308#else
309 HANDLE fd;
310#endif
311
312 /* An array of keys to data within the mapped region, for use by the
313 application. */
314
316
317};
318
319/* Bits to look at in the malloc descriptor flags word */
320
321#define MMALLOC_DEVZERO (1 << 0) /* Have mapped to /dev/zero */
322#define MMALLOC_INITIALIZED (1 << 1) /* Initialized mmalloc */
323#define MMALLOC_MMCHECK_USED (1 << 2) /* mmcheck() called already */
324
325/* Internal version of `mfree' used in `morecore'. */
326
327extern void __mmalloc_free PARAMS ((struct mdesc *, PTR));
328
329/* Hooks for debugging versions. */
330
331extern void (*__mfree_hook) PARAMS ((PTR, PTR));
332extern PTR (*__mmalloc_hook) PARAMS ((PTR, size_t));
333extern PTR (*__mrealloc_hook) PARAMS ((PTR, PTR, size_t));
334
335/* A default malloc descriptor for the single sbrk() managed region. */
336
337extern struct mdesc *__mmalloc_default_mdp;
338
339
340/* Grow or shrink a contiguous mapped region using mmap().
341 Works much like sbrk() */
342
343#if defined(R__HAVE_MMAP)
344
345extern PTR __mmalloc_mmap_morecore PARAMS ((struct mdesc *, int));
346
347#endif
348
349/* Remap a mmalloc region that was previously mapped. */
350
351extern PTR __mmalloc_remap_core PARAMS ((struct mdesc *));
352
353/* Macro to convert from a user supplied malloc descriptor to pointer to the
354 internal malloc descriptor. If the user supplied descriptor is NULL, then
355 use the default internal version, initializing it if necessary. Otherwise
356 just cast the user supplied version (which is void *) to the proper type
357 (struct mdesc *). */
358
359#ifndef NO_SBRK_MALLOC
360
361/* Initialize the first use of the default malloc descriptor, which uses
362 an sbrk() region. */
363
364extern struct mdesc *__mmalloc_sbrk_init PARAMS ((void));
365
366#define MD_TO_MDP(md) \
367((md) == NULL \
368? (__mmalloc_default_mdp == NULL \
369? __mmalloc_sbrk_init () \
370: __mmalloc_default_mdp) \
371: (struct mdesc *) (md))
372
373#else
374
375#define MD_TO_MDP(md) ((struct mdesc *) (md))
376
377#endif
378
379#endif /* __MMPRIVATE_H */
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define PARAMS(protos)
#define void
Definition Tailor.h:179
#define free
Definition civetweb.c:1578
void __mmalloc_free(struct mdesc *mdp, PTR ptr)
Definition mfree.c:34
static PTR morecore(struct mdesc *mdp, size_t size)
Definition mmalloc.c:81
#define PTR
Definition mmalloc.h:29
#define MMALLOC_MAGIC_SIZE
Definition mmprivate.h:69
#define BLOCKLOG
Definition mmprivate.h:80
struct mdesc * __mmalloc_default_mdp
Definition sbrksup.c:39
#define MMALLOC_KEYS
Definition mmprivate.h:71
struct mdesc * __mmalloc_sbrk_init()
Definition sbrksup.c:80
PTR aligned
Definition mmprivate.h:146
struct alignlist * next
Definition mmprivate.h:145
char magic[MMALLOC_MAGIC_SIZE]
Definition mmprivate.h:182
size_t heapsize
Definition mmprivate.h:246
int fd
Definition mmprivate.h:307
unsigned int flags
Definition mmprivate.h:195
size_t heaplimit
Definition mmprivate.h:259
mmalloc_info * heapinfo
Definition mmprivate.h:265
char * heapbase
Definition mmprivate.h:250
PTR mmalloc_hook PARAMS((PTR, size_t))
void mfree_hook PARAMS((PTR, PTR))
struct mmlist fraghead[BLOCKLOG]
Definition mmprivate.h:274
char * base
Definition mmprivate.h:284
PTR mrealloc_hook PARAMS((PTR, PTR, size_t))
struct mmstats_t heapstats
Definition mmprivate.h:269
PTR keys[MMALLOC_KEYS]
Definition mmprivate.h:315
char * top
Definition mmprivate.h:294
unsigned char version
Definition mmprivate.h:191
char * breakval
Definition mmprivate.h:289
long offset
Definition mmprivate.h:299
struct alignlist * aligned_blocks
Definition mmprivate.h:278
size_t heapindex
Definition mmprivate.h:255
PTR morecore PARAMS((struct mdesc *, int))
void abortfunc PARAMS((void))
int saved_errno
Definition mmprivate.h:200
unsigned int headersize
Definition mmprivate.h:187
struct mmlist * prev
Definition mmprivate.h:155
struct mmlist * next
Definition mmprivate.h:154
size_t bytes_used
Definition mmprivate.h:168
size_t bytes_free
Definition mmprivate.h:170
size_t chunks_free
Definition mmprivate.h:169
size_t chunks_used
Definition mmprivate.h:167
size_t bytes_total
Definition mmprivate.h:166
size_t nfree
Definition mmprivate.h:124
size_t prev
Definition mmprivate.h:137
size_t next
Definition mmprivate.h:136
size_t first
Definition mmprivate.h:125
size_t size
Definition mmprivate.h:128