Commit | Line | Data |
a0d0e21e |
1 | /* malloc.c |
8d063cd8 |
2 | * |
8d063cd8 |
3 | */ |
4 | |
87c6202a |
5 | /* |
741df71a |
6 | Here are some notes on configuring Perl's malloc. (For non-perl |
7 | usage see below.) |
87c6202a |
8 | |
9 | There are two macros which serve as bulk disablers of advanced |
10 | features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by |
11 | default). Look in the list of default values below to understand |
12 | their exact effect. Defining NO_FANCY_MALLOC returns malloc.c to the |
13 | state of the malloc in Perl 5.004. Additionally defining PLAIN_MALLOC |
14 | returns it to the state as of Perl 5.000. |
15 | |
16 | Note that some of the settings below may be ignored in the code based |
17 | on values of other macros. The PERL_CORE symbol is only defined when |
18 | perl itself is being compiled (so malloc can make some assumptions |
19 | about perl's facilities being available to it). |
20 | |
21 | Each config option has a short description, followed by its name, |
22 | default value, and a comment about the default (if applicable). Some |
23 | options take a precise value, while the others are just boolean. |
24 | The boolean ones are listed first. |
25 | |
26 | # Enable code for an emergency memory pool in $^M. See perlvar.pod |
27 | # for a description of $^M. |
28 | PERL_EMERGENCY_SBRK (!PLAIN_MALLOC && PERL_CORE) |
29 | |
30 | # Enable code for printing memory statistics. |
31 | DEBUGGING_MSTATS (!PLAIN_MALLOC && PERL_CORE) |
32 | |
33 | # Move allocation info for small buckets into separate areas. |
34 | # Memory optimization (especially for small allocations, of the |
35 | # less than 64 bytes). Since perl usually makes a large number |
36 | # of small allocations, this is usually a win. |
37 | PACK_MALLOC (!PLAIN_MALLOC && !RCHECK) |
38 | |
39 | # Add one page to big powers of two when calculating bucket size. |
40 | # This is targeted at big allocations, as are common in image |
41 | # processing. |
42 | TWO_POT_OPTIMIZE !PLAIN_MALLOC |
43 | |
44 | # Use intermediate bucket sizes between powers-of-two. This is |
45 | # generally a memory optimization, and a (small) speed pessimization. |
46 | BUCKETS_ROOT2 !NO_FANCY_MALLOC |
47 | |
48 | # Do not check small deallocations for bad free(). Memory |
49 | # and speed optimization, error reporting pessimization. |
50 | IGNORE_SMALL_BAD_FREE (!NO_FANCY_MALLOC && !RCHECK) |
51 | |
52 | # Use table lookup to decide in which bucket a given allocation will go. |
53 | SMALL_BUCKET_VIA_TABLE !NO_FANCY_MALLOC |
54 | |
38ac2dc8 |
55 | # Use a perl-defined sbrk() instead of the (presumably broken or |
56 | # missing) system-supplied sbrk(). |
57 | USE_PERL_SBRK undef |
58 | |
59 | # Use system malloc() (or calloc() etc.) to emulate sbrk(). Normally |
60 | # only used with broken sbrk()s. |
87c6202a |
61 | PERL_SBRK_VIA_MALLOC undef |
62 | |
38ac2dc8 |
63 | # Which allocator to use if PERL_SBRK_VIA_MALLOC |
64 | SYSTEM_ALLOC(a) malloc(a) |
65 | |
9ee81ef6 |
66 | # Minimal alignment (in bytes, should be a power of 2) of SYSTEM_ALLOC |
5bbd1ef5 |
67 | SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES |
68 | |
87c6202a |
69 | # Disable memory overwrite checking with DEBUGGING. Memory and speed |
70 | # optimization, error reporting pessimization. |
71 | NO_RCHECK undef |
72 | |
73 | # Enable memory overwrite checking with DEBUGGING. Memory and speed |
74 | # pessimization, error reporting optimization |
75 | RCHECK (DEBUGGING && !NO_RCHECK) |
76 | |
77 | # Failed allocations bigger than this size croak (if |
78 | # PERL_EMERGENCY_SBRK is enabled) without touching $^M. See |
79 | # perlvar.pod for a description of $^M. |
80 | BIG_SIZE (1<<16) # 64K |
81 | |
82 | # Starting from this power of two, add an extra page to the |
83 | # size of the bucket. This enables optimized allocations of sizes |
84 | # close to powers of 2. Note that the value is indexed at 0. |
85 | FIRST_BIG_POW2 15 # 32K, 16K is used too often |
86 | |
87 | # Estimate of minimal memory footprint. malloc uses this value to |
88 | # request the most reasonable largest blocks of memory from the system. |
89 | FIRST_SBRK (48*1024) |
90 | |
91 | # Round up sbrk()s to multiples of this. |
92 | MIN_SBRK 2048 |
93 | |
94 | # Round up sbrk()s to multiples of this percent of footprint. |
95 | MIN_SBRK_FRAC 3 |
96 | |
97 | # Add this much memory to big powers of two to get the bucket size. |
98 | PERL_PAGESIZE 4096 |
99 | |
100 | # This many sbrk() discontinuities should be tolerated even |
101 | # from the start without deciding that sbrk() is usually |
102 | # discontinuous. |
103 | SBRK_ALLOW_FAILURES 3 |
104 | |
105 | # This many continuous sbrk()s compensate for one discontinuous one. |
106 | SBRK_FAILURE_PRICE 50 |
107 | |
28ac10b1 |
108 | # Some configurations may ask for 12-byte-or-so allocations which |
109 | # require 8-byte alignment (?!). In such situation one needs to |
110 | # define this to disable 12-byte bucket (will increase memory footprint) |
111 | STRICT_ALIGNMENT undef |
112 | |
87c6202a |
113 | This implementation assumes that calling PerlIO_printf() does not |
114 | result in any memory allocation calls (used during a panic). |
115 | |
116 | */ |
117 | |
741df71a |
118 | /* |
119 | If used outside of Perl environment, it may be useful to redefine |
120 | the following macros (listed below with defaults): |
121 | |
122 | # Type of address returned by allocation functions |
123 | Malloc_t void * |
124 | |
125 | # Type of size argument for allocation functions |
126 | MEM_SIZE unsigned long |
127 | |
128 | # Maximal value in LONG |
129 | LONG_MAX 0x7FFFFFFF |
130 | |
131 | # Unsigned integer type big enough to keep a pointer |
132 | UV unsigned long |
133 | |
134 | # Type of pointer with 1-byte granularity |
135 | caddr_t char * |
136 | |
137 | # Type returned by free() |
138 | Free_t void |
139 | |
5bbd1ef5 |
140 | # Very fatal condition reporting function (cannot call any ) |
141 | fatalcroak(arg) write(2,arg,strlen(arg)) + exit(2) |
142 | |
741df71a |
143 | # Fatal error reporting function |
144 | croak(format, arg) warn(idem) + exit(1) |
145 | |
146 | # Error reporting function |
147 | warn(format, arg) fprintf(stderr, idem) |
148 | |
149 | # Locking/unlocking for MT operation |
cea2e8a9 |
150 | MALLOC_LOCK MUTEX_LOCK_NOCONTEXT(&PL_malloc_mutex) |
151 | MALLOC_UNLOCK MUTEX_UNLOCK_NOCONTEXT(&PL_malloc_mutex) |
741df71a |
152 | |
153 | # Locking/unlocking mutex for MT operation |
154 | MUTEX_LOCK(l) void |
155 | MUTEX_UNLOCK(l) void |
156 | */ |
157 | |
e8bc2b5c |
158 | #ifndef NO_FANCY_MALLOC |
159 | # ifndef SMALL_BUCKET_VIA_TABLE |
160 | # define SMALL_BUCKET_VIA_TABLE |
161 | # endif |
162 | # ifndef BUCKETS_ROOT2 |
163 | # define BUCKETS_ROOT2 |
164 | # endif |
165 | # ifndef IGNORE_SMALL_BAD_FREE |
166 | # define IGNORE_SMALL_BAD_FREE |
167 | # endif |
3562ef9b |
168 | #endif |
169 | |
e8bc2b5c |
170 | #ifndef PLAIN_MALLOC /* Bulk enable features */ |
171 | # ifndef PACK_MALLOC |
172 | # define PACK_MALLOC |
173 | # endif |
174 | # ifndef TWO_POT_OPTIMIZE |
175 | # define TWO_POT_OPTIMIZE |
176 | # endif |
d720c441 |
177 | # if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK) |
178 | # define PERL_EMERGENCY_SBRK |
e8bc2b5c |
179 | # endif |
180 | # if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS) |
181 | # define DEBUGGING_MSTATS |
182 | # endif |
183 | #endif |
184 | |
185 | #define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */ |
186 | #define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2) |
187 | |
61ae2fbf |
188 | #if !(defined(I286) || defined(atarist) || defined(__MINT__)) |
e8bc2b5c |
189 | /* take 2k unless the block is bigger than that */ |
190 | # define LOG_OF_MIN_ARENA 11 |
191 | #else |
192 | /* take 16k unless the block is bigger than that |
193 | (80286s like large segments!), probably good on the atari too */ |
194 | # define LOG_OF_MIN_ARENA 14 |
195 | #endif |
196 | |
8d063cd8 |
197 | #ifndef lint |
1944739a |
198 | # if defined(DEBUGGING) && !defined(NO_RCHECK) |
199 | # define RCHECK |
200 | # endif |
e8bc2b5c |
201 | # if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE) |
202 | # undef IGNORE_SMALL_BAD_FREE |
203 | # endif |
8d063cd8 |
204 | /* |
205 | * malloc.c (Caltech) 2/21/82 |
206 | * Chris Kingsley, kingsley@cit-20. |
207 | * |
208 | * This is a very fast storage allocator. It allocates blocks of a small |
209 | * number of different sizes, and keeps free lists of each size. Blocks that |
210 | * don't exactly fit are passed up to the next larger size. In this |
211 | * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. |
cf5c4ad8 |
212 | * If PACK_MALLOC is defined, small blocks are 2^n bytes long. |
8d063cd8 |
213 | * This is designed for use in a program that uses vast quantities of memory, |
741df71a |
214 | * but bombs when it runs out. |
215 | * |
4eb8286e |
216 | * Modifications Copyright Ilya Zakharevich 1996-99. |
741df71a |
217 | * |
218 | * Still very quick, but much more thrifty. (Std config is 10% slower |
219 | * than it was, and takes 67% of old heap size for typical usage.) |
220 | * |
221 | * Allocations of small blocks are now table-driven to many different |
222 | * buckets. Sizes of really big buckets are increased to accomodata |
223 | * common size=power-of-2 blocks. Running-out-of-memory is made into |
224 | * an exception. Deeply configurable and thread-safe. |
225 | * |
8d063cd8 |
226 | */ |
227 | |
d720c441 |
228 | #ifdef PERL_CORE |
229 | # include "EXTERN.h" |
864dbfa3 |
230 | #define PERL_IN_MALLOC_C |
d720c441 |
231 | # include "perl.h" |
cea2e8a9 |
232 | # if defined(PERL_IMPLICIT_CONTEXT) |
233 | # define croak Perl_croak_nocontext |
234 | # define warn Perl_warn_nocontext |
235 | # endif |
d720c441 |
236 | #else |
237 | # ifdef PERL_FOR_X2P |
238 | # include "../EXTERN.h" |
239 | # include "../perl.h" |
240 | # else |
241 | # include <stdlib.h> |
242 | # include <stdio.h> |
243 | # include <memory.h> |
244 | # define _(arg) arg |
245 | # ifndef Malloc_t |
246 | # define Malloc_t void * |
247 | # endif |
248 | # ifndef MEM_SIZE |
249 | # define MEM_SIZE unsigned long |
250 | # endif |
251 | # ifndef LONG_MAX |
252 | # define LONG_MAX 0x7FFFFFFF |
253 | # endif |
254 | # ifndef UV |
255 | # define UV unsigned long |
256 | # endif |
257 | # ifndef caddr_t |
258 | # define caddr_t char * |
259 | # endif |
260 | # ifndef Free_t |
261 | # define Free_t void |
262 | # endif |
263 | # define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t)) |
264 | # define PerlEnv_getenv getenv |
265 | # define PerlIO_printf fprintf |
266 | # define PerlIO_stderr() stderr |
267 | # endif |
e8bc2b5c |
268 | # ifndef croak /* make depend */ |
741df71a |
269 | # define croak(mess, arg) (warn((mess), (arg)), exit(1)) |
d720c441 |
270 | # endif |
271 | # ifndef warn |
741df71a |
272 | # define warn(mess, arg) fprintf(stderr, (mess), (arg)) |
e8bc2b5c |
273 | # endif |
274 | # ifdef DEBUG_m |
275 | # undef DEBUG_m |
276 | # endif |
277 | # define DEBUG_m(a) |
278 | # ifdef DEBUGGING |
279 | # undef DEBUGGING |
280 | # endif |
cea2e8a9 |
281 | # ifndef pTHX |
282 | # define pTHX void |
283 | # define pTHX_ |
284 | # define dTHX extern int Perl___notused |
285 | # define WITH_THX(s) s |
286 | # endif |
c5be433b |
287 | # ifndef PERL_GET_INTERP |
288 | # define PERL_GET_INTERP PL_curinterp |
289 | # endif |
e8bc2b5c |
290 | #endif |
291 | |
292 | #ifndef MUTEX_LOCK |
293 | # define MUTEX_LOCK(l) |
294 | #endif |
295 | |
296 | #ifndef MUTEX_UNLOCK |
297 | # define MUTEX_UNLOCK(l) |
298 | #endif |
299 | |
741df71a |
300 | #ifndef MALLOC_LOCK |
cea2e8a9 |
301 | # define MALLOC_LOCK MUTEX_LOCK_NOCONTEXT(&PL_malloc_mutex) |
741df71a |
302 | #endif |
303 | |
304 | #ifndef MALLOC_UNLOCK |
cea2e8a9 |
305 | # define MALLOC_UNLOCK MUTEX_UNLOCK_NOCONTEXT(&PL_malloc_mutex) |
741df71a |
306 | #endif |
307 | |
5bbd1ef5 |
308 | # ifndef fatalcroak /* make depend */ |
309 | # define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2)) |
310 | # endif |
311 | |
760ac839 |
312 | #ifdef DEBUGGING |
e8bc2b5c |
313 | # undef DEBUG_m |
c5be433b |
314 | # define DEBUG_m(a) if (PERL_GET_INTERP && PL_debug & 128) a |
760ac839 |
315 | #endif |
316 | |
e9397286 |
317 | /* |
318 | * Layout of memory: |
319 | * ~~~~~~~~~~~~~~~~ |
320 | * The memory is broken into "blocks" which occupy multiples of 2K (and |
321 | * generally speaking, have size "close" to a power of 2). The addresses |
322 | * of such *unused* blocks are kept in nextf[i] with big enough i. (nextf |
323 | * is an array of linked lists.) (Addresses of used blocks are not known.) |
324 | * |
325 | * Moreover, since the algorithm may try to "bite" smaller blocks of out |
326 | * of unused bigger ones, there are also regions of "irregular" size, |
327 | * managed separately, by a linked list chunk_chain. |
328 | * |
329 | * The third type of storage is the sbrk()ed-but-not-yet-used space, its |
330 | * end and size are kept in last_sbrk_top and sbrked_remains. |
331 | * |
332 | * Growing blocks "in place": |
333 | * ~~~~~~~~~~~~~~~~~~~~~~~~~ |
334 | * The address of the block with the greatest address is kept in last_op |
335 | * (if not known, last_op is 0). If it is known that the memory above |
336 | * last_op is not continuous, or contains a chunk from chunk_chain, |
337 | * last_op is set to 0. |
338 | * |
339 | * The chunk with address last_op may be grown by expanding into |
340 | * sbrk()ed-but-not-yet-used space, or trying to sbrk() more continuous |
341 | * memory. |
342 | * |
343 | * Management of last_op: |
344 | * ~~~~~~~~~~~~~~~~~~~~~ |
345 | * |
346 | * free() never changes the boundaries of blocks, so is not relevant. |
347 | * |
348 | * The only way realloc() may change the boundaries of blocks is if it |
349 | * grows a block "in place". However, in the case of success such a |
350 | * chunk is automatically last_op, and it remains last_op. In the case |
351 | * of failure getpages_adjacent() clears last_op. |
352 | * |
353 | * malloc() may change blocks by calling morecore() only. |
354 | * |
355 | * morecore() may create new blocks by: |
356 | * a) biting pieces from chunk_chain (cannot create one above last_op); |
357 | * b) biting a piece from an unused block (if block was last_op, this |
358 | * may create a chunk from chain above last_op, thus last_op is |
359 | * invalidated in such a case). |
360 | * c) biting of sbrk()ed-but-not-yet-used space. This creates |
361 | * a block which is last_op. |
362 | * d) Allocating new pages by calling getpages(); |
363 | * |
364 | * getpages() creates a new block. It marks last_op at the bottom of |
365 | * the chunk of memory it returns. |
366 | * |
367 | * Active pages footprint: |
368 | * ~~~~~~~~~~~~~~~~~~~~~~ |
369 | * Note that we do not need to traverse the lists in nextf[i], just take |
370 | * the first element of this list. However, we *need* to traverse the |
371 | * list in chunk_chain, but most the time it should be a very short one, |
372 | * so we do not step on a lot of pages we are not going to use. |
373 | * |
374 | * Flaws: |
375 | * ~~~~~ |
376 | * get_from_bigger_buckets(): forget to increment price => Quite |
377 | * aggressive. |
378 | */ |
379 | |
135863df |
380 | /* I don't much care whether these are defined in sys/types.h--LAW */ |
381 | |
382 | #define u_char unsigned char |
383 | #define u_int unsigned int |
e8bc2b5c |
384 | |
385 | #ifdef HAS_QUAD |
386 | # define u_bigint UV /* Needs to eat *void. */ |
387 | #else /* needed? */ |
388 | # define u_bigint unsigned long /* Needs to eat *void. */ |
389 | #endif |
390 | |
135863df |
391 | #define u_short unsigned short |
8d063cd8 |
392 | |
cf5c4ad8 |
393 | /* 286 and atarist like big chunks, which gives too much overhead. */ |
61ae2fbf |
394 | #if (defined(RCHECK) || defined(I286) || defined(atarist) || defined(__MINT__)) && defined(PACK_MALLOC) |
e8bc2b5c |
395 | # undef PACK_MALLOC |
cf5c4ad8 |
396 | #endif |
397 | |
8d063cd8 |
398 | /* |
cf5c4ad8 |
399 | * The description below is applicable if PACK_MALLOC is not defined. |
400 | * |
8d063cd8 |
401 | * The overhead on a block is at least 4 bytes. When free, this space |
402 | * contains a pointer to the next free block, and the bottom two bits must |
403 | * be zero. When in use, the first byte is set to MAGIC, and the second |
404 | * byte is the size index. The remaining bytes are for alignment. |
405 | * If range checking is enabled and the size of the block fits |
406 | * in two bytes, then the top two bytes hold the size of the requested block |
407 | * plus the range checking words, and the header word MINUS ONE. |
408 | */ |
409 | union overhead { |
410 | union overhead *ov_next; /* when free */ |
85e6fe83 |
411 | #if MEM_ALIGNBYTES > 4 |
c623bd54 |
412 | double strut; /* alignment problems */ |
a687059c |
413 | #endif |
8d063cd8 |
414 | struct { |
415 | u_char ovu_magic; /* magic number */ |
416 | u_char ovu_index; /* bucket # */ |
417 | #ifdef RCHECK |
418 | u_short ovu_size; /* actual block size */ |
419 | u_int ovu_rmagic; /* range magic number */ |
420 | #endif |
421 | } ovu; |
422 | #define ov_magic ovu.ovu_magic |
423 | #define ov_index ovu.ovu_index |
424 | #define ov_size ovu.ovu_size |
425 | #define ov_rmagic ovu.ovu_rmagic |
426 | }; |
427 | |
428 | #define MAGIC 0xff /* magic # on accounting info */ |
429 | #define RMAGIC 0x55555555 /* magic # on range info */ |
e8bc2b5c |
430 | #define RMAGIC_C 0x55 /* magic # on range info */ |
431 | |
8d063cd8 |
432 | #ifdef RCHECK |
c2a5c2d2 |
433 | # define RSLOP sizeof (u_int) |
434 | # ifdef TWO_POT_OPTIMIZE |
e8bc2b5c |
435 | # define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2) |
c2a5c2d2 |
436 | # else |
e8bc2b5c |
437 | # define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2) |
c2a5c2d2 |
438 | # endif |
8d063cd8 |
439 | #else |
c2a5c2d2 |
440 | # define RSLOP 0 |
8d063cd8 |
441 | #endif |
442 | |
e8bc2b5c |
443 | #if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2) |
444 | # undef BUCKETS_ROOT2 |
445 | #endif |
446 | |
447 | #ifdef BUCKETS_ROOT2 |
448 | # define BUCKET_TABLE_SHIFT 2 |
449 | # define BUCKET_POW2_SHIFT 1 |
450 | # define BUCKETS_PER_POW2 2 |
451 | #else |
452 | # define BUCKET_TABLE_SHIFT MIN_BUC_POW2 |
453 | # define BUCKET_POW2_SHIFT 0 |
454 | # define BUCKETS_PER_POW2 1 |
455 | #endif |
456 | |
274c7500 |
457 | #if !defined(MEM_ALIGNBYTES) || ((MEM_ALIGNBYTES > 4) && !defined(STRICT_ALIGNMENT)) |
458 | /* Figure out the alignment of void*. */ |
459 | struct aligner { |
460 | char c; |
461 | void *p; |
462 | }; |
463 | # define ALIGN_SMALL ((int)((caddr_t)&(((struct aligner*)0)->p))) |
464 | #else |
465 | # define ALIGN_SMALL MEM_ALIGNBYTES |
466 | #endif |
467 | |
468 | #define IF_ALIGN_8(yes,no) ((ALIGN_SMALL>4) ? (yes) : (no)) |
469 | |
e8bc2b5c |
470 | #ifdef BUCKETS_ROOT2 |
471 | # define MAX_BUCKET_BY_TABLE 13 |
472 | static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] = |
473 | { |
474 | 0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80, |
475 | }; |
476 | # define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT))) |
477 | # define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE \ |
478 | ? buck_size[i] \ |
479 | : ((1 << ((i) >> BUCKET_POW2_SHIFT)) \ |
480 | - MEM_OVERHEAD(i) \ |
481 | + POW2_OPTIMIZE_SURPLUS(i))) |
482 | #else |
483 | # define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT)) |
484 | # define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i)) |
485 | #endif |
486 | |
487 | |
cf5c4ad8 |
488 | #ifdef PACK_MALLOC |
e8bc2b5c |
489 | /* In this case it is assumed that if we do sbrk() in 2K units, we |
490 | * will get 2K aligned arenas (at least after some initial |
491 | * alignment). The bucket number of the given subblock is on the start |
492 | * of 2K arena which contains the subblock. Several following bytes |
493 | * contain the magic numbers for the subblocks in the block. |
cf5c4ad8 |
494 | * |
495 | * Sizes of chunks are powers of 2 for chunks in buckets <= |
496 | * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to |
497 | * get alignment right). |
498 | * |
e8bc2b5c |
499 | * Consider an arena for 2^n with n>MAX_PACKED. We suppose that |
500 | * starts of all the chunks in a 2K arena are in different |
501 | * 2^n-byte-long chunks. If the top of the last chunk is aligned on a |
502 | * boundary of 2K block, this means that sizeof(union |
503 | * overhead)*"number of chunks" < 2^n, or sizeof(union overhead)*2K < |
504 | * 4^n, or n > 6 + log2(sizeof()/2)/2, since a chunk of size 2^n - |
505 | * overhead is used. Since this rules out n = 7 for 8 byte alignment, |
506 | * we specialcase allocation of the first of 16 128-byte-long chunks. |
cf5c4ad8 |
507 | * |
508 | * Note that with the above assumption we automatically have enough |
509 | * place for MAGIC at the start of 2K block. Note also that we |
e8bc2b5c |
510 | * overlay union overhead over the chunk, thus the start of small chunks |
511 | * is immediately overwritten after freeing. */ |
512 | # define MAX_PACKED_POW2 6 |
513 | # define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT) |
514 | # define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD) |
515 | # define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1) |
516 | # define TWOK_MASKED(x) ((u_bigint)(x) & ~TWOK_MASK) |
517 | # define TWOK_SHIFT(x) ((u_bigint)(x) & TWOK_MASK) |
cf5c4ad8 |
518 | # define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block))) |
519 | # define OV_INDEX(block) (*OV_INDEXp(block)) |
520 | # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \ |
e8bc2b5c |
521 | (TWOK_SHIFT(block)>> \ |
522 | (bucket>>BUCKET_POW2_SHIFT)) + \ |
523 | (bucket >= MIN_NEEDS_SHIFT ? 1 : 0))) |
524 | /* A bucket can have a shift smaller than it size, we need to |
525 | shift its magic number so it will not overwrite index: */ |
526 | # ifdef BUCKETS_ROOT2 |
527 | # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */ |
528 | # else |
529 | # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */ |
530 | # endif |
cf5c4ad8 |
531 | # define CHUNK_SHIFT 0 |
532 | |
e8bc2b5c |
533 | /* Number of active buckets of given ordinal. */ |
534 | #ifdef IGNORE_SMALL_BAD_FREE |
535 | #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */ |
536 | # define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \ |
537 | ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \ |
538 | : n_blks[bucket] ) |
539 | #else |
540 | # define N_BLKS(bucket) n_blks[bucket] |
541 | #endif |
542 | |
543 | static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = |
544 | { |
545 | # if BUCKETS_PER_POW2==1 |
546 | 0, 0, |
547 | (MIN_BUC_POW2==2 ? 384 : 0), |
548 | 224, 120, 62, 31, 16, 8, 4, 2 |
549 | # else |
550 | 0, 0, 0, 0, |
551 | (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */ |
552 | 224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2 |
553 | # endif |
554 | }; |
555 | |
556 | /* Shift of the first bucket with the given ordinal inside 2K chunk. */ |
557 | #ifdef IGNORE_SMALL_BAD_FREE |
558 | # define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \ |
559 | ? ((1<<LOG_OF_MIN_ARENA) \ |
560 | - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \ |
561 | : blk_shift[bucket]) |
562 | #else |
563 | # define BLK_SHIFT(bucket) blk_shift[bucket] |
564 | #endif |
565 | |
566 | static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = |
567 | { |
568 | # if BUCKETS_PER_POW2==1 |
569 | 0, 0, |
570 | (MIN_BUC_POW2==2 ? 512 : 0), |
571 | 256, 128, 64, 64, /* 8 to 64 */ |
572 | 16*sizeof(union overhead), |
573 | 8*sizeof(union overhead), |
574 | 4*sizeof(union overhead), |
575 | 2*sizeof(union overhead), |
576 | # else |
577 | 0, 0, 0, 0, |
578 | (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0), |
579 | 256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */ |
580 | 16*sizeof(union overhead), 16*sizeof(union overhead), |
581 | 8*sizeof(union overhead), 8*sizeof(union overhead), |
582 | 4*sizeof(union overhead), 4*sizeof(union overhead), |
583 | 2*sizeof(union overhead), 2*sizeof(union overhead), |
584 | # endif |
585 | }; |
cf5c4ad8 |
586 | |
5bbd1ef5 |
587 | # define NEEDED_ALIGNMENT 0x800 /* 2k boundaries */ |
588 | # define WANTED_ALIGNMENT 0x800 /* 2k boundaries */ |
589 | |
cf5c4ad8 |
590 | #else /* !PACK_MALLOC */ |
591 | |
592 | # define OV_MAGIC(block,bucket) (block)->ov_magic |
593 | # define OV_INDEX(block) (block)->ov_index |
594 | # define CHUNK_SHIFT 1 |
e8bc2b5c |
595 | # define MAX_PACKED -1 |
5bbd1ef5 |
596 | # define NEEDED_ALIGNMENT MEM_ALIGNBYTES |
597 | # define WANTED_ALIGNMENT 0x400 /* 1k boundaries */ |
598 | |
cf5c4ad8 |
599 | #endif /* !PACK_MALLOC */ |
600 | |
e8bc2b5c |
601 | #define M_OVERHEAD (sizeof(union overhead) + RSLOP) |
602 | |
603 | #ifdef PACK_MALLOC |
604 | # define MEM_OVERHEAD(bucket) \ |
605 | (bucket <= MAX_PACKED ? 0 : M_OVERHEAD) |
606 | # ifdef SMALL_BUCKET_VIA_TABLE |
607 | # define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2) |
608 | # define START_SHIFT MAX_PACKED_POW2 |
609 | # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */ |
610 | # define SIZE_TABLE_MAX 80 |
611 | # else |
612 | # define SIZE_TABLE_MAX 64 |
613 | # endif |
614 | static char bucket_of[] = |
615 | { |
616 | # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */ |
617 | /* 0 to 15 in 4-byte increments. */ |
618 | (sizeof(void*) > 4 ? 6 : 5), /* 4/8, 5-th bucket for better reports */ |
619 | 6, /* 8 */ |
274c7500 |
620 | IF_ALIGN_8(8,7), 8, /* 16/12, 16 */ |
e8bc2b5c |
621 | 9, 9, 10, 10, /* 24, 32 */ |
622 | 11, 11, 11, 11, /* 48 */ |
623 | 12, 12, 12, 12, /* 64 */ |
624 | 13, 13, 13, 13, /* 80 */ |
625 | 13, 13, 13, 13 /* 80 */ |
626 | # else /* !BUCKETS_ROOT2 */ |
627 | /* 0 to 15 in 4-byte increments. */ |
628 | (sizeof(void*) > 4 ? 3 : 2), |
629 | 3, |
630 | 4, 4, |
631 | 5, 5, 5, 5, |
632 | 6, 6, 6, 6, |
633 | 6, 6, 6, 6 |
634 | # endif /* !BUCKETS_ROOT2 */ |
635 | }; |
636 | # else /* !SMALL_BUCKET_VIA_TABLE */ |
637 | # define START_SHIFTS_BUCKET MIN_BUCKET |
638 | # define START_SHIFT (MIN_BUC_POW2 - 1) |
639 | # endif /* !SMALL_BUCKET_VIA_TABLE */ |
640 | #else /* !PACK_MALLOC */ |
641 | # define MEM_OVERHEAD(bucket) M_OVERHEAD |
642 | # ifdef SMALL_BUCKET_VIA_TABLE |
643 | # undef SMALL_BUCKET_VIA_TABLE |
644 | # endif |
645 | # define START_SHIFTS_BUCKET MIN_BUCKET |
646 | # define START_SHIFT (MIN_BUC_POW2 - 1) |
647 | #endif /* !PACK_MALLOC */ |
cf5c4ad8 |
648 | |
8d063cd8 |
649 | /* |
55497cff |
650 | * Big allocations are often of the size 2^n bytes. To make them a |
651 | * little bit better, make blocks of size 2^n+pagesize for big n. |
652 | */ |
653 | |
654 | #ifdef TWO_POT_OPTIMIZE |
655 | |
5f05dabc |
656 | # ifndef PERL_PAGESIZE |
657 | # define PERL_PAGESIZE 4096 |
658 | # endif |
e8bc2b5c |
659 | # ifndef FIRST_BIG_POW2 |
660 | # define FIRST_BIG_POW2 15 /* 32K, 16K is used too often. */ |
5f05dabc |
661 | # endif |
e8bc2b5c |
662 | # define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2) |
55497cff |
663 | /* If this value or more, check against bigger blocks. */ |
664 | # define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD) |
665 | /* If less than this value, goes into 2^n-overhead-block. */ |
666 | # define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD) |
667 | |
e8bc2b5c |
668 | # define POW2_OPTIMIZE_ADJUST(nbytes) \ |
669 | ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0) |
670 | # define POW2_OPTIMIZE_SURPLUS(bucket) \ |
671 | ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0) |
672 | |
673 | #else /* !TWO_POT_OPTIMIZE */ |
674 | # define POW2_OPTIMIZE_ADJUST(nbytes) |
675 | # define POW2_OPTIMIZE_SURPLUS(bucket) 0 |
676 | #endif /* !TWO_POT_OPTIMIZE */ |
677 | |
678 | #if defined(HAS_64K_LIMIT) && defined(PERL_CORE) |
679 | # define BARK_64K_LIMIT(what,nbytes,size) \ |
680 | if (nbytes > 0xffff) { \ |
681 | PerlIO_printf(PerlIO_stderr(), \ |
682 | "%s too large: %lx\n", what, size); \ |
683 | my_exit(1); \ |
684 | } |
685 | #else /* !HAS_64K_LIMIT || !PERL_CORE */ |
686 | # define BARK_64K_LIMIT(what,nbytes,size) |
687 | #endif /* !HAS_64K_LIMIT || !PERL_CORE */ |
55497cff |
688 | |
e8bc2b5c |
689 | #ifndef MIN_SBRK |
690 | # define MIN_SBRK 2048 |
691 | #endif |
692 | |
693 | #ifndef FIRST_SBRK |
d720c441 |
694 | # define FIRST_SBRK (48*1024) |
e8bc2b5c |
695 | #endif |
696 | |
697 | /* Minimal sbrk in percents of what is already alloced. */ |
698 | #ifndef MIN_SBRK_FRAC |
699 | # define MIN_SBRK_FRAC 3 |
700 | #endif |
701 | |
702 | #ifndef SBRK_ALLOW_FAILURES |
703 | # define SBRK_ALLOW_FAILURES 3 |
704 | #endif |
55497cff |
705 | |
e8bc2b5c |
706 | #ifndef SBRK_FAILURE_PRICE |
707 | # define SBRK_FAILURE_PRICE 50 |
55497cff |
708 | #endif |
709 | |
e8bc2b5c |
710 | #if defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE) |
711 | |
712 | # ifndef BIG_SIZE |
713 | # define BIG_SIZE (1<<16) /* 64K */ |
714 | # endif |
715 | |
3541dd58 |
716 | #ifdef I_MACH_CTHREADS |
772fe5b3 |
717 | # undef MUTEX_LOCK |
718 | # define MUTEX_LOCK(m) STMT_START { if (*m) mutex_lock(*m); } STMT_END |
719 | # undef MUTEX_UNLOCK |
720 | # define MUTEX_UNLOCK(m) STMT_START { if (*m) mutex_unlock(*m); } STMT_END |
3541dd58 |
721 | #endif |
722 | |
55497cff |
723 | static char *emergency_buffer; |
724 | static MEM_SIZE emergency_buffer_size; |
725 | |
cea2e8a9 |
726 | static int findbucket (union overhead *freep, int srchlen); |
727 | static void morecore (register int bucket); |
728 | # if defined(DEBUGGING) |
729 | static void botch (char *diag, char *s); |
730 | # endif |
731 | static void add_to_chain (void *p, MEM_SIZE size, MEM_SIZE chip); |
732 | static Malloc_t emergency_sbrk (MEM_SIZE size); |
733 | static void* get_from_chain (MEM_SIZE size); |
734 | static void* get_from_bigger_buckets(int bucket, MEM_SIZE size); |
735 | static union overhead *getpages (int needed, int *nblksp, int bucket); |
736 | static int getpages_adjacent(int require); |
737 | |
738 | static Malloc_t |
739 | emergency_sbrk(MEM_SIZE size) |
55497cff |
740 | { |
28ac10b1 |
741 | MEM_SIZE rsize = (((size - 1)>>LOG_OF_MIN_ARENA) + 1)<<LOG_OF_MIN_ARENA; |
742 | |
55497cff |
743 | if (size >= BIG_SIZE) { |
744 | /* Give the possibility to recover: */ |
741df71a |
745 | MALLOC_UNLOCK; |
1b979e0a |
746 | croak("Out of memory during \"large\" request for %i bytes", size); |
55497cff |
747 | } |
748 | |
28ac10b1 |
749 | if (emergency_buffer_size >= rsize) { |
750 | char *old = emergency_buffer; |
751 | |
752 | emergency_buffer_size -= rsize; |
753 | emergency_buffer += rsize; |
754 | return old; |
755 | } else { |
cea2e8a9 |
756 | dTHX; |
55497cff |
757 | /* First offense, give a possibility to recover by dieing. */ |
758 | /* No malloc involved here: */ |
4a33f861 |
759 | GV **gvp = (GV**)hv_fetch(PL_defstash, "^M", 2, 0); |
55497cff |
760 | SV *sv; |
761 | char *pv; |
28ac10b1 |
762 | int have = 0; |
2d8e6c8d |
763 | STRLEN n_a; |
55497cff |
764 | |
28ac10b1 |
765 | if (emergency_buffer_size) { |
766 | add_to_chain(emergency_buffer, emergency_buffer_size, 0); |
767 | emergency_buffer_size = 0; |
768 | emergency_buffer = Nullch; |
769 | have = 1; |
770 | } |
4a33f861 |
771 | if (!gvp) gvp = (GV**)hv_fetch(PL_defstash, "\015", 1, 0); |
55497cff |
772 | if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv) |
28ac10b1 |
773 | || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD)) { |
774 | if (have) |
775 | goto do_croak; |
55497cff |
776 | return (char *)-1; /* Now die die die... */ |
28ac10b1 |
777 | } |
55497cff |
778 | /* Got it, now detach SvPV: */ |
2d8e6c8d |
779 | pv = SvPV(sv, n_a); |
55497cff |
780 | /* Check alignment: */ |
5bbd1ef5 |
781 | if (((UV)(pv - sizeof(union overhead))) & (NEEDED_ALIGNMENT - 1)) { |
55497cff |
782 | PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n"); |
bbce6d69 |
783 | return (char *)-1; /* die die die */ |
55497cff |
784 | } |
785 | |
28ac10b1 |
786 | emergency_buffer = pv - sizeof(union overhead); |
787 | emergency_buffer_size = malloced_size(pv) + M_OVERHEAD; |
55497cff |
788 | SvPOK_off(sv); |
28ac10b1 |
789 | SvPVX(sv) = Nullch; |
790 | SvCUR(sv) = SvLEN(sv) = 0; |
55497cff |
791 | } |
28ac10b1 |
792 | do_croak: |
741df71a |
793 | MALLOC_UNLOCK; |
28ac10b1 |
794 | croak("Out of memory during request for %i bytes", size); |
55497cff |
795 | } |
796 | |
e8bc2b5c |
797 | #else /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */ |
55497cff |
798 | # define emergency_sbrk(size) -1 |
e8bc2b5c |
799 | #endif /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */ |
55497cff |
800 | |
801 | /* |
e8bc2b5c |
802 | * nextf[i] is the pointer to the next free block of size 2^i. The |
8d063cd8 |
803 | * smallest allocatable block is 8 bytes. The overhead information |
804 | * precedes the data area returned to the user. |
805 | */ |
e8bc2b5c |
806 | #define NBUCKETS (32*BUCKETS_PER_POW2 + 1) |
8d063cd8 |
807 | static union overhead *nextf[NBUCKETS]; |
cf5c4ad8 |
808 | |
809 | #ifdef USE_PERL_SBRK |
810 | #define sbrk(a) Perl_sbrk(a) |
20ce7b12 |
811 | Malloc_t Perl_sbrk (int size); |
8ac85365 |
812 | #else |
813 | #ifdef DONT_DECLARE_STD |
814 | #ifdef I_UNISTD |
815 | #include <unistd.h> |
816 | #endif |
cf5c4ad8 |
817 | #else |
52082926 |
818 | extern Malloc_t sbrk(int); |
8ac85365 |
819 | #endif |
cf5c4ad8 |
820 | #endif |
8d063cd8 |
821 | |
c07a80fd |
822 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
823 | /* |
824 | * nmalloc[i] is the difference between the number of mallocs and frees |
825 | * for a given block size. |
826 | */ |
827 | static u_int nmalloc[NBUCKETS]; |
5f05dabc |
828 | static u_int sbrk_slack; |
829 | static u_int start_slack; |
8d063cd8 |
830 | #endif |
831 | |
e8bc2b5c |
832 | static u_int goodsbrk; |
833 | |
760ac839 |
834 | #ifdef DEBUGGING |
3541dd58 |
835 | #undef ASSERT |
836 | #define ASSERT(p,diag) if (!(p)) botch(diag,STRINGIFY(p)); else |
cea2e8a9 |
837 | static void |
838 | botch(char *diag, char *s) |
8d063cd8 |
839 | { |
d720c441 |
840 | PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s); |
3028581b |
841 | PerlProc_abort(); |
8d063cd8 |
842 | } |
843 | #else |
3541dd58 |
844 | #define ASSERT(p, diag) |
8d063cd8 |
845 | #endif |
846 | |
2304df62 |
847 | Malloc_t |
86058a2d |
848 | Perl_malloc(register size_t nbytes) |
8d063cd8 |
849 | { |
850 | register union overhead *p; |
e8bc2b5c |
851 | register int bucket; |
ee0007ab |
852 | register MEM_SIZE shiftr; |
8d063cd8 |
853 | |
c2a5c2d2 |
854 | #if defined(DEBUGGING) || defined(RCHECK) |
ee0007ab |
855 | MEM_SIZE size = nbytes; |
45d8adaa |
856 | #endif |
857 | |
e8bc2b5c |
858 | BARK_64K_LIMIT("Allocation",nbytes,nbytes); |
45d8adaa |
859 | #ifdef DEBUGGING |
860 | if ((long)nbytes < 0) |
cea2e8a9 |
861 | croak("%s", "panic: malloc"); |
45d8adaa |
862 | #endif |
45d8adaa |
863 | |
741df71a |
864 | MALLOC_LOCK; |
8d063cd8 |
865 | /* |
866 | * Convert amount of memory requested into |
867 | * closest block size stored in hash buckets |
868 | * which satisfies request. Account for |
869 | * space used per block for accounting. |
870 | */ |
cf5c4ad8 |
871 | #ifdef PACK_MALLOC |
e8bc2b5c |
872 | # ifdef SMALL_BUCKET_VIA_TABLE |
873 | if (nbytes == 0) |
874 | bucket = MIN_BUCKET; |
875 | else if (nbytes <= SIZE_TABLE_MAX) { |
876 | bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT]; |
877 | } else |
878 | # else |
043bf814 |
879 | if (nbytes == 0) |
880 | nbytes = 1; |
e8bc2b5c |
881 | if (nbytes <= MAX_POW2_ALGO) goto do_shifts; |
882 | else |
883 | # endif |
55497cff |
884 | #endif |
e8bc2b5c |
885 | { |
886 | POW2_OPTIMIZE_ADJUST(nbytes); |
887 | nbytes += M_OVERHEAD; |
888 | nbytes = (nbytes + 3) &~ 3; |
889 | do_shifts: |
890 | shiftr = (nbytes - 1) >> START_SHIFT; |
891 | bucket = START_SHIFTS_BUCKET; |
892 | /* apart from this loop, this is O(1) */ |
893 | while (shiftr >>= 1) |
894 | bucket += BUCKETS_PER_POW2; |
cf5c4ad8 |
895 | } |
8d063cd8 |
896 | /* |
897 | * If nothing in hash bucket right now, |
898 | * request more memory from the system. |
899 | */ |
900 | if (nextf[bucket] == NULL) |
901 | morecore(bucket); |
e8bc2b5c |
902 | if ((p = nextf[bucket]) == NULL) { |
741df71a |
903 | MALLOC_UNLOCK; |
55497cff |
904 | #ifdef PERL_CORE |
4a33f861 |
905 | if (!PL_nomemok) { |
760ac839 |
906 | PerlIO_puts(PerlIO_stderr(),"Out of memory!\n"); |
cea2e8a9 |
907 | WITH_THX(my_exit(1)); |
ee0007ab |
908 | } |
45d8adaa |
909 | #else |
8d063cd8 |
910 | return (NULL); |
45d8adaa |
911 | #endif |
912 | } |
913 | |
e8bc2b5c |
914 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
915 | "0x%lx: (%05lu) malloc %ld bytes\n", |
4a33f861 |
916 | (unsigned long)(p+1), (unsigned long)(PL_an++), |
e8bc2b5c |
917 | (long)size)); |
45d8adaa |
918 | |
8d063cd8 |
919 | /* remove from linked list */ |
802004fa |
920 | #if defined(RCHECK) |
921 | if (((UV)p) & (MEM_ALIGNBYTES - 1)) |
760ac839 |
922 | PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n", |
a0d0e21e |
923 | (unsigned long)*((int*)p),(unsigned long)p); |
bf38876a |
924 | #endif |
925 | nextf[bucket] = p->ov_next; |
e8bc2b5c |
926 | #ifdef IGNORE_SMALL_BAD_FREE |
927 | if (bucket >= FIRST_BUCKET_WITH_CHECK) |
928 | #endif |
929 | OV_MAGIC(p, bucket) = MAGIC; |
cf5c4ad8 |
930 | #ifndef PACK_MALLOC |
931 | OV_INDEX(p) = bucket; |
932 | #endif |
8d063cd8 |
933 | #ifdef RCHECK |
934 | /* |
935 | * Record allocated size of block and |
936 | * bound space with magic numbers. |
937 | */ |
8d063cd8 |
938 | p->ov_rmagic = RMAGIC; |
e8bc2b5c |
939 | if (bucket <= MAX_SHORT_BUCKET) { |
940 | int i; |
941 | |
942 | nbytes = size + M_OVERHEAD; |
943 | p->ov_size = nbytes - 1; |
944 | if ((i = nbytes & 3)) { |
945 | i = 4 - i; |
946 | while (i--) |
947 | *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C; |
948 | } |
949 | nbytes = (nbytes + 3) &~ 3; |
950 | *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; |
951 | } |
8d063cd8 |
952 | #endif |
741df71a |
953 | MALLOC_UNLOCK; |
cf5c4ad8 |
954 | return ((Malloc_t)(p + CHUNK_SHIFT)); |
8d063cd8 |
955 | } |
956 | |
e8bc2b5c |
957 | static char *last_sbrk_top; |
958 | static char *last_op; /* This arena can be easily extended. */ |
959 | static int sbrked_remains; |
960 | static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE; |
961 | |
962 | #ifdef DEBUGGING_MSTATS |
963 | static int sbrks; |
964 | #endif |
965 | |
966 | struct chunk_chain_s { |
967 | struct chunk_chain_s *next; |
968 | MEM_SIZE size; |
969 | }; |
970 | static struct chunk_chain_s *chunk_chain; |
971 | static int n_chunks; |
972 | static char max_bucket; |
973 | |
974 | /* Cutoff a piece of one of the chunks in the chain. Prefer smaller chunk. */ |
cea2e8a9 |
975 | static void * |
976 | get_from_chain(MEM_SIZE size) |
e8bc2b5c |
977 | { |
978 | struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain; |
979 | struct chunk_chain_s **oldgoodp = NULL; |
980 | long min_remain = LONG_MAX; |
981 | |
982 | while (elt) { |
983 | if (elt->size >= size) { |
984 | long remains = elt->size - size; |
985 | if (remains >= 0 && remains < min_remain) { |
986 | oldgoodp = oldp; |
987 | min_remain = remains; |
988 | } |
989 | if (remains == 0) { |
990 | break; |
991 | } |
992 | } |
993 | oldp = &( elt->next ); |
994 | elt = elt->next; |
995 | } |
996 | if (!oldgoodp) return NULL; |
997 | if (min_remain) { |
998 | void *ret = *oldgoodp; |
999 | struct chunk_chain_s *next = (*oldgoodp)->next; |
1000 | |
1001 | *oldgoodp = (struct chunk_chain_s *)((char*)ret + size); |
1002 | (*oldgoodp)->size = min_remain; |
1003 | (*oldgoodp)->next = next; |
1004 | return ret; |
1005 | } else { |
1006 | void *ret = *oldgoodp; |
1007 | *oldgoodp = (*oldgoodp)->next; |
1008 | n_chunks--; |
1009 | return ret; |
1010 | } |
1011 | } |
1012 | |
cea2e8a9 |
1013 | static void |
1014 | add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip) |
e8bc2b5c |
1015 | { |
1016 | struct chunk_chain_s *next = chunk_chain; |
1017 | char *cp = (char*)p; |
1018 | |
1019 | cp += chip; |
1020 | chunk_chain = (struct chunk_chain_s *)cp; |
1021 | chunk_chain->size = size - chip; |
1022 | chunk_chain->next = next; |
1023 | n_chunks++; |
1024 | } |
1025 | |
cea2e8a9 |
1026 | static void * |
1027 | get_from_bigger_buckets(int bucket, MEM_SIZE size) |
e8bc2b5c |
1028 | { |
1029 | int price = 1; |
1030 | static int bucketprice[NBUCKETS]; |
1031 | while (bucket <= max_bucket) { |
1032 | /* We postpone stealing from bigger buckets until we want it |
1033 | often enough. */ |
1034 | if (nextf[bucket] && bucketprice[bucket]++ >= price) { |
1035 | /* Steal it! */ |
1036 | void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT); |
1037 | bucketprice[bucket] = 0; |
1038 | if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) { |
1039 | last_op = NULL; /* Disable optimization */ |
1040 | } |
1041 | nextf[bucket] = nextf[bucket]->ov_next; |
1042 | #ifdef DEBUGGING_MSTATS |
1043 | nmalloc[bucket]--; |
1044 | start_slack -= M_OVERHEAD; |
1045 | #endif |
1046 | add_to_chain(ret, (BUCKET_SIZE(bucket) + |
1047 | POW2_OPTIMIZE_SURPLUS(bucket)), |
1048 | size); |
1049 | return ret; |
1050 | } |
1051 | bucket++; |
1052 | } |
1053 | return NULL; |
1054 | } |
1055 | |
cea2e8a9 |
1056 | static union overhead * |
1057 | getpages(int needed, int *nblksp, int bucket) |
fa423c5b |
1058 | { |
1059 | /* Need to do (possibly expensive) system call. Try to |
1060 | optimize it for rare calling. */ |
1061 | MEM_SIZE require = needed - sbrked_remains; |
1062 | char *cp; |
1063 | union overhead *ovp; |
1064 | int slack = 0; |
1065 | |
1066 | if (sbrk_good > 0) { |
1067 | if (!last_sbrk_top && require < FIRST_SBRK) |
1068 | require = FIRST_SBRK; |
1069 | else if (require < MIN_SBRK) require = MIN_SBRK; |
1070 | |
1071 | if (require < goodsbrk * MIN_SBRK_FRAC / 100) |
1072 | require = goodsbrk * MIN_SBRK_FRAC / 100; |
1073 | require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK; |
1074 | } else { |
1075 | require = needed; |
1076 | last_sbrk_top = 0; |
1077 | sbrked_remains = 0; |
1078 | } |
1079 | |
1080 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1081 | "sbrk(%ld) for %ld-byte-long arena\n", |
1082 | (long)require, (long) needed)); |
1083 | cp = (char *)sbrk(require); |
1084 | #ifdef DEBUGGING_MSTATS |
1085 | sbrks++; |
1086 | #endif |
1087 | if (cp == last_sbrk_top) { |
1088 | /* Common case, anything is fine. */ |
1089 | sbrk_good++; |
1090 | ovp = (union overhead *) (cp - sbrked_remains); |
e9397286 |
1091 | last_op = cp - sbrked_remains; |
fa423c5b |
1092 | sbrked_remains = require - (needed - sbrked_remains); |
1093 | } else if (cp == (char *)-1) { /* no more room! */ |
1094 | ovp = (union overhead *)emergency_sbrk(needed); |
1095 | if (ovp == (union overhead *)-1) |
1096 | return 0; |
e9397286 |
1097 | if (((char*)ovp) > last_op) { /* Cannot happen with current emergency_sbrk() */ |
1098 | last_op = 0; |
1099 | } |
fa423c5b |
1100 | return ovp; |
1101 | } else { /* Non-continuous or first sbrk(). */ |
1102 | long add = sbrked_remains; |
1103 | char *newcp; |
1104 | |
1105 | if (sbrked_remains) { /* Put rest into chain, we |
1106 | cannot use it right now. */ |
1107 | add_to_chain((void*)(last_sbrk_top - sbrked_remains), |
1108 | sbrked_remains, 0); |
1109 | } |
1110 | |
1111 | /* Second, check alignment. */ |
1112 | slack = 0; |
1113 | |
61ae2fbf |
1114 | #if !defined(atarist) && !defined(__MINT__) /* on the atari we dont have to worry about this */ |
fa423c5b |
1115 | # ifndef I286 /* The sbrk(0) call on the I286 always returns the next segment */ |
5bbd1ef5 |
1116 | /* WANTED_ALIGNMENT may be more than NEEDED_ALIGNMENT, but this may |
1117 | improve performance of memory access. */ |
1118 | if ((UV)cp & (WANTED_ALIGNMENT - 1)) { /* Not aligned. */ |
1119 | slack = WANTED_ALIGNMENT - ((UV)cp & (WANTED_ALIGNMENT - 1)); |
fa423c5b |
1120 | add += slack; |
1121 | } |
1122 | # endif |
61ae2fbf |
1123 | #endif /* !atarist && !MINT */ |
fa423c5b |
1124 | |
1125 | if (add) { |
1126 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1127 | "sbrk(%ld) to fix non-continuous/off-page sbrk:\n\t%ld for alignement,\t%ld were assumed to come from the tail of the previous sbrk\n", |
1128 | (long)add, (long) slack, |
1129 | (long) sbrked_remains)); |
1130 | newcp = (char *)sbrk(add); |
1131 | #if defined(DEBUGGING_MSTATS) |
1132 | sbrks++; |
1133 | sbrk_slack += add; |
1134 | #endif |
1135 | if (newcp != cp + require) { |
1136 | /* Too bad: even rounding sbrk() is not continuous.*/ |
1137 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1138 | "failed to fix bad sbrk()\n")); |
1139 | #ifdef PACK_MALLOC |
1140 | if (slack) { |
741df71a |
1141 | MALLOC_UNLOCK; |
5bbd1ef5 |
1142 | fatalcroak("panic: Off-page sbrk\n"); |
fa423c5b |
1143 | } |
1144 | #endif |
1145 | if (sbrked_remains) { |
1146 | /* Try again. */ |
1147 | #if defined(DEBUGGING_MSTATS) |
1148 | sbrk_slack += require; |
1149 | #endif |
1150 | require = needed; |
1151 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1152 | "straight sbrk(%ld)\n", |
1153 | (long)require)); |
1154 | cp = (char *)sbrk(require); |
1155 | #ifdef DEBUGGING_MSTATS |
1156 | sbrks++; |
1157 | #endif |
1158 | if (cp == (char *)-1) |
1159 | return 0; |
1160 | } |
1161 | sbrk_good = -1; /* Disable optimization! |
1162 | Continue with not-aligned... */ |
1163 | } else { |
1164 | cp += slack; |
1165 | require += sbrked_remains; |
1166 | } |
1167 | } |
1168 | |
1169 | if (last_sbrk_top) { |
1170 | sbrk_good -= SBRK_FAILURE_PRICE; |
1171 | } |
1172 | |
1173 | ovp = (union overhead *) cp; |
1174 | /* |
1175 | * Round up to minimum allocation size boundary |
1176 | * and deduct from block count to reflect. |
1177 | */ |
1178 | |
5bbd1ef5 |
1179 | # if NEEDED_ALIGNMENT > MEM_ALIGNBYTES |
1180 | if ((UV)ovp & (NEEDED_ALIGNMENT - 1)) |
1181 | fatalcroak("Misalignment of sbrk()\n"); |
1182 | else |
1183 | # endif |
fa423c5b |
1184 | #ifndef I286 /* Again, this should always be ok on an 80286 */ |
5bbd1ef5 |
1185 | if ((UV)ovp & (MEM_ALIGNBYTES - 1)) { |
fa423c5b |
1186 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1187 | "fixing sbrk(): %d bytes off machine alignement\n", |
5bbd1ef5 |
1188 | (int)((UV)ovp & (MEM_ALIGNBYTES - 1)))); |
1189 | ovp = (union overhead *)(((UV)ovp + MEM_ALIGNBYTES) & |
1190 | (MEM_ALIGNBYTES - 1)); |
fa423c5b |
1191 | (*nblksp)--; |
1192 | # if defined(DEBUGGING_MSTATS) |
1193 | /* This is only approx. if TWO_POT_OPTIMIZE: */ |
5bbd1ef5 |
1194 | sbrk_slack += (1 << (bucket >> BUCKET_POW2_SHIFT)); |
fa423c5b |
1195 | # endif |
1196 | } |
1197 | #endif |
5bbd1ef5 |
1198 | ; /* Finish `else' */ |
fa423c5b |
1199 | sbrked_remains = require - needed; |
e9397286 |
1200 | last_op = cp; |
fa423c5b |
1201 | } |
1202 | last_sbrk_top = cp + require; |
fa423c5b |
1203 | #ifdef DEBUGGING_MSTATS |
1204 | goodsbrk += require; |
1205 | #endif |
1206 | return ovp; |
1207 | } |
1208 | |
cea2e8a9 |
1209 | static int |
1210 | getpages_adjacent(int require) |
fa423c5b |
1211 | { |
1212 | if (require <= sbrked_remains) { |
1213 | sbrked_remains -= require; |
1214 | } else { |
1215 | char *cp; |
1216 | |
1217 | require -= sbrked_remains; |
1218 | /* We do not try to optimize sbrks here, we go for place. */ |
1219 | cp = (char*) sbrk(require); |
1220 | #ifdef DEBUGGING_MSTATS |
1221 | sbrks++; |
1222 | goodsbrk += require; |
1223 | #endif |
1224 | if (cp == last_sbrk_top) { |
1225 | sbrked_remains = 0; |
1226 | last_sbrk_top = cp + require; |
1227 | } else { |
28ac10b1 |
1228 | if (cp == (char*)-1) { /* Out of memory */ |
1229 | #ifdef DEBUGGING_MSTATS |
1230 | goodsbrk -= require; |
1231 | #endif |
1232 | return 0; |
1233 | } |
fa423c5b |
1234 | /* Report the failure: */ |
1235 | if (sbrked_remains) |
1236 | add_to_chain((void*)(last_sbrk_top - sbrked_remains), |
1237 | sbrked_remains, 0); |
1238 | add_to_chain((void*)cp, require, 0); |
1239 | sbrk_good -= SBRK_FAILURE_PRICE; |
1240 | sbrked_remains = 0; |
1241 | last_sbrk_top = 0; |
1242 | last_op = 0; |
1243 | return 0; |
1244 | } |
1245 | } |
1246 | |
1247 | return 1; |
1248 | } |
1249 | |
8d063cd8 |
1250 | /* |
1251 | * Allocate more memory to the indicated bucket. |
1252 | */ |
cea2e8a9 |
1253 | static void |
1254 | morecore(register int bucket) |
8d063cd8 |
1255 | { |
72aaf631 |
1256 | register union overhead *ovp; |
8d063cd8 |
1257 | register int rnu; /* 2^rnu bytes will be requested */ |
fa423c5b |
1258 | int nblks; /* become nblks blocks of the desired size */ |
bbce6d69 |
1259 | register MEM_SIZE siz, needed; |
8d063cd8 |
1260 | |
1261 | if (nextf[bucket]) |
1262 | return; |
e8bc2b5c |
1263 | if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) { |
741df71a |
1264 | MALLOC_UNLOCK; |
d720c441 |
1265 | croak("%s", "Out of memory during ridiculously large request"); |
55497cff |
1266 | } |
d720c441 |
1267 | if (bucket > max_bucket) |
e8bc2b5c |
1268 | max_bucket = bucket; |
d720c441 |
1269 | |
e8bc2b5c |
1270 | rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT)) |
1271 | ? LOG_OF_MIN_ARENA |
1272 | : (bucket >> BUCKET_POW2_SHIFT) ); |
1273 | /* This may be overwritten later: */ |
1274 | nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */ |
1275 | needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket); |
1276 | if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */ |
1277 | ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT; |
1278 | nextf[rnu << BUCKET_POW2_SHIFT] |
1279 | = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next; |
1280 | #ifdef DEBUGGING_MSTATS |
1281 | nmalloc[rnu << BUCKET_POW2_SHIFT]--; |
1282 | start_slack -= M_OVERHEAD; |
1283 | #endif |
1284 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1285 | "stealing %ld bytes from %ld arena\n", |
1286 | (long) needed, (long) rnu << BUCKET_POW2_SHIFT)); |
1287 | } else if (chunk_chain |
1288 | && (ovp = (union overhead*) get_from_chain(needed))) { |
1289 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1290 | "stealing %ld bytes from chain\n", |
1291 | (long) needed)); |
d720c441 |
1292 | } else if ( (ovp = (union overhead*) |
1293 | get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1, |
1294 | needed)) ) { |
e8bc2b5c |
1295 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1296 | "stealing %ld bytes from bigger buckets\n", |
1297 | (long) needed)); |
1298 | } else if (needed <= sbrked_remains) { |
1299 | ovp = (union overhead *)(last_sbrk_top - sbrked_remains); |
1300 | sbrked_remains -= needed; |
1301 | last_op = (char*)ovp; |
fa423c5b |
1302 | } else |
1303 | ovp = getpages(needed, &nblks, bucket); |
e8bc2b5c |
1304 | |
fa423c5b |
1305 | if (!ovp) |
1306 | return; |
e8bc2b5c |
1307 | |
8d063cd8 |
1308 | /* |
1309 | * Add new memory allocated to that on |
1310 | * free list for this hash bucket. |
1311 | */ |
e8bc2b5c |
1312 | siz = BUCKET_SIZE(bucket); |
cf5c4ad8 |
1313 | #ifdef PACK_MALLOC |
72aaf631 |
1314 | *(u_char*)ovp = bucket; /* Fill index. */ |
e8bc2b5c |
1315 | if (bucket <= MAX_PACKED) { |
1316 | ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket)); |
1317 | nblks = N_BLKS(bucket); |
cf5c4ad8 |
1318 | # ifdef DEBUGGING_MSTATS |
e8bc2b5c |
1319 | start_slack += BLK_SHIFT(bucket); |
cf5c4ad8 |
1320 | # endif |
e8bc2b5c |
1321 | } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) { |
1322 | ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket)); |
cf5c4ad8 |
1323 | siz -= sizeof(union overhead); |
72aaf631 |
1324 | } else ovp++; /* One chunk per block. */ |
e8bc2b5c |
1325 | #endif /* PACK_MALLOC */ |
72aaf631 |
1326 | nextf[bucket] = ovp; |
5f05dabc |
1327 | #ifdef DEBUGGING_MSTATS |
1328 | nmalloc[bucket] += nblks; |
e8bc2b5c |
1329 | if (bucket > MAX_PACKED) { |
1330 | start_slack += M_OVERHEAD * nblks; |
1331 | } |
5f05dabc |
1332 | #endif |
8d063cd8 |
1333 | while (--nblks > 0) { |
72aaf631 |
1334 | ovp->ov_next = (union overhead *)((caddr_t)ovp + siz); |
1335 | ovp = (union overhead *)((caddr_t)ovp + siz); |
8d063cd8 |
1336 | } |
8595d6f1 |
1337 | /* Not all sbrks return zeroed memory.*/ |
72aaf631 |
1338 | ovp->ov_next = (union overhead *)NULL; |
cf5c4ad8 |
1339 | #ifdef PACK_MALLOC |
e8bc2b5c |
1340 | if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */ |
1341 | union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next; |
1342 | nextf[7*BUCKETS_PER_POW2] = |
1343 | (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2] |
1344 | - sizeof(union overhead)); |
1345 | nextf[7*BUCKETS_PER_POW2]->ov_next = n_op; |
cf5c4ad8 |
1346 | } |
1347 | #endif /* !PACK_MALLOC */ |
8d063cd8 |
1348 | } |
1349 | |
94b6baf5 |
1350 | Free_t |
86058a2d |
1351 | Perl_mfree(void *mp) |
cea2e8a9 |
1352 | { |
ee0007ab |
1353 | register MEM_SIZE size; |
72aaf631 |
1354 | register union overhead *ovp; |
352d5a3a |
1355 | char *cp = (char*)mp; |
cf5c4ad8 |
1356 | #ifdef PACK_MALLOC |
1357 | u_char bucket; |
1358 | #endif |
8d063cd8 |
1359 | |
e8bc2b5c |
1360 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1361 | "0x%lx: (%05lu) free\n", |
4a33f861 |
1362 | (unsigned long)cp, (unsigned long)(PL_an++))); |
45d8adaa |
1363 | |
cf5c4ad8 |
1364 | if (cp == NULL) |
1365 | return; |
72aaf631 |
1366 | ovp = (union overhead *)((caddr_t)cp |
e8bc2b5c |
1367 | - sizeof (union overhead) * CHUNK_SHIFT); |
cf5c4ad8 |
1368 | #ifdef PACK_MALLOC |
72aaf631 |
1369 | bucket = OV_INDEX(ovp); |
cf5c4ad8 |
1370 | #endif |
e8bc2b5c |
1371 | #ifdef IGNORE_SMALL_BAD_FREE |
1372 | if ((bucket >= FIRST_BUCKET_WITH_CHECK) |
1373 | && (OV_MAGIC(ovp, bucket) != MAGIC)) |
1374 | #else |
1375 | if (OV_MAGIC(ovp, bucket) != MAGIC) |
1376 | #endif |
1377 | { |
68dc0745 |
1378 | static int bad_free_warn = -1; |
cf5c4ad8 |
1379 | if (bad_free_warn == -1) { |
5fd9e9a4 |
1380 | char *pbf = PerlEnv_getenv("PERL_BADFREE"); |
cf5c4ad8 |
1381 | bad_free_warn = (pbf) ? atoi(pbf) : 1; |
1382 | } |
1383 | if (!bad_free_warn) |
1384 | return; |
8990e307 |
1385 | #ifdef RCHECK |
a687059c |
1386 | warn("%s free() ignored", |
72aaf631 |
1387 | ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad"); |
8990e307 |
1388 | #else |
d720c441 |
1389 | warn("%s", "Bad free() ignored"); |
8990e307 |
1390 | #endif |
8d063cd8 |
1391 | return; /* sanity */ |
e8bc2b5c |
1392 | } |
741df71a |
1393 | MALLOC_LOCK; |
8d063cd8 |
1394 | #ifdef RCHECK |
3541dd58 |
1395 | ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite"); |
e8bc2b5c |
1396 | if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) { |
1397 | int i; |
1398 | MEM_SIZE nbytes = ovp->ov_size + 1; |
1399 | |
1400 | if ((i = nbytes & 3)) { |
1401 | i = 4 - i; |
1402 | while (i--) { |
3541dd58 |
1403 | ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i)) |
d720c441 |
1404 | == RMAGIC_C, "chunk's tail overwrite"); |
e8bc2b5c |
1405 | } |
1406 | } |
1407 | nbytes = (nbytes + 3) &~ 3; |
3541dd58 |
1408 | ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite"); |
e8bc2b5c |
1409 | } |
72aaf631 |
1410 | ovp->ov_rmagic = RMAGIC - 1; |
8d063cd8 |
1411 | #endif |
3541dd58 |
1412 | ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite"); |
72aaf631 |
1413 | size = OV_INDEX(ovp); |
1414 | ovp->ov_next = nextf[size]; |
1415 | nextf[size] = ovp; |
741df71a |
1416 | MALLOC_UNLOCK; |
8d063cd8 |
1417 | } |
1418 | |
1419 | /* |
1420 | * When a program attempts "storage compaction" as mentioned in the |
1421 | * old malloc man page, it realloc's an already freed block. Usually |
1422 | * this is the last block it freed; occasionally it might be farther |
1423 | * back. We have to search all the free lists for the block in order |
1424 | * to determine its bucket: 1st we make one pass thru the lists |
1425 | * checking only the first block in each; if that fails we search |
378cc40b |
1426 | * ``reall_srchlen'' blocks in each list for a match (the variable |
8d063cd8 |
1427 | * is extern so the caller can modify it). If that fails we just copy |
1428 | * however many bytes was given to realloc() and hope it's not huge. |
1429 | */ |
22c35a8c |
1430 | #define reall_srchlen 4 /* 4 should be plenty, -1 =>'s whole list */ |
8d063cd8 |
1431 | |
2304df62 |
1432 | Malloc_t |
86058a2d |
1433 | Perl_realloc(void *mp, size_t nbytes) |
cea2e8a9 |
1434 | { |
ee0007ab |
1435 | register MEM_SIZE onb; |
72aaf631 |
1436 | union overhead *ovp; |
d720c441 |
1437 | char *res; |
1438 | int prev_bucket; |
e8bc2b5c |
1439 | register int bucket; |
1440 | int was_alloced = 0, incr; |
352d5a3a |
1441 | char *cp = (char*)mp; |
8d063cd8 |
1442 | |
e8bc2b5c |
1443 | #if defined(DEBUGGING) || !defined(PERL_CORE) |
ee0007ab |
1444 | MEM_SIZE size = nbytes; |
45d8adaa |
1445 | |
45d8adaa |
1446 | if ((long)nbytes < 0) |
cea2e8a9 |
1447 | croak("%s", "panic: realloc"); |
45d8adaa |
1448 | #endif |
e8bc2b5c |
1449 | |
1450 | BARK_64K_LIMIT("Reallocation",nbytes,size); |
1451 | if (!cp) |
86058a2d |
1452 | return Perl_malloc(nbytes); |
45d8adaa |
1453 | |
741df71a |
1454 | MALLOC_LOCK; |
72aaf631 |
1455 | ovp = (union overhead *)((caddr_t)cp |
e8bc2b5c |
1456 | - sizeof (union overhead) * CHUNK_SHIFT); |
1457 | bucket = OV_INDEX(ovp); |
1458 | #ifdef IGNORE_SMALL_BAD_FREE |
1459 | if ((bucket < FIRST_BUCKET_WITH_CHECK) |
1460 | || (OV_MAGIC(ovp, bucket) == MAGIC)) |
1461 | #else |
1462 | if (OV_MAGIC(ovp, bucket) == MAGIC) |
1463 | #endif |
1464 | { |
55497cff |
1465 | was_alloced = 1; |
8d063cd8 |
1466 | } else { |
1467 | /* |
1468 | * Already free, doing "compaction". |
1469 | * |
1470 | * Search for the old block of memory on the |
1471 | * free list. First, check the most common |
1472 | * case (last element free'd), then (this failing) |
378cc40b |
1473 | * the last ``reall_srchlen'' items free'd. |
8d063cd8 |
1474 | * If all lookups fail, then assume the size of |
1475 | * the memory block being realloc'd is the |
1476 | * smallest possible. |
1477 | */ |
e8bc2b5c |
1478 | if ((bucket = findbucket(ovp, 1)) < 0 && |
1479 | (bucket = findbucket(ovp, reall_srchlen)) < 0) |
1480 | bucket = 0; |
8d063cd8 |
1481 | } |
e8bc2b5c |
1482 | onb = BUCKET_SIZE_REAL(bucket); |
55497cff |
1483 | /* |
1484 | * avoid the copy if same size block. |
e8bc2b5c |
1485 | * We are not agressive with boundary cases. Note that it might |
1486 | * (for a small number of cases) give false negative if |
55497cff |
1487 | * both new size and old one are in the bucket for |
e8bc2b5c |
1488 | * FIRST_BIG_POW2, but the new one is near the lower end. |
1489 | * |
1490 | * We do not try to go to 1.5 times smaller bucket so far. |
55497cff |
1491 | */ |
e8bc2b5c |
1492 | if (nbytes > onb) incr = 1; |
1493 | else { |
1494 | #ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING |
1495 | if ( /* This is a little bit pessimal if PACK_MALLOC: */ |
1496 | nbytes > ( (onb >> 1) - M_OVERHEAD ) |
1497 | # ifdef TWO_POT_OPTIMIZE |
1498 | || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND ) |
1499 | # endif |
1500 | ) |
1501 | #else /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */ |
1502 | prev_bucket = ( (bucket > MAX_PACKED + 1) |
1503 | ? bucket - BUCKETS_PER_POW2 |
1504 | : bucket - 1); |
1505 | if (nbytes > BUCKET_SIZE_REAL(prev_bucket)) |
1506 | #endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */ |
1507 | incr = 0; |
1508 | else incr = -1; |
1509 | } |
1510 | if (!was_alloced |
2ce36478 |
1511 | #ifdef STRESS_REALLOC |
e8bc2b5c |
1512 | || 1 /* always do it the hard way */ |
2ce36478 |
1513 | #endif |
e8bc2b5c |
1514 | ) goto hard_way; |
1515 | else if (incr == 0) { |
852c2e52 |
1516 | inplace_label: |
a687059c |
1517 | #ifdef RCHECK |
1518 | /* |
1519 | * Record new allocated size of block and |
1520 | * bound space with magic numbers. |
1521 | */ |
72aaf631 |
1522 | if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) { |
e8bc2b5c |
1523 | int i, nb = ovp->ov_size + 1; |
1524 | |
1525 | if ((i = nb & 3)) { |
1526 | i = 4 - i; |
1527 | while (i--) { |
3541dd58 |
1528 | ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite"); |
e8bc2b5c |
1529 | } |
1530 | } |
1531 | nb = (nb + 3) &~ 3; |
3541dd58 |
1532 | ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite"); |
a687059c |
1533 | /* |
1534 | * Convert amount of memory requested into |
1535 | * closest block size stored in hash buckets |
1536 | * which satisfies request. Account for |
1537 | * space used per block for accounting. |
1538 | */ |
cf5c4ad8 |
1539 | nbytes += M_OVERHEAD; |
72aaf631 |
1540 | ovp->ov_size = nbytes - 1; |
e8bc2b5c |
1541 | if ((i = nbytes & 3)) { |
1542 | i = 4 - i; |
1543 | while (i--) |
1544 | *((char *)((caddr_t)ovp + nbytes - RSLOP + i)) |
1545 | = RMAGIC_C; |
1546 | } |
1547 | nbytes = (nbytes + 3) &~ 3; |
72aaf631 |
1548 | *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC; |
a687059c |
1549 | } |
1550 | #endif |
45d8adaa |
1551 | res = cp; |
741df71a |
1552 | MALLOC_UNLOCK; |
42ac124e |
1553 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1554 | "0x%lx: (%05lu) realloc %ld bytes inplace\n", |
1555 | (unsigned long)res,(unsigned long)(PL_an++), |
1556 | (long)size)); |
e8bc2b5c |
1557 | } else if (incr == 1 && (cp - M_OVERHEAD == last_op) |
1558 | && (onb > (1 << LOG_OF_MIN_ARENA))) { |
1559 | MEM_SIZE require, newarena = nbytes, pow; |
1560 | int shiftr; |
1561 | |
1562 | POW2_OPTIMIZE_ADJUST(newarena); |
1563 | newarena = newarena + M_OVERHEAD; |
1564 | /* newarena = (newarena + 3) &~ 3; */ |
1565 | shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA; |
1566 | pow = LOG_OF_MIN_ARENA + 1; |
1567 | /* apart from this loop, this is O(1) */ |
1568 | while (shiftr >>= 1) |
1569 | pow++; |
1570 | newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2); |
1571 | require = newarena - onb - M_OVERHEAD; |
1572 | |
fa423c5b |
1573 | if (getpages_adjacent(require)) { |
e8bc2b5c |
1574 | #ifdef DEBUGGING_MSTATS |
fa423c5b |
1575 | nmalloc[bucket]--; |
1576 | nmalloc[pow * BUCKETS_PER_POW2]++; |
e8bc2b5c |
1577 | #endif |
fa423c5b |
1578 | *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */ |
1579 | goto inplace_label; |
1580 | } else |
1581 | goto hard_way; |
e8bc2b5c |
1582 | } else { |
1583 | hard_way: |
741df71a |
1584 | MALLOC_UNLOCK; |
42ac124e |
1585 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1586 | "0x%lx: (%05lu) realloc %ld bytes the hard way\n", |
1587 | (unsigned long)cp,(unsigned long)(PL_an++), |
1588 | (long)size)); |
86058a2d |
1589 | if ((res = (char*)Perl_malloc(nbytes)) == NULL) |
e8bc2b5c |
1590 | return (NULL); |
1591 | if (cp != res) /* common optimization */ |
1592 | Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char); |
1593 | if (was_alloced) |
86058a2d |
1594 | Perl_mfree(cp); |
45d8adaa |
1595 | } |
2304df62 |
1596 | return ((Malloc_t)res); |
8d063cd8 |
1597 | } |
1598 | |
1599 | /* |
1600 | * Search ``srchlen'' elements of each free list for a block whose |
1601 | * header starts at ``freep''. If srchlen is -1 search the whole list. |
1602 | * Return bucket number, or -1 if not found. |
1603 | */ |
ee0007ab |
1604 | static int |
8ac85365 |
1605 | findbucket(union overhead *freep, int srchlen) |
8d063cd8 |
1606 | { |
1607 | register union overhead *p; |
1608 | register int i, j; |
1609 | |
1610 | for (i = 0; i < NBUCKETS; i++) { |
1611 | j = 0; |
1612 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { |
1613 | if (p == freep) |
1614 | return (i); |
1615 | j++; |
1616 | } |
1617 | } |
1618 | return (-1); |
1619 | } |
1620 | |
cf5c4ad8 |
1621 | Malloc_t |
86058a2d |
1622 | Perl_calloc(register size_t elements, register size_t size) |
cf5c4ad8 |
1623 | { |
1624 | long sz = elements * size; |
86058a2d |
1625 | Malloc_t p = Perl_malloc(sz); |
cf5c4ad8 |
1626 | |
1627 | if (p) { |
1628 | memset((void*)p, 0, sz); |
1629 | } |
1630 | return p; |
1631 | } |
1632 | |
e8bc2b5c |
1633 | MEM_SIZE |
cea2e8a9 |
1634 | Perl_malloced_size(void *p) |
e8bc2b5c |
1635 | { |
8d6dde3e |
1636 | union overhead *ovp = (union overhead *) |
1637 | ((caddr_t)p - sizeof (union overhead) * CHUNK_SHIFT); |
1638 | int bucket = OV_INDEX(ovp); |
1639 | #ifdef RCHECK |
1640 | /* The caller wants to have a complete control over the chunk, |
1641 | disable the memory checking inside the chunk. */ |
1642 | if (bucket <= MAX_SHORT_BUCKET) { |
1643 | MEM_SIZE size = BUCKET_SIZE_REAL(bucket); |
1644 | ovp->ov_size = size + M_OVERHEAD - 1; |
1645 | *((u_int *)((caddr_t)ovp + size + M_OVERHEAD - RSLOP)) = RMAGIC; |
1646 | } |
1647 | #endif |
e8bc2b5c |
1648 | return BUCKET_SIZE_REAL(bucket); |
1649 | } |
1650 | |
e8bc2b5c |
1651 | # ifdef BUCKETS_ROOT2 |
1652 | # define MIN_EVEN_REPORT 6 |
1653 | # else |
1654 | # define MIN_EVEN_REPORT MIN_BUCKET |
1655 | # endif |
8d063cd8 |
1656 | /* |
1657 | * mstats - print out statistics about malloc |
1658 | * |
1659 | * Prints two lines of numbers, one showing the length of the free list |
1660 | * for each size category, the second showing the number of mallocs - |
1661 | * frees for each size category. |
1662 | */ |
ee0007ab |
1663 | void |
864dbfa3 |
1664 | Perl_dump_mstats(pTHX_ char *s) |
8d063cd8 |
1665 | { |
df31f264 |
1666 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
1667 | register int i, j; |
1668 | register union overhead *p; |
e8bc2b5c |
1669 | int topbucket=0, topbucket_ev=0, topbucket_odd=0, totfree=0, total=0; |
c07a80fd |
1670 | u_int nfree[NBUCKETS]; |
e8bc2b5c |
1671 | int total_chain = 0; |
1672 | struct chunk_chain_s* nextchain = chunk_chain; |
8d063cd8 |
1673 | |
e8bc2b5c |
1674 | for (i = MIN_BUCKET ; i < NBUCKETS; i++) { |
8d063cd8 |
1675 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) |
1676 | ; |
c07a80fd |
1677 | nfree[i] = j; |
e8bc2b5c |
1678 | totfree += nfree[i] * BUCKET_SIZE_REAL(i); |
1679 | total += nmalloc[i] * BUCKET_SIZE_REAL(i); |
1680 | if (nmalloc[i]) { |
1681 | i % 2 ? (topbucket_odd = i) : (topbucket_ev = i); |
1682 | topbucket = i; |
1683 | } |
c07a80fd |
1684 | } |
1685 | if (s) |
e8bc2b5c |
1686 | PerlIO_printf(PerlIO_stderr(), |
d720c441 |
1687 | "Memory allocation statistics %s (buckets %ld(%ld)..%ld(%ld)\n", |
e8bc2b5c |
1688 | s, |
d720c441 |
1689 | (long)BUCKET_SIZE_REAL(MIN_BUCKET), |
1690 | (long)BUCKET_SIZE(MIN_BUCKET), |
1691 | (long)BUCKET_SIZE_REAL(topbucket), (long)BUCKET_SIZE(topbucket)); |
5f05dabc |
1692 | PerlIO_printf(PerlIO_stderr(), "%8d free:", totfree); |
e8bc2b5c |
1693 | for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) { |
1694 | PerlIO_printf(PerlIO_stderr(), |
1695 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
1696 | ? " %5d" |
1697 | : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")), |
1698 | nfree[i]); |
1699 | } |
1700 | #ifdef BUCKETS_ROOT2 |
1701 | PerlIO_printf(PerlIO_stderr(), "\n\t "); |
1702 | for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) { |
1703 | PerlIO_printf(PerlIO_stderr(), |
1704 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
1705 | ? " %5d" |
1706 | : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")), |
1707 | nfree[i]); |
8d063cd8 |
1708 | } |
e8bc2b5c |
1709 | #endif |
5f05dabc |
1710 | PerlIO_printf(PerlIO_stderr(), "\n%8d used:", total - totfree); |
e8bc2b5c |
1711 | for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) { |
1712 | PerlIO_printf(PerlIO_stderr(), |
1713 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
1714 | ? " %5d" |
1715 | : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")), |
1716 | nmalloc[i] - nfree[i]); |
c07a80fd |
1717 | } |
e8bc2b5c |
1718 | #ifdef BUCKETS_ROOT2 |
1719 | PerlIO_printf(PerlIO_stderr(), "\n\t "); |
1720 | for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) { |
1721 | PerlIO_printf(PerlIO_stderr(), |
1722 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
1723 | ? " %5d" |
1724 | : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")), |
1725 | nmalloc[i] - nfree[i]); |
1726 | } |
1727 | #endif |
1728 | while (nextchain) { |
1729 | total_chain += nextchain->size; |
1730 | nextchain = nextchain->next; |
1731 | } |
1732 | PerlIO_printf(PerlIO_stderr(), "\nTotal sbrk(): %d/%d:%d. Odd ends: pad+heads+chain+tail: %d+%d+%d+%d.\n", |
1733 | goodsbrk + sbrk_slack, sbrks, sbrk_good, sbrk_slack, |
1734 | start_slack, total_chain, sbrked_remains); |
df31f264 |
1735 | #endif /* DEBUGGING_MSTATS */ |
c07a80fd |
1736 | } |
a687059c |
1737 | #endif /* lint */ |
cf5c4ad8 |
1738 | |
cf5c4ad8 |
1739 | #ifdef USE_PERL_SBRK |
1740 | |
8f1f23e8 |
1741 | # if defined(__MACHTEN_PPC__) || defined(NeXT) || defined(__NeXT__) |
38ac2dc8 |
1742 | # define PERL_SBRK_VIA_MALLOC |
1743 | /* |
1744 | * MachTen's malloc() returns a buffer aligned on a two-byte boundary. |
1745 | * While this is adequate, it may slow down access to longer data |
1746 | * types by forcing multiple memory accesses. It also causes |
1747 | * complaints when RCHECK is in force. So we allocate six bytes |
1748 | * more than we need to, and return an address rounded up to an |
1749 | * eight-byte boundary. |
1750 | * |
1751 | * 980701 Dominic Dunlop <domo@computer.org> |
1752 | */ |
5bbd1ef5 |
1753 | # define SYSTEM_ALLOC_ALIGNMENT 2 |
38ac2dc8 |
1754 | # endif |
1755 | |
760ac839 |
1756 | # ifdef PERL_SBRK_VIA_MALLOC |
cf5c4ad8 |
1757 | |
1758 | /* it may seem schizophrenic to use perl's malloc and let it call system */ |
1759 | /* malloc, the reason for that is only the 3.2 version of the OS that had */ |
1760 | /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */ |
1761 | /* end to the cores */ |
1762 | |
38ac2dc8 |
1763 | # ifndef SYSTEM_ALLOC |
1764 | # define SYSTEM_ALLOC(a) malloc(a) |
1765 | # endif |
5bbd1ef5 |
1766 | # ifndef SYSTEM_ALLOC_ALIGNMENT |
1767 | # define SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES |
1768 | # endif |
cf5c4ad8 |
1769 | |
760ac839 |
1770 | # endif /* PERL_SBRK_VIA_MALLOC */ |
cf5c4ad8 |
1771 | |
1772 | static IV Perl_sbrk_oldchunk; |
1773 | static long Perl_sbrk_oldsize; |
1774 | |
760ac839 |
1775 | # define PERLSBRK_32_K (1<<15) |
1776 | # define PERLSBRK_64_K (1<<16) |
cf5c4ad8 |
1777 | |
b63effbb |
1778 | Malloc_t |
df0003d4 |
1779 | Perl_sbrk(int size) |
cf5c4ad8 |
1780 | { |
1781 | IV got; |
1782 | int small, reqsize; |
1783 | |
1784 | if (!size) return 0; |
55497cff |
1785 | #ifdef PERL_CORE |
cf5c4ad8 |
1786 | reqsize = size; /* just for the DEBUG_m statement */ |
1787 | #endif |
57569e04 |
1788 | #ifdef PACK_MALLOC |
1789 | size = (size + 0x7ff) & ~0x7ff; |
1790 | #endif |
cf5c4ad8 |
1791 | if (size <= Perl_sbrk_oldsize) { |
1792 | got = Perl_sbrk_oldchunk; |
1793 | Perl_sbrk_oldchunk += size; |
1794 | Perl_sbrk_oldsize -= size; |
1795 | } else { |
1796 | if (size >= PERLSBRK_32_K) { |
1797 | small = 0; |
1798 | } else { |
cf5c4ad8 |
1799 | size = PERLSBRK_64_K; |
1800 | small = 1; |
1801 | } |
5bbd1ef5 |
1802 | # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT |
1803 | size += NEEDED_ALIGNMENT - SYSTEM_ALLOC_ALIGNMENT; |
1804 | # endif |
cf5c4ad8 |
1805 | got = (IV)SYSTEM_ALLOC(size); |
5bbd1ef5 |
1806 | # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT |
5a7d6335 |
1807 | got = (got + NEEDED_ALIGNMENT - 1) & ~(NEEDED_ALIGNMENT - 1); |
5bbd1ef5 |
1808 | # endif |
cf5c4ad8 |
1809 | if (small) { |
1810 | /* Chunk is small, register the rest for future allocs. */ |
1811 | Perl_sbrk_oldchunk = got + reqsize; |
1812 | Perl_sbrk_oldsize = size - reqsize; |
1813 | } |
1814 | } |
1815 | |
fb73857a |
1816 | DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n", |
cf5c4ad8 |
1817 | size, reqsize, Perl_sbrk_oldsize, got)); |
cf5c4ad8 |
1818 | |
1819 | return (void *)got; |
1820 | } |
1821 | |
1822 | #endif /* ! defined USE_PERL_SBRK */ |