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 | |
c7374474 |
128 | # size of void* |
129 | PTRSIZE 4 |
130 | |
741df71a |
131 | # Maximal value in LONG |
132 | LONG_MAX 0x7FFFFFFF |
133 | |
134 | # Unsigned integer type big enough to keep a pointer |
135 | UV unsigned long |
136 | |
137 | # Type of pointer with 1-byte granularity |
138 | caddr_t char * |
139 | |
140 | # Type returned by free() |
141 | Free_t void |
142 | |
5bbd1ef5 |
143 | # Very fatal condition reporting function (cannot call any ) |
144 | fatalcroak(arg) write(2,arg,strlen(arg)) + exit(2) |
145 | |
741df71a |
146 | # Fatal error reporting function |
147 | croak(format, arg) warn(idem) + exit(1) |
148 | |
b022d2d2 |
149 | # Fatal error reporting function |
150 | croak2(format, arg1, arg2) warn2(idem) + exit(1) |
151 | |
741df71a |
152 | # Error reporting function |
153 | warn(format, arg) fprintf(stderr, idem) |
154 | |
b022d2d2 |
155 | # Error reporting function |
156 | warn2(format, arg1, arg2) fprintf(stderr, idem) |
157 | |
741df71a |
158 | # Locking/unlocking for MT operation |
efc57feb |
159 | MALLOC_LOCK MUTEX_LOCK(&PL_malloc_mutex) |
160 | MALLOC_UNLOCK MUTEX_UNLOCK(&PL_malloc_mutex) |
741df71a |
161 | |
162 | # Locking/unlocking mutex for MT operation |
163 | MUTEX_LOCK(l) void |
164 | MUTEX_UNLOCK(l) void |
165 | */ |
166 | |
e8bc2b5c |
167 | #ifndef NO_FANCY_MALLOC |
168 | # ifndef SMALL_BUCKET_VIA_TABLE |
169 | # define SMALL_BUCKET_VIA_TABLE |
170 | # endif |
171 | # ifndef BUCKETS_ROOT2 |
172 | # define BUCKETS_ROOT2 |
173 | # endif |
174 | # ifndef IGNORE_SMALL_BAD_FREE |
175 | # define IGNORE_SMALL_BAD_FREE |
176 | # endif |
3562ef9b |
177 | #endif |
178 | |
e8bc2b5c |
179 | #ifndef PLAIN_MALLOC /* Bulk enable features */ |
180 | # ifndef PACK_MALLOC |
181 | # define PACK_MALLOC |
182 | # endif |
183 | # ifndef TWO_POT_OPTIMIZE |
184 | # define TWO_POT_OPTIMIZE |
185 | # endif |
d720c441 |
186 | # if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK) |
187 | # define PERL_EMERGENCY_SBRK |
e8bc2b5c |
188 | # endif |
189 | # if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS) |
190 | # define DEBUGGING_MSTATS |
191 | # endif |
192 | #endif |
193 | |
194 | #define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */ |
195 | #define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2) |
196 | |
61ae2fbf |
197 | #if !(defined(I286) || defined(atarist) || defined(__MINT__)) |
e8bc2b5c |
198 | /* take 2k unless the block is bigger than that */ |
199 | # define LOG_OF_MIN_ARENA 11 |
200 | #else |
201 | /* take 16k unless the block is bigger than that |
202 | (80286s like large segments!), probably good on the atari too */ |
203 | # define LOG_OF_MIN_ARENA 14 |
204 | #endif |
205 | |
8d063cd8 |
206 | #ifndef lint |
1944739a |
207 | # if defined(DEBUGGING) && !defined(NO_RCHECK) |
208 | # define RCHECK |
209 | # endif |
e8bc2b5c |
210 | # if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE) |
211 | # undef IGNORE_SMALL_BAD_FREE |
212 | # endif |
8d063cd8 |
213 | /* |
214 | * malloc.c (Caltech) 2/21/82 |
215 | * Chris Kingsley, kingsley@cit-20. |
216 | * |
217 | * This is a very fast storage allocator. It allocates blocks of a small |
218 | * number of different sizes, and keeps free lists of each size. Blocks that |
219 | * don't exactly fit are passed up to the next larger size. In this |
220 | * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. |
cf5c4ad8 |
221 | * If PACK_MALLOC is defined, small blocks are 2^n bytes long. |
8d063cd8 |
222 | * This is designed for use in a program that uses vast quantities of memory, |
741df71a |
223 | * but bombs when it runs out. |
224 | * |
4eb8286e |
225 | * Modifications Copyright Ilya Zakharevich 1996-99. |
741df71a |
226 | * |
227 | * Still very quick, but much more thrifty. (Std config is 10% slower |
228 | * than it was, and takes 67% of old heap size for typical usage.) |
229 | * |
230 | * Allocations of small blocks are now table-driven to many different |
231 | * buckets. Sizes of really big buckets are increased to accomodata |
232 | * common size=power-of-2 blocks. Running-out-of-memory is made into |
233 | * an exception. Deeply configurable and thread-safe. |
234 | * |
8d063cd8 |
235 | */ |
236 | |
d720c441 |
237 | #ifdef PERL_CORE |
238 | # include "EXTERN.h" |
4ad56ec9 |
239 | # define PERL_IN_MALLOC_C |
d720c441 |
240 | # include "perl.h" |
cea2e8a9 |
241 | # if defined(PERL_IMPLICIT_CONTEXT) |
242 | # define croak Perl_croak_nocontext |
b022d2d2 |
243 | # define croak2 Perl_croak_nocontext |
cea2e8a9 |
244 | # define warn Perl_warn_nocontext |
b022d2d2 |
245 | # define warn2 Perl_warn_nocontext |
246 | # else |
247 | # define croak2 croak |
248 | # define warn2 warn |
cea2e8a9 |
249 | # endif |
d720c441 |
250 | #else |
251 | # ifdef PERL_FOR_X2P |
252 | # include "../EXTERN.h" |
253 | # include "../perl.h" |
254 | # else |
255 | # include <stdlib.h> |
256 | # include <stdio.h> |
257 | # include <memory.h> |
258 | # define _(arg) arg |
259 | # ifndef Malloc_t |
260 | # define Malloc_t void * |
261 | # endif |
c7374474 |
262 | # ifndef PTRSIZE |
263 | # define PTRSIZE 4 |
264 | # endif |
d720c441 |
265 | # ifndef MEM_SIZE |
266 | # define MEM_SIZE unsigned long |
267 | # endif |
268 | # ifndef LONG_MAX |
269 | # define LONG_MAX 0x7FFFFFFF |
270 | # endif |
271 | # ifndef UV |
272 | # define UV unsigned long |
273 | # endif |
274 | # ifndef caddr_t |
275 | # define caddr_t char * |
276 | # endif |
277 | # ifndef Free_t |
278 | # define Free_t void |
279 | # endif |
280 | # define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t)) |
281 | # define PerlEnv_getenv getenv |
282 | # define PerlIO_printf fprintf |
283 | # define PerlIO_stderr() stderr |
284 | # endif |
e8bc2b5c |
285 | # ifndef croak /* make depend */ |
741df71a |
286 | # define croak(mess, arg) (warn((mess), (arg)), exit(1)) |
d720c441 |
287 | # endif |
b022d2d2 |
288 | # ifndef croak2 /* make depend */ |
289 | # define croak2(mess, arg1, arg2) (warn2((mess), (arg1), (arg2)), exit(1)) |
290 | # endif |
d720c441 |
291 | # ifndef warn |
741df71a |
292 | # define warn(mess, arg) fprintf(stderr, (mess), (arg)) |
e8bc2b5c |
293 | # endif |
0b415716 |
294 | # ifndef warn2 |
b022d2d2 |
295 | # define warn2(mess, arg1) fprintf(stderr, (mess), (arg1), (arg2)) |
296 | # endif |
e8bc2b5c |
297 | # ifdef DEBUG_m |
298 | # undef DEBUG_m |
299 | # endif |
300 | # define DEBUG_m(a) |
301 | # ifdef DEBUGGING |
302 | # undef DEBUGGING |
303 | # endif |
cea2e8a9 |
304 | # ifndef pTHX |
305 | # define pTHX void |
306 | # define pTHX_ |
307 | # define dTHX extern int Perl___notused |
308 | # define WITH_THX(s) s |
309 | # endif |
c5be433b |
310 | # ifndef PERL_GET_INTERP |
311 | # define PERL_GET_INTERP PL_curinterp |
312 | # endif |
4ad56ec9 |
313 | # ifndef Perl_malloc |
314 | # define Perl_malloc malloc |
315 | # endif |
316 | # ifndef Perl_mfree |
317 | # define Perl_mfree free |
318 | # endif |
319 | # ifndef Perl_realloc |
320 | # define Perl_realloc realloc |
321 | # endif |
322 | # ifndef Perl_calloc |
323 | # define Perl_calloc calloc |
324 | # endif |
325 | # ifndef Perl_strdup |
326 | # define Perl_strdup strdup |
327 | # endif |
e8bc2b5c |
328 | #endif |
329 | |
330 | #ifndef MUTEX_LOCK |
331 | # define MUTEX_LOCK(l) |
332 | #endif |
333 | |
334 | #ifndef MUTEX_UNLOCK |
335 | # define MUTEX_UNLOCK(l) |
336 | #endif |
337 | |
741df71a |
338 | #ifndef MALLOC_LOCK |
efc57feb |
339 | # define MALLOC_LOCK MUTEX_LOCK(&PL_malloc_mutex) |
741df71a |
340 | #endif |
341 | |
342 | #ifndef MALLOC_UNLOCK |
efc57feb |
343 | # define MALLOC_UNLOCK MUTEX_UNLOCK(&PL_malloc_mutex) |
741df71a |
344 | #endif |
345 | |
5bbd1ef5 |
346 | # ifndef fatalcroak /* make depend */ |
347 | # define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2)) |
348 | # endif |
349 | |
760ac839 |
350 | #ifdef DEBUGGING |
e8bc2b5c |
351 | # undef DEBUG_m |
0b250b9e |
352 | # define DEBUG_m(a) \ |
353 | STMT_START { \ |
354 | if (PERL_GET_INTERP) { dTHX; if (PL_debug & 128) { a; } } \ |
355 | } STMT_END |
760ac839 |
356 | #endif |
357 | |
e476b1b5 |
358 | #ifdef PERL_IMPLICIT_CONTEXT |
359 | # define PERL_IS_ALIVE aTHX |
360 | #else |
361 | # define PERL_IS_ALIVE TRUE |
362 | #endif |
363 | |
364 | |
e9397286 |
365 | /* |
366 | * Layout of memory: |
367 | * ~~~~~~~~~~~~~~~~ |
368 | * The memory is broken into "blocks" which occupy multiples of 2K (and |
369 | * generally speaking, have size "close" to a power of 2). The addresses |
370 | * of such *unused* blocks are kept in nextf[i] with big enough i. (nextf |
371 | * is an array of linked lists.) (Addresses of used blocks are not known.) |
372 | * |
4ad56ec9 |
373 | * Moreover, since the algorithm may try to "bite" smaller blocks out |
e9397286 |
374 | * of unused bigger ones, there are also regions of "irregular" size, |
375 | * managed separately, by a linked list chunk_chain. |
376 | * |
377 | * The third type of storage is the sbrk()ed-but-not-yet-used space, its |
378 | * end and size are kept in last_sbrk_top and sbrked_remains. |
379 | * |
380 | * Growing blocks "in place": |
381 | * ~~~~~~~~~~~~~~~~~~~~~~~~~ |
382 | * The address of the block with the greatest address is kept in last_op |
383 | * (if not known, last_op is 0). If it is known that the memory above |
384 | * last_op is not continuous, or contains a chunk from chunk_chain, |
385 | * last_op is set to 0. |
386 | * |
387 | * The chunk with address last_op may be grown by expanding into |
388 | * sbrk()ed-but-not-yet-used space, or trying to sbrk() more continuous |
389 | * memory. |
390 | * |
391 | * Management of last_op: |
392 | * ~~~~~~~~~~~~~~~~~~~~~ |
393 | * |
394 | * free() never changes the boundaries of blocks, so is not relevant. |
395 | * |
396 | * The only way realloc() may change the boundaries of blocks is if it |
397 | * grows a block "in place". However, in the case of success such a |
398 | * chunk is automatically last_op, and it remains last_op. In the case |
399 | * of failure getpages_adjacent() clears last_op. |
400 | * |
401 | * malloc() may change blocks by calling morecore() only. |
402 | * |
403 | * morecore() may create new blocks by: |
404 | * a) biting pieces from chunk_chain (cannot create one above last_op); |
405 | * b) biting a piece from an unused block (if block was last_op, this |
406 | * may create a chunk from chain above last_op, thus last_op is |
407 | * invalidated in such a case). |
408 | * c) biting of sbrk()ed-but-not-yet-used space. This creates |
409 | * a block which is last_op. |
410 | * d) Allocating new pages by calling getpages(); |
411 | * |
412 | * getpages() creates a new block. It marks last_op at the bottom of |
413 | * the chunk of memory it returns. |
414 | * |
415 | * Active pages footprint: |
416 | * ~~~~~~~~~~~~~~~~~~~~~~ |
417 | * Note that we do not need to traverse the lists in nextf[i], just take |
418 | * the first element of this list. However, we *need* to traverse the |
419 | * list in chunk_chain, but most the time it should be a very short one, |
420 | * so we do not step on a lot of pages we are not going to use. |
421 | * |
422 | * Flaws: |
423 | * ~~~~~ |
424 | * get_from_bigger_buckets(): forget to increment price => Quite |
425 | * aggressive. |
426 | */ |
427 | |
135863df |
428 | /* I don't much care whether these are defined in sys/types.h--LAW */ |
429 | |
430 | #define u_char unsigned char |
431 | #define u_int unsigned int |
56431972 |
432 | /* |
433 | * I removed the definition of u_bigint which appeared to be u_bigint = UV |
434 | * u_bigint was only used in TWOK_MASKED and TWOK_SHIFT |
435 | * where I have used PTR2UV. RMB |
436 | */ |
135863df |
437 | #define u_short unsigned short |
8d063cd8 |
438 | |
cf5c4ad8 |
439 | /* 286 and atarist like big chunks, which gives too much overhead. */ |
61ae2fbf |
440 | #if (defined(RCHECK) || defined(I286) || defined(atarist) || defined(__MINT__)) && defined(PACK_MALLOC) |
e8bc2b5c |
441 | # undef PACK_MALLOC |
cf5c4ad8 |
442 | #endif |
443 | |
8d063cd8 |
444 | /* |
cf5c4ad8 |
445 | * The description below is applicable if PACK_MALLOC is not defined. |
446 | * |
8d063cd8 |
447 | * The overhead on a block is at least 4 bytes. When free, this space |
448 | * contains a pointer to the next free block, and the bottom two bits must |
449 | * be zero. When in use, the first byte is set to MAGIC, and the second |
450 | * byte is the size index. The remaining bytes are for alignment. |
451 | * If range checking is enabled and the size of the block fits |
452 | * in two bytes, then the top two bytes hold the size of the requested block |
453 | * plus the range checking words, and the header word MINUS ONE. |
454 | */ |
455 | union overhead { |
456 | union overhead *ov_next; /* when free */ |
85e6fe83 |
457 | #if MEM_ALIGNBYTES > 4 |
c623bd54 |
458 | double strut; /* alignment problems */ |
a687059c |
459 | #endif |
8d063cd8 |
460 | struct { |
00ff3b56 |
461 | /* |
462 | * Keep the ovu_index and ovu_magic in this order, having a char |
463 | * field first gives alignment indigestion in some systems, such as |
464 | * MachTen. |
465 | */ |
8d063cd8 |
466 | u_char ovu_index; /* bucket # */ |
b72ff565 |
467 | u_char ovu_magic; /* magic number */ |
8d063cd8 |
468 | #ifdef RCHECK |
469 | u_short ovu_size; /* actual block size */ |
470 | u_int ovu_rmagic; /* range magic number */ |
471 | #endif |
472 | } ovu; |
473 | #define ov_magic ovu.ovu_magic |
474 | #define ov_index ovu.ovu_index |
475 | #define ov_size ovu.ovu_size |
476 | #define ov_rmagic ovu.ovu_rmagic |
477 | }; |
478 | |
479 | #define MAGIC 0xff /* magic # on accounting info */ |
480 | #define RMAGIC 0x55555555 /* magic # on range info */ |
e8bc2b5c |
481 | #define RMAGIC_C 0x55 /* magic # on range info */ |
482 | |
8d063cd8 |
483 | #ifdef RCHECK |
c2a5c2d2 |
484 | # define RSLOP sizeof (u_int) |
485 | # ifdef TWO_POT_OPTIMIZE |
e8bc2b5c |
486 | # define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2) |
c2a5c2d2 |
487 | # else |
e8bc2b5c |
488 | # define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2) |
c2a5c2d2 |
489 | # endif |
8d063cd8 |
490 | #else |
c2a5c2d2 |
491 | # define RSLOP 0 |
8d063cd8 |
492 | #endif |
493 | |
e8bc2b5c |
494 | #if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2) |
495 | # undef BUCKETS_ROOT2 |
496 | #endif |
497 | |
498 | #ifdef BUCKETS_ROOT2 |
499 | # define BUCKET_TABLE_SHIFT 2 |
500 | # define BUCKET_POW2_SHIFT 1 |
501 | # define BUCKETS_PER_POW2 2 |
502 | #else |
503 | # define BUCKET_TABLE_SHIFT MIN_BUC_POW2 |
504 | # define BUCKET_POW2_SHIFT 0 |
505 | # define BUCKETS_PER_POW2 1 |
506 | #endif |
507 | |
274c7500 |
508 | #if !defined(MEM_ALIGNBYTES) || ((MEM_ALIGNBYTES > 4) && !defined(STRICT_ALIGNMENT)) |
509 | /* Figure out the alignment of void*. */ |
510 | struct aligner { |
511 | char c; |
512 | void *p; |
513 | }; |
514 | # define ALIGN_SMALL ((int)((caddr_t)&(((struct aligner*)0)->p))) |
515 | #else |
516 | # define ALIGN_SMALL MEM_ALIGNBYTES |
517 | #endif |
518 | |
519 | #define IF_ALIGN_8(yes,no) ((ALIGN_SMALL>4) ? (yes) : (no)) |
520 | |
e8bc2b5c |
521 | #ifdef BUCKETS_ROOT2 |
522 | # define MAX_BUCKET_BY_TABLE 13 |
523 | static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] = |
524 | { |
525 | 0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80, |
526 | }; |
527 | # define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT))) |
528 | # define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE \ |
529 | ? buck_size[i] \ |
530 | : ((1 << ((i) >> BUCKET_POW2_SHIFT)) \ |
531 | - MEM_OVERHEAD(i) \ |
532 | + POW2_OPTIMIZE_SURPLUS(i))) |
533 | #else |
534 | # define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT)) |
535 | # define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i)) |
536 | #endif |
537 | |
538 | |
cf5c4ad8 |
539 | #ifdef PACK_MALLOC |
4ad56ec9 |
540 | /* In this case there are several possible layout of arenas depending |
541 | * on the size. Arenas are of sizes multiple to 2K, 2K-aligned, and |
542 | * have a size close to a power of 2. |
543 | * |
544 | * Arenas of the size >= 4K keep one chunk only. Arenas of size 2K |
545 | * may keep one chunk or multiple chunks. Here are the possible |
546 | * layouts of arenas: |
547 | * |
548 | * # One chunk only, chunksize 2^k + SOMETHING - ALIGN, k >= 11 |
549 | * |
550 | * INDEX MAGIC1 UNUSED CHUNK1 |
551 | * |
552 | * # Multichunk with sanity checking and chunksize 2^k-ALIGN, k>7 |
553 | * |
554 | * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 CHUNK2 CHUNK3 ... |
555 | * |
556 | * # Multichunk with sanity checking and size 2^k-ALIGN, k=7 |
557 | * |
558 | * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 UNUSED CHUNK2 CHUNK3 ... |
559 | * |
560 | * # Multichunk with sanity checking and size up to 80 |
561 | * |
562 | * INDEX UNUSED MAGIC1 UNUSED MAGIC2 UNUSED ... CHUNK1 CHUNK2 CHUNK3 ... |
563 | * |
564 | * # No sanity check (usually up to 48=byte-long buckets) |
565 | * INDEX UNUSED CHUNK1 CHUNK2 ... |
566 | * |
567 | * Above INDEX and MAGIC are one-byte-long. Sizes of UNUSED are |
568 | * appropriate to keep algorithms simple and memory aligned. INDEX |
569 | * encodes the size of the chunk, while MAGICn encodes state (used, |
570 | * free or non-managed-by-us-so-it-indicates-a-bug) of CHUNKn. MAGIC |
571 | * is used for sanity checking purposes only. SOMETHING is 0 or 4K |
572 | * (to make size of big CHUNK accomodate allocations for powers of two |
573 | * better). |
574 | * |
575 | * [There is no need to alignment between chunks, since C rules ensure |
576 | * that structs which need 2^k alignment have sizeof which is |
577 | * divisible by 2^k. Thus as far as the last chunk is aligned at the |
578 | * end of the arena, and 2K-alignment does not contradict things, |
579 | * everything is going to be OK for sizes of chunks 2^n and 2^n + |
580 | * 2^k. Say, 80-bit buckets will be 16-bit aligned, and as far as we |
581 | * put allocations for requests in 65..80 range, all is fine. |
582 | * |
583 | * Note, however, that standard malloc() puts more strict |
584 | * requirements than the above C rules. Moreover, our algorithms of |
585 | * realloc() may break this idyll, but we suppose that realloc() does |
586 | * need not change alignment.] |
587 | * |
588 | * Is very important to make calculation of the offset of MAGICm as |
589 | * quick as possible, since it is done on each malloc()/free(). In |
590 | * fact it is so quick that it has quite little effect on the speed of |
591 | * doing malloc()/free(). [By default] We forego such calculations |
592 | * for small chunks, but only to save extra 3% of memory, not because |
593 | * of speed considerations. |
594 | * |
595 | * Here is the algorithm [which is the same for all the allocations |
596 | * schemes above], see OV_MAGIC(block,bucket). Let OFFSETm be the |
597 | * offset of the CHUNKm from the start of ARENA. Then offset of |
598 | * MAGICm is (OFFSET1 >> SHIFT) + ADDOFFSET. Here SHIFT and ADDOFFSET |
599 | * are numbers which depend on the size of the chunks only. |
600 | * |
601 | * Let as check some sanity conditions. Numbers OFFSETm>>SHIFT are |
602 | * different for all the chunks in the arena if 2^SHIFT is not greater |
603 | * than size of the chunks in the arena. MAGIC1 will not overwrite |
604 | * INDEX provided ADDOFFSET is >0 if OFFSET1 < 2^SHIFT. MAGIClast |
605 | * will not overwrite CHUNK1 if OFFSET1 > (OFFSETlast >> SHIFT) + |
606 | * ADDOFFSET. |
607 | * |
608 | * Make SHIFT the maximal possible (there is no point in making it |
609 | * smaller). Since OFFSETlast is 2K - CHUNKSIZE, above restrictions |
610 | * give restrictions on OFFSET1 and on ADDOFFSET. |
611 | * |
612 | * In particular, for chunks of size 2^k with k>=6 we can put |
613 | * ADDOFFSET to be from 0 to 2^k - 2^(11-k), and have |
614 | * OFFSET1==chunksize. For chunks of size 80 OFFSET1 of 2K%80=48 is |
615 | * large enough to have ADDOFFSET between 1 and 16 (similarly for 96, |
616 | * when ADDOFFSET should be 1). In particular, keeping MAGICs for |
617 | * these sizes gives no additional size penalty. |
618 | * |
619 | * However, for chunks of size 2^k with k<=5 this gives OFFSET1 >= |
620 | * ADDOFSET + 2^(11-k). Keeping ADDOFFSET 0 allows for 2^(11-k)-2^(11-2k) |
621 | * chunks per arena. This is smaller than 2^(11-k) - 1 which are |
622 | * needed if no MAGIC is kept. [In fact, having a negative ADDOFFSET |
623 | * would allow for slightly more buckets per arena for k=2,3.] |
624 | * |
625 | * Similarly, for chunks of size 3/2*2^k with k<=5 MAGICs would span |
626 | * the area up to 2^(11-k)+ADDOFFSET. For k=4 this give optimal |
627 | * ADDOFFSET as -7..0. For k=3 ADDOFFSET can go up to 4 (with tiny |
628 | * savings for negative ADDOFFSET). For k=5 ADDOFFSET can go -1..16 |
629 | * (with no savings for negative values). |
cf5c4ad8 |
630 | * |
4ad56ec9 |
631 | * In particular, keeping ADDOFFSET 0 for sizes of chunks up to 2^6 |
632 | * leads to tiny pessimizations in case of sizes 4, 8, 12, 24, and |
633 | * leads to no contradictions except for size=80 (or 96.) |
cf5c4ad8 |
634 | * |
4ad56ec9 |
635 | * However, it also makes sense to keep no magic for sizes 48 or less. |
636 | * This is what we do. In this case one needs ADDOFFSET>=1 also for |
637 | * chunksizes 12, 24, and 48, unless one gets one less chunk per |
638 | * arena. |
639 | * |
640 | * The algo of OV_MAGIC(block,bucket) keeps ADDOFFSET 0 until |
641 | * chunksize of 64, then makes it 1. |
cf5c4ad8 |
642 | * |
4ad56ec9 |
643 | * This allows for an additional optimization: the above scheme leads |
644 | * to giant overheads for sizes 128 or more (one whole chunk needs to |
645 | * be sacrifised to keep INDEX). Instead we use chunks not of size |
646 | * 2^k, but of size 2^k-ALIGN. If we pack these chunks at the end of |
647 | * the arena, then the beginnings are still in different 2^k-long |
648 | * sections of the arena if k>=7 for ALIGN==4, and k>=8 if ALIGN=8. |
649 | * Thus for k>7 the above algo of calculating the offset of the magic |
650 | * will still give different answers for different chunks. And to |
651 | * avoid the overrun of MAGIC1 into INDEX, one needs ADDOFFSET of >=1. |
652 | * In the case k=7 we just move the first chunk an extra ALIGN |
653 | * backward inside the ARENA (this is done once per arena lifetime, |
654 | * thus is not a big overhead). */ |
e8bc2b5c |
655 | # define MAX_PACKED_POW2 6 |
656 | # define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT) |
657 | # define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD) |
658 | # define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1) |
56431972 |
659 | # define TWOK_MASKED(x) (PTR2UV(x) & ~TWOK_MASK) |
660 | # define TWOK_SHIFT(x) (PTR2UV(x) & TWOK_MASK) |
661 | # define OV_INDEXp(block) (INT2PTR(u_char*,TWOK_MASKED(block))) |
cf5c4ad8 |
662 | # define OV_INDEX(block) (*OV_INDEXp(block)) |
663 | # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \ |
e8bc2b5c |
664 | (TWOK_SHIFT(block)>> \ |
665 | (bucket>>BUCKET_POW2_SHIFT)) + \ |
666 | (bucket >= MIN_NEEDS_SHIFT ? 1 : 0))) |
667 | /* A bucket can have a shift smaller than it size, we need to |
668 | shift its magic number so it will not overwrite index: */ |
669 | # ifdef BUCKETS_ROOT2 |
670 | # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */ |
671 | # else |
672 | # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */ |
673 | # endif |
cf5c4ad8 |
674 | # define CHUNK_SHIFT 0 |
675 | |
e8bc2b5c |
676 | /* Number of active buckets of given ordinal. */ |
677 | #ifdef IGNORE_SMALL_BAD_FREE |
678 | #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */ |
679 | # define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \ |
680 | ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \ |
681 | : n_blks[bucket] ) |
682 | #else |
683 | # define N_BLKS(bucket) n_blks[bucket] |
684 | #endif |
685 | |
686 | static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = |
687 | { |
688 | # if BUCKETS_PER_POW2==1 |
689 | 0, 0, |
690 | (MIN_BUC_POW2==2 ? 384 : 0), |
691 | 224, 120, 62, 31, 16, 8, 4, 2 |
692 | # else |
693 | 0, 0, 0, 0, |
694 | (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */ |
695 | 224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2 |
696 | # endif |
697 | }; |
698 | |
699 | /* Shift of the first bucket with the given ordinal inside 2K chunk. */ |
700 | #ifdef IGNORE_SMALL_BAD_FREE |
701 | # define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \ |
702 | ? ((1<<LOG_OF_MIN_ARENA) \ |
703 | - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \ |
704 | : blk_shift[bucket]) |
705 | #else |
706 | # define BLK_SHIFT(bucket) blk_shift[bucket] |
707 | #endif |
708 | |
709 | static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = |
710 | { |
711 | # if BUCKETS_PER_POW2==1 |
712 | 0, 0, |
713 | (MIN_BUC_POW2==2 ? 512 : 0), |
714 | 256, 128, 64, 64, /* 8 to 64 */ |
715 | 16*sizeof(union overhead), |
716 | 8*sizeof(union overhead), |
717 | 4*sizeof(union overhead), |
718 | 2*sizeof(union overhead), |
719 | # else |
720 | 0, 0, 0, 0, |
721 | (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0), |
722 | 256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */ |
723 | 16*sizeof(union overhead), 16*sizeof(union overhead), |
724 | 8*sizeof(union overhead), 8*sizeof(union overhead), |
725 | 4*sizeof(union overhead), 4*sizeof(union overhead), |
726 | 2*sizeof(union overhead), 2*sizeof(union overhead), |
727 | # endif |
728 | }; |
cf5c4ad8 |
729 | |
5bbd1ef5 |
730 | # define NEEDED_ALIGNMENT 0x800 /* 2k boundaries */ |
731 | # define WANTED_ALIGNMENT 0x800 /* 2k boundaries */ |
732 | |
cf5c4ad8 |
733 | #else /* !PACK_MALLOC */ |
734 | |
735 | # define OV_MAGIC(block,bucket) (block)->ov_magic |
736 | # define OV_INDEX(block) (block)->ov_index |
737 | # define CHUNK_SHIFT 1 |
e8bc2b5c |
738 | # define MAX_PACKED -1 |
5bbd1ef5 |
739 | # define NEEDED_ALIGNMENT MEM_ALIGNBYTES |
740 | # define WANTED_ALIGNMENT 0x400 /* 1k boundaries */ |
741 | |
cf5c4ad8 |
742 | #endif /* !PACK_MALLOC */ |
743 | |
e8bc2b5c |
744 | #define M_OVERHEAD (sizeof(union overhead) + RSLOP) |
745 | |
746 | #ifdef PACK_MALLOC |
747 | # define MEM_OVERHEAD(bucket) \ |
748 | (bucket <= MAX_PACKED ? 0 : M_OVERHEAD) |
749 | # ifdef SMALL_BUCKET_VIA_TABLE |
750 | # define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2) |
751 | # define START_SHIFT MAX_PACKED_POW2 |
752 | # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */ |
753 | # define SIZE_TABLE_MAX 80 |
754 | # else |
755 | # define SIZE_TABLE_MAX 64 |
756 | # endif |
757 | static char bucket_of[] = |
758 | { |
759 | # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */ |
760 | /* 0 to 15 in 4-byte increments. */ |
761 | (sizeof(void*) > 4 ? 6 : 5), /* 4/8, 5-th bucket for better reports */ |
762 | 6, /* 8 */ |
274c7500 |
763 | IF_ALIGN_8(8,7), 8, /* 16/12, 16 */ |
e8bc2b5c |
764 | 9, 9, 10, 10, /* 24, 32 */ |
765 | 11, 11, 11, 11, /* 48 */ |
766 | 12, 12, 12, 12, /* 64 */ |
767 | 13, 13, 13, 13, /* 80 */ |
768 | 13, 13, 13, 13 /* 80 */ |
769 | # else /* !BUCKETS_ROOT2 */ |
770 | /* 0 to 15 in 4-byte increments. */ |
771 | (sizeof(void*) > 4 ? 3 : 2), |
772 | 3, |
773 | 4, 4, |
774 | 5, 5, 5, 5, |
775 | 6, 6, 6, 6, |
776 | 6, 6, 6, 6 |
777 | # endif /* !BUCKETS_ROOT2 */ |
778 | }; |
779 | # else /* !SMALL_BUCKET_VIA_TABLE */ |
780 | # define START_SHIFTS_BUCKET MIN_BUCKET |
781 | # define START_SHIFT (MIN_BUC_POW2 - 1) |
782 | # endif /* !SMALL_BUCKET_VIA_TABLE */ |
783 | #else /* !PACK_MALLOC */ |
784 | # define MEM_OVERHEAD(bucket) M_OVERHEAD |
785 | # ifdef SMALL_BUCKET_VIA_TABLE |
786 | # undef SMALL_BUCKET_VIA_TABLE |
787 | # endif |
788 | # define START_SHIFTS_BUCKET MIN_BUCKET |
789 | # define START_SHIFT (MIN_BUC_POW2 - 1) |
790 | #endif /* !PACK_MALLOC */ |
cf5c4ad8 |
791 | |
8d063cd8 |
792 | /* |
55497cff |
793 | * Big allocations are often of the size 2^n bytes. To make them a |
794 | * little bit better, make blocks of size 2^n+pagesize for big n. |
795 | */ |
796 | |
797 | #ifdef TWO_POT_OPTIMIZE |
798 | |
5f05dabc |
799 | # ifndef PERL_PAGESIZE |
800 | # define PERL_PAGESIZE 4096 |
801 | # endif |
e8bc2b5c |
802 | # ifndef FIRST_BIG_POW2 |
803 | # define FIRST_BIG_POW2 15 /* 32K, 16K is used too often. */ |
5f05dabc |
804 | # endif |
e8bc2b5c |
805 | # define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2) |
55497cff |
806 | /* If this value or more, check against bigger blocks. */ |
807 | # define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD) |
808 | /* If less than this value, goes into 2^n-overhead-block. */ |
809 | # define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD) |
810 | |
e8bc2b5c |
811 | # define POW2_OPTIMIZE_ADJUST(nbytes) \ |
812 | ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0) |
813 | # define POW2_OPTIMIZE_SURPLUS(bucket) \ |
814 | ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0) |
815 | |
816 | #else /* !TWO_POT_OPTIMIZE */ |
817 | # define POW2_OPTIMIZE_ADJUST(nbytes) |
818 | # define POW2_OPTIMIZE_SURPLUS(bucket) 0 |
819 | #endif /* !TWO_POT_OPTIMIZE */ |
820 | |
821 | #if defined(HAS_64K_LIMIT) && defined(PERL_CORE) |
822 | # define BARK_64K_LIMIT(what,nbytes,size) \ |
823 | if (nbytes > 0xffff) { \ |
824 | PerlIO_printf(PerlIO_stderr(), \ |
825 | "%s too large: %lx\n", what, size); \ |
826 | my_exit(1); \ |
827 | } |
828 | #else /* !HAS_64K_LIMIT || !PERL_CORE */ |
829 | # define BARK_64K_LIMIT(what,nbytes,size) |
830 | #endif /* !HAS_64K_LIMIT || !PERL_CORE */ |
55497cff |
831 | |
e8bc2b5c |
832 | #ifndef MIN_SBRK |
833 | # define MIN_SBRK 2048 |
834 | #endif |
835 | |
836 | #ifndef FIRST_SBRK |
d720c441 |
837 | # define FIRST_SBRK (48*1024) |
e8bc2b5c |
838 | #endif |
839 | |
840 | /* Minimal sbrk in percents of what is already alloced. */ |
841 | #ifndef MIN_SBRK_FRAC |
842 | # define MIN_SBRK_FRAC 3 |
843 | #endif |
844 | |
845 | #ifndef SBRK_ALLOW_FAILURES |
846 | # define SBRK_ALLOW_FAILURES 3 |
847 | #endif |
55497cff |
848 | |
e8bc2b5c |
849 | #ifndef SBRK_FAILURE_PRICE |
850 | # define SBRK_FAILURE_PRICE 50 |
55497cff |
851 | #endif |
852 | |
24dd13bf |
853 | static void morecore (register int bucket); |
854 | # if defined(DEBUGGING) |
855 | static void botch (char *diag, char *s); |
856 | # endif |
857 | static void add_to_chain (void *p, MEM_SIZE size, MEM_SIZE chip); |
858 | static void* get_from_chain (MEM_SIZE size); |
859 | static void* get_from_bigger_buckets(int bucket, MEM_SIZE size); |
860 | static union overhead *getpages (MEM_SIZE needed, int *nblksp, int bucket); |
861 | static int getpages_adjacent(MEM_SIZE require); |
862 | |
febabd2a |
863 | #ifdef PERL_CORE |
e8bc2b5c |
864 | |
3541dd58 |
865 | #ifdef I_MACH_CTHREADS |
772fe5b3 |
866 | # undef MUTEX_LOCK |
867 | # define MUTEX_LOCK(m) STMT_START { if (*m) mutex_lock(*m); } STMT_END |
868 | # undef MUTEX_UNLOCK |
869 | # define MUTEX_UNLOCK(m) STMT_START { if (*m) mutex_unlock(*m); } STMT_END |
3541dd58 |
870 | #endif |
871 | |
b022d2d2 |
872 | #ifndef BITS_IN_PTR |
873 | # define BITS_IN_PTR (8*PTRSIZE) |
874 | #endif |
875 | |
876 | /* |
877 | * nextf[i] is the pointer to the next free block of size 2^i. The |
878 | * smallest allocatable block is 8 bytes. The overhead information |
879 | * precedes the data area returned to the user. |
880 | */ |
881 | #define NBUCKETS (BITS_IN_PTR*BUCKETS_PER_POW2 + 1) |
882 | static union overhead *nextf[NBUCKETS]; |
883 | |
884 | #if defined(PURIFY) && !defined(USE_PERL_SBRK) |
885 | # define USE_PERL_SBRK |
886 | #endif |
887 | |
888 | #ifdef USE_PERL_SBRK |
3f270f98 |
889 | # define sbrk(a) Perl_sbrk(a) |
b022d2d2 |
890 | Malloc_t Perl_sbrk (int size); |
b022d2d2 |
891 | #else |
3f270f98 |
892 | # ifndef HAS_SBRK_PROTO /* <unistd.h> usually takes care of this */ |
b022d2d2 |
893 | extern Malloc_t sbrk(int); |
3f270f98 |
894 | # endif |
ef9f17be |
895 | #endif |
b022d2d2 |
896 | |
897 | #ifdef DEBUGGING_MSTATS |
898 | /* |
899 | * nmalloc[i] is the difference between the number of mallocs and frees |
900 | * for a given block size. |
901 | */ |
902 | static u_int nmalloc[NBUCKETS]; |
903 | static u_int sbrk_slack; |
904 | static u_int start_slack; |
905 | #else /* !( defined DEBUGGING_MSTATS ) */ |
906 | # define sbrk_slack 0 |
907 | #endif |
908 | |
909 | static u_int goodsbrk; |
910 | |
febabd2a |
911 | # ifdef PERL_EMERGENCY_SBRK |
912 | |
913 | # ifndef BIG_SIZE |
914 | # define BIG_SIZE (1<<16) /* 64K */ |
915 | # endif |
916 | |
55497cff |
917 | static char *emergency_buffer; |
918 | static MEM_SIZE emergency_buffer_size; |
b022d2d2 |
919 | static int no_mem; /* 0 if the last request for more memory succeeded. |
920 | Otherwise the size of the failing request. */ |
55497cff |
921 | |
cea2e8a9 |
922 | static Malloc_t |
923 | emergency_sbrk(MEM_SIZE size) |
55497cff |
924 | { |
28ac10b1 |
925 | MEM_SIZE rsize = (((size - 1)>>LOG_OF_MIN_ARENA) + 1)<<LOG_OF_MIN_ARENA; |
926 | |
b022d2d2 |
927 | if (size >= BIG_SIZE && (!no_mem || (size < no_mem))) { |
928 | /* Give the possibility to recover, but avoid an infinite cycle. */ |
741df71a |
929 | MALLOC_UNLOCK; |
b022d2d2 |
930 | no_mem = size; |
931 | croak2("Out of memory during \"large\" request for %"UVuf" bytes, total sbrk() is %"UVuf" bytes", (UV)size, (UV)(goodsbrk + sbrk_slack)); |
55497cff |
932 | } |
933 | |
28ac10b1 |
934 | if (emergency_buffer_size >= rsize) { |
935 | char *old = emergency_buffer; |
936 | |
937 | emergency_buffer_size -= rsize; |
938 | emergency_buffer += rsize; |
939 | return old; |
940 | } else { |
cea2e8a9 |
941 | dTHX; |
55497cff |
942 | /* First offense, give a possibility to recover by dieing. */ |
943 | /* No malloc involved here: */ |
4a33f861 |
944 | GV **gvp = (GV**)hv_fetch(PL_defstash, "^M", 2, 0); |
55497cff |
945 | SV *sv; |
946 | char *pv; |
28ac10b1 |
947 | int have = 0; |
2d8e6c8d |
948 | STRLEN n_a; |
55497cff |
949 | |
28ac10b1 |
950 | if (emergency_buffer_size) { |
951 | add_to_chain(emergency_buffer, emergency_buffer_size, 0); |
952 | emergency_buffer_size = 0; |
953 | emergency_buffer = Nullch; |
954 | have = 1; |
955 | } |
4a33f861 |
956 | if (!gvp) gvp = (GV**)hv_fetch(PL_defstash, "\015", 1, 0); |
55497cff |
957 | if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv) |
28ac10b1 |
958 | || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD)) { |
959 | if (have) |
960 | goto do_croak; |
55497cff |
961 | return (char *)-1; /* Now die die die... */ |
28ac10b1 |
962 | } |
55497cff |
963 | /* Got it, now detach SvPV: */ |
2d8e6c8d |
964 | pv = SvPV(sv, n_a); |
55497cff |
965 | /* Check alignment: */ |
56431972 |
966 | if ((PTR2UV(pv) - sizeof(union overhead)) & (NEEDED_ALIGNMENT - 1)) { |
55497cff |
967 | PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n"); |
bbce6d69 |
968 | return (char *)-1; /* die die die */ |
55497cff |
969 | } |
970 | |
28ac10b1 |
971 | emergency_buffer = pv - sizeof(union overhead); |
972 | emergency_buffer_size = malloced_size(pv) + M_OVERHEAD; |
55497cff |
973 | SvPOK_off(sv); |
28ac10b1 |
974 | SvPVX(sv) = Nullch; |
975 | SvCUR(sv) = SvLEN(sv) = 0; |
55497cff |
976 | } |
28ac10b1 |
977 | do_croak: |
741df71a |
978 | MALLOC_UNLOCK; |
b022d2d2 |
979 | croak("Out of memory during request for %"UVuf" bytes, total sbrk() is %"UVuf" bytes", (UV)size, (UV)(goodsbrk + sbrk_slack)); |
ce70748c |
980 | /* NOTREACHED */ |
981 | return Nullch; |
55497cff |
982 | } |
983 | |
febabd2a |
984 | # else /* !defined(PERL_EMERGENCY_SBRK) */ |
55497cff |
985 | # define emergency_sbrk(size) -1 |
febabd2a |
986 | # endif |
987 | #endif /* ifdef PERL_CORE */ |
55497cff |
988 | |
760ac839 |
989 | #ifdef DEBUGGING |
3541dd58 |
990 | #undef ASSERT |
991 | #define ASSERT(p,diag) if (!(p)) botch(diag,STRINGIFY(p)); else |
cea2e8a9 |
992 | static void |
993 | botch(char *diag, char *s) |
8d063cd8 |
994 | { |
e8cd8248 |
995 | dTHX; |
d720c441 |
996 | PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s); |
3028581b |
997 | PerlProc_abort(); |
8d063cd8 |
998 | } |
999 | #else |
3541dd58 |
1000 | #define ASSERT(p, diag) |
8d063cd8 |
1001 | #endif |
1002 | |
2304df62 |
1003 | Malloc_t |
86058a2d |
1004 | Perl_malloc(register size_t nbytes) |
8d063cd8 |
1005 | { |
1006 | register union overhead *p; |
e8bc2b5c |
1007 | register int bucket; |
ee0007ab |
1008 | register MEM_SIZE shiftr; |
8d063cd8 |
1009 | |
c2a5c2d2 |
1010 | #if defined(DEBUGGING) || defined(RCHECK) |
ee0007ab |
1011 | MEM_SIZE size = nbytes; |
45d8adaa |
1012 | #endif |
1013 | |
e8bc2b5c |
1014 | BARK_64K_LIMIT("Allocation",nbytes,nbytes); |
45d8adaa |
1015 | #ifdef DEBUGGING |
1016 | if ((long)nbytes < 0) |
cea2e8a9 |
1017 | croak("%s", "panic: malloc"); |
45d8adaa |
1018 | #endif |
45d8adaa |
1019 | |
8d063cd8 |
1020 | /* |
1021 | * Convert amount of memory requested into |
1022 | * closest block size stored in hash buckets |
1023 | * which satisfies request. Account for |
1024 | * space used per block for accounting. |
1025 | */ |
cf5c4ad8 |
1026 | #ifdef PACK_MALLOC |
e8bc2b5c |
1027 | # ifdef SMALL_BUCKET_VIA_TABLE |
1028 | if (nbytes == 0) |
1029 | bucket = MIN_BUCKET; |
1030 | else if (nbytes <= SIZE_TABLE_MAX) { |
1031 | bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT]; |
1032 | } else |
1033 | # else |
043bf814 |
1034 | if (nbytes == 0) |
1035 | nbytes = 1; |
e8bc2b5c |
1036 | if (nbytes <= MAX_POW2_ALGO) goto do_shifts; |
1037 | else |
1038 | # endif |
55497cff |
1039 | #endif |
e8bc2b5c |
1040 | { |
1041 | POW2_OPTIMIZE_ADJUST(nbytes); |
1042 | nbytes += M_OVERHEAD; |
1043 | nbytes = (nbytes + 3) &~ 3; |
1044 | do_shifts: |
1045 | shiftr = (nbytes - 1) >> START_SHIFT; |
1046 | bucket = START_SHIFTS_BUCKET; |
1047 | /* apart from this loop, this is O(1) */ |
1048 | while (shiftr >>= 1) |
1049 | bucket += BUCKETS_PER_POW2; |
cf5c4ad8 |
1050 | } |
4ad56ec9 |
1051 | MALLOC_LOCK; |
8d063cd8 |
1052 | /* |
1053 | * If nothing in hash bucket right now, |
1054 | * request more memory from the system. |
1055 | */ |
1056 | if (nextf[bucket] == NULL) |
1057 | morecore(bucket); |
e8bc2b5c |
1058 | if ((p = nextf[bucket]) == NULL) { |
741df71a |
1059 | MALLOC_UNLOCK; |
55497cff |
1060 | #ifdef PERL_CORE |
0b250b9e |
1061 | { |
1062 | dTHX; |
1063 | if (!PL_nomemok) { |
febabd2a |
1064 | #if defined(PLAIN_MALLOC) && defined(NO_FANCY_MALLOC) |
1065 | PerlIO_puts(PerlIO_stderr(),"Out of memory!\n"); |
1066 | #else |
b022d2d2 |
1067 | char buff[80]; |
1068 | char *eb = buff + sizeof(buff) - 1; |
1069 | char *s = eb; |
1070 | size_t n = nbytes; |
1071 | |
1072 | PerlIO_puts(PerlIO_stderr(),"Out of memory during request for "); |
1073 | #if defined(DEBUGGING) || defined(RCHECK) |
1074 | n = size; |
1075 | #endif |
1076 | *s = 0; |
1077 | do { |
1078 | *--s = '0' + (n % 10); |
1079 | } while (n /= 10); |
1080 | PerlIO_puts(PerlIO_stderr(),s); |
1081 | PerlIO_puts(PerlIO_stderr()," bytes, total sbrk() is "); |
1082 | s = eb; |
1083 | n = goodsbrk + sbrk_slack; |
1084 | do { |
1085 | *--s = '0' + (n % 10); |
1086 | } while (n /= 10); |
1087 | PerlIO_puts(PerlIO_stderr(),s); |
1088 | PerlIO_puts(PerlIO_stderr()," bytes!\n"); |
febabd2a |
1089 | #endif /* defined(PLAIN_MALLOC) && defined(NO_FANCY_MALLOC) */ |
0b250b9e |
1090 | my_exit(1); |
1091 | } |
ee0007ab |
1092 | } |
45d8adaa |
1093 | #endif |
4ad56ec9 |
1094 | return (NULL); |
45d8adaa |
1095 | } |
1096 | |
e8bc2b5c |
1097 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
b900a521 |
1098 | "0x%"UVxf": (%05lu) malloc %ld bytes\n", |
1099 | PTR2UV(p+1), (unsigned long)(PL_an++), |
e8bc2b5c |
1100 | (long)size)); |
45d8adaa |
1101 | |
8d063cd8 |
1102 | /* remove from linked list */ |
802004fa |
1103 | #if defined(RCHECK) |
32e30700 |
1104 | if ((PTR2UV(p)) & (MEM_ALIGNBYTES - 1)) { |
e8cd8248 |
1105 | dTHX; |
b900a521 |
1106 | PerlIO_printf(PerlIO_stderr(), |
7fa2f4f1 |
1107 | "Unaligned pointer in the free chain 0x%"UVxf"\n", |
1108 | PTR2UV(p)); |
1109 | } |
1110 | if ((PTR2UV(p->ov_next)) & (MEM_ALIGNBYTES - 1)) { |
e8cd8248 |
1111 | dTHX; |
7fa2f4f1 |
1112 | PerlIO_printf(PerlIO_stderr(), |
1113 | "Unaligned `next' pointer in the free " |
d2560b70 |
1114 | "chain 0x%"UVxf" at 0x%"UVxf"\n", |
7fa2f4f1 |
1115 | PTR2UV(p->ov_next), PTR2UV(p)); |
32e30700 |
1116 | } |
bf38876a |
1117 | #endif |
1118 | nextf[bucket] = p->ov_next; |
4ad56ec9 |
1119 | |
1120 | MALLOC_UNLOCK; |
1121 | |
e8bc2b5c |
1122 | #ifdef IGNORE_SMALL_BAD_FREE |
1123 | if (bucket >= FIRST_BUCKET_WITH_CHECK) |
1124 | #endif |
1125 | OV_MAGIC(p, bucket) = MAGIC; |
cf5c4ad8 |
1126 | #ifndef PACK_MALLOC |
1127 | OV_INDEX(p) = bucket; |
1128 | #endif |
8d063cd8 |
1129 | #ifdef RCHECK |
1130 | /* |
1131 | * Record allocated size of block and |
1132 | * bound space with magic numbers. |
1133 | */ |
8d063cd8 |
1134 | p->ov_rmagic = RMAGIC; |
e8bc2b5c |
1135 | if (bucket <= MAX_SHORT_BUCKET) { |
1136 | int i; |
1137 | |
1138 | nbytes = size + M_OVERHEAD; |
1139 | p->ov_size = nbytes - 1; |
1140 | if ((i = nbytes & 3)) { |
1141 | i = 4 - i; |
1142 | while (i--) |
1143 | *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C; |
1144 | } |
1145 | nbytes = (nbytes + 3) &~ 3; |
1146 | *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; |
1147 | } |
8d063cd8 |
1148 | #endif |
cf5c4ad8 |
1149 | return ((Malloc_t)(p + CHUNK_SHIFT)); |
8d063cd8 |
1150 | } |
1151 | |
e8bc2b5c |
1152 | static char *last_sbrk_top; |
1153 | static char *last_op; /* This arena can be easily extended. */ |
1154 | static int sbrked_remains; |
1155 | static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE; |
1156 | |
1157 | #ifdef DEBUGGING_MSTATS |
1158 | static int sbrks; |
1159 | #endif |
1160 | |
1161 | struct chunk_chain_s { |
1162 | struct chunk_chain_s *next; |
1163 | MEM_SIZE size; |
1164 | }; |
1165 | static struct chunk_chain_s *chunk_chain; |
1166 | static int n_chunks; |
1167 | static char max_bucket; |
1168 | |
1169 | /* Cutoff a piece of one of the chunks in the chain. Prefer smaller chunk. */ |
cea2e8a9 |
1170 | static void * |
1171 | get_from_chain(MEM_SIZE size) |
e8bc2b5c |
1172 | { |
1173 | struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain; |
1174 | struct chunk_chain_s **oldgoodp = NULL; |
1175 | long min_remain = LONG_MAX; |
1176 | |
1177 | while (elt) { |
1178 | if (elt->size >= size) { |
1179 | long remains = elt->size - size; |
1180 | if (remains >= 0 && remains < min_remain) { |
1181 | oldgoodp = oldp; |
1182 | min_remain = remains; |
1183 | } |
1184 | if (remains == 0) { |
1185 | break; |
1186 | } |
1187 | } |
1188 | oldp = &( elt->next ); |
1189 | elt = elt->next; |
1190 | } |
1191 | if (!oldgoodp) return NULL; |
1192 | if (min_remain) { |
1193 | void *ret = *oldgoodp; |
1194 | struct chunk_chain_s *next = (*oldgoodp)->next; |
1195 | |
1196 | *oldgoodp = (struct chunk_chain_s *)((char*)ret + size); |
1197 | (*oldgoodp)->size = min_remain; |
1198 | (*oldgoodp)->next = next; |
1199 | return ret; |
1200 | } else { |
1201 | void *ret = *oldgoodp; |
1202 | *oldgoodp = (*oldgoodp)->next; |
1203 | n_chunks--; |
1204 | return ret; |
1205 | } |
1206 | } |
1207 | |
cea2e8a9 |
1208 | static void |
1209 | add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip) |
e8bc2b5c |
1210 | { |
1211 | struct chunk_chain_s *next = chunk_chain; |
1212 | char *cp = (char*)p; |
1213 | |
1214 | cp += chip; |
1215 | chunk_chain = (struct chunk_chain_s *)cp; |
1216 | chunk_chain->size = size - chip; |
1217 | chunk_chain->next = next; |
1218 | n_chunks++; |
1219 | } |
1220 | |
cea2e8a9 |
1221 | static void * |
1222 | get_from_bigger_buckets(int bucket, MEM_SIZE size) |
e8bc2b5c |
1223 | { |
1224 | int price = 1; |
1225 | static int bucketprice[NBUCKETS]; |
1226 | while (bucket <= max_bucket) { |
1227 | /* We postpone stealing from bigger buckets until we want it |
1228 | often enough. */ |
1229 | if (nextf[bucket] && bucketprice[bucket]++ >= price) { |
1230 | /* Steal it! */ |
1231 | void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT); |
1232 | bucketprice[bucket] = 0; |
1233 | if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) { |
1234 | last_op = NULL; /* Disable optimization */ |
1235 | } |
1236 | nextf[bucket] = nextf[bucket]->ov_next; |
1237 | #ifdef DEBUGGING_MSTATS |
1238 | nmalloc[bucket]--; |
1239 | start_slack -= M_OVERHEAD; |
1240 | #endif |
1241 | add_to_chain(ret, (BUCKET_SIZE(bucket) + |
1242 | POW2_OPTIMIZE_SURPLUS(bucket)), |
1243 | size); |
1244 | return ret; |
1245 | } |
1246 | bucket++; |
1247 | } |
1248 | return NULL; |
1249 | } |
1250 | |
cea2e8a9 |
1251 | static union overhead * |
c7374474 |
1252 | getpages(MEM_SIZE needed, int *nblksp, int bucket) |
fa423c5b |
1253 | { |
1254 | /* Need to do (possibly expensive) system call. Try to |
1255 | optimize it for rare calling. */ |
1256 | MEM_SIZE require = needed - sbrked_remains; |
1257 | char *cp; |
1258 | union overhead *ovp; |
c7374474 |
1259 | MEM_SIZE slack = 0; |
fa423c5b |
1260 | |
1261 | if (sbrk_good > 0) { |
1262 | if (!last_sbrk_top && require < FIRST_SBRK) |
1263 | require = FIRST_SBRK; |
1264 | else if (require < MIN_SBRK) require = MIN_SBRK; |
1265 | |
1266 | if (require < goodsbrk * MIN_SBRK_FRAC / 100) |
1267 | require = goodsbrk * MIN_SBRK_FRAC / 100; |
1268 | require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK; |
1269 | } else { |
1270 | require = needed; |
1271 | last_sbrk_top = 0; |
1272 | sbrked_remains = 0; |
1273 | } |
1274 | |
1275 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1276 | "sbrk(%ld) for %ld-byte-long arena\n", |
1277 | (long)require, (long) needed)); |
1278 | cp = (char *)sbrk(require); |
1279 | #ifdef DEBUGGING_MSTATS |
1280 | sbrks++; |
1281 | #endif |
1282 | if (cp == last_sbrk_top) { |
1283 | /* Common case, anything is fine. */ |
1284 | sbrk_good++; |
1285 | ovp = (union overhead *) (cp - sbrked_remains); |
e9397286 |
1286 | last_op = cp - sbrked_remains; |
fa423c5b |
1287 | sbrked_remains = require - (needed - sbrked_remains); |
1288 | } else if (cp == (char *)-1) { /* no more room! */ |
1289 | ovp = (union overhead *)emergency_sbrk(needed); |
1290 | if (ovp == (union overhead *)-1) |
1291 | return 0; |
e9397286 |
1292 | if (((char*)ovp) > last_op) { /* Cannot happen with current emergency_sbrk() */ |
1293 | last_op = 0; |
1294 | } |
fa423c5b |
1295 | return ovp; |
1296 | } else { /* Non-continuous or first sbrk(). */ |
1297 | long add = sbrked_remains; |
1298 | char *newcp; |
1299 | |
1300 | if (sbrked_remains) { /* Put rest into chain, we |
1301 | cannot use it right now. */ |
1302 | add_to_chain((void*)(last_sbrk_top - sbrked_remains), |
1303 | sbrked_remains, 0); |
1304 | } |
1305 | |
1306 | /* Second, check alignment. */ |
1307 | slack = 0; |
1308 | |
61ae2fbf |
1309 | #if !defined(atarist) && !defined(__MINT__) /* on the atari we dont have to worry about this */ |
fa423c5b |
1310 | # ifndef I286 /* The sbrk(0) call on the I286 always returns the next segment */ |
5bbd1ef5 |
1311 | /* WANTED_ALIGNMENT may be more than NEEDED_ALIGNMENT, but this may |
1312 | improve performance of memory access. */ |
56431972 |
1313 | if (PTR2UV(cp) & (WANTED_ALIGNMENT - 1)) { /* Not aligned. */ |
1314 | slack = WANTED_ALIGNMENT - (PTR2UV(cp) & (WANTED_ALIGNMENT - 1)); |
fa423c5b |
1315 | add += slack; |
1316 | } |
1317 | # endif |
61ae2fbf |
1318 | #endif /* !atarist && !MINT */ |
fa423c5b |
1319 | |
1320 | if (add) { |
1321 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1322 | "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", |
1323 | (long)add, (long) slack, |
1324 | (long) sbrked_remains)); |
1325 | newcp = (char *)sbrk(add); |
1326 | #if defined(DEBUGGING_MSTATS) |
1327 | sbrks++; |
1328 | sbrk_slack += add; |
1329 | #endif |
1330 | if (newcp != cp + require) { |
1331 | /* Too bad: even rounding sbrk() is not continuous.*/ |
1332 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1333 | "failed to fix bad sbrk()\n")); |
1334 | #ifdef PACK_MALLOC |
1335 | if (slack) { |
741df71a |
1336 | MALLOC_UNLOCK; |
5bbd1ef5 |
1337 | fatalcroak("panic: Off-page sbrk\n"); |
fa423c5b |
1338 | } |
1339 | #endif |
1340 | if (sbrked_remains) { |
1341 | /* Try again. */ |
1342 | #if defined(DEBUGGING_MSTATS) |
1343 | sbrk_slack += require; |
1344 | #endif |
1345 | require = needed; |
1346 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1347 | "straight sbrk(%ld)\n", |
1348 | (long)require)); |
1349 | cp = (char *)sbrk(require); |
1350 | #ifdef DEBUGGING_MSTATS |
1351 | sbrks++; |
1352 | #endif |
1353 | if (cp == (char *)-1) |
1354 | return 0; |
1355 | } |
1356 | sbrk_good = -1; /* Disable optimization! |
1357 | Continue with not-aligned... */ |
1358 | } else { |
1359 | cp += slack; |
1360 | require += sbrked_remains; |
1361 | } |
1362 | } |
1363 | |
1364 | if (last_sbrk_top) { |
1365 | sbrk_good -= SBRK_FAILURE_PRICE; |
1366 | } |
1367 | |
1368 | ovp = (union overhead *) cp; |
1369 | /* |
1370 | * Round up to minimum allocation size boundary |
1371 | * and deduct from block count to reflect. |
1372 | */ |
1373 | |
5bbd1ef5 |
1374 | # if NEEDED_ALIGNMENT > MEM_ALIGNBYTES |
56431972 |
1375 | if (PTR2UV(ovp) & (NEEDED_ALIGNMENT - 1)) |
5bbd1ef5 |
1376 | fatalcroak("Misalignment of sbrk()\n"); |
1377 | else |
1378 | # endif |
fa423c5b |
1379 | #ifndef I286 /* Again, this should always be ok on an 80286 */ |
56431972 |
1380 | if (PTR2UV(ovp) & (MEM_ALIGNBYTES - 1)) { |
fa423c5b |
1381 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1382 | "fixing sbrk(): %d bytes off machine alignement\n", |
56431972 |
1383 | (int)(PTR2UV(ovp) & (MEM_ALIGNBYTES - 1)))); |
1384 | ovp = INT2PTR(union overhead *,(PTR2UV(ovp) + MEM_ALIGNBYTES) & |
5bbd1ef5 |
1385 | (MEM_ALIGNBYTES - 1)); |
fa423c5b |
1386 | (*nblksp)--; |
1387 | # if defined(DEBUGGING_MSTATS) |
1388 | /* This is only approx. if TWO_POT_OPTIMIZE: */ |
5bbd1ef5 |
1389 | sbrk_slack += (1 << (bucket >> BUCKET_POW2_SHIFT)); |
fa423c5b |
1390 | # endif |
1391 | } |
1392 | #endif |
5bbd1ef5 |
1393 | ; /* Finish `else' */ |
fa423c5b |
1394 | sbrked_remains = require - needed; |
e9397286 |
1395 | last_op = cp; |
fa423c5b |
1396 | } |
febabd2a |
1397 | #if !defined(PLAIN_MALLOC) && !defined(NO_FANCY_MALLOC) |
b022d2d2 |
1398 | no_mem = 0; |
febabd2a |
1399 | #endif |
fa423c5b |
1400 | last_sbrk_top = cp + require; |
fa423c5b |
1401 | #ifdef DEBUGGING_MSTATS |
1402 | goodsbrk += require; |
1403 | #endif |
1404 | return ovp; |
1405 | } |
1406 | |
cea2e8a9 |
1407 | static int |
c7374474 |
1408 | getpages_adjacent(MEM_SIZE require) |
fa423c5b |
1409 | { |
1410 | if (require <= sbrked_remains) { |
1411 | sbrked_remains -= require; |
1412 | } else { |
1413 | char *cp; |
1414 | |
1415 | require -= sbrked_remains; |
1416 | /* We do not try to optimize sbrks here, we go for place. */ |
1417 | cp = (char*) sbrk(require); |
1418 | #ifdef DEBUGGING_MSTATS |
1419 | sbrks++; |
1420 | goodsbrk += require; |
1421 | #endif |
1422 | if (cp == last_sbrk_top) { |
1423 | sbrked_remains = 0; |
1424 | last_sbrk_top = cp + require; |
1425 | } else { |
28ac10b1 |
1426 | if (cp == (char*)-1) { /* Out of memory */ |
1427 | #ifdef DEBUGGING_MSTATS |
1428 | goodsbrk -= require; |
1429 | #endif |
1430 | return 0; |
1431 | } |
fa423c5b |
1432 | /* Report the failure: */ |
1433 | if (sbrked_remains) |
1434 | add_to_chain((void*)(last_sbrk_top - sbrked_remains), |
1435 | sbrked_remains, 0); |
1436 | add_to_chain((void*)cp, require, 0); |
1437 | sbrk_good -= SBRK_FAILURE_PRICE; |
1438 | sbrked_remains = 0; |
1439 | last_sbrk_top = 0; |
1440 | last_op = 0; |
1441 | return 0; |
1442 | } |
1443 | } |
1444 | |
1445 | return 1; |
1446 | } |
1447 | |
8d063cd8 |
1448 | /* |
1449 | * Allocate more memory to the indicated bucket. |
1450 | */ |
cea2e8a9 |
1451 | static void |
1452 | morecore(register int bucket) |
8d063cd8 |
1453 | { |
72aaf631 |
1454 | register union overhead *ovp; |
8d063cd8 |
1455 | register int rnu; /* 2^rnu bytes will be requested */ |
fa423c5b |
1456 | int nblks; /* become nblks blocks of the desired size */ |
bbce6d69 |
1457 | register MEM_SIZE siz, needed; |
8d063cd8 |
1458 | |
1459 | if (nextf[bucket]) |
1460 | return; |
e8bc2b5c |
1461 | if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) { |
741df71a |
1462 | MALLOC_UNLOCK; |
d720c441 |
1463 | croak("%s", "Out of memory during ridiculously large request"); |
55497cff |
1464 | } |
d720c441 |
1465 | if (bucket > max_bucket) |
e8bc2b5c |
1466 | max_bucket = bucket; |
d720c441 |
1467 | |
e8bc2b5c |
1468 | rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT)) |
1469 | ? LOG_OF_MIN_ARENA |
1470 | : (bucket >> BUCKET_POW2_SHIFT) ); |
1471 | /* This may be overwritten later: */ |
1472 | nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */ |
1473 | needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket); |
1474 | if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */ |
1475 | ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT; |
1476 | nextf[rnu << BUCKET_POW2_SHIFT] |
1477 | = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next; |
1478 | #ifdef DEBUGGING_MSTATS |
1479 | nmalloc[rnu << BUCKET_POW2_SHIFT]--; |
1480 | start_slack -= M_OVERHEAD; |
1481 | #endif |
1482 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1483 | "stealing %ld bytes from %ld arena\n", |
1484 | (long) needed, (long) rnu << BUCKET_POW2_SHIFT)); |
1485 | } else if (chunk_chain |
1486 | && (ovp = (union overhead*) get_from_chain(needed))) { |
1487 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1488 | "stealing %ld bytes from chain\n", |
1489 | (long) needed)); |
d720c441 |
1490 | } else if ( (ovp = (union overhead*) |
1491 | get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1, |
1492 | needed)) ) { |
e8bc2b5c |
1493 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
1494 | "stealing %ld bytes from bigger buckets\n", |
1495 | (long) needed)); |
1496 | } else if (needed <= sbrked_remains) { |
1497 | ovp = (union overhead *)(last_sbrk_top - sbrked_remains); |
1498 | sbrked_remains -= needed; |
1499 | last_op = (char*)ovp; |
fa423c5b |
1500 | } else |
1501 | ovp = getpages(needed, &nblks, bucket); |
e8bc2b5c |
1502 | |
fa423c5b |
1503 | if (!ovp) |
1504 | return; |
e8bc2b5c |
1505 | |
8d063cd8 |
1506 | /* |
1507 | * Add new memory allocated to that on |
1508 | * free list for this hash bucket. |
1509 | */ |
e8bc2b5c |
1510 | siz = BUCKET_SIZE(bucket); |
cf5c4ad8 |
1511 | #ifdef PACK_MALLOC |
72aaf631 |
1512 | *(u_char*)ovp = bucket; /* Fill index. */ |
e8bc2b5c |
1513 | if (bucket <= MAX_PACKED) { |
1514 | ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket)); |
1515 | nblks = N_BLKS(bucket); |
cf5c4ad8 |
1516 | # ifdef DEBUGGING_MSTATS |
e8bc2b5c |
1517 | start_slack += BLK_SHIFT(bucket); |
cf5c4ad8 |
1518 | # endif |
e8bc2b5c |
1519 | } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) { |
1520 | ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket)); |
cf5c4ad8 |
1521 | siz -= sizeof(union overhead); |
72aaf631 |
1522 | } else ovp++; /* One chunk per block. */ |
e8bc2b5c |
1523 | #endif /* PACK_MALLOC */ |
72aaf631 |
1524 | nextf[bucket] = ovp; |
5f05dabc |
1525 | #ifdef DEBUGGING_MSTATS |
1526 | nmalloc[bucket] += nblks; |
e8bc2b5c |
1527 | if (bucket > MAX_PACKED) { |
1528 | start_slack += M_OVERHEAD * nblks; |
1529 | } |
5f05dabc |
1530 | #endif |
8d063cd8 |
1531 | while (--nblks > 0) { |
72aaf631 |
1532 | ovp->ov_next = (union overhead *)((caddr_t)ovp + siz); |
1533 | ovp = (union overhead *)((caddr_t)ovp + siz); |
8d063cd8 |
1534 | } |
8595d6f1 |
1535 | /* Not all sbrks return zeroed memory.*/ |
72aaf631 |
1536 | ovp->ov_next = (union overhead *)NULL; |
cf5c4ad8 |
1537 | #ifdef PACK_MALLOC |
e8bc2b5c |
1538 | if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */ |
1539 | union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next; |
1540 | nextf[7*BUCKETS_PER_POW2] = |
1541 | (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2] |
1542 | - sizeof(union overhead)); |
1543 | nextf[7*BUCKETS_PER_POW2]->ov_next = n_op; |
cf5c4ad8 |
1544 | } |
1545 | #endif /* !PACK_MALLOC */ |
8d063cd8 |
1546 | } |
1547 | |
94b6baf5 |
1548 | Free_t |
86058a2d |
1549 | Perl_mfree(void *mp) |
cea2e8a9 |
1550 | { |
ee0007ab |
1551 | register MEM_SIZE size; |
72aaf631 |
1552 | register union overhead *ovp; |
352d5a3a |
1553 | char *cp = (char*)mp; |
cf5c4ad8 |
1554 | #ifdef PACK_MALLOC |
1555 | u_char bucket; |
1556 | #endif |
8d063cd8 |
1557 | |
e8bc2b5c |
1558 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
b900a521 |
1559 | "0x%"UVxf": (%05lu) free\n", |
1560 | PTR2UV(cp), (unsigned long)(PL_an++))); |
45d8adaa |
1561 | |
cf5c4ad8 |
1562 | if (cp == NULL) |
1563 | return; |
72aaf631 |
1564 | ovp = (union overhead *)((caddr_t)cp |
e8bc2b5c |
1565 | - sizeof (union overhead) * CHUNK_SHIFT); |
cf5c4ad8 |
1566 | #ifdef PACK_MALLOC |
72aaf631 |
1567 | bucket = OV_INDEX(ovp); |
cf5c4ad8 |
1568 | #endif |
e8bc2b5c |
1569 | #ifdef IGNORE_SMALL_BAD_FREE |
1570 | if ((bucket >= FIRST_BUCKET_WITH_CHECK) |
1571 | && (OV_MAGIC(ovp, bucket) != MAGIC)) |
1572 | #else |
1573 | if (OV_MAGIC(ovp, bucket) != MAGIC) |
1574 | #endif |
1575 | { |
68dc0745 |
1576 | static int bad_free_warn = -1; |
cf5c4ad8 |
1577 | if (bad_free_warn == -1) { |
e8cd8248 |
1578 | dTHX; |
5fd9e9a4 |
1579 | char *pbf = PerlEnv_getenv("PERL_BADFREE"); |
cf5c4ad8 |
1580 | bad_free_warn = (pbf) ? atoi(pbf) : 1; |
1581 | } |
1582 | if (!bad_free_warn) |
1583 | return; |
8990e307 |
1584 | #ifdef RCHECK |
2ba999ec |
1585 | #ifdef PERL_CORE |
e8cd8248 |
1586 | { |
1587 | dTHX; |
1588 | if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC)) |
1d860e85 |
1589 | Perl_warner(aTHX_ WARN_MALLOC, "%s free() ignored", |
e8cd8248 |
1590 | ovp->ov_rmagic == RMAGIC - 1 ? |
1591 | "Duplicate" : "Bad"); |
2ba999ec |
1592 | } |
e476b1b5 |
1593 | #else |
2ba999ec |
1594 | warn("%s free() ignored", |
1595 | ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad"); |
e476b1b5 |
1596 | #endif |
1597 | #else |
1598 | #ifdef PERL_CORE |
2ba999ec |
1599 | { |
1600 | dTHX; |
1d860e85 |
1601 | if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC)) |
1602 | Perl_warner(aTHX_ WARN_MALLOC, "%s", "Bad free() ignored"); |
2ba999ec |
1603 | } |
8990e307 |
1604 | #else |
2ba999ec |
1605 | warn("%s", "Bad free() ignored"); |
8990e307 |
1606 | #endif |
e476b1b5 |
1607 | #endif |
8d063cd8 |
1608 | return; /* sanity */ |
e8bc2b5c |
1609 | } |
8d063cd8 |
1610 | #ifdef RCHECK |
3541dd58 |
1611 | ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite"); |
e8bc2b5c |
1612 | if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) { |
1613 | int i; |
1614 | MEM_SIZE nbytes = ovp->ov_size + 1; |
1615 | |
1616 | if ((i = nbytes & 3)) { |
1617 | i = 4 - i; |
1618 | while (i--) { |
3541dd58 |
1619 | ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i)) |
d720c441 |
1620 | == RMAGIC_C, "chunk's tail overwrite"); |
e8bc2b5c |
1621 | } |
1622 | } |
1623 | nbytes = (nbytes + 3) &~ 3; |
3541dd58 |
1624 | ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite"); |
e8bc2b5c |
1625 | } |
72aaf631 |
1626 | ovp->ov_rmagic = RMAGIC - 1; |
8d063cd8 |
1627 | #endif |
3541dd58 |
1628 | ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite"); |
72aaf631 |
1629 | size = OV_INDEX(ovp); |
4ad56ec9 |
1630 | |
1631 | MALLOC_LOCK; |
72aaf631 |
1632 | ovp->ov_next = nextf[size]; |
1633 | nextf[size] = ovp; |
741df71a |
1634 | MALLOC_UNLOCK; |
8d063cd8 |
1635 | } |
1636 | |
4ad56ec9 |
1637 | /* There is no need to do any locking in realloc (with an exception of |
1638 | trying to grow in place if we are at the end of the chain). |
1639 | If somebody calls us from a different thread with the same address, |
1640 | we are sole anyway. */ |
8d063cd8 |
1641 | |
2304df62 |
1642 | Malloc_t |
86058a2d |
1643 | Perl_realloc(void *mp, size_t nbytes) |
cea2e8a9 |
1644 | { |
ee0007ab |
1645 | register MEM_SIZE onb; |
72aaf631 |
1646 | union overhead *ovp; |
d720c441 |
1647 | char *res; |
1648 | int prev_bucket; |
e8bc2b5c |
1649 | register int bucket; |
4ad56ec9 |
1650 | int incr; /* 1 if does not fit, -1 if "easily" fits in a |
1651 | smaller bucket, otherwise 0. */ |
352d5a3a |
1652 | char *cp = (char*)mp; |
8d063cd8 |
1653 | |
e8bc2b5c |
1654 | #if defined(DEBUGGING) || !defined(PERL_CORE) |
ee0007ab |
1655 | MEM_SIZE size = nbytes; |
45d8adaa |
1656 | |
45d8adaa |
1657 | if ((long)nbytes < 0) |
cea2e8a9 |
1658 | croak("%s", "panic: realloc"); |
45d8adaa |
1659 | #endif |
e8bc2b5c |
1660 | |
1661 | BARK_64K_LIMIT("Reallocation",nbytes,size); |
1662 | if (!cp) |
86058a2d |
1663 | return Perl_malloc(nbytes); |
45d8adaa |
1664 | |
72aaf631 |
1665 | ovp = (union overhead *)((caddr_t)cp |
e8bc2b5c |
1666 | - sizeof (union overhead) * CHUNK_SHIFT); |
1667 | bucket = OV_INDEX(ovp); |
4ad56ec9 |
1668 | |
e8bc2b5c |
1669 | #ifdef IGNORE_SMALL_BAD_FREE |
4ad56ec9 |
1670 | if ((bucket >= FIRST_BUCKET_WITH_CHECK) |
1671 | && (OV_MAGIC(ovp, bucket) != MAGIC)) |
e8bc2b5c |
1672 | #else |
4ad56ec9 |
1673 | if (OV_MAGIC(ovp, bucket) != MAGIC) |
e8bc2b5c |
1674 | #endif |
4ad56ec9 |
1675 | { |
1676 | static int bad_free_warn = -1; |
1677 | if (bad_free_warn == -1) { |
e8cd8248 |
1678 | dTHX; |
4ad56ec9 |
1679 | char *pbf = PerlEnv_getenv("PERL_BADFREE"); |
1680 | bad_free_warn = (pbf) ? atoi(pbf) : 1; |
1681 | } |
1682 | if (!bad_free_warn) |
ce70748c |
1683 | return Nullch; |
4ad56ec9 |
1684 | #ifdef RCHECK |
2ba999ec |
1685 | #ifdef PERL_CORE |
e8cd8248 |
1686 | { |
1687 | dTHX; |
1688 | if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC)) |
1d860e85 |
1689 | Perl_warner(aTHX_ WARN_MALLOC, "%srealloc() %signored", |
e8cd8248 |
1690 | (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "), |
1691 | ovp->ov_rmagic == RMAGIC - 1 |
1692 | ? "of freed memory " : ""); |
2ba999ec |
1693 | } |
e476b1b5 |
1694 | #else |
2ba999ec |
1695 | warn("%srealloc() %signored", |
1696 | (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "), |
1697 | ovp->ov_rmagic == RMAGIC - 1 ? "of freed memory " : ""); |
e476b1b5 |
1698 | #endif |
1699 | #else |
1700 | #ifdef PERL_CORE |
2ba999ec |
1701 | { |
1702 | dTHX; |
1d860e85 |
1703 | if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC)) |
1704 | Perl_warner(aTHX_ WARN_MALLOC, "%s", |
1705 | "Bad realloc() ignored"); |
2ba999ec |
1706 | } |
4ad56ec9 |
1707 | #else |
2ba999ec |
1708 | warn("%s", "Bad realloc() ignored"); |
4ad56ec9 |
1709 | #endif |
e476b1b5 |
1710 | #endif |
ce70748c |
1711 | return Nullch; /* sanity */ |
4ad56ec9 |
1712 | } |
1713 | |
e8bc2b5c |
1714 | onb = BUCKET_SIZE_REAL(bucket); |
55497cff |
1715 | /* |
1716 | * avoid the copy if same size block. |
e8bc2b5c |
1717 | * We are not agressive with boundary cases. Note that it might |
1718 | * (for a small number of cases) give false negative if |
55497cff |
1719 | * both new size and old one are in the bucket for |
e8bc2b5c |
1720 | * FIRST_BIG_POW2, but the new one is near the lower end. |
1721 | * |
1722 | * We do not try to go to 1.5 times smaller bucket so far. |
55497cff |
1723 | */ |
e8bc2b5c |
1724 | if (nbytes > onb) incr = 1; |
1725 | else { |
1726 | #ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING |
1727 | if ( /* This is a little bit pessimal if PACK_MALLOC: */ |
1728 | nbytes > ( (onb >> 1) - M_OVERHEAD ) |
1729 | # ifdef TWO_POT_OPTIMIZE |
1730 | || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND ) |
1731 | # endif |
1732 | ) |
1733 | #else /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */ |
1734 | prev_bucket = ( (bucket > MAX_PACKED + 1) |
1735 | ? bucket - BUCKETS_PER_POW2 |
1736 | : bucket - 1); |
1737 | if (nbytes > BUCKET_SIZE_REAL(prev_bucket)) |
1738 | #endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */ |
1739 | incr = 0; |
1740 | else incr = -1; |
1741 | } |
2ce36478 |
1742 | #ifdef STRESS_REALLOC |
4ad56ec9 |
1743 | goto hard_way; |
2ce36478 |
1744 | #endif |
4ad56ec9 |
1745 | if (incr == 0) { |
852c2e52 |
1746 | inplace_label: |
a687059c |
1747 | #ifdef RCHECK |
1748 | /* |
1749 | * Record new allocated size of block and |
1750 | * bound space with magic numbers. |
1751 | */ |
72aaf631 |
1752 | if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) { |
e8bc2b5c |
1753 | int i, nb = ovp->ov_size + 1; |
1754 | |
1755 | if ((i = nb & 3)) { |
1756 | i = 4 - i; |
1757 | while (i--) { |
3541dd58 |
1758 | ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite"); |
e8bc2b5c |
1759 | } |
1760 | } |
1761 | nb = (nb + 3) &~ 3; |
3541dd58 |
1762 | ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite"); |
a687059c |
1763 | /* |
1764 | * Convert amount of memory requested into |
1765 | * closest block size stored in hash buckets |
1766 | * which satisfies request. Account for |
1767 | * space used per block for accounting. |
1768 | */ |
cf5c4ad8 |
1769 | nbytes += M_OVERHEAD; |
72aaf631 |
1770 | ovp->ov_size = nbytes - 1; |
e8bc2b5c |
1771 | if ((i = nbytes & 3)) { |
1772 | i = 4 - i; |
1773 | while (i--) |
1774 | *((char *)((caddr_t)ovp + nbytes - RSLOP + i)) |
1775 | = RMAGIC_C; |
1776 | } |
1777 | nbytes = (nbytes + 3) &~ 3; |
72aaf631 |
1778 | *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC; |
a687059c |
1779 | } |
1780 | #endif |
45d8adaa |
1781 | res = cp; |
42ac124e |
1782 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
b900a521 |
1783 | "0x%"UVxf": (%05lu) realloc %ld bytes inplace\n", |
1784 | PTR2UV(res),(unsigned long)(PL_an++), |
42ac124e |
1785 | (long)size)); |
e8bc2b5c |
1786 | } else if (incr == 1 && (cp - M_OVERHEAD == last_op) |
1787 | && (onb > (1 << LOG_OF_MIN_ARENA))) { |
1788 | MEM_SIZE require, newarena = nbytes, pow; |
1789 | int shiftr; |
1790 | |
1791 | POW2_OPTIMIZE_ADJUST(newarena); |
1792 | newarena = newarena + M_OVERHEAD; |
1793 | /* newarena = (newarena + 3) &~ 3; */ |
1794 | shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA; |
1795 | pow = LOG_OF_MIN_ARENA + 1; |
1796 | /* apart from this loop, this is O(1) */ |
1797 | while (shiftr >>= 1) |
1798 | pow++; |
1799 | newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2); |
1800 | require = newarena - onb - M_OVERHEAD; |
1801 | |
4ad56ec9 |
1802 | MALLOC_LOCK; |
1803 | if (cp - M_OVERHEAD == last_op /* We *still* are the last chunk */ |
1804 | && getpages_adjacent(require)) { |
e8bc2b5c |
1805 | #ifdef DEBUGGING_MSTATS |
fa423c5b |
1806 | nmalloc[bucket]--; |
1807 | nmalloc[pow * BUCKETS_PER_POW2]++; |
e8bc2b5c |
1808 | #endif |
4d6cd4d8 |
1809 | *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */ |
4ad56ec9 |
1810 | MALLOC_UNLOCK; |
fa423c5b |
1811 | goto inplace_label; |
4ad56ec9 |
1812 | } else { |
1813 | MALLOC_UNLOCK; |
fa423c5b |
1814 | goto hard_way; |
4ad56ec9 |
1815 | } |
e8bc2b5c |
1816 | } else { |
1817 | hard_way: |
42ac124e |
1818 | DEBUG_m(PerlIO_printf(Perl_debug_log, |
b900a521 |
1819 | "0x%"UVxf": (%05lu) realloc %ld bytes the hard way\n", |
1820 | PTR2UV(cp),(unsigned long)(PL_an++), |
42ac124e |
1821 | (long)size)); |
86058a2d |
1822 | if ((res = (char*)Perl_malloc(nbytes)) == NULL) |
e8bc2b5c |
1823 | return (NULL); |
1824 | if (cp != res) /* common optimization */ |
1825 | Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char); |
4ad56ec9 |
1826 | Perl_mfree(cp); |
45d8adaa |
1827 | } |
2304df62 |
1828 | return ((Malloc_t)res); |
8d063cd8 |
1829 | } |
1830 | |
cf5c4ad8 |
1831 | Malloc_t |
86058a2d |
1832 | Perl_calloc(register size_t elements, register size_t size) |
cf5c4ad8 |
1833 | { |
1834 | long sz = elements * size; |
86058a2d |
1835 | Malloc_t p = Perl_malloc(sz); |
cf5c4ad8 |
1836 | |
1837 | if (p) { |
1838 | memset((void*)p, 0, sz); |
1839 | } |
1840 | return p; |
1841 | } |
1842 | |
4ad56ec9 |
1843 | char * |
1844 | Perl_strdup(const char *s) |
1845 | { |
1846 | MEM_SIZE l = strlen(s); |
b48f1ba5 |
1847 | char *s1 = (char *)Perl_malloc(l+1); |
4ad56ec9 |
1848 | |
b48f1ba5 |
1849 | Copy(s, s1, (MEM_SIZE)(l+1), char); |
4ad56ec9 |
1850 | return s1; |
1851 | } |
1852 | |
1853 | #ifdef PERL_CORE |
1854 | int |
1855 | Perl_putenv(char *a) |
1856 | { |
1857 | /* Sometimes system's putenv conflicts with my_setenv() - this is system |
1858 | malloc vs Perl's free(). */ |
1859 | dTHX; |
1860 | char *var; |
1861 | char *val = a; |
1862 | MEM_SIZE l; |
1863 | char buf[80]; |
1864 | |
1865 | while (*val && *val != '=') |
1866 | val++; |
1867 | if (!*val) |
1868 | return -1; |
1869 | l = val - a; |
1870 | if (l < sizeof(buf)) |
1871 | var = buf; |
1872 | else |
1873 | var = Perl_malloc(l + 1); |
1874 | Copy(a, var, l, char); |
b48f1ba5 |
1875 | var[l + 1] = 0; |
1876 | my_setenv(var, val+1); |
4ad56ec9 |
1877 | if (var != buf) |
1878 | Perl_mfree(var); |
1879 | return 0; |
1880 | } |
1881 | # endif |
1882 | |
e8bc2b5c |
1883 | MEM_SIZE |
cea2e8a9 |
1884 | Perl_malloced_size(void *p) |
e8bc2b5c |
1885 | { |
8d6dde3e |
1886 | union overhead *ovp = (union overhead *) |
1887 | ((caddr_t)p - sizeof (union overhead) * CHUNK_SHIFT); |
1888 | int bucket = OV_INDEX(ovp); |
1889 | #ifdef RCHECK |
1890 | /* The caller wants to have a complete control over the chunk, |
1891 | disable the memory checking inside the chunk. */ |
1892 | if (bucket <= MAX_SHORT_BUCKET) { |
1893 | MEM_SIZE size = BUCKET_SIZE_REAL(bucket); |
1894 | ovp->ov_size = size + M_OVERHEAD - 1; |
1895 | *((u_int *)((caddr_t)ovp + size + M_OVERHEAD - RSLOP)) = RMAGIC; |
1896 | } |
1897 | #endif |
e8bc2b5c |
1898 | return BUCKET_SIZE_REAL(bucket); |
1899 | } |
1900 | |
e8bc2b5c |
1901 | # ifdef BUCKETS_ROOT2 |
1902 | # define MIN_EVEN_REPORT 6 |
1903 | # else |
1904 | # define MIN_EVEN_REPORT MIN_BUCKET |
1905 | # endif |
827e134a |
1906 | |
1907 | int |
1908 | Perl_get_mstats(pTHX_ perl_mstats_t *buf, int buflen, int level) |
8d063cd8 |
1909 | { |
df31f264 |
1910 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
1911 | register int i, j; |
1912 | register union overhead *p; |
4ad56ec9 |
1913 | struct chunk_chain_s* nextchain; |
8d063cd8 |
1914 | |
827e134a |
1915 | buf->topbucket = buf->topbucket_ev = buf->topbucket_odd |
1916 | = buf->totfree = buf->total = buf->total_chain = 0; |
1917 | |
1918 | buf->minbucket = MIN_BUCKET; |
4ad56ec9 |
1919 | MALLOC_LOCK; |
e8bc2b5c |
1920 | for (i = MIN_BUCKET ; i < NBUCKETS; i++) { |
8d063cd8 |
1921 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) |
1922 | ; |
827e134a |
1923 | if (i < buflen) { |
1924 | buf->nfree[i] = j; |
1925 | buf->ntotal[i] = nmalloc[i]; |
1926 | } |
1927 | buf->totfree += j * BUCKET_SIZE_REAL(i); |
1928 | buf->total += nmalloc[i] * BUCKET_SIZE_REAL(i); |
e8bc2b5c |
1929 | if (nmalloc[i]) { |
827e134a |
1930 | i % 2 ? (buf->topbucket_odd = i) : (buf->topbucket_ev = i); |
1931 | buf->topbucket = i; |
e8bc2b5c |
1932 | } |
c07a80fd |
1933 | } |
4ad56ec9 |
1934 | nextchain = chunk_chain; |
1935 | while (nextchain) { |
827e134a |
1936 | buf->total_chain += nextchain->size; |
4ad56ec9 |
1937 | nextchain = nextchain->next; |
1938 | } |
827e134a |
1939 | buf->total_sbrk = goodsbrk + sbrk_slack; |
1940 | buf->sbrks = sbrks; |
1941 | buf->sbrk_good = sbrk_good; |
1942 | buf->sbrk_slack = sbrk_slack; |
1943 | buf->start_slack = start_slack; |
1944 | buf->sbrked_remains = sbrked_remains; |
4ad56ec9 |
1945 | MALLOC_UNLOCK; |
880b20b6 |
1946 | buf->nbuckets = NBUCKETS; |
827e134a |
1947 | if (level) { |
1948 | for (i = MIN_BUCKET ; i < NBUCKETS; i++) { |
1949 | if (i >= buflen) |
1950 | break; |
1951 | buf->bucket_mem_size[i] = BUCKET_SIZE(i); |
1952 | buf->bucket_available_size[i] = BUCKET_SIZE_REAL(i); |
1953 | } |
1954 | } |
1955 | #endif /* defined DEBUGGING_MSTATS */ |
fe52b3b7 |
1956 | return 0; /* XXX unused */ |
827e134a |
1957 | } |
1958 | /* |
1959 | * mstats - print out statistics about malloc |
1960 | * |
1961 | * Prints two lines of numbers, one showing the length of the free list |
1962 | * for each size category, the second showing the number of mallocs - |
1963 | * frees for each size category. |
1964 | */ |
1965 | void |
1966 | Perl_dump_mstats(pTHX_ char *s) |
1967 | { |
1968 | #ifdef DEBUGGING_MSTATS |
880b20b6 |
1969 | register int i; |
827e134a |
1970 | perl_mstats_t buffer; |
880b20b6 |
1971 | UV nf[NBUCKETS]; |
1972 | UV nt[NBUCKETS]; |
827e134a |
1973 | |
1974 | buffer.nfree = nf; |
1975 | buffer.ntotal = nt; |
1976 | get_mstats(&buffer, NBUCKETS, 0); |
1977 | |
c07a80fd |
1978 | if (s) |
bf49b057 |
1979 | PerlIO_printf(Perl_error_log, |
880b20b6 |
1980 | "Memory allocation statistics %s (buckets %"IVdf"(%"IVdf")..%"IVdf"(%"IVdf")\n", |
e8bc2b5c |
1981 | s, |
880b20b6 |
1982 | (IV)BUCKET_SIZE_REAL(MIN_BUCKET), |
1983 | (IV)BUCKET_SIZE(MIN_BUCKET), |
1984 | (IV)BUCKET_SIZE_REAL(buffer.topbucket), |
1985 | (IV)BUCKET_SIZE(buffer.topbucket)); |
1986 | PerlIO_printf(Perl_error_log, "%8"IVdf" free:", buffer.totfree); |
827e134a |
1987 | for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) { |
bf49b057 |
1988 | PerlIO_printf(Perl_error_log, |
e8bc2b5c |
1989 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
880b20b6 |
1990 | ? " %5"UVuf |
1991 | : ((i < 12*BUCKETS_PER_POW2) ? " %3"UVuf : " %"UVuf)), |
827e134a |
1992 | buffer.nfree[i]); |
e8bc2b5c |
1993 | } |
1994 | #ifdef BUCKETS_ROOT2 |
bf49b057 |
1995 | PerlIO_printf(Perl_error_log, "\n\t "); |
827e134a |
1996 | for (i = MIN_BUCKET + 1; i <= buffer.topbucket_odd; i += BUCKETS_PER_POW2) { |
bf49b057 |
1997 | PerlIO_printf(Perl_error_log, |
e8bc2b5c |
1998 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
880b20b6 |
1999 | ? " %5"UVuf |
2000 | : ((i < 12*BUCKETS_PER_POW2) ? " %3"UVuf : " %"UVuf)), |
827e134a |
2001 | buffer.nfree[i]); |
8d063cd8 |
2002 | } |
e8bc2b5c |
2003 | #endif |
880b20b6 |
2004 | PerlIO_printf(Perl_error_log, "\n%8"IVdf" used:", buffer.total - buffer.totfree); |
827e134a |
2005 | for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) { |
bf49b057 |
2006 | PerlIO_printf(Perl_error_log, |
e8bc2b5c |
2007 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
880b20b6 |
2008 | ? " %5"IVdf |
2009 | : ((i < 12*BUCKETS_PER_POW2) ? " %3"IVdf : " %"IVdf)), |
827e134a |
2010 | buffer.ntotal[i] - buffer.nfree[i]); |
c07a80fd |
2011 | } |
e8bc2b5c |
2012 | #ifdef BUCKETS_ROOT2 |
bf49b057 |
2013 | PerlIO_printf(Perl_error_log, "\n\t "); |
827e134a |
2014 | for (i = MIN_BUCKET + 1; i <= buffer.topbucket_odd; i += BUCKETS_PER_POW2) { |
bf49b057 |
2015 | PerlIO_printf(Perl_error_log, |
e8bc2b5c |
2016 | ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2) |
880b20b6 |
2017 | ? " %5"IVdf |
2018 | : ((i < 12*BUCKETS_PER_POW2) ? " %3"IVdf : " %"IVdf)), |
827e134a |
2019 | buffer.ntotal[i] - buffer.nfree[i]); |
e8bc2b5c |
2020 | } |
2021 | #endif |
880b20b6 |
2022 | PerlIO_printf(Perl_error_log, "\nTotal sbrk(): %"IVdf"/%"IVdf":%"IVdf". Odd ends: pad+heads+chain+tail: %"IVdf"+%"IVdf"+%"IVdf"+%"IVdf".\n", |
827e134a |
2023 | buffer.total_sbrk, buffer.sbrks, buffer.sbrk_good, |
2024 | buffer.sbrk_slack, buffer.start_slack, |
2025 | buffer.total_chain, buffer.sbrked_remains); |
df31f264 |
2026 | #endif /* DEBUGGING_MSTATS */ |
c07a80fd |
2027 | } |
a687059c |
2028 | #endif /* lint */ |
cf5c4ad8 |
2029 | |
cf5c4ad8 |
2030 | #ifdef USE_PERL_SBRK |
2031 | |
e3663bad |
2032 | # if defined(__MACHTEN_PPC__) || defined(NeXT) || defined(__NeXT__) || defined(PURIFY) |
38ac2dc8 |
2033 | # define PERL_SBRK_VIA_MALLOC |
38ac2dc8 |
2034 | # endif |
2035 | |
760ac839 |
2036 | # ifdef PERL_SBRK_VIA_MALLOC |
cf5c4ad8 |
2037 | |
2038 | /* it may seem schizophrenic to use perl's malloc and let it call system */ |
2039 | /* malloc, the reason for that is only the 3.2 version of the OS that had */ |
2040 | /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */ |
2041 | /* end to the cores */ |
2042 | |
38ac2dc8 |
2043 | # ifndef SYSTEM_ALLOC |
2044 | # define SYSTEM_ALLOC(a) malloc(a) |
2045 | # endif |
5bbd1ef5 |
2046 | # ifndef SYSTEM_ALLOC_ALIGNMENT |
2047 | # define SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES |
2048 | # endif |
cf5c4ad8 |
2049 | |
760ac839 |
2050 | # endif /* PERL_SBRK_VIA_MALLOC */ |
cf5c4ad8 |
2051 | |
2052 | static IV Perl_sbrk_oldchunk; |
2053 | static long Perl_sbrk_oldsize; |
2054 | |
760ac839 |
2055 | # define PERLSBRK_32_K (1<<15) |
2056 | # define PERLSBRK_64_K (1<<16) |
cf5c4ad8 |
2057 | |
b63effbb |
2058 | Malloc_t |
df0003d4 |
2059 | Perl_sbrk(int size) |
cf5c4ad8 |
2060 | { |
2061 | IV got; |
2062 | int small, reqsize; |
2063 | |
2064 | if (!size) return 0; |
55497cff |
2065 | #ifdef PERL_CORE |
cf5c4ad8 |
2066 | reqsize = size; /* just for the DEBUG_m statement */ |
2067 | #endif |
57569e04 |
2068 | #ifdef PACK_MALLOC |
2069 | size = (size + 0x7ff) & ~0x7ff; |
2070 | #endif |
cf5c4ad8 |
2071 | if (size <= Perl_sbrk_oldsize) { |
2072 | got = Perl_sbrk_oldchunk; |
2073 | Perl_sbrk_oldchunk += size; |
2074 | Perl_sbrk_oldsize -= size; |
2075 | } else { |
2076 | if (size >= PERLSBRK_32_K) { |
2077 | small = 0; |
2078 | } else { |
cf5c4ad8 |
2079 | size = PERLSBRK_64_K; |
2080 | small = 1; |
2081 | } |
5bbd1ef5 |
2082 | # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT |
2083 | size += NEEDED_ALIGNMENT - SYSTEM_ALLOC_ALIGNMENT; |
2084 | # endif |
cf5c4ad8 |
2085 | got = (IV)SYSTEM_ALLOC(size); |
5bbd1ef5 |
2086 | # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT |
5a7d6335 |
2087 | got = (got + NEEDED_ALIGNMENT - 1) & ~(NEEDED_ALIGNMENT - 1); |
5bbd1ef5 |
2088 | # endif |
cf5c4ad8 |
2089 | if (small) { |
2090 | /* Chunk is small, register the rest for future allocs. */ |
2091 | Perl_sbrk_oldchunk = got + reqsize; |
2092 | Perl_sbrk_oldsize = size - reqsize; |
2093 | } |
2094 | } |
2095 | |
b900a521 |
2096 | DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%"UVxf"\n", |
2097 | size, reqsize, Perl_sbrk_oldsize, PTR2UV(got))); |
cf5c4ad8 |
2098 | |
2099 | return (void *)got; |
2100 | } |
2101 | |
2102 | #endif /* ! defined USE_PERL_SBRK */ |