OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [malloc-standard/] [malloc.h] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
  This is a version (aka dlmalloc) of malloc/free/realloc written by
3
  Doug Lea and released to the public domain.  Use, modify, and
4
  redistribute this code without permission or acknowledgement in any
5
  way you wish.  Send questions, comments, complaints, performance
6
  data, etc to dl@cs.oswego.edu
7
 
8
  VERSION 2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
9
 
10
  Note: There may be an updated version of this malloc obtainable at
11
           ftp://gee.cs.oswego.edu/pub/misc/malloc.c
12
  Check before installing!
13
 
14
  Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
15
*/
16
 
17
#include <features.h>
18
#include <stddef.h>
19
#include <unistd.h>
20
#include <errno.h>
21
#include <string.h>
22
#include <malloc.h>
23
 
24
 
25
#ifdef __UCLIBC_HAS_THREADS__
26
#include <pthread.h>
27
extern pthread_mutex_t __malloc_lock;
28
# define LOCK   __pthread_mutex_lock(&__malloc_lock)
29
# define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
30
#else
31
# define LOCK
32
# define UNLOCK
33
#endif
34
 
35
 
36
 
37
/*
38
  MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
39
  It must be a power of two at least 2 * (sizeof(size_t)), even on machines
40
  for which smaller alignments would suffice. It may be defined as
41
  larger than this though. Note however that code and data structures
42
  are optimized for the case of 8-byte alignment.
43
*/
44
#ifndef MALLOC_ALIGNMENT
45
#define MALLOC_ALIGNMENT       (2 * (sizeof(size_t)))
46
#endif
47
 
48
/* The corresponding bit mask value */
49
#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
50
 
51
/*
52
  TRIM_FASTBINS controls whether free() of a very small chunk can
53
  immediately lead to trimming. Setting to true (1) can reduce memory
54
  footprint, but will almost always slow down programs that use a lot
55
  of small chunks.
56
 
57
  Define this only if you are willing to give up some speed to more
58
  aggressively reduce system-level memory footprint when releasing
59
  memory in programs that use many small chunks.  You can get
60
  essentially the same effect by setting MXFAST to 0, but this can
61
  lead to even greater slowdowns in programs using many small chunks.
62
  TRIM_FASTBINS is an in-between compile-time option, that disables
63
  only those chunks bordering topmost memory from being placed in
64
  fastbins.
65
*/
66
#ifndef TRIM_FASTBINS
67
#define TRIM_FASTBINS  0
68
#endif
69
 
70
 
71
/*
72
  MORECORE-related declarations. By default, rely on sbrk
73
*/
74
 
75
 
76
/*
77
  MORECORE is the name of the routine to call to obtain more memory
78
  from the system.  See below for general guidance on writing
79
  alternative MORECORE functions, as well as a version for WIN32 and a
80
  sample version for pre-OSX macos.
81
*/
82
#ifndef MORECORE
83
#define MORECORE sbrk
84
#endif
85
 
86
/*
87
  MORECORE_FAILURE is the value returned upon failure of MORECORE
88
  as well as mmap. Since it cannot be an otherwise valid memory address,
89
  and must reflect values of standard sys calls, you probably ought not
90
  try to redefine it.
91
*/
92
#ifndef MORECORE_FAILURE
93
#define MORECORE_FAILURE (-1)
94
#endif
95
 
96
/*
97
  If MORECORE_CONTIGUOUS is true, take advantage of fact that
98
  consecutive calls to MORECORE with positive arguments always return
99
  contiguous increasing addresses.  This is true of unix sbrk.  Even
100
  if not defined, when regions happen to be contiguous, malloc will
101
  permit allocations spanning regions obtained from different
102
  calls. But defining this when applicable enables some stronger
103
  consistency checks and space efficiencies.
104
*/
105
#ifndef MORECORE_CONTIGUOUS
106
#define MORECORE_CONTIGUOUS 1
107
#endif
108
 
109
/*
110
   MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
111
   sbrk fails, and mmap is used as a backup (which is done only if
112
   HAVE_MMAP).  The value must be a multiple of page size.  This
113
   backup strategy generally applies only when systems have "holes" in
114
   address space, so sbrk cannot perform contiguous expansion, but
115
   there is still space available on system.  On systems for which
116
   this is known to be useful (i.e. most linux kernels), this occurs
117
   only when programs allocate huge amounts of memory.  Between this,
118
   and the fact that mmap regions tend to be limited, the size should
119
   be large, to avoid too many mmap calls and thus avoid running out
120
   of kernel resources.
121
*/
122
#ifndef MMAP_AS_MORECORE_SIZE
123
#define MMAP_AS_MORECORE_SIZE (1024 * 1024)
124
#endif
125
 
126
/*
127
  The system page size. To the extent possible, this malloc manages
128
  memory from the system in page-size units.  Note that this value is
129
  cached during initialization into a field of malloc_state. So even
130
  if malloc_getpagesize is a function, it is only called once.
131
 
132
  The following mechanics for getpagesize were adapted from bsd/gnu
133
  getpagesize.h. If none of the system-probes here apply, a value of
134
  4096 is used, which should be OK: If they don't apply, then using
135
  the actual value probably doesn't impact performance.
136
*/
137
#ifndef malloc_getpagesize
138
#  include <unistd.h>
139
#  define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
140
#else /* just guess */
141
#  define malloc_getpagesize (4096)
142
#endif
143
 
144
 
145
/* mallopt tuning options */
146
 
147
/*
148
  M_MXFAST is the maximum request size used for "fastbins", special bins
149
  that hold returned chunks without consolidating their spaces. This
150
  enables future requests for chunks of the same size to be handled
151
  very quickly, but can increase fragmentation, and thus increase the
152
  overall memory footprint of a program.
153
 
154
  This malloc manages fastbins very conservatively yet still
155
  efficiently, so fragmentation is rarely a problem for values less
156
  than or equal to the default.  The maximum supported value of MXFAST
157
  is 80. You wouldn't want it any higher than this anyway.  Fastbins
158
  are designed especially for use with many small structs, objects or
159
  strings -- the default handles structs/objects/arrays with sizes up
160
  to 16 4byte fields, or small strings representing words, tokens,
161
  etc. Using fastbins for larger objects normally worsens
162
  fragmentation without improving speed.
163
 
164
  M_MXFAST is set in REQUEST size units. It is internally used in
165
  chunksize units, which adds padding and alignment.  You can reduce
166
  M_MXFAST to 0 to disable all use of fastbins.  This causes the malloc
167
  algorithm to be a closer approximation of fifo-best-fit in all cases,
168
  not just for larger requests, but will generally cause it to be
169
  slower.
170
*/
171
 
172
 
173
/* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
174
#ifndef M_MXFAST
175
#define M_MXFAST            1
176
#endif
177
 
178
#ifndef DEFAULT_MXFAST
179
#define DEFAULT_MXFAST     64
180
#endif
181
 
182
 
183
/*
184
  M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
185
  to keep before releasing via malloc_trim in free().
186
 
187
  Automatic trimming is mainly useful in long-lived programs.
188
  Because trimming via sbrk can be slow on some systems, and can
189
  sometimes be wasteful (in cases where programs immediately
190
  afterward allocate more large chunks) the value should be high
191
  enough so that your overall system performance would improve by
192
  releasing this much memory.
193
 
194
  The trim threshold and the mmap control parameters (see below)
195
  can be traded off with one another. Trimming and mmapping are
196
  two different ways of releasing unused memory back to the
197
  system. Between these two, it is often possible to keep
198
  system-level demands of a long-lived program down to a bare
199
  minimum. For example, in one test suite of sessions measuring
200
  the XF86 X server on Linux, using a trim threshold of 128K and a
201
  mmap threshold of 192K led to near-minimal long term resource
202
  consumption.
203
 
204
  If you are using this malloc in a long-lived program, it should
205
  pay to experiment with these values.  As a rough guide, you
206
  might set to a value close to the average size of a process
207
  (program) running on your system.  Releasing this much memory
208
  would allow such a process to run in memory.  Generally, it's
209
  worth it to tune for trimming rather tham memory mapping when a
210
  program undergoes phases where several large chunks are
211
  allocated and released in ways that can reuse each other's
212
  storage, perhaps mixed with phases where there are no such
213
  chunks at all.  And in well-behaved long-lived programs,
214
  controlling release of large blocks via trimming versus mapping
215
  is usually faster.
216
 
217
  However, in most programs, these parameters serve mainly as
218
  protection against the system-level effects of carrying around
219
  massive amounts of unneeded memory. Since frequent calls to
220
  sbrk, mmap, and munmap otherwise degrade performance, the default
221
  parameters are set to relatively high values that serve only as
222
  safeguards.
223
 
224
  The trim value must be greater than page size to have any useful
225
  effect.  To disable trimming completely, you can set to
226
  (unsigned long)(-1)
227
 
228
  Trim settings interact with fastbin (MXFAST) settings: Unless
229
  TRIM_FASTBINS is defined, automatic trimming never takes place upon
230
  freeing a chunk with size less than or equal to MXFAST. Trimming is
231
  instead delayed until subsequent freeing of larger chunks. However,
232
  you can still force an attempted trim by calling malloc_trim.
233
 
234
  Also, trimming is not generally possible in cases where
235
  the main arena is obtained via mmap.
236
 
237
  Note that the trick some people use of mallocing a huge space and
238
  then freeing it at program startup, in an attempt to reserve system
239
  memory, doesn't have the intended effect under automatic trimming,
240
  since that memory will immediately be returned to the system.
241
*/
242
#define M_TRIM_THRESHOLD       -1
243
 
244
#ifndef DEFAULT_TRIM_THRESHOLD
245
#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
246
#endif
247
 
248
/*
249
  M_TOP_PAD is the amount of extra `padding' space to allocate or
250
  retain whenever sbrk is called. It is used in two ways internally:
251
 
252
  * When sbrk is called to extend the top of the arena to satisfy
253
  a new malloc request, this much padding is added to the sbrk
254
  request.
255
 
256
  * When malloc_trim is called automatically from free(),
257
  it is used as the `pad' argument.
258
 
259
  In both cases, the actual amount of padding is rounded
260
  so that the end of the arena is always a system page boundary.
261
 
262
  The main reason for using padding is to avoid calling sbrk so
263
  often. Having even a small pad greatly reduces the likelihood
264
  that nearly every malloc request during program start-up (or
265
  after trimming) will invoke sbrk, which needlessly wastes
266
  time.
267
 
268
  Automatic rounding-up to page-size units is normally sufficient
269
  to avoid measurable overhead, so the default is 0.  However, in
270
  systems where sbrk is relatively slow, it can pay to increase
271
  this value, at the expense of carrying around more memory than
272
  the program needs.
273
*/
274
#define M_TOP_PAD              -2
275
 
276
#ifndef DEFAULT_TOP_PAD
277
#define DEFAULT_TOP_PAD        (0)
278
#endif
279
 
280
/*
281
  M_MMAP_THRESHOLD is the request size threshold for using mmap()
282
  to service a request. Requests of at least this size that cannot
283
  be allocated using already-existing space will be serviced via mmap.
284
  (If enough normal freed space already exists it is used instead.)
285
 
286
  Using mmap segregates relatively large chunks of memory so that
287
  they can be individually obtained and released from the host
288
  system. A request serviced through mmap is never reused by any
289
  other request (at least not directly; the system may just so
290
  happen to remap successive requests to the same locations).
291
 
292
  Segregating space in this way has the benefits that:
293
 
294
   1. Mmapped space can ALWAYS be individually released back
295
      to the system, which helps keep the system level memory
296
      demands of a long-lived program low.
297
   2. Mapped memory can never become `locked' between
298
      other chunks, as can happen with normally allocated chunks, which
299
      means that even trimming via malloc_trim would not release them.
300
   3. On some systems with "holes" in address spaces, mmap can obtain
301
      memory that sbrk cannot.
302
 
303
  However, it has the disadvantages that:
304
 
305
   1. The space cannot be reclaimed, consolidated, and then
306
      used to service later requests, as happens with normal chunks.
307
   2. It can lead to more wastage because of mmap page alignment
308
      requirements
309
   3. It causes malloc performance to be more dependent on host
310
      system memory management support routines which may vary in
311
      implementation quality and may impose arbitrary
312
      limitations. Generally, servicing a request via normal
313
      malloc steps is faster than going through a system's mmap.
314
 
315
  The advantages of mmap nearly always outweigh disadvantages for
316
  "large" chunks, but the value of "large" varies across systems.  The
317
  default is an empirically derived value that works well in most
318
  systems.
319
*/
320
#define M_MMAP_THRESHOLD      -3
321
 
322
#ifndef DEFAULT_MMAP_THRESHOLD
323
#define DEFAULT_MMAP_THRESHOLD (256 * 1024)
324
#endif
325
 
326
/*
327
  M_MMAP_MAX is the maximum number of requests to simultaneously
328
  service using mmap. This parameter exists because
329
. Some systems have a limited number of internal tables for
330
  use by mmap, and using more than a few of them may degrade
331
  performance.
332
 
333
  The default is set to a value that serves only as a safeguard.
334
  Setting to 0 disables use of mmap for servicing large requests.  If
335
  HAVE_MMAP is not set, the default value is 0, and attempts to set it
336
  to non-zero values in mallopt will fail.
337
*/
338
#define M_MMAP_MAX             -4
339
 
340
#ifndef DEFAULT_MMAP_MAX
341
#define DEFAULT_MMAP_MAX       (65536)
342
#endif
343
 
344
 
345
/* ------------------ MMAP support ------------------  */
346
#include <fcntl.h>
347
#include <sys/mman.h>
348
 
349
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
350
#define MAP_ANONYMOUS MAP_ANON
351
#endif
352
 
353
#define MMAP(addr, size, prot, flags) \
354
 (mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS, -1, 0))
355
 
356
 
357
/* -----------------------  Chunk representations ----------------------- */
358
 
359
 
360
/*
361
  This struct declaration is misleading (but accurate and necessary).
362
  It declares a "view" into memory allowing access to necessary
363
  fields at known offsets from a given base. See explanation below.
364
*/
365
 
366
struct malloc_chunk {
367
 
368
  size_t      prev_size;  /* Size of previous chunk (if free).  */
369
  size_t      size;       /* Size in bytes, including overhead. */
370
 
371
  struct malloc_chunk* fd;         /* double links -- used only if free. */
372
  struct malloc_chunk* bk;
373
};
374
 
375
 
376
typedef struct malloc_chunk* mchunkptr;
377
 
378
/*
379
   malloc_chunk details:
380
 
381
    (The following includes lightly edited explanations by Colin Plumb.)
382
 
383
    Chunks of memory are maintained using a `boundary tag' method as
384
    described in e.g., Knuth or Standish.  (See the paper by Paul
385
    Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
386
    survey of such techniques.)  Sizes of free chunks are stored both
387
    in the front of each chunk and at the end.  This makes
388
    consolidating fragmented chunks into bigger chunks very fast.  The
389
    size fields also hold bits representing whether chunks are free or
390
    in use.
391
 
392
    An allocated chunk looks like this:
393
 
394
 
395
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
396
            |             Size of previous chunk, if allocated            | |
397
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398
            |             Size of chunk, in bytes                         |P|
399
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400
            |             User data starts here...                          .
401
            .                                                               .
402
            .             (malloc_usable_space() bytes)                     .
403
            .                                                               |
404
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405
            |             Size of chunk                                     |
406
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407
 
408
 
409
    Where "chunk" is the front of the chunk for the purpose of most of
410
    the malloc code, but "mem" is the pointer that is returned to the
411
    user.  "Nextchunk" is the beginning of the next contiguous chunk.
412
 
413
    Chunks always begin on even word boundries, so the mem portion
414
    (which is returned to the user) is also on an even word boundary, and
415
    thus at least double-word aligned.
416
 
417
    Free chunks are stored in circular doubly-linked lists, and look like this:
418
 
419
    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
420
            |             Size of previous chunk                            |
421
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
422
    `head:' |             Size of chunk, in bytes                         |P|
423
      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424
            |             Forward pointer to next chunk in list             |
425
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
426
            |             Back pointer to previous chunk in list            |
427
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428
            |             Unused space (may be 0 bytes long)                .
429
            .                                                               .
430
            .                                                               |
431
nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
432
    `foot:' |             Size of chunk, in bytes                           |
433
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
434
 
435
    The P (PREV_INUSE) bit, stored in the unused low-order bit of the
436
    chunk size (which is always a multiple of two words), is an in-use
437
    bit for the *previous* chunk.  If that bit is *clear*, then the
438
    word before the current chunk size contains the previous chunk
439
    size, and can be used to find the front of the previous chunk.
440
    The very first chunk allocated always has this bit set,
441
    preventing access to non-existent (or non-owned) memory. If
442
    prev_inuse is set for any given chunk, then you CANNOT determine
443
    the size of the previous chunk, and might even get a memory
444
    addressing fault when trying to do so.
445
 
446
    Note that the `foot' of the current chunk is actually represented
447
    as the prev_size of the NEXT chunk. This makes it easier to
448
    deal with alignments etc but can be very confusing when trying
449
    to extend or adapt this code.
450
 
451
    The two exceptions to all this are
452
 
453
     1. The special chunk `top' doesn't bother using the
454
        trailing size field since there is no next contiguous chunk
455
        that would have to index off it. After initialization, `top'
456
        is forced to always exist.  If it would become less than
457
        MINSIZE bytes long, it is replenished.
458
 
459
     2. Chunks allocated via mmap, which have the second-lowest-order
460
        bit (IS_MMAPPED) set in their size fields.  Because they are
461
        allocated one-by-one, each must contain its own trailing size field.
462
 
463
*/
464
 
465
/*
466
  ---------- Size and alignment checks and conversions ----------
467
*/
468
 
469
/* conversion from malloc headers to user pointers, and back */
470
 
471
#define chunk2mem(p)   ((void*)((char*)(p) + 2*(sizeof(size_t))))
472
#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*(sizeof(size_t))))
473
 
474
/* The smallest possible chunk */
475
#define MIN_CHUNK_SIZE        (sizeof(struct malloc_chunk))
476
 
477
/* The smallest size we can malloc is an aligned minimal chunk */
478
 
479
#define MINSIZE  \
480
  (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
481
 
482
/* Check if m has acceptable alignment */
483
 
484
#define aligned_OK(m)  (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0)
485
 
486
 
487
/* Check if a request is so large that it would wrap around zero when
488
   padded and aligned. To simplify some other code, the bound is made
489
   low enough so that adding MINSIZE will also not wrap around sero.
490
*/
491
 
492
#define REQUEST_OUT_OF_RANGE(req)                                 \
493
  ((unsigned long)(req) >=                                        \
494
   (unsigned long)(size_t)(-2 * MINSIZE))
495
 
496
/* pad request bytes into a usable size -- internal version */
497
 
498
#define request2size(req)                                         \
499
  (((req) + (sizeof(size_t)) + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
500
   MINSIZE :                                                      \
501
   ((req) + (sizeof(size_t)) + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
502
 
503
/*  Same, except also perform argument check */
504
 
505
#define checked_request2size(req, sz)                             \
506
  if (REQUEST_OUT_OF_RANGE(req)) {                                \
507
    errno = ENOMEM;                                               \
508
    return 0;                                                     \
509
  }                                                               \
510
  (sz) = request2size(req);
511
 
512
/*
513
  --------------- Physical chunk operations ---------------
514
*/
515
 
516
 
517
/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
518
#define PREV_INUSE 0x1
519
 
520
/* extract inuse bit of previous chunk */
521
#define prev_inuse(p)       ((p)->size & PREV_INUSE)
522
 
523
 
524
/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
525
#define IS_MMAPPED 0x2
526
 
527
/* check for mmap()'ed chunk */
528
#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
529
 
530
/* Bits to mask off when extracting size
531
 
532
  Note: IS_MMAPPED is intentionally not masked off from size field in
533
  macros for which mmapped chunks should never be seen. This should
534
  cause helpful core dumps to occur if it is tried by accident by
535
  people extending or adapting this malloc.
536
*/
537
#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
538
 
539
/* Get size, ignoring use bits */
540
#define chunksize(p)         ((p)->size & ~(SIZE_BITS))
541
 
542
 
543
/* Ptr to next physical malloc_chunk. */
544
#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
545
 
546
/* Ptr to previous physical malloc_chunk */
547
#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
548
 
549
/* Treat space at ptr + offset as a chunk */
550
#define chunk_at_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))
551
 
552
/* extract p's inuse bit */
553
#define inuse(p)\
554
((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
555
 
556
/* set/clear chunk as being inuse without otherwise disturbing */
557
#define set_inuse(p)\
558
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
559
 
560
#define clear_inuse(p)\
561
((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
562
 
563
 
564
/* check/set/clear inuse bits in known places */
565
#define inuse_bit_at_offset(p, s)\
566
 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
567
 
568
#define set_inuse_bit_at_offset(p, s)\
569
 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
570
 
571
#define clear_inuse_bit_at_offset(p, s)\
572
 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
573
 
574
 
575
/* Set size at head, without disturbing its use bit */
576
#define set_head_size(p, s)  ((p)->size = (((p)->size & PREV_INUSE) | (s)))
577
 
578
/* Set size/use field */
579
#define set_head(p, s)       ((p)->size = (s))
580
 
581
/* Set size at footer (only when chunk is not in use) */
582
#define set_foot(p, s)       (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
583
 
584
 
585
/* -------------------- Internal data structures -------------------- */
586
 
587
/*
588
  Bins
589
 
590
    An array of bin headers for free chunks. Each bin is doubly
591
    linked.  The bins are approximately proportionally (log) spaced.
592
    There are a lot of these bins (128). This may look excessive, but
593
    works very well in practice.  Most bins hold sizes that are
594
    unusual as malloc request sizes, but are more usual for fragments
595
    and consolidated sets of chunks, which is what these bins hold, so
596
    they can be found quickly.  All procedures maintain the invariant
597
    that no consolidated chunk physically borders another one, so each
598
    chunk in a list is known to be preceeded and followed by either
599
    inuse chunks or the ends of memory.
600
 
601
    Chunks in bins are kept in size order, with ties going to the
602
    approximately least recently used chunk. Ordering isn't needed
603
    for the small bins, which all contain the same-sized chunks, but
604
    facilitates best-fit allocation for larger chunks. These lists
605
    are just sequential. Keeping them in order almost never requires
606
    enough traversal to warrant using fancier ordered data
607
    structures.
608
 
609
    Chunks of the same size are linked with the most
610
    recently freed at the front, and allocations are taken from the
611
    back.  This results in LRU (FIFO) allocation order, which tends
612
    to give each chunk an equal opportunity to be consolidated with
613
    adjacent freed chunks, resulting in larger free chunks and less
614
    fragmentation.
615
 
616
    To simplify use in double-linked lists, each bin header acts
617
    as a malloc_chunk. This avoids special-casing for headers.
618
    But to conserve space and improve locality, we allocate
619
    only the fd/bk pointers of bins, and then use repositioning tricks
620
    to treat these as the fields of a malloc_chunk*.
621
*/
622
 
623
typedef struct malloc_chunk* mbinptr;
624
 
625
/* addressing -- note that bin_at(0) does not exist */
626
#define bin_at(m, i) ((mbinptr)((char*)&((m)->bins[(i)<<1]) - ((sizeof(size_t))<<1)))
627
 
628
/* analog of ++bin */
629
#define next_bin(b)  ((mbinptr)((char*)(b) + (sizeof(mchunkptr)<<1)))
630
 
631
/* Reminders about list directionality within bins */
632
#define first(b)     ((b)->fd)
633
#define last(b)      ((b)->bk)
634
 
635
/* Take a chunk off a bin list */
636
#define unlink(P, BK, FD) {                                            \
637
  FD = P->fd;                                                          \
638
  BK = P->bk;                                                          \
639
  FD->bk = BK;                                                         \
640
  BK->fd = FD;                                                         \
641
}
642
 
643
/*
644
  Indexing
645
 
646
    Bins for sizes < 512 bytes contain chunks of all the same size, spaced
647
    8 bytes apart. Larger bins are approximately logarithmically spaced:
648
 
649
    64 bins of size       8
650
    32 bins of size      64
651
    16 bins of size     512
652
     8 bins of size    4096
653
     4 bins of size   32768
654
     2 bins of size  262144
655
     1 bin  of size what's left
656
 
657
    The bins top out around 1MB because we expect to service large
658
    requests via mmap.
659
*/
660
 
661
#define NBINS              96
662
#define NSMALLBINS         32
663
#define SMALLBIN_WIDTH      8
664
#define MIN_LARGE_SIZE    256
665
 
666
#define in_smallbin_range(sz)  \
667
  ((unsigned long)(sz) < (unsigned long)MIN_LARGE_SIZE)
668
 
669
#define smallbin_index(sz)     (((unsigned)(sz)) >> 3)
670
 
671
#define bin_index(sz) \
672
 ((in_smallbin_range(sz)) ? smallbin_index(sz) : __malloc_largebin_index(sz))
673
 
674
/*
675
  FIRST_SORTED_BIN_SIZE is the chunk size corresponding to the
676
  first bin that is maintained in sorted order. This must
677
  be the smallest size corresponding to a given bin.
678
 
679
  Normally, this should be MIN_LARGE_SIZE. But you can weaken
680
  best fit guarantees to sometimes speed up malloc by increasing value.
681
  Doing this means that malloc may choose a chunk that is
682
  non-best-fitting by up to the width of the bin.
683
 
684
  Some useful cutoff values:
685
      512 - all bins sorted
686
     2560 - leaves bins <=     64 bytes wide unsorted
687
    12288 - leaves bins <=    512 bytes wide unsorted
688
    65536 - leaves bins <=   4096 bytes wide unsorted
689
   262144 - leaves bins <=  32768 bytes wide unsorted
690
       -1 - no bins sorted (not recommended!)
691
*/
692
 
693
#define FIRST_SORTED_BIN_SIZE MIN_LARGE_SIZE
694
/* #define FIRST_SORTED_BIN_SIZE 65536 */
695
 
696
/*
697
  Unsorted chunks
698
 
699
    All remainders from chunk splits, as well as all returned chunks,
700
    are first placed in the "unsorted" bin. They are then placed
701
    in regular bins after malloc gives them ONE chance to be used before
702
    binning. So, basically, the unsorted_chunks list acts as a queue,
703
    with chunks being placed on it in free (and __malloc_consolidate),
704
    and taken off (to be either used or placed in bins) in malloc.
705
*/
706
 
707
/* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
708
#define unsorted_chunks(M)          (bin_at(M, 1))
709
 
710
/*
711
  Top
712
 
713
    The top-most available chunk (i.e., the one bordering the end of
714
    available memory) is treated specially. It is never included in
715
    any bin, is used only if no other chunk is available, and is
716
    released back to the system if it is very large (see
717
    M_TRIM_THRESHOLD).  Because top initially
718
    points to its own bin with initial zero size, thus forcing
719
    extension on the first malloc request, we avoid having any special
720
    code in malloc to check whether it even exists yet. But we still
721
    need to do so when getting memory from system, so we make
722
    initial_top treat the bin as a legal but unusable chunk during the
723
    interval between initialization and the first call to
724
    __malloc_alloc. (This is somewhat delicate, since it relies on
725
    the 2 preceding words to be zero during this interval as well.)
726
*/
727
 
728
/* Conveniently, the unsorted bin can be used as dummy top on first call */
729
#define initial_top(M)              (unsorted_chunks(M))
730
 
731
/*
732
  Binmap
733
 
734
    To help compensate for the large number of bins, a one-level index
735
    structure is used for bin-by-bin searching.  `binmap' is a
736
    bitvector recording whether bins are definitely empty so they can
737
    be skipped over during during traversals.  The bits are NOT always
738
    cleared as soon as bins are empty, but instead only
739
    when they are noticed to be empty during traversal in malloc.
740
*/
741
 
742
/* Conservatively use 32 bits per map word, even if on 64bit system */
743
#define BINMAPSHIFT      5
744
#define BITSPERMAP       (1U << BINMAPSHIFT)
745
#define BINMAPSIZE       (NBINS / BITSPERMAP)
746
 
747
#define idx2block(i)     ((i) >> BINMAPSHIFT)
748
#define idx2bit(i)       ((1U << ((i) & ((1U << BINMAPSHIFT)-1))))
749
 
750
#define mark_bin(m,i)    ((m)->binmap[idx2block(i)] |=  idx2bit(i))
751
#define unmark_bin(m,i)  ((m)->binmap[idx2block(i)] &= ~(idx2bit(i)))
752
#define get_binmap(m,i)  ((m)->binmap[idx2block(i)] &   idx2bit(i))
753
 
754
/*
755
  Fastbins
756
 
757
    An array of lists holding recently freed small chunks.  Fastbins
758
    are not doubly linked.  It is faster to single-link them, and
759
    since chunks are never removed from the middles of these lists,
760
    double linking is not necessary. Also, unlike regular bins, they
761
    are not even processed in FIFO order (they use faster LIFO) since
762
    ordering doesn't much matter in the transient contexts in which
763
    fastbins are normally used.
764
 
765
    Chunks in fastbins keep their inuse bit set, so they cannot
766
    be consolidated with other free chunks. __malloc_consolidate
767
    releases all chunks in fastbins and consolidates them with
768
    other free chunks.
769
*/
770
 
771
typedef struct malloc_chunk* mfastbinptr;
772
 
773
/* offset 2 to use otherwise unindexable first 2 bins */
774
#define fastbin_index(sz)        ((((unsigned int)(sz)) >> 3) - 2)
775
 
776
/* The maximum fastbin request size we support */
777
#define MAX_FAST_SIZE     80
778
 
779
#define NFASTBINS  (fastbin_index(request2size(MAX_FAST_SIZE))+1)
780
 
781
/*
782
  FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
783
  that triggers automatic consolidation of possibly-surrounding
784
  fastbin chunks. This is a heuristic, so the exact value should not
785
  matter too much. It is defined at half the default trim threshold as a
786
  compromise heuristic to only attempt consolidation if it is likely
787
  to lead to trimming. However, it is not dynamically tunable, since
788
  consolidation reduces fragmentation surrounding loarge chunks even
789
  if trimming is not used.
790
*/
791
 
792
#define FASTBIN_CONSOLIDATION_THRESHOLD  \
793
  ((unsigned long)(DEFAULT_TRIM_THRESHOLD) >> 1)
794
 
795
/*
796
  Since the lowest 2 bits in max_fast don't matter in size comparisons,
797
  they are used as flags.
798
*/
799
 
800
/*
801
  ANYCHUNKS_BIT held in max_fast indicates that there may be any
802
  freed chunks at all. It is set true when entering a chunk into any
803
  bin.
804
*/
805
 
806
#define ANYCHUNKS_BIT        (1U)
807
 
808
#define have_anychunks(M)     (((M)->max_fast &  ANYCHUNKS_BIT))
809
#define set_anychunks(M)      ((M)->max_fast |=  ANYCHUNKS_BIT)
810
#define clear_anychunks(M)    ((M)->max_fast &= ~ANYCHUNKS_BIT)
811
 
812
/*
813
  FASTCHUNKS_BIT held in max_fast indicates that there are probably
814
  some fastbin chunks. It is set true on entering a chunk into any
815
  fastbin, and cleared only in __malloc_consolidate.
816
*/
817
 
818
#define FASTCHUNKS_BIT        (2U)
819
 
820
#define have_fastchunks(M)   (((M)->max_fast &  FASTCHUNKS_BIT))
821
#define set_fastchunks(M)    ((M)->max_fast |=  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
822
#define clear_fastchunks(M)  ((M)->max_fast &= ~(FASTCHUNKS_BIT))
823
 
824
/* Set value of max_fast.  Use impossibly small value if 0.  */
825
#define set_max_fast(M, s) \
826
  (M)->max_fast = (((s) == 0)? SMALLBIN_WIDTH: request2size(s)) | \
827
  ((M)->max_fast &  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
828
 
829
#define get_max_fast(M) \
830
  ((M)->max_fast & ~(FASTCHUNKS_BIT | ANYCHUNKS_BIT))
831
 
832
 
833
/*
834
  morecore_properties is a status word holding dynamically discovered
835
  or controlled properties of the morecore function
836
*/
837
 
838
#define MORECORE_CONTIGUOUS_BIT  (1U)
839
 
840
#define contiguous(M) \
841
        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT))
842
#define noncontiguous(M) \
843
        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT) == 0)
844
#define set_contiguous(M) \
845
        ((M)->morecore_properties |=  MORECORE_CONTIGUOUS_BIT)
846
#define set_noncontiguous(M) \
847
        ((M)->morecore_properties &= ~MORECORE_CONTIGUOUS_BIT)
848
 
849
 
850
/*
851
   ----------- Internal state representation and initialization -----------
852
*/
853
 
854
struct malloc_state {
855
 
856
  /* The maximum chunk size to be eligible for fastbin */
857
  size_t  max_fast;   /* low 2 bits used as flags */
858
 
859
  /* Fastbins */
860
  mfastbinptr      fastbins[NFASTBINS];
861
 
862
  /* Base of the topmost chunk -- not otherwise kept in a bin */
863
  mchunkptr        top;
864
 
865
  /* The remainder from the most recent split of a small request */
866
  mchunkptr        last_remainder;
867
 
868
  /* Normal bins packed as described above */
869
  mchunkptr        bins[NBINS * 2];
870
 
871
  /* Bitmap of bins. Trailing zero map handles cases of largest binned size */
872
  unsigned int     binmap[BINMAPSIZE+1];
873
 
874
  /* Tunable parameters */
875
  unsigned long     trim_threshold;
876
  size_t  top_pad;
877
  size_t  mmap_threshold;
878
 
879
  /* Memory map support */
880
  int              n_mmaps;
881
  int              n_mmaps_max;
882
  int              max_n_mmaps;
883
 
884
  /* Cache malloc_getpagesize */
885
  unsigned int     pagesize;
886
 
887
  /* Track properties of MORECORE */
888
  unsigned int     morecore_properties;
889
 
890
  /* Statistics */
891
  size_t  mmapped_mem;
892
  size_t  sbrked_mem;
893
  size_t  max_sbrked_mem;
894
  size_t  max_mmapped_mem;
895
  size_t  max_total_mem;
896
};
897
 
898
typedef struct malloc_state *mstate;
899
 
900
/*
901
   There is exactly one instance of this struct in this malloc.
902
   If you are adapting this malloc in a way that does NOT use a static
903
   malloc_state, you MUST explicitly zero-fill it before using. This
904
   malloc relies on the property that malloc_state is initialized to
905
   all zeroes (as is true of C statics).
906
*/
907
extern struct malloc_state __malloc_state;  /* never directly referenced */
908
 
909
/*
910
   All uses of av_ are via get_malloc_state().
911
   At most one "call" to get_malloc_state is made per invocation of
912
   the public versions of malloc and free, but other routines
913
   that in turn invoke malloc and/or free may call more then once.
914
   Also, it is called in check* routines if __MALLOC_DEBUGGING is set.
915
*/
916
 
917
#define get_malloc_state() (&(__malloc_state))
918
 
919
/* External internal utilities operating on mstates */
920
void   __malloc_consolidate(mstate);
921
 
922
 
923
/* Debugging support */
924
#if ! __MALLOC_DEBUGGING
925
 
926
#define check_chunk(P)
927
#define check_free_chunk(P)
928
#define check_inuse_chunk(P)
929
#define check_remalloced_chunk(P,N)
930
#define check_malloced_chunk(P,N)
931
#define check_malloc_state()
932
#define assert(x) ((void)0)
933
 
934
 
935
#else
936
 
937
#define check_chunk(P)              __do_check_chunk(P)
938
#define check_free_chunk(P)         __do_check_free_chunk(P)
939
#define check_inuse_chunk(P)        __do_check_inuse_chunk(P)
940
#define check_remalloced_chunk(P,N) __do_check_remalloced_chunk(P,N)
941
#define check_malloced_chunk(P,N)   __do_check_malloced_chunk(P,N)
942
#define check_malloc_state()        __do_check_malloc_state()
943
 
944
extern void __do_check_chunk(mchunkptr p);
945
extern void __do_check_free_chunk(mchunkptr p);
946
extern void __do_check_inuse_chunk(mchunkptr p);
947
extern void __do_check_remalloced_chunk(mchunkptr p, size_t s);
948
extern void __do_check_malloced_chunk(mchunkptr p, size_t s);
949
extern void __do_check_malloc_state(void);
950
 
951
#include <assert.h>
952
 
953
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.