kernel/eka/include/dla.h
author hgs
Mon, 12 Jul 2010 14:24:01 +0100
changeset 202 9aca3be14c27
parent 189 a5496987b1da
permissions -rw-r--r--
201027_08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
189
a5496987b1da 201025_04
hgs
parents:
diff changeset
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
a5496987b1da 201025_04
hgs
parents:
diff changeset
     2
// All rights reserved.
a5496987b1da 201025_04
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
a5496987b1da 201025_04
hgs
parents:
diff changeset
     4
// under the terms of the License "Eclipse Public License v1.0"
a5496987b1da 201025_04
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
a5496987b1da 201025_04
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
a5496987b1da 201025_04
hgs
parents:
diff changeset
     7
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
     8
// Initial Contributors:
a5496987b1da 201025_04
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
a5496987b1da 201025_04
hgs
parents:
diff changeset
    10
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    11
// Contributors:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    12
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    13
// Description:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    14
// kernel\eka\include\dla.h
a5496987b1da 201025_04
hgs
parents:
diff changeset
    15
// 
a5496987b1da 201025_04
hgs
parents:
diff changeset
    16
// Uses malloc (aka dlmalloc) written by Doug Lea version 2.8.4
a5496987b1da 201025_04
hgs
parents:
diff changeset
    17
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    18
a5496987b1da 201025_04
hgs
parents:
diff changeset
    19
#ifndef __DLA__
a5496987b1da 201025_04
hgs
parents:
diff changeset
    20
#define __DLA__
a5496987b1da 201025_04
hgs
parents:
diff changeset
    21
a5496987b1da 201025_04
hgs
parents:
diff changeset
    22
#define DEFAULT_TRIM_THRESHOLD ((size_t)4U * (size_t)1024U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    23
a5496987b1da 201025_04
hgs
parents:
diff changeset
    24
#define MSPACES 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    25
#define HAVE_MORECORE 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    26
#define	MORECORE_CONTIGUOUS 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    27
#define	HAVE_MMAP 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    28
#define HAVE_MREMAP 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    29
#define DEFAULT_GRANULARITY (4096U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    30
#define FOOTERS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    31
#define USE_LOCKS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    32
#define INSECURE 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    33
#define NO_MALLINFO 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    34
a5496987b1da 201025_04
hgs
parents:
diff changeset
    35
#define LACKS_SYS_TYPES_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
    36
#ifndef LACKS_SYS_TYPES_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
    37
#include <sys/types.h>  /* For size_t */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    38
#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
    39
#ifndef _SIZE_T_DECLARED
a5496987b1da 201025_04
hgs
parents:
diff changeset
    40
typedef unsigned int size_t;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    41
#define	_SIZE_T_DECLARED
a5496987b1da 201025_04
hgs
parents:
diff changeset
    42
#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
    43
#endif  /* LACKS_SYS_TYPES_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    44
a5496987b1da 201025_04
hgs
parents:
diff changeset
    45
/* The maximum possible size_t value has all bits set */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    46
#define MAX_SIZE_T           (~(size_t)0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    47
a5496987b1da 201025_04
hgs
parents:
diff changeset
    48
#ifndef ONLY_MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
    49
	#define ONLY_MSPACES 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    50
#endif  /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    51
a5496987b1da 201025_04
hgs
parents:
diff changeset
    52
#ifndef MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
    53
	#if ONLY_MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
    54
		#define MSPACES 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    55
	#else   /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    56
		#define MSPACES 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    57
	#endif  /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    58
#endif  /* MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    59
a5496987b1da 201025_04
hgs
parents:
diff changeset
    60
//#ifndef MALLOC_ALIGNMENT
a5496987b1da 201025_04
hgs
parents:
diff changeset
    61
//	#define MALLOC_ALIGNMENT ((size_t)8U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    62
//#endif  /* MALLOC_ALIGNMENT */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    63
a5496987b1da 201025_04
hgs
parents:
diff changeset
    64
#ifndef FOOTERS
a5496987b1da 201025_04
hgs
parents:
diff changeset
    65
	#define FOOTERS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    66
#endif  /* FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    67
a5496987b1da 201025_04
hgs
parents:
diff changeset
    68
#ifndef ABORT
a5496987b1da 201025_04
hgs
parents:
diff changeset
    69
//	#define ABORT  abort()
a5496987b1da 201025_04
hgs
parents:
diff changeset
    70
//	#define ABORT  User::Invariant()// redefined so euser isn't dependant on oe
a5496987b1da 201025_04
hgs
parents:
diff changeset
    71
  	#define ABORT  HEAP_PANIC(ETHeapBadCellAddress)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    72
#endif  /* ABORT */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    73
a5496987b1da 201025_04
hgs
parents:
diff changeset
    74
#ifndef PROCEED_ON_ERROR
a5496987b1da 201025_04
hgs
parents:
diff changeset
    75
	#define PROCEED_ON_ERROR 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    76
#endif  /* PROCEED_ON_ERROR */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    77
a5496987b1da 201025_04
hgs
parents:
diff changeset
    78
#ifndef USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
    79
	#define USE_LOCKS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    80
#endif  /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    81
a5496987b1da 201025_04
hgs
parents:
diff changeset
    82
#ifndef INSECURE
a5496987b1da 201025_04
hgs
parents:
diff changeset
    83
	#define INSECURE 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    84
#endif  /* INSECURE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    85
a5496987b1da 201025_04
hgs
parents:
diff changeset
    86
#ifndef HAVE_MMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
    87
	#define HAVE_MMAP 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    88
#endif  /* HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    89
a5496987b1da 201025_04
hgs
parents:
diff changeset
    90
#ifndef MMAP_CLEARS
a5496987b1da 201025_04
hgs
parents:
diff changeset
    91
	#define MMAP_CLEARS 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    92
#endif  /* MMAP_CLEARS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    93
a5496987b1da 201025_04
hgs
parents:
diff changeset
    94
#ifndef HAVE_MREMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
    95
	#ifdef linux
a5496987b1da 201025_04
hgs
parents:
diff changeset
    96
		#define HAVE_MREMAP 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
    97
	#else   /* linux */
a5496987b1da 201025_04
hgs
parents:
diff changeset
    98
		#define HAVE_MREMAP 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
    99
	#endif  /* linux */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   100
#endif  /* HAVE_MREMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   101
a5496987b1da 201025_04
hgs
parents:
diff changeset
   102
#ifndef MALLOC_FAILURE_ACTION
a5496987b1da 201025_04
hgs
parents:
diff changeset
   103
	//#define MALLOC_FAILURE_ACTION  errno = ENOMEM;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   104
	#define MALLOC_FAILURE_ACTION ;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   105
#endif  /* MALLOC_FAILURE_ACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   106
a5496987b1da 201025_04
hgs
parents:
diff changeset
   107
#ifndef HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   108
	#if ONLY_MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
   109
		#define HAVE_MORECORE 1 /*AMOD: has changed */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   110
	#else   /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   111
		#define HAVE_MORECORE 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
   112
	#endif  /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   113
#endif  /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   114
a5496987b1da 201025_04
hgs
parents:
diff changeset
   115
#if !HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   116
	#define MORECORE_CONTIGUOUS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   117
#else   /* !HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   118
	#ifndef MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   119
		#define MORECORE DLAdjust
a5496987b1da 201025_04
hgs
parents:
diff changeset
   120
	#endif  /* MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   121
	#ifndef MORECORE_CONTIGUOUS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   122
		#define MORECORE_CONTIGUOUS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   123
	#endif  /* MORECORE_CONTIGUOUS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   124
#endif  /* !HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   125
a5496987b1da 201025_04
hgs
parents:
diff changeset
   126
#ifndef DEFAULT_GRANULARITY
a5496987b1da 201025_04
hgs
parents:
diff changeset
   127
	#if MORECORE_CONTIGUOUS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   128
		#define DEFAULT_GRANULARITY 4096  /* 0 means to compute in init_mparams */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   129
	#else   /* MORECORE_CONTIGUOUS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   130
		#define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   131
	#endif  /* MORECORE_CONTIGUOUS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   132
#endif  /* DEFAULT_GRANULARITY */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   133
a5496987b1da 201025_04
hgs
parents:
diff changeset
   134
#ifndef DEFAULT_TRIM_THRESHOLD
a5496987b1da 201025_04
hgs
parents:
diff changeset
   135
	#ifndef MORECORE_CANNOT_TRIM
a5496987b1da 201025_04
hgs
parents:
diff changeset
   136
		#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   137
	#else   /* MORECORE_CANNOT_TRIM */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   138
		#define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
a5496987b1da 201025_04
hgs
parents:
diff changeset
   139
	#endif  /* MORECORE_CANNOT_TRIM */	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   140
#endif  /* DEFAULT_TRIM_THRESHOLD */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   141
a5496987b1da 201025_04
hgs
parents:
diff changeset
   142
#ifndef DEFAULT_MMAP_THRESHOLD
a5496987b1da 201025_04
hgs
parents:
diff changeset
   143
	#if HAVE_MMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
   144
		#define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   145
	#else   /* HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   146
		#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
a5496987b1da 201025_04
hgs
parents:
diff changeset
   147
	#endif  /* HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   148
#endif  /* DEFAULT_MMAP_THRESHOLD */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   149
a5496987b1da 201025_04
hgs
parents:
diff changeset
   150
#ifndef USE_BUILTIN_FFS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   151
	#define USE_BUILTIN_FFS 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   152
#endif  /* USE_BUILTIN_FFS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   153
a5496987b1da 201025_04
hgs
parents:
diff changeset
   154
#ifndef USE_DEV_RANDOM
a5496987b1da 201025_04
hgs
parents:
diff changeset
   155
	#define USE_DEV_RANDOM 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   156
#endif  /* USE_DEV_RANDOM */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   157
a5496987b1da 201025_04
hgs
parents:
diff changeset
   158
#ifndef NO_MALLINFO
a5496987b1da 201025_04
hgs
parents:
diff changeset
   159
	#define NO_MALLINFO 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   160
#endif  /* NO_MALLINFO */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   161
#ifndef MALLINFO_FIELD_TYPE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   162
	#define MALLINFO_FIELD_TYPE size_t
a5496987b1da 201025_04
hgs
parents:
diff changeset
   163
#endif  /* MALLINFO_FIELD_TYPE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   164
a5496987b1da 201025_04
hgs
parents:
diff changeset
   165
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   166
  mallopt tuning options.  SVID/XPG defines four standard parameter
a5496987b1da 201025_04
hgs
parents:
diff changeset
   167
  numbers for mallopt, normally defined in malloc.h.  None of these
a5496987b1da 201025_04
hgs
parents:
diff changeset
   168
  are used in this malloc, so setting them has no effect. But this
a5496987b1da 201025_04
hgs
parents:
diff changeset
   169
  malloc does support the following options.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   170
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   171
a5496987b1da 201025_04
hgs
parents:
diff changeset
   172
#define M_TRIM_THRESHOLD     (-1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   173
#define M_GRANULARITY        (-2)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   174
#define M_MMAP_THRESHOLD     (-3)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   175
a5496987b1da 201025_04
hgs
parents:
diff changeset
   176
#if !NO_MALLINFO
a5496987b1da 201025_04
hgs
parents:
diff changeset
   177
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   178
  This version of malloc supports the standard SVID/XPG mallinfo
a5496987b1da 201025_04
hgs
parents:
diff changeset
   179
  routine that returns a struct containing usage properties and
a5496987b1da 201025_04
hgs
parents:
diff changeset
   180
  statistics. It should work on any system that has a
a5496987b1da 201025_04
hgs
parents:
diff changeset
   181
  /usr/include/malloc.h defining struct mallinfo.  The main
a5496987b1da 201025_04
hgs
parents:
diff changeset
   182
  declaration needed is the mallinfo struct that is returned (by-copy)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   183
  by mallinfo().  The malloinfo struct contains a bunch of fields that
a5496987b1da 201025_04
hgs
parents:
diff changeset
   184
  are not even meaningful in this version of malloc.  These fields are
a5496987b1da 201025_04
hgs
parents:
diff changeset
   185
  are instead filled by mallinfo() with other numbers that might be of
a5496987b1da 201025_04
hgs
parents:
diff changeset
   186
  interest.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   187
a5496987b1da 201025_04
hgs
parents:
diff changeset
   188
  HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
a5496987b1da 201025_04
hgs
parents:
diff changeset
   189
  /usr/include/malloc.h file that includes a declaration of struct
a5496987b1da 201025_04
hgs
parents:
diff changeset
   190
  mallinfo.  If so, it is included; else a compliant version is
a5496987b1da 201025_04
hgs
parents:
diff changeset
   191
  declared below.  These must be precisely the same for mallinfo() to
a5496987b1da 201025_04
hgs
parents:
diff changeset
   192
  work.  The original SVID version of this struct, defined on most
a5496987b1da 201025_04
hgs
parents:
diff changeset
   193
  systems with mallinfo, declares all fields as ints. But some others
a5496987b1da 201025_04
hgs
parents:
diff changeset
   194
  define as unsigned long. If your system defines the fields using a
a5496987b1da 201025_04
hgs
parents:
diff changeset
   195
  type of different width than listed here, you MUST #include your
a5496987b1da 201025_04
hgs
parents:
diff changeset
   196
  system version and #define HAVE_USR_INCLUDE_MALLOC_H.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   197
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   198
a5496987b1da 201025_04
hgs
parents:
diff changeset
   199
/* #define HAVE_USR_INCLUDE_MALLOC_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   200
a5496987b1da 201025_04
hgs
parents:
diff changeset
   201
#ifdef HAVE_USR_INCLUDE_MALLOC_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   202
#include "/usr/include/malloc.h"
a5496987b1da 201025_04
hgs
parents:
diff changeset
   203
#else /* HAVE_USR_INCLUDE_MALLOC_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   204
a5496987b1da 201025_04
hgs
parents:
diff changeset
   205
struct mallinfo {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   206
  MALLINFO_FIELD_TYPE iArena;    /* non-mmapped space allocated from system */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   207
  MALLINFO_FIELD_TYPE iOrdblks;  /* number of free chunks */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   208
  MALLINFO_FIELD_TYPE iSmblks;   /* always 0 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   209
  MALLINFO_FIELD_TYPE iHblks;    /* always 0 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   210
  MALLINFO_FIELD_TYPE iHblkhd;   /* space in mmapped regions */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   211
  MALLINFO_FIELD_TYPE iUsmblks;  /* maximum total allocated space */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   212
  MALLINFO_FIELD_TYPE iFsmblks;  /* always 0 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   213
  MALLINFO_FIELD_TYPE iUordblks; /* total allocated space */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   214
  MALLINFO_FIELD_TYPE iFordblks; /* total free space */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   215
  MALLINFO_FIELD_TYPE iKeepcost; /* releasable (via malloc_trim) space */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   216
  MALLINFO_FIELD_TYPE iCellCount;/* Number of chunks allocated*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   217
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   218
a5496987b1da 201025_04
hgs
parents:
diff changeset
   219
#endif /* HAVE_USR_INCLUDE_MALLOC_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   220
#endif /* NO_MALLINFO */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   221
a5496987b1da 201025_04
hgs
parents:
diff changeset
   222
#if MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
   223
	typedef void* mspace;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   224
#endif /* MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   225
a5496987b1da 201025_04
hgs
parents:
diff changeset
   226
#if 0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   227
a5496987b1da 201025_04
hgs
parents:
diff changeset
   228
#include <stdio.h>/* for printing in malloc_stats */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   229
a5496987b1da 201025_04
hgs
parents:
diff changeset
   230
#ifndef LACKS_ERRNO_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   231
	#include <errno.h>       /* for MALLOC_FAILURE_ACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   232
#endif /* LACKS_ERRNO_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   233
a5496987b1da 201025_04
hgs
parents:
diff changeset
   234
#if FOOTERS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   235
	#include <time.h>        /* for iMagic initialization */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   236
#endif /* FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   237
a5496987b1da 201025_04
hgs
parents:
diff changeset
   238
#ifndef LACKS_STDLIB_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   239
	#include <stdlib.h>      /* for abort() */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   240
#endif /* LACKS_STDLIB_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   241
a5496987b1da 201025_04
hgs
parents:
diff changeset
   242
#if !defined(ASSERT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   243
#define ASSERT(x) __ASSERT_DEBUG(x, HEAP_PANIC(ETHeapBadCellAddress))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   244
#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   245
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   246
#ifndef LACKS_STRING_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   247
	#include <string.h>      /* for memset etc */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   248
#endif  /* LACKS_STRING_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   249
a5496987b1da 201025_04
hgs
parents:
diff changeset
   250
#if USE_BUILTIN_FFS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   251
	#ifndef LACKS_STRINGS_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   252
		#include <strings.h>     /* for ffs */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   253
	#endif /* LACKS_STRINGS_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   254
#endif /* USE_BUILTIN_FFS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   255
a5496987b1da 201025_04
hgs
parents:
diff changeset
   256
#if HAVE_MMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
   257
	#ifndef LACKS_SYS_MMAN_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   258
		#include <sys/mman.h>    /* for mmap */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   259
	#endif /* LACKS_SYS_MMAN_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   260
	#ifndef LACKS_FCNTL_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   261
		#include <fcntl.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
   262
	#endif /* LACKS_FCNTL_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   263
#endif /* HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   264
a5496987b1da 201025_04
hgs
parents:
diff changeset
   265
#if HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   266
	#ifndef LACKS_UNISTD_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   267
		#include <unistd.h>     /* for sbrk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   268
	extern void*     sbrk(size_t);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   269
	#else /* LACKS_UNISTD_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   270
		#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   271
			extern void*     sbrk(ptrdiff_t);			
a5496987b1da 201025_04
hgs
parents:
diff changeset
   272
			/*Amod sbrk is not defined in WIN32 need to check in symbian*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   273
		#endif /* FreeBSD etc */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   274
	#endif /* LACKS_UNISTD_H */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   275
#endif /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   276
a5496987b1da 201025_04
hgs
parents:
diff changeset
   277
#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   278
a5496987b1da 201025_04
hgs
parents:
diff changeset
   279
/*AMOD: For MALLOC_GETPAGESIZE*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   280
#if 0	// replaced with GET_PAGE_SIZE() defined in heap.cpp
a5496987b1da 201025_04
hgs
parents:
diff changeset
   281
#ifndef WIN32
a5496987b1da 201025_04
hgs
parents:
diff changeset
   282
	#ifndef MALLOC_GETPAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   283
		#ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   284
			#ifndef _SC_PAGE_SIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   285
				#define _SC_PAGE_SIZE _SC_PAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   286
			#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   287
		#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   288
		#ifdef _SC_PAGE_SIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   289
			#define MALLOC_GETPAGESIZE sysconf(_SC_PAGE_SIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   290
		#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   291
			#if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   292
				extern size_t getpagesize();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   293
				#define MALLOC_GETPAGESIZE getpagesize()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   294
			#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   295
				#ifdef WIN32 /* use supplied emulation of getpagesize */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   296
					#define MALLOC_GETPAGESIZE getpagesize()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   297
				#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   298
					#ifndef LACKS_SYS_PARAM_H
a5496987b1da 201025_04
hgs
parents:
diff changeset
   299
						#include <sys/param.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
   300
					#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   301
					#ifdef EXEC_PAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   302
						#define MALLOC_GETPAGESIZE EXEC_PAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   303
					#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   304
						#ifdef NBPG
a5496987b1da 201025_04
hgs
parents:
diff changeset
   305
							#ifndef CLSIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   306
								#define MALLOC_GETPAGESIZE NBPG
a5496987b1da 201025_04
hgs
parents:
diff changeset
   307
							#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   308
								#define MALLOC_GETPAGESIZE (NBPG * CLSIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   309
							#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   310
						#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   311
							#ifdef NBPC
a5496987b1da 201025_04
hgs
parents:
diff changeset
   312
								#define MALLOC_GETPAGESIZE NBPC
a5496987b1da 201025_04
hgs
parents:
diff changeset
   313
							#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   314
								#ifdef PAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   315
									#define MALLOC_GETPAGESIZE PAGESIZE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   316
								#else /* just guess */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   317
									#define MALLOC_GETPAGESIZE ((size_t)4096U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   318
								#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   319
							#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   320
						#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   321
					#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   322
				#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   323
			#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   324
		#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   325
	#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   326
#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   327
#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   328
/*AMOD: For MALLOC_GETPAGESIZE*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   329
a5496987b1da 201025_04
hgs
parents:
diff changeset
   330
/* ------------------- size_t and alignment properties -------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   331
a5496987b1da 201025_04
hgs
parents:
diff changeset
   332
/* The byte and bit size of a size_t */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   333
#define SIZE_T_SIZE         (sizeof(size_t))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   334
#define SIZE_T_BITSIZE      (sizeof(size_t) << 3)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   335
a5496987b1da 201025_04
hgs
parents:
diff changeset
   336
/* Some constants coerced to size_t */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   337
/* Annoying but necessary to avoid errors on some plaftorms */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   338
#define SIZE_T_ZERO         ((size_t)0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   339
#define SIZE_T_ONE          ((size_t)1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   340
#define SIZE_T_TWO          ((size_t)2)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   341
#define TWO_SIZE_T_SIZES    (SIZE_T_SIZE<<1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   342
#define FOUR_SIZE_T_SIZES   (SIZE_T_SIZE<<2)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   343
#define SIX_SIZE_T_SIZES    (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   344
#define HALF_MAX_SIZE_T     (MAX_SIZE_T / 2U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   345
a5496987b1da 201025_04
hgs
parents:
diff changeset
   346
/* The bit mask value corresponding to MALLOC_ALIGNMENT */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   347
#define CHUNK_ALIGN_MASK    (MALLOC_ALIGNMENT - SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   348
a5496987b1da 201025_04
hgs
parents:
diff changeset
   349
/* True if address a has acceptable alignment */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   350
//#define IS_ALIGNED(A)       (((size_t)((A)) & (CHUNK_ALIGN_MASK)) == 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   351
#define IS_ALIGNED(A)       (((unsigned int)((A)) & (CHUNK_ALIGN_MASK)) == 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   352
a5496987b1da 201025_04
hgs
parents:
diff changeset
   353
/* the number of bytes to offset an address to align it */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   354
#define ALIGN_OFFSET(A)\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   355
	((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   356
	((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   357
				
a5496987b1da 201025_04
hgs
parents:
diff changeset
   358
/* -------------------------- MMAP preliminaries ------------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   359
a5496987b1da 201025_04
hgs
parents:
diff changeset
   360
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   361
   If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and
a5496987b1da 201025_04
hgs
parents:
diff changeset
   362
   checks to fail so compiler optimizer can delete code rather than
a5496987b1da 201025_04
hgs
parents:
diff changeset
   363
   using so many "#if"s.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   364
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   365
a5496987b1da 201025_04
hgs
parents:
diff changeset
   366
a5496987b1da 201025_04
hgs
parents:
diff changeset
   367
/* MORECORE and MMAP must return MFAIL on failure */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   368
#define MFAIL                ((void*)(MAX_SIZE_T))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   369
#define CMFAIL               ((TUint8*)(MFAIL)) /* defined for convenience */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   370
a5496987b1da 201025_04
hgs
parents:
diff changeset
   371
#if !HAVE_MMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
   372
	#define IS_MMAPPED_BIT       (SIZE_T_ZERO)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   373
	#define USE_MMAP_BIT         (SIZE_T_ZERO)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   374
	#define CALL_MMAP(s)         MFAIL
a5496987b1da 201025_04
hgs
parents:
diff changeset
   375
	#define CALL_MUNMAP(a, s)    (-1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   376
	#define DIRECT_MMAP(s)       MFAIL
a5496987b1da 201025_04
hgs
parents:
diff changeset
   377
#else /* !HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   378
	#define IS_MMAPPED_BIT       (SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   379
	#define USE_MMAP_BIT         (SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   380
		#ifndef WIN32
a5496987b1da 201025_04
hgs
parents:
diff changeset
   381
			#define CALL_MUNMAP(a, s)    DLUMMAP((a),(s)) /*munmap((a), (s))*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   382
			#define MMAP_PROT            (PROT_READ|PROT_WRITE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   383
			#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   384
				#define MAP_ANONYMOUS        MAP_ANON
a5496987b1da 201025_04
hgs
parents:
diff changeset
   385
			#endif /* MAP_ANON */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   386
			#ifdef MAP_ANONYMOUS       
a5496987b1da 201025_04
hgs
parents:
diff changeset
   387
				#define MMAP_FLAGS           (MAP_PRIVATE|MAP_ANONYMOUS)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   388
				#define CALL_MMAP(s)         mmap(0, (s), MMAP_PROT, (int)MMAP_FLAGS, -1, 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   389
			#else /* MAP_ANONYMOUS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   390
				/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   391
				   Nearly all versions of mmap support MAP_ANONYMOUS, so the following
a5496987b1da 201025_04
hgs
parents:
diff changeset
   392
				   is unlikely to be needed, but is supplied just in case.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   393
				*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   394
				#define MMAP_FLAGS           (MAP_PRIVATE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   395
				//static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   396
				#define CALL_MMAP(s) DLMMAP(s)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   397
				/*#define CALL_MMAP(s) ((dev_zero_fd < 0) ? \
a5496987b1da 201025_04
hgs
parents:
diff changeset
   398
			           (dev_zero_fd = open("/dev/zero", O_RDWR), \
a5496987b1da 201025_04
hgs
parents:
diff changeset
   399
			            mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \
a5496987b1da 201025_04
hgs
parents:
diff changeset
   400
			            mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   401
			            */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   402
				#define CALL_REMAP(a, s, d)    DLREMAP((a),(s),(d))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   403
			#endif /* MAP_ANONYMOUS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   404
			#define DIRECT_MMAP(s)       CALL_MMAP(s)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   405
		#else /* WIN32 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   406
			#define CALL_MMAP(s)         win32mmap(s)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   407
			#define CALL_MUNMAP(a, s)    win32munmap((a), (s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   408
			#define DIRECT_MMAP(s)       win32direct_mmap(s)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   409
		#endif /* WIN32 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   410
#endif /* HAVE_MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   411
a5496987b1da 201025_04
hgs
parents:
diff changeset
   412
#if HAVE_MMAP && HAVE_MREMAP
a5496987b1da 201025_04
hgs
parents:
diff changeset
   413
	#define CALL_MREMAP(addr, osz, nsz, mv) mremap((addr), (osz), (nsz), (mv))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   414
#else  /* HAVE_MMAP && HAVE_MREMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   415
	#define CALL_MREMAP(addr, osz, nsz, mv) MFAIL
a5496987b1da 201025_04
hgs
parents:
diff changeset
   416
#endif /* HAVE_MMAP && HAVE_MREMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   417
a5496987b1da 201025_04
hgs
parents:
diff changeset
   418
#if HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   419
	#define CALL_MORECORE(S)     SetBrk(S)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   420
#else  /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   421
	#define CALL_MORECORE(S)     MFAIL
a5496987b1da 201025_04
hgs
parents:
diff changeset
   422
#endif /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   423
a5496987b1da 201025_04
hgs
parents:
diff changeset
   424
/* mstate bit set if continguous morecore disabled or failed */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   425
#define USE_NONCONTIGUOUS_BIT (4U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   426
a5496987b1da 201025_04
hgs
parents:
diff changeset
   427
/* segment bit set in create_mspace_with_base */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   428
#define EXTERN_BIT            (8U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   429
a5496987b1da 201025_04
hgs
parents:
diff changeset
   430
a5496987b1da 201025_04
hgs
parents:
diff changeset
   431
#if USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   432
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   433
  When locks are defined, there are up to two global locks:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   434
  * If HAVE_MORECORE, iMorecoreMutex protects sequences of calls to
a5496987b1da 201025_04
hgs
parents:
diff changeset
   435
    MORECORE.  In many cases sys_alloc requires two calls, that should
a5496987b1da 201025_04
hgs
parents:
diff changeset
   436
    not be interleaved with calls by other threads.  This does not
a5496987b1da 201025_04
hgs
parents:
diff changeset
   437
    protect against direct calls to MORECORE by other threads not
a5496987b1da 201025_04
hgs
parents:
diff changeset
   438
    using this lock, so there is still code to cope the best we can on
a5496987b1da 201025_04
hgs
parents:
diff changeset
   439
    interference.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   440
  * iMagicInitMutex ensures that mparams.iMagic and other
a5496987b1da 201025_04
hgs
parents:
diff changeset
   441
    unique mparams values are initialized only once.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   442
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   443
	#ifndef WIN32
a5496987b1da 201025_04
hgs
parents:
diff changeset
   444
		/* By default use posix locks */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   445
		#include <pthread.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
   446
		#define MLOCK_T pthread_mutex_t
a5496987b1da 201025_04
hgs
parents:
diff changeset
   447
		#define INITIAL_LOCK(l)      pthread_mutex_init(l, NULL)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   448
		#define ACQUIRE_LOCK(l)      pthread_mutex_lock(l)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   449
		#define RELEASE_LOCK(l)      pthread_mutex_unlock(l)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   450
		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   451
		#if HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   452
			//static MLOCK_T iMorecoreMutex = PTHREAD_MUTEX_INITIALIZER;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   453
		#endif /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   454
			//static MLOCK_T iMagicInitMutex = PTHREAD_MUTEX_INITIALIZER;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   455
	#else /* WIN32 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   456
		#define MLOCK_T long
a5496987b1da 201025_04
hgs
parents:
diff changeset
   457
		#define INITIAL_LOCK(l)      *(l)=0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   458
		#define ACQUIRE_LOCK(l)      win32_acquire_lock(l)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   459
		#define RELEASE_LOCK(l)      win32_release_lock(l)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   460
		#if HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   461
			static MLOCK_T iMorecoreMutex;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   462
		#endif /* HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   463
		static MLOCK_T iMagicInitMutex;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   464
	#endif /* WIN32 */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   465
	#define USE_LOCK_BIT               (2U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   466
#else  /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   467
	#define USE_LOCK_BIT               (0U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   468
	#define INITIAL_LOCK(l)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   469
#endif /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   470
a5496987b1da 201025_04
hgs
parents:
diff changeset
   471
#if USE_LOCKS && HAVE_MORECORE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   472
	#define ACQUIRE_MORECORE_LOCK(M)    ACQUIRE_LOCK((M->iMorecoreMutex)/*&iMorecoreMutex*/);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   473
	#define RELEASE_MORECORE_LOCK(M)    RELEASE_LOCK((M->iMorecoreMutex)/*&iMorecoreMutex*/);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   474
#else /* USE_LOCKS && HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   475
	#define ACQUIRE_MORECORE_LOCK(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   476
	#define RELEASE_MORECORE_LOCK(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   477
#endif /* USE_LOCKS && HAVE_MORECORE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   478
a5496987b1da 201025_04
hgs
parents:
diff changeset
   479
#if USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   480
		/*Currently not suporting this*/				
a5496987b1da 201025_04
hgs
parents:
diff changeset
   481
	#define ACQUIRE_MAGIC_INIT_LOCK(M)  ACQUIRE_LOCK(((M)->iMagicInitMutex));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   482
	//AMOD: changed #define ACQUIRE_MAGIC_INIT_LOCK()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   483
	//#define RELEASE_MAGIC_INIT_LOCK()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   484
	#define RELEASE_MAGIC_INIT_LOCK(M)  RELEASE_LOCK(((M)->iMagicInitMutex));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   485
#else  /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   486
	#define ACQUIRE_MAGIC_INIT_LOCK(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   487
	#define RELEASE_MAGIC_INIT_LOCK(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   488
#endif /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   489
a5496987b1da 201025_04
hgs
parents:
diff changeset
   490
/*CHUNK representation*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   491
struct malloc_chunk {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   492
  size_t               iPrevFoot;  /* Size of previous chunk (if free).  */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   493
  size_t               iHead;       /* Size and inuse bits. */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   494
  struct malloc_chunk* iFd;         /* double links -- used only if free. */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   495
  struct malloc_chunk* iBk;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   496
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   497
a5496987b1da 201025_04
hgs
parents:
diff changeset
   498
typedef struct malloc_chunk  mchunk;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   499
typedef struct malloc_chunk* mchunkptr;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   500
typedef struct malloc_chunk* sbinptr;  /* The type of bins of chunks */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   501
typedef unsigned int bindex_t;         /* Described below */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   502
typedef unsigned int binmap_t;         /* Described below */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   503
typedef unsigned int flag_t;           /* The type of various bit flag sets */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   504
a5496987b1da 201025_04
hgs
parents:
diff changeset
   505
a5496987b1da 201025_04
hgs
parents:
diff changeset
   506
/* ------------------- Chunks sizes and alignments ----------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   507
#define MCHUNK_SIZE         (sizeof(mchunk))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   508
a5496987b1da 201025_04
hgs
parents:
diff changeset
   509
//#if FOOTERS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   510
//	#define CHUNK_OVERHEAD      (TWO_SIZE_T_SIZES)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   511
//#else /* FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   512
//	#define CHUNK_OVERHEAD      (SIZE_T_SIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   513
//#endif /* FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   514
a5496987b1da 201025_04
hgs
parents:
diff changeset
   515
/* MMapped chunks need a second word of overhead ... */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   516
#define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   517
/* ... and additional padding for fake next-chunk at foot */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   518
#define MMAP_FOOT_PAD       (FOUR_SIZE_T_SIZES)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   519
a5496987b1da 201025_04
hgs
parents:
diff changeset
   520
/* The smallest size we can malloc is an aligned minimal chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   521
#define MIN_CHUNK_SIZE ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   522
a5496987b1da 201025_04
hgs
parents:
diff changeset
   523
/* conversion from malloc headers to user pointers, and back */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   524
#define CHUNK2MEM(p)        ((void*)((TUint8*)(p)       + TWO_SIZE_T_SIZES))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   525
#define MEM2CHUNK(mem)      ((mchunkptr)((TUint8*)(mem) - TWO_SIZE_T_SIZES))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   526
/* chunk associated with aligned address A */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   527
#define ALIGN_AS_CHUNK(A)   (mchunkptr)((A) + ALIGN_OFFSET(CHUNK2MEM(A)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   528
a5496987b1da 201025_04
hgs
parents:
diff changeset
   529
/* Bounds on request (not chunk) sizes. */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   530
#define MAX_REQUEST         ((-MIN_CHUNK_SIZE) << 2)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   531
#define MIN_REQUEST         (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   532
a5496987b1da 201025_04
hgs
parents:
diff changeset
   533
/* pad request bytes into a usable size */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   534
#define PAD_REQUEST(req) (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   535
a5496987b1da 201025_04
hgs
parents:
diff changeset
   536
/* pad request, checking for minimum (but not maximum) */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   537
#define REQUEST2SIZE(req) (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : PAD_REQUEST(req))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   538
a5496987b1da 201025_04
hgs
parents:
diff changeset
   539
/* ------------------ Operations on iHead and foot fields ----------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   540
a5496987b1da 201025_04
hgs
parents:
diff changeset
   541
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   542
  The iHead field of a chunk is or'ed with PINUSE_BIT when previous
a5496987b1da 201025_04
hgs
parents:
diff changeset
   543
  adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in
a5496987b1da 201025_04
hgs
parents:
diff changeset
   544
  use. If the chunk was obtained with mmap, the iPrevFoot field has
a5496987b1da 201025_04
hgs
parents:
diff changeset
   545
  IS_MMAPPED_BIT set, otherwise holding the offset of the base of the
a5496987b1da 201025_04
hgs
parents:
diff changeset
   546
  mmapped region to the base of the chunk.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   547
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   548
#define PINUSE_BIT          (SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   549
#define CINUSE_BIT          (SIZE_T_TWO)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   550
#define INUSE_BITS          (PINUSE_BIT|CINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   551
a5496987b1da 201025_04
hgs
parents:
diff changeset
   552
/* Head value for fenceposts */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   553
#define FENCEPOST_HEAD      (INUSE_BITS|SIZE_T_SIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   554
a5496987b1da 201025_04
hgs
parents:
diff changeset
   555
/* extraction of fields from iHead words */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   556
#define CINUSE(p)           ((p)->iHead & CINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   557
#define PINUSE(p)           ((p)->iHead & PINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   558
#define CHUNKSIZE(p)        ((p)->iHead & ~(INUSE_BITS))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   559
a5496987b1da 201025_04
hgs
parents:
diff changeset
   560
#define CLEAR_PINUSE(p)     ((p)->iHead &= ~PINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   561
#define CLEAR_CINUSE(p)     ((p)->iHead &= ~CINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   562
a5496987b1da 201025_04
hgs
parents:
diff changeset
   563
/* Treat space at ptr +/- offset as a chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   564
#define CHUNK_PLUS_OFFSET(p, s)  ((mchunkptr)(((TUint8*)(p)) + (s)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   565
#define CHUNK_MINUS_OFFSET(p, s) ((mchunkptr)(((TUint8*)(p)) - (s)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   566
a5496987b1da 201025_04
hgs
parents:
diff changeset
   567
/* Ptr to next or previous physical malloc_chunk. */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   568
#define NEXT_CHUNK(p) ((mchunkptr)( ((TUint8*)(p)) + ((p)->iHead & ~INUSE_BITS)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   569
#define PREV_CHUNK(p) ((mchunkptr)( ((TUint8*)(p)) - ((p)->iPrevFoot) ))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   570
a5496987b1da 201025_04
hgs
parents:
diff changeset
   571
/* extract next chunk's PINUSE bit */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   572
#define NEXT_PINUSE(p)  ((NEXT_CHUNK(p)->iHead) & PINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   573
a5496987b1da 201025_04
hgs
parents:
diff changeset
   574
/* Get/set size at footer */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   575
#define GET_FOOT(p, s)  (((mchunkptr)((TUint8*)(p) + (s)))->iPrevFoot)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   576
#define SET_FOOT(p, s)  (((mchunkptr)((TUint8*)(p) + (s)))->iPrevFoot = (s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   577
a5496987b1da 201025_04
hgs
parents:
diff changeset
   578
/* Set size, PINUSE bit, and foot */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   579
#define SET_SIZE_AND_PINUSE_OF_FREE_CHUNK(p, s) ((p)->iHead = (s|PINUSE_BIT), SET_FOOT(p, s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   580
a5496987b1da 201025_04
hgs
parents:
diff changeset
   581
/* Set size, PINUSE bit, foot, and clear next PINUSE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   582
#define SET_FREE_WITH_PINUSE(p, s, n) (CLEAR_PINUSE(n), SET_SIZE_AND_PINUSE_OF_FREE_CHUNK(p, s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   583
a5496987b1da 201025_04
hgs
parents:
diff changeset
   584
#define IS_MMAPPED(p) (!((p)->iHead & PINUSE_BIT) && ((p)->iPrevFoot & IS_MMAPPED_BIT))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   585
a5496987b1da 201025_04
hgs
parents:
diff changeset
   586
/* Get the internal overhead associated with chunk p */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   587
#define OVERHEAD_FOR(p) (IS_MMAPPED(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   588
a5496987b1da 201025_04
hgs
parents:
diff changeset
   589
/* Return true if malloced space is not necessarily cleared */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   590
#if MMAP_CLEARS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   591
	#define CALLOC_MUST_CLEAR(p) (!IS_MMAPPED(p))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   592
#else /* MMAP_CLEARS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   593
	#define CALLOC_MUST_CLEAR(p) (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   594
#endif /* MMAP_CLEARS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   595
a5496987b1da 201025_04
hgs
parents:
diff changeset
   596
/* ---------------------- Overlaid data structures ----------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   597
struct malloc_tree_chunk {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   598
  /* The first four fields must be compatible with malloc_chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   599
  size_t                    				iPrevFoot;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   600
  size_t                    				iHead;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   601
  struct malloc_tree_chunk*	iFd;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   602
  struct malloc_tree_chunk*	iBk;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   603
a5496987b1da 201025_04
hgs
parents:
diff changeset
   604
  struct malloc_tree_chunk* iChild[2];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   605
  struct malloc_tree_chunk* iParent;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   606
  bindex_t                  iIndex;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   607
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   608
a5496987b1da 201025_04
hgs
parents:
diff changeset
   609
typedef struct malloc_tree_chunk  tchunk;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   610
typedef struct malloc_tree_chunk* tchunkptr;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   611
typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   612
a5496987b1da 201025_04
hgs
parents:
diff changeset
   613
/* A little helper macro for trees */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   614
#define LEFTMOST_CHILD(t) ((t)->iChild[0] != 0? (t)->iChild[0] : (t)->iChild[1])
a5496987b1da 201025_04
hgs
parents:
diff changeset
   615
/*Segment structur*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   616
//struct malloc_segment {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   617
//  TUint8*        iBase;             /* base address */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   618
//  size_t       iSize;             /* allocated size */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   619
//};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   620
a5496987b1da 201025_04
hgs
parents:
diff changeset
   621
#define IS_MMAPPED_SEGMENT(S)  ((S)->iSflags & IS_MMAPPED_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   622
#define IS_EXTERN_SEGMENT(S)   ((S)->iSflags & EXTERN_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   623
a5496987b1da 201025_04
hgs
parents:
diff changeset
   624
typedef struct malloc_segment  msegment;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   625
typedef struct malloc_segment* msegmentptr;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   626
a5496987b1da 201025_04
hgs
parents:
diff changeset
   627
/*Malloc State data structur*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   628
a5496987b1da 201025_04
hgs
parents:
diff changeset
   629
//#define NSMALLBINS        (32U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   630
//#define NTREEBINS         (32U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   631
#define SMALLBIN_SHIFT    (3U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   632
#define SMALLBIN_WIDTH    (SIZE_T_ONE << SMALLBIN_SHIFT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   633
#define TREEBIN_SHIFT     (8U)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   634
#define MIN_LARGE_SIZE    (SIZE_T_ONE << TREEBIN_SHIFT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   635
#define MAX_SMALL_SIZE    (MIN_LARGE_SIZE - SIZE_T_ONE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   636
#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   637
a5496987b1da 201025_04
hgs
parents:
diff changeset
   638
/*struct malloc_state {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   639
	binmap_t	 iSmallMap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   640
	binmap_t	 iTreeMap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   641
	size_t		 iDvSize;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   642
	size_t		 iTopSize;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   643
	mchunkptr	iDv;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   644
	mchunkptr	iTop;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   645
	size_t		 iTrimCheck;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   646
	mchunkptr	iSmallBins[(NSMALLBINS+1)*2];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   647
	tbinptr		iTreeBins[NTREEBINS];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   648
	msegment	iSeg;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   649
	};*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   650
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   651
struct malloc_state {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   652
  binmap_t   iSmallMap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   653
  binmap_t   iTreeMap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   654
  size_t     iDvSize;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   655
  size_t     iTopSize;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   656
  TUint8*      iLeastAddr;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   657
  mchunkptr  iDv;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   658
  mchunkptr  iTop;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   659
  size_t     iTrimCheck;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   660
  size_t     iMagic;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   661
  mchunkptr  iSmallBins[(NSMALLBINS+1)*2];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   662
  tbinptr    iTreeBins[NTREEBINS];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   663
  size_t     iFootprint;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   664
  size_t     iMaxFootprint;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   665
  flag_t     iMflags;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   666
#if USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   667
  MLOCK_T    iMutex;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   668
  MLOCK_T	iMagicInitMutex;  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   669
  MLOCK_T	iMorecoreMutex;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   670
#endif 
a5496987b1da 201025_04
hgs
parents:
diff changeset
   671
  msegment   iSeg;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   672
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   673
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   674
typedef struct malloc_state*    mstate;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   675
a5496987b1da 201025_04
hgs
parents:
diff changeset
   676
/* ------------- Global malloc_state and malloc_params ------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   677
a5496987b1da 201025_04
hgs
parents:
diff changeset
   678
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   679
  malloc_params holds global properties, including those that can be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   680
  dynamically set using mallopt. There is a single instance, mparams,
a5496987b1da 201025_04
hgs
parents:
diff changeset
   681
  initialized in init_mparams.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   682
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   683
a5496987b1da 201025_04
hgs
parents:
diff changeset
   684
struct malloc_params {
a5496987b1da 201025_04
hgs
parents:
diff changeset
   685
  size_t iMagic;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   686
  size_t iPageSize;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   687
  size_t iGranularity;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   688
  size_t iMmapThreshold;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   689
  size_t iTrimThreshold;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   690
  flag_t iDefaultMflags;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   691
#if USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   692
  MLOCK_T	iMagicInitMutex;  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   693
#endif /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   694
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   695
a5496987b1da 201025_04
hgs
parents:
diff changeset
   696
/* The global malloc_state used for all non-"mspace" calls */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   697
/*AMOD: Need to check this as this will be the member of the class*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   698
a5496987b1da 201025_04
hgs
parents:
diff changeset
   699
//static struct malloc_state _gm_;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   700
//#define GM                 (&_gm_)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   701
 
a5496987b1da 201025_04
hgs
parents:
diff changeset
   702
//#define IS_GLOBAL(M)       ((M) == &_gm_)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   703
/*AMOD: has changed*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   704
#define IS_GLOBAL(M)       ((M) == GM)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   705
#define IS_INITIALIZED(M)  ((M)->iTop != 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   706
a5496987b1da 201025_04
hgs
parents:
diff changeset
   707
/* -------------------------- system alloc setup ------------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   708
a5496987b1da 201025_04
hgs
parents:
diff changeset
   709
/* Operations on iMflags */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   710
a5496987b1da 201025_04
hgs
parents:
diff changeset
   711
#define USE_LOCK(M)           ((M)->iMflags &   USE_LOCK_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   712
#define ENABLE_LOCK(M)        ((M)->iMflags |=  USE_LOCK_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   713
#define DISABLE_LOCK(M)       ((M)->iMflags &= ~USE_LOCK_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   714
a5496987b1da 201025_04
hgs
parents:
diff changeset
   715
#define USE_MMAP(M)           ((M)->iMflags &   USE_MMAP_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   716
#define ENABLE_MMAP(M)        ((M)->iMflags |=  USE_MMAP_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   717
#define DISABLE_MMAP(M)       ((M)->iMflags &= ~USE_MMAP_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   718
a5496987b1da 201025_04
hgs
parents:
diff changeset
   719
#define USE_NONCONTIGUOUS(M)  ((M)->iMflags &   USE_NONCONTIGUOUS_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   720
#define DISABLE_CONTIGUOUS(M) ((M)->iMflags |=  USE_NONCONTIGUOUS_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   721
a5496987b1da 201025_04
hgs
parents:
diff changeset
   722
#define SET_LOCK(M,L) ((M)->iMflags = (L)? ((M)->iMflags | USE_LOCK_BIT) :  ((M)->iMflags & ~USE_LOCK_BIT))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   723
a5496987b1da 201025_04
hgs
parents:
diff changeset
   724
/* page-align a size */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   725
#define PAGE_ALIGN(S) (((S) + (mparams.iPageSize)) & ~(mparams.iPageSize - SIZE_T_ONE))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   726
a5496987b1da 201025_04
hgs
parents:
diff changeset
   727
/* iGranularity-align a size */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   728
#define GRANULARITY_ALIGN(S)  (((S) + (mparams.iGranularity)) & ~(mparams.iGranularity - SIZE_T_ONE))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   729
a5496987b1da 201025_04
hgs
parents:
diff changeset
   730
#define IS_PAGE_ALIGNED(S)   (((size_t)(S) & (mparams.iPageSize - SIZE_T_ONE)) == 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   731
#define IS_GRANULARITY_ALIGNED(S)   (((size_t)(S) & (mparams.iGranularity - SIZE_T_ONE)) == 0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   732
a5496987b1da 201025_04
hgs
parents:
diff changeset
   733
/*  True if segment S holds address A */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   734
#define SEGMENT_HOLDS(S, A)  ((TUint8*)(A) >= S->iBase && (TUint8*)(A) < S->iBase + S->iSize)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   735
a5496987b1da 201025_04
hgs
parents:
diff changeset
   736
#ifndef MORECORE_CANNOT_TRIM
a5496987b1da 201025_04
hgs
parents:
diff changeset
   737
	#define SHOULD_TRIM(M,s)  ((s) > (M)->iTrimCheck)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   738
#else  /* MORECORE_CANNOT_TRIM */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   739
	#define SHOULD_TRIM(M,s)  (0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   740
#endif /* MORECORE_CANNOT_TRIM */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   741
a5496987b1da 201025_04
hgs
parents:
diff changeset
   742
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   743
  TOP_FOOT_SIZE is padding at the end of a segment, including space
a5496987b1da 201025_04
hgs
parents:
diff changeset
   744
  that may be needed to place segment records and fenceposts when new
a5496987b1da 201025_04
hgs
parents:
diff changeset
   745
  noncontiguous segments are added.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   746
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   747
#define TOP_FOOT_SIZE  (ALIGN_OFFSET(CHUNK2MEM(0))+PAD_REQUEST(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   748
a5496987b1da 201025_04
hgs
parents:
diff changeset
   749
#define SYS_ALLOC_PADDING (TOP_FOOT_SIZE + MALLOC_ALIGNMENT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   750
/* -------------------------------  Hooks -------------------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   751
a5496987b1da 201025_04
hgs
parents:
diff changeset
   752
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   753
  PREACTION should be defined to return 0 on success, and nonzero on
a5496987b1da 201025_04
hgs
parents:
diff changeset
   754
  failure. If you are not using locking, you can redefine these to do
a5496987b1da 201025_04
hgs
parents:
diff changeset
   755
  anything you like.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   756
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   757
a5496987b1da 201025_04
hgs
parents:
diff changeset
   758
#if USE_LOCKS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   759
	/* Ensure locks are initialized */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   760
	#define GLOBALLY_INITIALIZE() (mparams.iPageSize == 0 && init_mparams())
a5496987b1da 201025_04
hgs
parents:
diff changeset
   761
	#define PREACTION(M) (USE_LOCK((M))?(ACQUIRE_LOCK((M)->iMutex),0):0) /*Action to take like lock before alloc*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   762
	#define POSTACTION(M) { if (USE_LOCK(M)) RELEASE_LOCK((M)->iMutex); }
a5496987b1da 201025_04
hgs
parents:
diff changeset
   763
a5496987b1da 201025_04
hgs
parents:
diff changeset
   764
#else /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   765
	#ifndef PREACTION
a5496987b1da 201025_04
hgs
parents:
diff changeset
   766
		#define PREACTION(M) (0)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   767
	#endif  /* PREACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   768
	#ifndef POSTACTION
a5496987b1da 201025_04
hgs
parents:
diff changeset
   769
		#define POSTACTION(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   770
	#endif  /* POSTACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   771
#endif /* USE_LOCKS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   772
a5496987b1da 201025_04
hgs
parents:
diff changeset
   773
/*
a5496987b1da 201025_04
hgs
parents:
diff changeset
   774
  CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   775
  USAGE_ERROR_ACTION is triggered on detected bad frees and
a5496987b1da 201025_04
hgs
parents:
diff changeset
   776
  reallocs. The argument p is an address that might have triggered the
a5496987b1da 201025_04
hgs
parents:
diff changeset
   777
  fault. It is ignored by the two predefined actions, but might be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   778
  useful in custom actions that try to help diagnose errors.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   779
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
   780
a5496987b1da 201025_04
hgs
parents:
diff changeset
   781
#if PROCEED_ON_ERROR
a5496987b1da 201025_04
hgs
parents:
diff changeset
   782
	/* A count of the number of corruption errors causing resets */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   783
	int malloc_corruption_error_count;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   784
	/* default corruption action */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   785
	static void ResetOnError(mstate m);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   786
	#define CORRUPTION_ERROR_ACTION(m)  ResetOnError(m)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   787
	#define USAGE_ERROR_ACTION(m, p)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   788
#else /* PROCEED_ON_ERROR */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   789
	#ifndef CORRUPTION_ERROR_ACTION
a5496987b1da 201025_04
hgs
parents:
diff changeset
   790
		#define CORRUPTION_ERROR_ACTION(m) ABORT
a5496987b1da 201025_04
hgs
parents:
diff changeset
   791
	#endif /* CORRUPTION_ERROR_ACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   792
	#ifndef USAGE_ERROR_ACTION
a5496987b1da 201025_04
hgs
parents:
diff changeset
   793
		#define USAGE_ERROR_ACTION(m,p) ABORT
a5496987b1da 201025_04
hgs
parents:
diff changeset
   794
	#endif /* USAGE_ERROR_ACTION */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   795
#endif /* PROCEED_ON_ERROR */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   796
a5496987b1da 201025_04
hgs
parents:
diff changeset
   797
a5496987b1da 201025_04
hgs
parents:
diff changeset
   798
#ifdef _DEBUG
a5496987b1da 201025_04
hgs
parents:
diff changeset
   799
	#define CHECK_FREE_CHUNK(M,P)       DoCheckFreeChunk(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   800
	#define CHECK_INUSE_CHUNK(M,P)      DoCheckInuseChunk(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   801
	#define CHECK_TOP_CHUNK(M,P)        DoCheckTopChunk(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   802
	#define CHECK_MALLOCED_CHUNK(M,P,N) DoCheckMallocedChunk(M,P,N)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   803
	#define CHECK_MMAPPED_CHUNK(M,P)    DoCheckMmappedChunk(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   804
	#define CHECK_MALLOC_STATE(M)       DoCheckMallocState(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   805
#else /* DEBUG */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   806
	#define CHECK_FREE_CHUNK(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   807
	#define CHECK_INUSE_CHUNK(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   808
	#define CHECK_MALLOCED_CHUNK(M,P,N)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   809
	#define CHECK_MMAPPED_CHUNK(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   810
	#define CHECK_MALLOC_STATE(M)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   811
	#define CHECK_TOP_CHUNK(M,P)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   812
#endif /* DEBUG */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   813
a5496987b1da 201025_04
hgs
parents:
diff changeset
   814
/* ---------------------------- Indexing Bins ---------------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   815
a5496987b1da 201025_04
hgs
parents:
diff changeset
   816
#define IS_SMALL(s)         (((s) >> SMALLBIN_SHIFT) < NSMALLBINS)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   817
#define SMALL_INDEX(s)      ((s)  >> SMALLBIN_SHIFT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   818
#define SMALL_INDEX2SIZE(i) ((i)  << SMALLBIN_SHIFT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   819
#define MIN_SMALL_INDEX     (SMALL_INDEX(MIN_CHUNK_SIZE))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   820
a5496987b1da 201025_04
hgs
parents:
diff changeset
   821
/* addressing by index. See above about smallbin repositioning */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   822
#define SMALLBIN_AT(M, i)   ((sbinptr)((TUint8*)&((M)->iSmallBins[(i)<<1])))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   823
#define TREEBIN_AT(M,i)     (&((M)->iTreeBins[i]))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   824
a5496987b1da 201025_04
hgs
parents:
diff changeset
   825
a5496987b1da 201025_04
hgs
parents:
diff changeset
   826
/* Bit representing maximum resolved size in a treebin at i */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   827
#define BIT_FOR_TREE_INDEX(i) (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   828
a5496987b1da 201025_04
hgs
parents:
diff changeset
   829
/* Shift placing maximum resolved bit in a treebin at i as sign bit */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   830
#define LEFTSHIFT_FOR_TREE_INDEX(i) ((i == NTREEBINS-1)? 0 : ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   831
a5496987b1da 201025_04
hgs
parents:
diff changeset
   832
/* The size of the smallest chunk held in bin with index i */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   833
#define MINSIZE_FOR_TREE_INDEX(i) ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) |  (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1)))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   834
a5496987b1da 201025_04
hgs
parents:
diff changeset
   835
a5496987b1da 201025_04
hgs
parents:
diff changeset
   836
/* ------------------------ Operations on bin maps ----------------------- */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   837
/* bit corresponding to given index */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   838
#define IDX2BIT(i)              ((binmap_t)(1) << (i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   839
/* Mark/Clear bits with given index */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   840
#define MARK_SMALLMAP(M,i)      ((M)->iSmallMap |=  IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   841
#define CLEAR_SMALLMAP(M,i)     ((M)->iSmallMap &= ~IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   842
#define SMALLMAP_IS_MARKED(M,i) ((M)->iSmallMap &   IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   843
#define MARK_TREEMAP(M,i)       ((M)->iTreeMap  |=  IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   844
#define CLEAR_TREEMAP(M,i)      ((M)->iTreeMap  &= ~IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   845
#define TREEMAP_IS_MARKED(M,i)  ((M)->iTreeMap  &   IDX2BIT(i))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   846
a5496987b1da 201025_04
hgs
parents:
diff changeset
   847
	/* isolate the least set bit of a bitmap */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   848
#define LEAST_BIT(x)         ((x) & -(x))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   849
a5496987b1da 201025_04
hgs
parents:
diff changeset
   850
/* mask with all bits to left of least bit of x on */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   851
#define LEFT_BITS(x)         ((x<<1) | -(x<<1))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   852
a5496987b1da 201025_04
hgs
parents:
diff changeset
   853
/* mask with all bits to left of or equal to least bit of x on */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   854
#define SAME_OR_LEFT_BITS(x) ((x) | -(x))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   855
a5496987b1da 201025_04
hgs
parents:
diff changeset
   856
#if !INSECURE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   857
	/* Check if address a is at least as high as any from MORECORE or MMAP */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   858
	#define OK_ADDRESS(M, a) ((TUint8*)(a) >= (M)->iLeastAddr)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   859
	/* Check if address of next chunk n is higher than base chunk p */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   860
	#define OK_NEXT(p, n)    ((TUint8*)(p) < (TUint8*)(n))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   861
	/* Check if p has its CINUSE bit on */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   862
	#define OK_CINUSE(p)     CINUSE(p)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   863
	/* Check if p has its PINUSE bit on */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   864
	#define OK_PINUSE(p)     PINUSE(p)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   865
#else /* !INSECURE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   866
	#define OK_ADDRESS(M, a) (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   867
	#define OK_NEXT(b, n)    (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   868
	#define OK_CINUSE(p)     (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   869
	#define OK_PINUSE(p)     (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   870
#endif /* !INSECURE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   871
a5496987b1da 201025_04
hgs
parents:
diff changeset
   872
#if (FOOTERS && !INSECURE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   873
	/* Check if (alleged) mstate m has expected iMagic field */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   874
	#define OK_MAGIC(M)      ((M)->iMagic == mparams.iMagic)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   875
#else  /* (FOOTERS && !INSECURE) */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   876
	#define OK_MAGIC(M)      (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   877
#endif /* (FOOTERS && !INSECURE) */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   878
a5496987b1da 201025_04
hgs
parents:
diff changeset
   879
/* In gcc, use __builtin_expect to minimize impact of checks */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   880
#if !INSECURE
a5496987b1da 201025_04
hgs
parents:
diff changeset
   881
	#if defined(__GNUC__) && __GNUC__ >= 3
a5496987b1da 201025_04
hgs
parents:
diff changeset
   882
		#define RTCHECK(e)  __builtin_expect(e, 1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   883
	#else /* GNUC */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   884
		#define RTCHECK(e)  (e)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   885
	#endif /* GNUC */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   886
a5496987b1da 201025_04
hgs
parents:
diff changeset
   887
#else /* !INSECURE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   888
	#define RTCHECK(e)  (1)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   889
#endif /* !INSECURE */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   890
/* macros to set up inuse chunks with or without footers */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   891
#if !FOOTERS
a5496987b1da 201025_04
hgs
parents:
diff changeset
   892
	#define MARK_INUSE_FOOT(M,p,s)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   893
	/* Set CINUSE bit and PINUSE bit of next chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   894
	#define SET_INUSE(M,p,s)  ((p)->iHead = (((p)->iHead & PINUSE_BIT)|s|CINUSE_BIT),((mchunkptr)(((TUint8*)(p)) + (s)))->iHead |= PINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   895
	/* Set CINUSE and PINUSE of this chunk and PINUSE of next chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   896
	#define SET_INUSE_AND_PINUSE(M,p,s) ((p)->iHead = (s|PINUSE_BIT|CINUSE_BIT),((mchunkptr)(((TUint8*)(p)) + (s)))->iHead |= PINUSE_BIT)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   897
	/* Set size, CINUSE and PINUSE bit of this chunk */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   898
	#define SET_SIZE_AND_PINUSE_OF_INUSE_CHUNK(M, p, s) ((p)->iHead = (s|PINUSE_BIT|CINUSE_BIT))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   899
#else /* FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   900
	/* Set foot of inuse chunk to be xor of mstate and seed */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   901
	#define MARK_INUSE_FOOT(M,p,s) (((mchunkptr)((TUint8*)(p) + (s)))->iPrevFoot = ((size_t)(M) ^ mparams.iMagic))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   902
	#define GET_MSTATE_FOR(p) ((mstate)(((mchunkptr)((TUint8*)(p)+(CHUNKSIZE(p))))->iPrevFoot ^ mparams.iMagic))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   903
	#define SET_INUSE(M,p,s)\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   904
		((p)->iHead = (((p)->iHead & PINUSE_BIT)|s|CINUSE_BIT),\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   905
		(((mchunkptr)(((TUint8*)(p)) + (s)))->iHead |= PINUSE_BIT), \
a5496987b1da 201025_04
hgs
parents:
diff changeset
   906
		MARK_INUSE_FOOT(M,p,s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   907
	#define SET_INUSE_AND_PINUSE(M,p,s)\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   908
	((p)->iHead = (s|PINUSE_BIT|CINUSE_BIT),\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   909
	(((mchunkptr)(((TUint8*)(p)) + (s)))->iHead |= PINUSE_BIT),\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   910
	MARK_INUSE_FOOT(M,p,s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   911
	#define SET_SIZE_AND_PINUSE_OF_INUSE_CHUNK(M, p, s)\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   912
	((p)->iHead = (s|PINUSE_BIT|CINUSE_BIT),\
a5496987b1da 201025_04
hgs
parents:
diff changeset
   913
	MARK_INUSE_FOOT(M, p, s))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   914
#endif /* !FOOTERS */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   915
a5496987b1da 201025_04
hgs
parents:
diff changeset
   916
a5496987b1da 201025_04
hgs
parents:
diff changeset
   917
#if ONLY_MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
   918
#define INTERNAL_MALLOC(m, b) mspace_malloc(m, b)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   919
#define INTERNAL_FREE(m, mem) mspace_free(m,mem);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   920
#else /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   921
	#if MSPACES
a5496987b1da 201025_04
hgs
parents:
diff changeset
   922
		#define INTERNAL_MALLOC(m, b) (m == GM)? dlmalloc(b) : mspace_malloc(m, b)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   923
		#define INTERNAL_FREE(m, mem) if (m == GM) dlfree(mem); else mspace_free(m,mem);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   924
	#else /* MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   925
		#define INTERNAL_MALLOC(m, b) dlmalloc(b)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   926
		#define INTERNAL_FREE(m, mem) dlfree(mem)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   927
	#endif /* MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   928
#endif /* ONLY_MSPACES */
a5496987b1da 201025_04
hgs
parents:
diff changeset
   929
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   930
	#ifndef NDEBUG
a5496987b1da 201025_04
hgs
parents:
diff changeset
   931
	#define CHECKING 1
a5496987b1da 201025_04
hgs
parents:
diff changeset
   932
	#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   933
//  #define HYSTERESIS 4
a5496987b1da 201025_04
hgs
parents:
diff changeset
   934
    #define HYSTERESIS 1	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   935
	#define HYSTERESIS_BYTES (2*PAGESIZE)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   936
    #define HYSTERESIS_GROW (HYSTERESIS*PAGESIZE)	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   937
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   938
	#if CHECKING
a5496987b1da 201025_04
hgs
parents:
diff changeset
   939
	#define CHECK(x) x
a5496987b1da 201025_04
hgs
parents:
diff changeset
   940
	#else
a5496987b1da 201025_04
hgs
parents:
diff changeset
   941
	#undef ASSERT
a5496987b1da 201025_04
hgs
parents:
diff changeset
   942
	#define ASSERT(x) (void)0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   943
	#define CHECK(x) (void)0
a5496987b1da 201025_04
hgs
parents:
diff changeset
   944
	#endif
a5496987b1da 201025_04
hgs
parents:
diff changeset
   945
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   946
#endif/*__DLA__*/