Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* malloc.c |
8d063cd8 | 2 | * |
8d063cd8 LW |
3 | */ |
4 | ||
5 | #ifndef lint | |
a687059c | 6 | #ifdef DEBUGGING |
8d063cd8 | 7 | #define RCHECK |
a687059c | 8 | #endif |
8d063cd8 LW |
9 | /* |
10 | * malloc.c (Caltech) 2/21/82 | |
11 | * Chris Kingsley, kingsley@cit-20. | |
12 | * | |
13 | * This is a very fast storage allocator. It allocates blocks of a small | |
14 | * number of different sizes, and keeps free lists of each size. Blocks that | |
15 | * don't exactly fit are passed up to the next larger size. In this | |
16 | * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. | |
cf5c4ad8 | 17 | * If PACK_MALLOC is defined, small blocks are 2^n bytes long. |
8d063cd8 LW |
18 | * This is designed for use in a program that uses vast quantities of memory, |
19 | * but bombs when it runs out. | |
20 | */ | |
21 | ||
135863df | 22 | #include "EXTERN.h" |
135863df AB |
23 | #include "perl.h" |
24 | ||
760ac839 LW |
25 | #ifdef DEBUGGING |
26 | #undef DEBUG_m | |
27 | #define DEBUG_m(a) if (debug & 128) a | |
28 | #endif | |
29 | ||
135863df AB |
30 | /* I don't much care whether these are defined in sys/types.h--LAW */ |
31 | ||
32 | #define u_char unsigned char | |
33 | #define u_int unsigned int | |
34 | #define u_short unsigned short | |
8d063cd8 | 35 | |
cf5c4ad8 | 36 | /* 286 and atarist like big chunks, which gives too much overhead. */ |
37 | #if (defined(RCHECK) || defined(I286) || defined(atarist)) && defined(PACK_MALLOC) | |
38 | #undef PACK_MALLOC | |
39 | #endif | |
40 | ||
41 | ||
8d063cd8 | 42 | /* |
cf5c4ad8 | 43 | * The description below is applicable if PACK_MALLOC is not defined. |
44 | * | |
8d063cd8 LW |
45 | * The overhead on a block is at least 4 bytes. When free, this space |
46 | * contains a pointer to the next free block, and the bottom two bits must | |
47 | * be zero. When in use, the first byte is set to MAGIC, and the second | |
48 | * byte is the size index. The remaining bytes are for alignment. | |
49 | * If range checking is enabled and the size of the block fits | |
50 | * in two bytes, then the top two bytes hold the size of the requested block | |
51 | * plus the range checking words, and the header word MINUS ONE. | |
52 | */ | |
53 | union overhead { | |
54 | union overhead *ov_next; /* when free */ | |
85e6fe83 | 55 | #if MEM_ALIGNBYTES > 4 |
c623bd54 | 56 | double strut; /* alignment problems */ |
a687059c | 57 | #endif |
8d063cd8 LW |
58 | struct { |
59 | u_char ovu_magic; /* magic number */ | |
60 | u_char ovu_index; /* bucket # */ | |
61 | #ifdef RCHECK | |
62 | u_short ovu_size; /* actual block size */ | |
63 | u_int ovu_rmagic; /* range magic number */ | |
64 | #endif | |
65 | } ovu; | |
66 | #define ov_magic ovu.ovu_magic | |
67 | #define ov_index ovu.ovu_index | |
68 | #define ov_size ovu.ovu_size | |
69 | #define ov_rmagic ovu.ovu_rmagic | |
70 | }; | |
71 | ||
760ac839 | 72 | #ifdef DEBUGGING |
a0d0e21e LW |
73 | static void botch _((char *s)); |
74 | #endif | |
75 | static void morecore _((int bucket)); | |
76 | static int findbucket _((union overhead *freep, int srchlen)); | |
77 | ||
8d063cd8 LW |
78 | #define MAGIC 0xff /* magic # on accounting info */ |
79 | #define RMAGIC 0x55555555 /* magic # on range info */ | |
80 | #ifdef RCHECK | |
81 | #define RSLOP sizeof (u_int) | |
82 | #else | |
83 | #define RSLOP 0 | |
84 | #endif | |
85 | ||
cf5c4ad8 | 86 | #ifdef PACK_MALLOC |
87 | /* | |
88 | * In this case it is assumed that if we do sbrk() in 2K units, we | |
89 | * will get 2K aligned blocks. The bucket number of the given subblock is | |
90 | * on the boundary of 2K block which contains the subblock. | |
91 | * Several following bytes contain the magic numbers for the subblocks | |
92 | * in the block. | |
93 | * | |
94 | * Sizes of chunks are powers of 2 for chunks in buckets <= | |
95 | * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to | |
96 | * get alignment right). | |
97 | * | |
98 | * We suppose that starts of all the chunks in a 2K block are in | |
99 | * different 2^n-byte-long chunks. If the top of the last chunk is | |
100 | * aligned on a boundary of 2K block, this means that | |
101 | * sizeof(union overhead)*"number of chunks" < 2^n, or | |
102 | * sizeof(union overhead)*2K < 4^n, or n > 6 + log2(sizeof()/2)/2, if a | |
103 | * chunk of size 2^n - overhead is used. Since this rules out n = 7 | |
104 | * for 8 byte alignment, we specialcase allocation of the first of 16 | |
105 | * 128-byte-long chunks. | |
106 | * | |
107 | * Note that with the above assumption we automatically have enough | |
108 | * place for MAGIC at the start of 2K block. Note also that we | |
109 | * overlay union overhead over the chunk, thus the start of the chunk | |
110 | * is immediately overwritten after freeing. | |
111 | */ | |
112 | # define MAX_PACKED 6 | |
113 | # define MAX_2_POT_ALGO ((1<<(MAX_PACKED + 1)) - M_OVERHEAD) | |
114 | # define TWOK_MASK ((1<<11) - 1) | |
115 | # define TWOK_MASKED(x) ((int)x & ~TWOK_MASK) | |
116 | # define TWOK_SHIFT(x) ((int)x & TWOK_MASK) | |
117 | # define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block))) | |
118 | # define OV_INDEX(block) (*OV_INDEXp(block)) | |
119 | # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \ | |
120 | (TWOK_SHIFT(block)>>(bucket + 3)) + \ | |
121 | (bucket > MAX_NONSHIFT ? 1 : 0))) | |
122 | # define CHUNK_SHIFT 0 | |
123 | ||
124 | static u_char n_blks[11 - 3] = {224, 120, 62, 31, 16, 8, 4, 2}; | |
125 | static u_short blk_shift[11 - 3] = {256, 128, 64, 32, | |
126 | 16*sizeof(union overhead), | |
127 | 8*sizeof(union overhead), | |
128 | 4*sizeof(union overhead), | |
129 | 2*sizeof(union overhead), | |
130 | # define MAX_NONSHIFT 2 /* Shift 64 greater than chunk 32. */ | |
131 | }; | |
132 | ||
133 | # ifdef DEBUGGING_MSTATS | |
134 | static u_int sbrk_slack; | |
135 | static u_int start_slack; | |
136 | # endif | |
137 | ||
138 | #else /* !PACK_MALLOC */ | |
139 | ||
140 | # define OV_MAGIC(block,bucket) (block)->ov_magic | |
141 | # define OV_INDEX(block) (block)->ov_index | |
142 | # define CHUNK_SHIFT 1 | |
143 | #endif /* !PACK_MALLOC */ | |
144 | ||
145 | # define M_OVERHEAD (sizeof(union overhead) + RSLOP) | |
146 | ||
8d063cd8 LW |
147 | /* |
148 | * nextf[i] is the pointer to the next free block of size 2^(i+3). The | |
149 | * smallest allocatable block is 8 bytes. The overhead information | |
150 | * precedes the data area returned to the user. | |
151 | */ | |
152 | #define NBUCKETS 30 | |
153 | static union overhead *nextf[NBUCKETS]; | |
cf5c4ad8 | 154 | |
155 | #ifdef USE_PERL_SBRK | |
156 | #define sbrk(a) Perl_sbrk(a) | |
157 | char * Perl_sbrk _((int size)); | |
158 | #else | |
8d063cd8 | 159 | extern char *sbrk(); |
cf5c4ad8 | 160 | #endif |
8d063cd8 | 161 | |
c07a80fd | 162 | #ifdef DEBUGGING_MSTATS |
8d063cd8 LW |
163 | /* |
164 | * nmalloc[i] is the difference between the number of mallocs and frees | |
165 | * for a given block size. | |
166 | */ | |
167 | static u_int nmalloc[NBUCKETS]; | |
8d063cd8 LW |
168 | #endif |
169 | ||
760ac839 | 170 | #ifdef DEBUGGING |
4dfc412b | 171 | #define ASSERT(p) if (!(p)) botch(STRINGIFY(p)); else |
ee0007ab | 172 | static void |
8d063cd8 LW |
173 | botch(s) |
174 | char *s; | |
175 | { | |
4dfc412b | 176 | PerlIO_printf(PerlIO_stderr(), "assertion botched: %s\n", s); |
8d063cd8 LW |
177 | abort(); |
178 | } | |
179 | #else | |
180 | #define ASSERT(p) | |
181 | #endif | |
182 | ||
2304df62 | 183 | Malloc_t |
8d063cd8 | 184 | malloc(nbytes) |
ee0007ab | 185 | register MEM_SIZE nbytes; |
8d063cd8 LW |
186 | { |
187 | register union overhead *p; | |
188 | register int bucket = 0; | |
ee0007ab | 189 | register MEM_SIZE shiftr; |
8d063cd8 | 190 | |
45d8adaa LW |
191 | #ifdef safemalloc |
192 | #ifdef DEBUGGING | |
ee0007ab | 193 | MEM_SIZE size = nbytes; |
45d8adaa LW |
194 | #endif |
195 | ||
196 | #ifdef MSDOS | |
197 | if (nbytes > 0xffff) { | |
760ac839 | 198 | PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", (long)nbytes); |
79072805 | 199 | my_exit(1); |
45d8adaa LW |
200 | } |
201 | #endif /* MSDOS */ | |
202 | #ifdef DEBUGGING | |
203 | if ((long)nbytes < 0) | |
463ee0b2 | 204 | croak("panic: malloc"); |
45d8adaa LW |
205 | #endif |
206 | #endif /* safemalloc */ | |
207 | ||
8d063cd8 LW |
208 | /* |
209 | * Convert amount of memory requested into | |
210 | * closest block size stored in hash buckets | |
211 | * which satisfies request. Account for | |
212 | * space used per block for accounting. | |
213 | */ | |
cf5c4ad8 | 214 | #ifdef PACK_MALLOC |
215 | if (nbytes > MAX_2_POT_ALGO) { | |
216 | #endif | |
217 | nbytes += M_OVERHEAD; | |
218 | nbytes = (nbytes + 3) &~ 3; | |
219 | #ifdef PACK_MALLOC | |
220 | } else if (nbytes == 0) { | |
221 | nbytes = 1; | |
222 | } | |
223 | #endif | |
8d063cd8 LW |
224 | shiftr = (nbytes - 1) >> 2; |
225 | /* apart from this loop, this is O(1) */ | |
226 | while (shiftr >>= 1) | |
227 | bucket++; | |
228 | /* | |
229 | * If nothing in hash bucket right now, | |
230 | * request more memory from the system. | |
231 | */ | |
232 | if (nextf[bucket] == NULL) | |
233 | morecore(bucket); | |
45d8adaa LW |
234 | if ((p = (union overhead *)nextf[bucket]) == NULL) { |
235 | #ifdef safemalloc | |
ee0007ab | 236 | if (!nomemok) { |
760ac839 | 237 | PerlIO_puts(PerlIO_stderr(),"Out of memory!\n"); |
79072805 | 238 | my_exit(1); |
ee0007ab | 239 | } |
45d8adaa | 240 | #else |
8d063cd8 | 241 | return (NULL); |
45d8adaa LW |
242 | #endif |
243 | } | |
244 | ||
245 | #ifdef safemalloc | |
760ac839 | 246 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n", |
a0d0e21e | 247 | (unsigned long)(p+1),an++,(long)size)); |
45d8adaa LW |
248 | #endif /* safemalloc */ |
249 | ||
8d063cd8 | 250 | /* remove from linked list */ |
bf38876a LW |
251 | #ifdef RCHECK |
252 | if (*((int*)p) & (sizeof(union overhead) - 1)) | |
760ac839 | 253 | PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n", |
a0d0e21e | 254 | (unsigned long)*((int*)p),(unsigned long)p); |
bf38876a LW |
255 | #endif |
256 | nextf[bucket] = p->ov_next; | |
cf5c4ad8 | 257 | OV_MAGIC(p, bucket) = MAGIC; |
258 | #ifndef PACK_MALLOC | |
259 | OV_INDEX(p) = bucket; | |
260 | #endif | |
c07a80fd | 261 | #ifdef DEBUGGING_MSTATS |
8d063cd8 LW |
262 | nmalloc[bucket]++; |
263 | #endif | |
264 | #ifdef RCHECK | |
265 | /* | |
266 | * Record allocated size of block and | |
267 | * bound space with magic numbers. | |
268 | */ | |
269 | if (nbytes <= 0x10000) | |
270 | p->ov_size = nbytes - 1; | |
271 | p->ov_rmagic = RMAGIC; | |
272 | *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; | |
273 | #endif | |
cf5c4ad8 | 274 | return ((Malloc_t)(p + CHUNK_SHIFT)); |
8d063cd8 LW |
275 | } |
276 | ||
277 | /* | |
278 | * Allocate more memory to the indicated bucket. | |
279 | */ | |
a0d0e21e | 280 | static void |
8d063cd8 | 281 | morecore(bucket) |
a687059c | 282 | register int bucket; |
8d063cd8 LW |
283 | { |
284 | register union overhead *op; | |
285 | register int rnu; /* 2^rnu bytes will be requested */ | |
286 | register int nblks; /* become nblks blocks of the desired size */ | |
ee0007ab | 287 | register MEM_SIZE siz; |
cf5c4ad8 | 288 | int slack = 0; |
8d063cd8 LW |
289 | |
290 | if (nextf[bucket]) | |
291 | return; | |
292 | /* | |
293 | * Insure memory is allocated | |
294 | * on a page boundary. Should | |
295 | * make getpageize call? | |
296 | */ | |
ee0007ab | 297 | #ifndef atarist /* on the atari we dont have to worry about this */ |
8d063cd8 | 298 | op = (union overhead *)sbrk(0); |
cf5c4ad8 | 299 | # ifndef I286 |
300 | # ifdef PACK_MALLOC | |
301 | if ((int)op & 0x7ff) | |
302 | (void)sbrk(slack = 2048 - ((int)op & 0x7ff)); | |
303 | # else | |
8d063cd8 | 304 | if ((int)op & 0x3ff) |
cf5c4ad8 | 305 | (void)sbrk(slack = 1024 - ((int)op & 0x3ff)); |
306 | # endif | |
307 | # if defined(DEBUGGING_MSTATS) && defined(PACK_MALLOC) | |
308 | sbrk_slack += slack; | |
309 | # endif | |
310 | # else | |
a687059c | 311 | /* The sbrk(0) call on the I286 always returns the next segment */ |
cf5c4ad8 | 312 | # endif |
ee0007ab | 313 | #endif /* atarist */ |
a687059c | 314 | |
ee0007ab | 315 | #if !(defined(I286) || defined(atarist)) |
8d063cd8 LW |
316 | /* take 2k unless the block is bigger than that */ |
317 | rnu = (bucket <= 8) ? 11 : bucket + 3; | |
a687059c LW |
318 | #else |
319 | /* take 16k unless the block is bigger than that | |
ee0007ab | 320 | (80286s like large segments!), probably good on the atari too */ |
a687059c LW |
321 | rnu = (bucket <= 11) ? 14 : bucket + 3; |
322 | #endif | |
8d063cd8 | 323 | nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ |
cf5c4ad8 | 324 | /* if (rnu < bucket) |
325 | rnu = bucket; Why anyone needs this? */ | |
ee0007ab | 326 | op = (union overhead *)sbrk(1L << rnu); |
8d063cd8 LW |
327 | /* no more room! */ |
328 | if ((int)op == -1) | |
329 | return; | |
330 | /* | |
331 | * Round up to minimum allocation size boundary | |
332 | * and deduct from block count to reflect. | |
333 | */ | |
a687059c | 334 | #ifndef I286 |
cf5c4ad8 | 335 | # ifdef PACK_MALLOC |
336 | if ((int)op & 0x7ff) | |
337 | croak("panic: Off-page sbrk"); | |
338 | # endif | |
8d063cd8 | 339 | if ((int)op & 7) { |
ee0007ab | 340 | op = (union overhead *)(((MEM_SIZE)op + 8) &~ 7); |
8d063cd8 LW |
341 | nblks--; |
342 | } | |
a687059c LW |
343 | #else |
344 | /* Again, this should always be ok on an 80286 */ | |
345 | #endif | |
8d063cd8 LW |
346 | /* |
347 | * Add new memory allocated to that on | |
348 | * free list for this hash bucket. | |
349 | */ | |
8d063cd8 | 350 | siz = 1 << (bucket + 3); |
cf5c4ad8 | 351 | #ifdef PACK_MALLOC |
352 | *(u_char*)op = bucket; /* Fill index. */ | |
353 | if (bucket <= MAX_PACKED - 3) { | |
354 | op = (union overhead *) ((char*)op + blk_shift[bucket]); | |
355 | nblks = n_blks[bucket]; | |
356 | # ifdef DEBUGGING_MSTATS | |
357 | start_slack += blk_shift[bucket]; | |
358 | # endif | |
359 | } else if (bucket <= 11 - 1 - 3) { | |
360 | op = (union overhead *) ((char*)op + blk_shift[bucket]); | |
361 | /* nblks = n_blks[bucket]; */ | |
362 | siz -= sizeof(union overhead); | |
363 | } else op++; /* One chunk per block. */ | |
364 | #endif /* !PACK_MALLOC */ | |
365 | nextf[bucket] = op; | |
8d063cd8 LW |
366 | while (--nblks > 0) { |
367 | op->ov_next = (union overhead *)((caddr_t)op + siz); | |
368 | op = (union overhead *)((caddr_t)op + siz); | |
369 | } | |
8595d6f1 | 370 | /* Not all sbrks return zeroed memory.*/ |
cf5c4ad8 | 371 | op->ov_next = (union overhead *)NULL; |
cf5c4ad8 | 372 | #ifdef PACK_MALLOC |
373 | if (bucket == 7 - 3) { /* Special case, explanation is above. */ | |
374 | union overhead *n_op = nextf[7 - 3]->ov_next; | |
375 | nextf[7 - 3] = (union overhead *)((caddr_t)nextf[7 - 3] | |
376 | - sizeof(union overhead)); | |
377 | nextf[7 - 3]->ov_next = n_op; | |
378 | } | |
379 | #endif /* !PACK_MALLOC */ | |
8d063cd8 LW |
380 | } |
381 | ||
94b6baf5 | 382 | Free_t |
352d5a3a | 383 | free(mp) |
2304df62 | 384 | Malloc_t mp; |
8d063cd8 | 385 | { |
ee0007ab | 386 | register MEM_SIZE size; |
8d063cd8 | 387 | register union overhead *op; |
352d5a3a | 388 | char *cp = (char*)mp; |
cf5c4ad8 | 389 | #ifdef PACK_MALLOC |
390 | u_char bucket; | |
391 | #endif | |
8d063cd8 | 392 | |
45d8adaa | 393 | #ifdef safemalloc |
760ac839 | 394 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(unsigned long)cp,an++)); |
45d8adaa LW |
395 | #endif /* safemalloc */ |
396 | ||
cf5c4ad8 | 397 | if (cp == NULL) |
398 | return; | |
399 | op = (union overhead *)((caddr_t)cp | |
400 | - sizeof (union overhead) * CHUNK_SHIFT); | |
401 | #ifdef PACK_MALLOC | |
402 | bucket = OV_INDEX(op); | |
403 | #endif | |
760ac839 | 404 | #ifdef DEBUGGING |
cf5c4ad8 | 405 | ASSERT(OV_MAGIC(op, bucket) == MAGIC); /* make sure it was in use */ |
8d063cd8 | 406 | #else |
cf5c4ad8 | 407 | if (OV_MAGIC(op, bucket) != MAGIC) { |
408 | static bad_free_warn = -1; | |
409 | if (bad_free_warn == -1) { | |
410 | char *pbf = getenv("PERL_BADFREE"); | |
411 | bad_free_warn = (pbf) ? atoi(pbf) : 1; | |
412 | } | |
413 | if (!bad_free_warn) | |
414 | return; | |
8990e307 | 415 | #ifdef RCHECK |
a687059c | 416 | warn("%s free() ignored", |
8990e307 LW |
417 | op->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad"); |
418 | #else | |
419 | warn("Bad free() ignored"); | |
420 | #endif | |
8d063cd8 | 421 | return; /* sanity */ |
378cc40b | 422 | } |
8d063cd8 LW |
423 | #endif |
424 | #ifdef RCHECK | |
425 | ASSERT(op->ov_rmagic == RMAGIC); | |
cf5c4ad8 | 426 | if (OV_INDEX(op) <= 13) |
8d063cd8 | 427 | ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC); |
8990e307 | 428 | op->ov_rmagic = RMAGIC - 1; |
8d063cd8 | 429 | #endif |
cf5c4ad8 | 430 | ASSERT(OV_INDEX(op) < NBUCKETS); |
431 | size = OV_INDEX(op); | |
8d063cd8 LW |
432 | op->ov_next = nextf[size]; |
433 | nextf[size] = op; | |
c07a80fd | 434 | #ifdef DEBUGGING_MSTATS |
8d063cd8 LW |
435 | nmalloc[size]--; |
436 | #endif | |
437 | } | |
438 | ||
439 | /* | |
440 | * When a program attempts "storage compaction" as mentioned in the | |
441 | * old malloc man page, it realloc's an already freed block. Usually | |
442 | * this is the last block it freed; occasionally it might be farther | |
443 | * back. We have to search all the free lists for the block in order | |
444 | * to determine its bucket: 1st we make one pass thru the lists | |
445 | * checking only the first block in each; if that fails we search | |
378cc40b | 446 | * ``reall_srchlen'' blocks in each list for a match (the variable |
8d063cd8 LW |
447 | * is extern so the caller can modify it). If that fails we just copy |
448 | * however many bytes was given to realloc() and hope it's not huge. | |
449 | */ | |
378cc40b | 450 | int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ |
8d063cd8 | 451 | |
2304df62 | 452 | Malloc_t |
352d5a3a | 453 | realloc(mp, nbytes) |
2304df62 | 454 | Malloc_t mp; |
ee0007ab | 455 | MEM_SIZE nbytes; |
8d063cd8 | 456 | { |
ee0007ab | 457 | register MEM_SIZE onb; |
8d063cd8 LW |
458 | union overhead *op; |
459 | char *res; | |
460 | register int i; | |
461 | int was_alloced = 0; | |
352d5a3a | 462 | char *cp = (char*)mp; |
8d063cd8 | 463 | |
45d8adaa LW |
464 | #ifdef safemalloc |
465 | #ifdef DEBUGGING | |
ee0007ab | 466 | MEM_SIZE size = nbytes; |
45d8adaa LW |
467 | #endif |
468 | ||
469 | #ifdef MSDOS | |
470 | if (nbytes > 0xffff) { | |
760ac839 | 471 | PerlIO_printf(PerlIO_stderr(), "Reallocation too large: %lx\n", size); |
79072805 | 472 | my_exit(1); |
45d8adaa LW |
473 | } |
474 | #endif /* MSDOS */ | |
475 | if (!cp) | |
ee0007ab | 476 | return malloc(nbytes); |
45d8adaa LW |
477 | #ifdef DEBUGGING |
478 | if ((long)nbytes < 0) | |
463ee0b2 | 479 | croak("panic: realloc"); |
45d8adaa LW |
480 | #endif |
481 | #endif /* safemalloc */ | |
482 | ||
cf5c4ad8 | 483 | op = (union overhead *)((caddr_t)cp |
484 | - sizeof (union overhead) * CHUNK_SHIFT); | |
485 | i = OV_INDEX(op); | |
486 | if (OV_MAGIC(op, i) == MAGIC) { | |
8d063cd8 | 487 | was_alloced++; |
8d063cd8 LW |
488 | } else { |
489 | /* | |
490 | * Already free, doing "compaction". | |
491 | * | |
492 | * Search for the old block of memory on the | |
493 | * free list. First, check the most common | |
494 | * case (last element free'd), then (this failing) | |
378cc40b | 495 | * the last ``reall_srchlen'' items free'd. |
8d063cd8 LW |
496 | * If all lookups fail, then assume the size of |
497 | * the memory block being realloc'd is the | |
498 | * smallest possible. | |
499 | */ | |
500 | if ((i = findbucket(op, 1)) < 0 && | |
378cc40b | 501 | (i = findbucket(op, reall_srchlen)) < 0) |
8d063cd8 LW |
502 | i = 0; |
503 | } | |
cf5c4ad8 | 504 | onb = (1L << (i + 3)) - |
505 | #ifdef PACK_MALLOC | |
506 | (i <= (MAX_PACKED - 3) ? 0 : M_OVERHEAD) | |
507 | #else | |
508 | M_OVERHEAD | |
509 | #endif | |
510 | ; | |
8d063cd8 LW |
511 | /* avoid the copy if same size block */ |
512 | if (was_alloced && | |
cf5c4ad8 | 513 | nbytes <= onb && nbytes > (onb >> 1) - M_OVERHEAD) { |
a687059c LW |
514 | #ifdef RCHECK |
515 | /* | |
516 | * Record new allocated size of block and | |
517 | * bound space with magic numbers. | |
518 | */ | |
cf5c4ad8 | 519 | if (OV_INDEX(op) <= 13) { |
a687059c LW |
520 | /* |
521 | * Convert amount of memory requested into | |
522 | * closest block size stored in hash buckets | |
523 | * which satisfies request. Account for | |
524 | * space used per block for accounting. | |
525 | */ | |
cf5c4ad8 | 526 | nbytes += M_OVERHEAD; |
a687059c LW |
527 | nbytes = (nbytes + 3) &~ 3; |
528 | op->ov_size = nbytes - 1; | |
529 | *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC; | |
530 | } | |
531 | #endif | |
45d8adaa | 532 | res = cp; |
a687059c | 533 | } |
45d8adaa LW |
534 | else { |
535 | if ((res = (char*)malloc(nbytes)) == NULL) | |
536 | return (NULL); | |
537 | if (cp != res) /* common optimization */ | |
ee0007ab | 538 | Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char); |
45d8adaa LW |
539 | if (was_alloced) |
540 | free(cp); | |
541 | } | |
542 | ||
543 | #ifdef safemalloc | |
544 | #ifdef DEBUGGING | |
a0d0e21e | 545 | if (debug & 128) { |
760ac839 LW |
546 | PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) rfree\n",(unsigned long)res,an++); |
547 | PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) realloc %ld bytes\n", | |
a0d0e21e LW |
548 | (unsigned long)res,an++,(long)size); |
549 | } | |
45d8adaa LW |
550 | #endif |
551 | #endif /* safemalloc */ | |
2304df62 | 552 | return ((Malloc_t)res); |
8d063cd8 LW |
553 | } |
554 | ||
555 | /* | |
556 | * Search ``srchlen'' elements of each free list for a block whose | |
557 | * header starts at ``freep''. If srchlen is -1 search the whole list. | |
558 | * Return bucket number, or -1 if not found. | |
559 | */ | |
ee0007ab | 560 | static int |
8d063cd8 LW |
561 | findbucket(freep, srchlen) |
562 | union overhead *freep; | |
563 | int srchlen; | |
564 | { | |
565 | register union overhead *p; | |
566 | register int i, j; | |
567 | ||
568 | for (i = 0; i < NBUCKETS; i++) { | |
569 | j = 0; | |
570 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | |
571 | if (p == freep) | |
572 | return (i); | |
573 | j++; | |
574 | } | |
575 | } | |
576 | return (-1); | |
577 | } | |
578 | ||
cf5c4ad8 | 579 | Malloc_t |
580 | calloc(elements, size) | |
581 | register MEM_SIZE elements; | |
582 | register MEM_SIZE size; | |
583 | { | |
584 | long sz = elements * size; | |
585 | Malloc_t p = malloc(sz); | |
586 | ||
587 | if (p) { | |
588 | memset((void*)p, 0, sz); | |
589 | } | |
590 | return p; | |
591 | } | |
592 | ||
c07a80fd | 593 | #ifdef DEBUGGING_MSTATS |
8d063cd8 LW |
594 | /* |
595 | * mstats - print out statistics about malloc | |
596 | * | |
597 | * Prints two lines of numbers, one showing the length of the free list | |
598 | * for each size category, the second showing the number of mallocs - | |
599 | * frees for each size category. | |
600 | */ | |
ee0007ab | 601 | void |
c07a80fd | 602 | dump_mstats(s) |
8d063cd8 LW |
603 | char *s; |
604 | { | |
605 | register int i, j; | |
606 | register union overhead *p; | |
c07a80fd | 607 | int topbucket=0, totfree=0, totused=0; |
608 | u_int nfree[NBUCKETS]; | |
8d063cd8 | 609 | |
c07a80fd | 610 | for (i=0; i < NBUCKETS; i++) { |
8d063cd8 LW |
611 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) |
612 | ; | |
c07a80fd | 613 | nfree[i] = j; |
614 | totfree += nfree[i] * (1 << (i + 3)); | |
8d063cd8 | 615 | totused += nmalloc[i] * (1 << (i + 3)); |
c07a80fd | 616 | if (nfree[i] || nmalloc[i]) |
617 | topbucket = i; | |
618 | } | |
619 | if (s) | |
760ac839 | 620 | PerlIO_printf(PerlIO_stderr(), "Memory allocation statistics %s (buckets 8..%d)\n", |
c07a80fd | 621 | s, (1 << (topbucket + 3)) ); |
760ac839 | 622 | PerlIO_printf(PerlIO_stderr(), " %7d free: ", totfree); |
c07a80fd | 623 | for (i=0; i <= topbucket; i++) { |
760ac839 | 624 | PerlIO_printf(PerlIO_stderr(), (i<5)?" %5d":" %3d", nfree[i]); |
8d063cd8 | 625 | } |
760ac839 | 626 | PerlIO_printf(PerlIO_stderr(), "\n %7d used: ", totused); |
c07a80fd | 627 | for (i=0; i <= topbucket; i++) { |
760ac839 | 628 | PerlIO_printf(PerlIO_stderr(), (i<5)?" %5d":" %3d", nmalloc[i]); |
c07a80fd | 629 | } |
760ac839 | 630 | PerlIO_printf(PerlIO_stderr(), "\n"); |
cf5c4ad8 | 631 | #ifdef PACK_MALLOC |
632 | if (sbrk_slack || start_slack) { | |
760ac839 | 633 | PerlIO_printf(PerlIO_stderr(), "Odd ends: %7d bytes from sbrk(), %7d from malloc.\n", |
cf5c4ad8 | 634 | sbrk_slack, start_slack); |
635 | } | |
636 | #endif | |
c07a80fd | 637 | } |
638 | #else | |
639 | void | |
640 | dump_mstats(s) | |
641 | char *s; | |
642 | { | |
8d063cd8 LW |
643 | } |
644 | #endif | |
a687059c | 645 | #endif /* lint */ |
cf5c4ad8 | 646 | |
647 | ||
648 | #ifdef USE_PERL_SBRK | |
649 | ||
760ac839 LW |
650 | # ifdef NeXT |
651 | # define PERL_SBRK_VIA_MALLOC | |
652 | # endif | |
653 | ||
654 | # ifdef PERL_SBRK_VIA_MALLOC | |
655 | # ifdef HIDEMYMALLOC | |
656 | # undef malloc | |
657 | # else | |
658 | # include "Error: -DPERL_SBRK_VIA_MALLOC requires -DHIDEMYMALLOC" | |
659 | # endif | |
cf5c4ad8 | 660 | |
661 | /* it may seem schizophrenic to use perl's malloc and let it call system */ | |
662 | /* malloc, the reason for that is only the 3.2 version of the OS that had */ | |
663 | /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */ | |
664 | /* end to the cores */ | |
665 | ||
760ac839 | 666 | # define SYSTEM_ALLOC(a) malloc(a) |
cf5c4ad8 | 667 | |
760ac839 | 668 | # endif /* PERL_SBRK_VIA_MALLOC */ |
cf5c4ad8 | 669 | |
670 | static IV Perl_sbrk_oldchunk; | |
671 | static long Perl_sbrk_oldsize; | |
672 | ||
760ac839 LW |
673 | # define PERLSBRK_32_K (1<<15) |
674 | # define PERLSBRK_64_K (1<<16) | |
cf5c4ad8 | 675 | |
676 | char * | |
677 | Perl_sbrk(size) | |
678 | int size; | |
679 | { | |
680 | IV got; | |
681 | int small, reqsize; | |
682 | ||
683 | if (!size) return 0; | |
684 | #ifdef safemalloc | |
685 | reqsize = size; /* just for the DEBUG_m statement */ | |
686 | #endif | |
687 | if (size <= Perl_sbrk_oldsize) { | |
688 | got = Perl_sbrk_oldchunk; | |
689 | Perl_sbrk_oldchunk += size; | |
690 | Perl_sbrk_oldsize -= size; | |
691 | } else { | |
692 | if (size >= PERLSBRK_32_K) { | |
693 | small = 0; | |
694 | } else { | |
695 | #ifndef safemalloc | |
696 | reqsize = size; | |
697 | #endif | |
698 | size = PERLSBRK_64_K; | |
699 | small = 1; | |
700 | } | |
701 | got = (IV)SYSTEM_ALLOC(size); | |
702 | if (small) { | |
703 | /* Chunk is small, register the rest for future allocs. */ | |
704 | Perl_sbrk_oldchunk = got + reqsize; | |
705 | Perl_sbrk_oldsize = size - reqsize; | |
706 | } | |
707 | } | |
708 | ||
709 | #ifdef safemalloc | |
760ac839 | 710 | DEBUG_m(PerlIO_printf(PerlIO_stderr(), "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n", |
cf5c4ad8 | 711 | size, reqsize, Perl_sbrk_oldsize, got)); |
712 | #endif | |
713 | ||
714 | return (void *)got; | |
715 | } | |
716 | ||
717 | #endif /* ! defined USE_PERL_SBRK */ |