blob: 82b09eba0621e7df79e7108108e20c1efc011b92 [file] [log] [blame]
Jari Aaltof73dda02001-11-13 17:56:06 +00001/* imalloc.h -- internal malloc definitions shared by source files. */
2
Jari Aaltob80f6442004-07-27 13:29:18 +00003/* Copyright (C) 2001-2003 Free Software Foundation, Inc.
Jari Aaltof73dda02001-11-13 17:56:06 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aaltof73dda02001-11-13 17:56:06 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aaltof73dda02001-11-13 17:56:06 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltof73dda02001-11-13 17:56:06 +000020
21/* Must be included *after* config.h */
22
Jari Aalto7117c2d2002-07-17 14:10:11 +000023#ifndef _IMALLOC_H
Jari Aaltof73dda02001-11-13 17:56:06 +000024#define _IMALLOC_H
25
26#ifdef MALLOC_DEBUG
27#define MALLOC_STATS
28#define MALLOC_TRACE
29#define MALLOC_REGISTER
Jari Aalto7117c2d2002-07-17 14:10:11 +000030#define MALLOC_WATCH
Jari Aaltof73dda02001-11-13 17:56:06 +000031#endif
32
Jari Aalto7117c2d2002-07-17 14:10:11 +000033#define MALLOC_WRAPFUNCS
34
Jari Aaltof73dda02001-11-13 17:56:06 +000035/* Generic pointer type. */
36#ifndef PTR_T
37# if defined (__STDC__)
38# define PTR_T void *
39# else
40# define PTR_T char *
41# endif
42#endif
43
44#if !defined (NULL)
45# define NULL 0
46#endif
47
Chet Rameyac50fba2014-02-26 09:36:43 -050048#if !defined (CPP_STRING)
Jari Aaltof73dda02001-11-13 17:56:06 +000049# if defined (HAVE_STRINGIZE)
Chet Rameyac50fba2014-02-26 09:36:43 -050050# define CPP_STRING(x) #x
Jari Aaltof73dda02001-11-13 17:56:06 +000051# else
Chet Rameyac50fba2014-02-26 09:36:43 -050052# define CPP_STRING(x) "x"
Jari Aaltof73dda02001-11-13 17:56:06 +000053# endif /* !HAVE_STRINGIZE */
54#endif /* !__STRING */
55
56#if __GNUC__ > 1
57# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
58#else /* !__GNUC__ */
59# if !defined (HAVE_BCOPY)
60# if !defined (HAVE_MEMMOVE)
61# define FASTCOPY(s, d, n) memcpy (d, s, n)
62# else
63# define FASTCOPY(s, d, n) memmove (d, s, n)
64# endif /* !HAVE_MEMMOVE */
65# else /* HAVE_BCOPY */
66# define FASTCOPY(s, d, n) bcopy (s, d, n)
67# endif /* HAVE_BCOPY */
68#endif /* !__GNUC__ */
69
Jari Aalto7117c2d2002-07-17 14:10:11 +000070#if !defined (__P)
71# if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus) || defined (PROTOTYPES)
72# define __P(protos) protos
73# else
74# define __P(protos) ()
75# endif
Jari Aaltof73dda02001-11-13 17:56:06 +000076#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +000077
78/* Use Duff's device for good zeroing/copying performance. DO NOT call the
79 Duff's device macros with NBYTES == 0. */
80
81#define MALLOC_BZERO(charp, nbytes) \
82do { \
83 if ((nbytes) <= 32) { \
84 size_t * mzp = (size_t *)(charp); \
85 unsigned long mctmp = (nbytes)/sizeof(size_t); \
86 long mcn; \
87 if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \
88 switch (mctmp) { \
89 case 0: for(;;) { *mzp++ = 0; \
90 case 7: *mzp++ = 0; \
91 case 6: *mzp++ = 0; \
92 case 5: *mzp++ = 0; \
93 case 4: *mzp++ = 0; \
94 case 3: *mzp++ = 0; \
95 case 2: *mzp++ = 0; \
96 case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
97 } \
98 else \
99 memset ((charp), 0, (nbytes)); \
100} while(0)
101
102#define MALLOC_ZERO(charp, nbytes) \
103do { \
104 size_t mzsz = (nbytes); \
105 if (mzsz <= 9 * sizeof(mzsz) { \
106 size_t *mz = (size_t *)(charp); \
107 if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
108 *mz++ = 0; \
109 if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
110 *mz++ = 0; \
111 if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
112 *mz++ = 0; }}} \
113 *mz++ = 0; \
114 *mz++ = 0; \
115 *mz = 0; \
116 } else \
117 memset ((charp), 0, mzsz); \
118} while (0)
119
120#define MALLOC_MEMSET(charp, xch, nbytes) \
121do { \
122 if ((nbytes) <= 32) { \
123 register char * mzp = (charp); \
124 unsigned long mctmp = (nbytes); \
125 register long mcn; \
126 if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \
127 switch (mctmp) { \
128 case 0: for(;;) { *mzp++ = xch; \
129 case 7: *mzp++ = xch; \
130 case 6: *mzp++ = xch; \
131 case 5: *mzp++ = xch; \
132 case 4: *mzp++ = xch; \
133 case 3: *mzp++ = xch; \
134 case 2: *mzp++ = xch; \
135 case 1: *mzp++ = xch; if(mcn <= 0) break; mcn--; } \
136 } \
137 } else \
138 memset ((charp), (xch), (nbytes)); \
139} while(0)
140
141#define MALLOC_MEMCPY(dest,src,nbytes) \
142do { \
143 if ((nbytes) <= 32) { \
144 size_t* mcsrc = (size_t*) src; \
145 size_t* mcdst = (size_t*) dest; \
146 unsigned long mctmp = (nbytes)/sizeof(size_t); \
147 long mcn; \
148 if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \
149 switch (mctmp) { \
150 case 0: for(;;) { *mcdst++ = *mcsrc++; \
151 case 7: *mcdst++ = *mcsrc++; \
152 case 6: *mcdst++ = *mcsrc++; \
153 case 5: *mcdst++ = *mcsrc++; \
154 case 4: *mcdst++ = *mcsrc++; \
155 case 3: *mcdst++ = *mcsrc++; \
156 case 2: *mcdst++ = *mcsrc++; \
157 case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
158 } else \
159 memcpy ((dest), (src), (nbytes)) \
160} while(0)
161
Jari Aaltob80f6442004-07-27 13:29:18 +0000162#if defined (SHELL)
163# include "bashintl.h"
164#else
165# define _(x) x
166#endif
167
Chet Rameyac50fba2014-02-26 09:36:43 -0500168#include <signal.h>
169
170extern void _malloc_block_signals __P((sigset_t *, sigset_t *));
171extern void _malloc_unblock_signals __P((sigset_t *, sigset_t *));
172
Jari Aalto7117c2d2002-07-17 14:10:11 +0000173#endif /* _IMALLOC_H */