1/*---------------------------------------------------------------------------*
2 *  pmalloc.h  *
3 *                                                                           *
4 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5 *                                                                           *
6 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7 *  you may not use this file except in compliance with the License.         *
8 *                                                                           *
9 *  You may obtain a copy of the License at                                  *
10 *      http://www.apache.org/licenses/LICENSE-2.0                           *
11 *                                                                           *
12 *  Unless required by applicable law or agreed to in writing, software      *
13 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 *  See the License for the specific language governing permissions and      *
16 *  limitations under the License.                                           *
17 *                                                                           *
18 *---------------------------------------------------------------------------*/
19
20
21
22/* xalloc.h internal header */
23#ifndef _PMALLOC
24#define _PMALLOC
25
26#ifdef __cplusplus
27extern "C"
28{
29#endif
30
31#ifndef _STD
32#define _STD
33#endif
34#define _STD_BEGIN
35#define _STD_END
36
37  typedef unsigned int psize_t;
38
39#ifndef DKoffsetof
40#define DKoffsetof(T, member) ((_STD psize_t)&(((T *)0)->member))
41#endif
42
43  /* storage alignment properties */
44#define DK_AUPBND 1U /* even-byte boundaries (2^^1) */
45#define DK_ADNBND 1U
46#define DK_MEMBND 2U /* cts : 4 byte malloc boundaries (3 ==> 8 byte) */
47
48#ifndef NULL
49#define NULL 0
50#endif
51
52  _STD_BEGIN
53  /* macros */
54#define M_MASK ((1 << DK_MEMBND) - 1) /* rounds all sizes */
55#define CELL_OFF ((DKoffsetof(_Cell, _Next) + M_MASK) & ~M_MASK)
56#define SIZE_BLOCK   256   /* minimum block size, power of 2 */
57#define SIZE_CELL ((sizeof (_Cell) + M_MASK) & ~M_MASK)
58  /* type definitions */
59  typedef struct _Cell
60  {
61    psize_t _Size; /* CELL_OFF <= SIZE_CELL <= _Size */
62    struct _Cell *_Next; /* reused if CELL_OFF < SIZE_CELL */
63  }
64  _Cell;
65  typedef struct
66  {
67    _Cell **_Plast; /* null, or where to resume malloc scan */
68    _Cell *_Head; /* null, or lowest addressed free cell */
69  }
70  _Altab;
71  /* declarations */
72
73  void *_Getmem(psize_t);
74  extern _Altab _Aldata; /* free list initially empty */
75
76#if _INTELx86
77  /* #define _PTR_NORM(p) (void __huge *)(p) should have worked */
78#define _PTR_NORM(p) \
79  ( (((unsigned long)(p) & 0xFFFF0000L)>>12) \
80    + ((unsigned long)(p) & 0xFFFFL) )
81#else
82#define _PTR_NORM(p) (p)
83#endif
84
85
86#if DEBUG
87  int _OK_Cell(_Cell *p);
88  int _OK_Altab(_Altab *p);
89  void _UPD_Altab(psize_t d_heap, psize_t d_alloc, psize_t d_free);
90#else
91#define _OK_Cell(p) (void)0
92#define _OK_Altab(p) (void)0
93#define _UPD_Altab(d_heap, d_alloc, d_free) (void)0
94#endif /*DEBUG*/
95  _STD_END
96
97  /* function prototypes */
98  void    PortMallocSetPoolSize(psize_t size);
99  psize_t PortMallocGetPoolSize(void);
100  int     PortMallocGetMaxMemUsed(void);
101  void    PortMallocInit(void);
102  void    PortMallocTerm(void);
103  void *(PortMalloc)(psize_t size_arg);
104  void(PortFree)(void *ptr);
105
106#ifdef __cplusplus
107} /* end extern "C" */
108#endif
109
110#endif /* _PMALLOC */
111
112
113
114