Commit | Line | Data |
a0d0e21e |
1 | /* malloc.c |
8d063cd8 |
2 | * |
8d063cd8 |
3 | */ |
4 | |
5 | #ifndef lint |
a687059c |
6 | #ifdef DEBUGGING |
8d063cd8 |
7 | #define RCHECK |
a687059c |
8 | #endif |
8d063cd8 |
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 |
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 |
23 | #include "perl.h" |
24 | |
760ac839 |
25 | #ifdef DEBUGGING |
26 | #undef DEBUG_m |
27 | #define DEBUG_m(a) if (debug & 128) a |
28 | #endif |
29 | |
135863df |
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 |
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 |
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 |
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 |
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) |
ff68c719 |
115 | # define TWOK_MASKED(x) ((u_int)(x) & ~TWOK_MASK) |
116 | # define TWOK_SHIFT(x) ((u_int)(x) & TWOK_MASK) |
cf5c4ad8 |
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 |
147 | /* |
55497cff |
148 | * Big allocations are often of the size 2^n bytes. To make them a |
149 | * little bit better, make blocks of size 2^n+pagesize for big n. |
150 | */ |
151 | |
152 | #ifdef TWO_POT_OPTIMIZE |
153 | |
154 | # define PERL_PAGESIZE 4096 |
155 | # define FIRST_BIG_TWO_POT 14 /* 16K */ |
156 | # define FIRST_BIG_BLOCK (1<<FIRST_BIG_TWO_POT) /* 16K */ |
157 | /* If this value or more, check against bigger blocks. */ |
158 | # define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD) |
159 | /* If less than this value, goes into 2^n-overhead-block. */ |
160 | # define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD) |
161 | |
162 | #endif /* TWO_POT_OPTIMIZE */ |
163 | |
164 | #ifdef PERL_EMERGENCY_SBRK |
165 | |
166 | #ifndef BIG_SIZE |
167 | # define BIG_SIZE (1<<16) /* 64K */ |
168 | #endif |
169 | |
170 | static char *emergency_buffer; |
171 | static MEM_SIZE emergency_buffer_size; |
172 | |
173 | static char * |
174 | emergency_sbrk(size) |
175 | MEM_SIZE size; |
176 | { |
177 | if (size >= BIG_SIZE) { |
178 | /* Give the possibility to recover: */ |
179 | die("Out of memory during request for %i bytes", size); |
180 | /* croak may eat too much memory. */ |
181 | } |
182 | |
183 | if (!emergency_buffer) { |
184 | /* First offense, give a possibility to recover by dieing. */ |
185 | /* No malloc involved here: */ |
186 | GV **gvp = (GV**)hv_fetch(defstash, "^M", 2, 0); |
187 | SV *sv; |
188 | char *pv; |
189 | |
190 | if (!gvp) gvp = (GV**)hv_fetch(defstash, "\015", 1, 0); |
191 | if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv) |
192 | || (SvLEN(sv) < (1<<11) - M_OVERHEAD)) |
193 | return (char *)-1; /* Now die die die... */ |
194 | |
195 | /* Got it, now detach SvPV: */ |
bbce6d69 |
196 | pv = SvPV(sv, na); |
55497cff |
197 | /* Check alignment: */ |
ff68c719 |
198 | if (((u_int)(pv - M_OVERHEAD)) & ((1<<11) - 1)) { |
55497cff |
199 | PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n"); |
bbce6d69 |
200 | return (char *)-1; /* die die die */ |
55497cff |
201 | } |
202 | |
203 | emergency_buffer = pv - M_OVERHEAD; |
204 | emergency_buffer_size = SvLEN(sv) + M_OVERHEAD; |
205 | SvPOK_off(sv); |
206 | SvREADONLY_on(sv); |
207 | die("Out of memory!"); /* croak may eat too much memory. */ |
ff68c719 |
208 | } |
209 | else if (emergency_buffer_size >= size) { |
55497cff |
210 | emergency_buffer_size -= size; |
211 | return emergency_buffer + emergency_buffer_size; |
212 | } |
213 | |
214 | return (char *)-1; /* poor guy... */ |
215 | } |
216 | |
217 | #else /* !PERL_EMERGENCY_SBRK */ |
218 | # define emergency_sbrk(size) -1 |
219 | #endif /* !PERL_EMERGENCY_SBRK */ |
220 | |
221 | /* |
8d063cd8 |
222 | * nextf[i] is the pointer to the next free block of size 2^(i+3). The |
223 | * smallest allocatable block is 8 bytes. The overhead information |
224 | * precedes the data area returned to the user. |
225 | */ |
226 | #define NBUCKETS 30 |
227 | static union overhead *nextf[NBUCKETS]; |
cf5c4ad8 |
228 | |
229 | #ifdef USE_PERL_SBRK |
230 | #define sbrk(a) Perl_sbrk(a) |
231 | char * Perl_sbrk _((int size)); |
232 | #else |
8d063cd8 |
233 | extern char *sbrk(); |
cf5c4ad8 |
234 | #endif |
8d063cd8 |
235 | |
c07a80fd |
236 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
237 | /* |
238 | * nmalloc[i] is the difference between the number of mallocs and frees |
239 | * for a given block size. |
240 | */ |
241 | static u_int nmalloc[NBUCKETS]; |
8d063cd8 |
242 | #endif |
243 | |
760ac839 |
244 | #ifdef DEBUGGING |
4dfc412b |
245 | #define ASSERT(p) if (!(p)) botch(STRINGIFY(p)); else |
ee0007ab |
246 | static void |
8d063cd8 |
247 | botch(s) |
248 | char *s; |
249 | { |
4dfc412b |
250 | PerlIO_printf(PerlIO_stderr(), "assertion botched: %s\n", s); |
8d063cd8 |
251 | abort(); |
252 | } |
253 | #else |
254 | #define ASSERT(p) |
255 | #endif |
256 | |
2304df62 |
257 | Malloc_t |
8d063cd8 |
258 | malloc(nbytes) |
ee0007ab |
259 | register MEM_SIZE nbytes; |
8d063cd8 |
260 | { |
261 | register union overhead *p; |
262 | register int bucket = 0; |
ee0007ab |
263 | register MEM_SIZE shiftr; |
8d063cd8 |
264 | |
55497cff |
265 | #ifdef PERL_CORE |
45d8adaa |
266 | #ifdef DEBUGGING |
ee0007ab |
267 | MEM_SIZE size = nbytes; |
45d8adaa |
268 | #endif |
269 | |
55497cff |
270 | #ifdef HAS_64K_LIMIT |
45d8adaa |
271 | if (nbytes > 0xffff) { |
760ac839 |
272 | PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", (long)nbytes); |
79072805 |
273 | my_exit(1); |
45d8adaa |
274 | } |
55497cff |
275 | #endif /* HAS_64K_LIMIT */ |
45d8adaa |
276 | #ifdef DEBUGGING |
277 | if ((long)nbytes < 0) |
463ee0b2 |
278 | croak("panic: malloc"); |
45d8adaa |
279 | #endif |
55497cff |
280 | #endif /* PERL_CORE */ |
45d8adaa |
281 | |
8d063cd8 |
282 | /* |
283 | * Convert amount of memory requested into |
284 | * closest block size stored in hash buckets |
285 | * which satisfies request. Account for |
286 | * space used per block for accounting. |
287 | */ |
cf5c4ad8 |
288 | #ifdef PACK_MALLOC |
289 | if (nbytes > MAX_2_POT_ALGO) { |
290 | #endif |
55497cff |
291 | #ifdef TWO_POT_OPTIMIZE |
292 | if (nbytes >= FIRST_BIG_BOUND) { |
293 | nbytes -= PERL_PAGESIZE; |
294 | } |
295 | #endif |
cf5c4ad8 |
296 | nbytes += M_OVERHEAD; |
297 | nbytes = (nbytes + 3) &~ 3; |
298 | #ifdef PACK_MALLOC |
299 | } else if (nbytes == 0) { |
300 | nbytes = 1; |
301 | } |
302 | #endif |
8d063cd8 |
303 | shiftr = (nbytes - 1) >> 2; |
304 | /* apart from this loop, this is O(1) */ |
305 | while (shiftr >>= 1) |
306 | bucket++; |
307 | /* |
308 | * If nothing in hash bucket right now, |
309 | * request more memory from the system. |
310 | */ |
311 | if (nextf[bucket] == NULL) |
312 | morecore(bucket); |
45d8adaa |
313 | if ((p = (union overhead *)nextf[bucket]) == NULL) { |
55497cff |
314 | #ifdef PERL_CORE |
ee0007ab |
315 | if (!nomemok) { |
760ac839 |
316 | PerlIO_puts(PerlIO_stderr(),"Out of memory!\n"); |
79072805 |
317 | my_exit(1); |
ee0007ab |
318 | } |
45d8adaa |
319 | #else |
8d063cd8 |
320 | return (NULL); |
45d8adaa |
321 | #endif |
322 | } |
323 | |
55497cff |
324 | #ifdef PERL_CORE |
760ac839 |
325 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n", |
a0d0e21e |
326 | (unsigned long)(p+1),an++,(long)size)); |
55497cff |
327 | #endif /* PERL_CORE */ |
45d8adaa |
328 | |
8d063cd8 |
329 | /* remove from linked list */ |
bf38876a |
330 | #ifdef RCHECK |
331 | if (*((int*)p) & (sizeof(union overhead) - 1)) |
760ac839 |
332 | PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n", |
a0d0e21e |
333 | (unsigned long)*((int*)p),(unsigned long)p); |
bf38876a |
334 | #endif |
335 | nextf[bucket] = p->ov_next; |
cf5c4ad8 |
336 | OV_MAGIC(p, bucket) = MAGIC; |
337 | #ifndef PACK_MALLOC |
338 | OV_INDEX(p) = bucket; |
339 | #endif |
c07a80fd |
340 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
341 | nmalloc[bucket]++; |
342 | #endif |
343 | #ifdef RCHECK |
344 | /* |
345 | * Record allocated size of block and |
346 | * bound space with magic numbers. |
347 | */ |
348 | if (nbytes <= 0x10000) |
349 | p->ov_size = nbytes - 1; |
350 | p->ov_rmagic = RMAGIC; |
351 | *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; |
352 | #endif |
cf5c4ad8 |
353 | return ((Malloc_t)(p + CHUNK_SHIFT)); |
8d063cd8 |
354 | } |
355 | |
356 | /* |
357 | * Allocate more memory to the indicated bucket. |
358 | */ |
a0d0e21e |
359 | static void |
8d063cd8 |
360 | morecore(bucket) |
a687059c |
361 | register int bucket; |
8d063cd8 |
362 | { |
363 | register union overhead *op; |
364 | register int rnu; /* 2^rnu bytes will be requested */ |
365 | register int nblks; /* become nblks blocks of the desired size */ |
bbce6d69 |
366 | register MEM_SIZE siz, needed; |
cf5c4ad8 |
367 | int slack = 0; |
8d063cd8 |
368 | |
369 | if (nextf[bucket]) |
370 | return; |
55497cff |
371 | if (bucket == (sizeof(MEM_SIZE)*8 - 3)) { |
372 | croak("Allocation too large"); |
373 | } |
8d063cd8 |
374 | /* |
375 | * Insure memory is allocated |
376 | * on a page boundary. Should |
377 | * make getpageize call? |
378 | */ |
ee0007ab |
379 | #ifndef atarist /* on the atari we dont have to worry about this */ |
8d063cd8 |
380 | op = (union overhead *)sbrk(0); |
cf5c4ad8 |
381 | # ifndef I286 |
382 | # ifdef PACK_MALLOC |
ff68c719 |
383 | if ((u_int)op & 0x7ff) |
384 | (void)sbrk(slack = 2048 - ((u_int)op & 0x7ff)); |
cf5c4ad8 |
385 | # else |
ff68c719 |
386 | if ((u_int)op & 0x3ff) |
387 | (void)sbrk(slack = 1024 - ((u_int)op & 0x3ff)); |
cf5c4ad8 |
388 | # endif |
389 | # if defined(DEBUGGING_MSTATS) && defined(PACK_MALLOC) |
390 | sbrk_slack += slack; |
391 | # endif |
392 | # else |
a687059c |
393 | /* The sbrk(0) call on the I286 always returns the next segment */ |
cf5c4ad8 |
394 | # endif |
ee0007ab |
395 | #endif /* atarist */ |
a687059c |
396 | |
ee0007ab |
397 | #if !(defined(I286) || defined(atarist)) |
8d063cd8 |
398 | /* take 2k unless the block is bigger than that */ |
399 | rnu = (bucket <= 8) ? 11 : bucket + 3; |
a687059c |
400 | #else |
401 | /* take 16k unless the block is bigger than that |
ee0007ab |
402 | (80286s like large segments!), probably good on the atari too */ |
a687059c |
403 | rnu = (bucket <= 11) ? 14 : bucket + 3; |
404 | #endif |
8d063cd8 |
405 | nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ |
bbce6d69 |
406 | needed = (MEM_SIZE)1 << rnu; |
55497cff |
407 | #ifdef TWO_POT_OPTIMIZE |
bbce6d69 |
408 | needed += (bucket >= (FIRST_BIG_TWO_POT - 3) ? PERL_PAGESIZE : 0); |
55497cff |
409 | #endif |
bbce6d69 |
410 | op = (union overhead *)sbrk(needed); |
8d063cd8 |
411 | /* no more room! */ |
ff68c719 |
412 | if (op == (union overhead *)-1) { |
413 | op = (union overhead *)emergency_sbrk(needed); |
414 | if (op == (union overhead *)-1) |
8d063cd8 |
415 | return; |
ff68c719 |
416 | } |
8d063cd8 |
417 | /* |
418 | * Round up to minimum allocation size boundary |
419 | * and deduct from block count to reflect. |
420 | */ |
a687059c |
421 | #ifndef I286 |
cf5c4ad8 |
422 | # ifdef PACK_MALLOC |
ff68c719 |
423 | if ((u_int)op & 0x7ff) |
cf5c4ad8 |
424 | croak("panic: Off-page sbrk"); |
425 | # endif |
ff68c719 |
426 | if ((u_int)op & 7) { |
ee0007ab |
427 | op = (union overhead *)(((MEM_SIZE)op + 8) &~ 7); |
8d063cd8 |
428 | nblks--; |
429 | } |
a687059c |
430 | #else |
431 | /* Again, this should always be ok on an 80286 */ |
432 | #endif |
8d063cd8 |
433 | /* |
434 | * Add new memory allocated to that on |
435 | * free list for this hash bucket. |
436 | */ |
8d063cd8 |
437 | siz = 1 << (bucket + 3); |
cf5c4ad8 |
438 | #ifdef PACK_MALLOC |
439 | *(u_char*)op = bucket; /* Fill index. */ |
440 | if (bucket <= MAX_PACKED - 3) { |
441 | op = (union overhead *) ((char*)op + blk_shift[bucket]); |
442 | nblks = n_blks[bucket]; |
443 | # ifdef DEBUGGING_MSTATS |
444 | start_slack += blk_shift[bucket]; |
445 | # endif |
446 | } else if (bucket <= 11 - 1 - 3) { |
447 | op = (union overhead *) ((char*)op + blk_shift[bucket]); |
448 | /* nblks = n_blks[bucket]; */ |
449 | siz -= sizeof(union overhead); |
450 | } else op++; /* One chunk per block. */ |
451 | #endif /* !PACK_MALLOC */ |
452 | nextf[bucket] = op; |
8d063cd8 |
453 | while (--nblks > 0) { |
454 | op->ov_next = (union overhead *)((caddr_t)op + siz); |
455 | op = (union overhead *)((caddr_t)op + siz); |
456 | } |
8595d6f1 |
457 | /* Not all sbrks return zeroed memory.*/ |
cf5c4ad8 |
458 | op->ov_next = (union overhead *)NULL; |
cf5c4ad8 |
459 | #ifdef PACK_MALLOC |
460 | if (bucket == 7 - 3) { /* Special case, explanation is above. */ |
461 | union overhead *n_op = nextf[7 - 3]->ov_next; |
462 | nextf[7 - 3] = (union overhead *)((caddr_t)nextf[7 - 3] |
463 | - sizeof(union overhead)); |
464 | nextf[7 - 3]->ov_next = n_op; |
465 | } |
466 | #endif /* !PACK_MALLOC */ |
8d063cd8 |
467 | } |
468 | |
94b6baf5 |
469 | Free_t |
352d5a3a |
470 | free(mp) |
2304df62 |
471 | Malloc_t mp; |
8d063cd8 |
472 | { |
ee0007ab |
473 | register MEM_SIZE size; |
8d063cd8 |
474 | register union overhead *op; |
352d5a3a |
475 | char *cp = (char*)mp; |
cf5c4ad8 |
476 | #ifdef PACK_MALLOC |
477 | u_char bucket; |
478 | #endif |
8d063cd8 |
479 | |
55497cff |
480 | #ifdef PERL_CORE |
760ac839 |
481 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(unsigned long)cp,an++)); |
55497cff |
482 | #endif /* PERL_CORE */ |
45d8adaa |
483 | |
cf5c4ad8 |
484 | if (cp == NULL) |
485 | return; |
486 | op = (union overhead *)((caddr_t)cp |
487 | - sizeof (union overhead) * CHUNK_SHIFT); |
488 | #ifdef PACK_MALLOC |
489 | bucket = OV_INDEX(op); |
490 | #endif |
760ac839 |
491 | #ifdef DEBUGGING |
cf5c4ad8 |
492 | ASSERT(OV_MAGIC(op, bucket) == MAGIC); /* make sure it was in use */ |
8d063cd8 |
493 | #else |
cf5c4ad8 |
494 | if (OV_MAGIC(op, bucket) != MAGIC) { |
495 | static bad_free_warn = -1; |
496 | if (bad_free_warn == -1) { |
497 | char *pbf = getenv("PERL_BADFREE"); |
498 | bad_free_warn = (pbf) ? atoi(pbf) : 1; |
499 | } |
500 | if (!bad_free_warn) |
501 | return; |
8990e307 |
502 | #ifdef RCHECK |
a687059c |
503 | warn("%s free() ignored", |
8990e307 |
504 | op->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad"); |
505 | #else |
506 | warn("Bad free() ignored"); |
507 | #endif |
8d063cd8 |
508 | return; /* sanity */ |
378cc40b |
509 | } |
8d063cd8 |
510 | #endif |
511 | #ifdef RCHECK |
512 | ASSERT(op->ov_rmagic == RMAGIC); |
cf5c4ad8 |
513 | if (OV_INDEX(op) <= 13) |
8d063cd8 |
514 | ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC); |
8990e307 |
515 | op->ov_rmagic = RMAGIC - 1; |
8d063cd8 |
516 | #endif |
cf5c4ad8 |
517 | ASSERT(OV_INDEX(op) < NBUCKETS); |
518 | size = OV_INDEX(op); |
8d063cd8 |
519 | op->ov_next = nextf[size]; |
520 | nextf[size] = op; |
c07a80fd |
521 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
522 | nmalloc[size]--; |
523 | #endif |
524 | } |
525 | |
526 | /* |
527 | * When a program attempts "storage compaction" as mentioned in the |
528 | * old malloc man page, it realloc's an already freed block. Usually |
529 | * this is the last block it freed; occasionally it might be farther |
530 | * back. We have to search all the free lists for the block in order |
531 | * to determine its bucket: 1st we make one pass thru the lists |
532 | * checking only the first block in each; if that fails we search |
378cc40b |
533 | * ``reall_srchlen'' blocks in each list for a match (the variable |
8d063cd8 |
534 | * is extern so the caller can modify it). If that fails we just copy |
535 | * however many bytes was given to realloc() and hope it's not huge. |
536 | */ |
378cc40b |
537 | int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ |
8d063cd8 |
538 | |
2304df62 |
539 | Malloc_t |
352d5a3a |
540 | realloc(mp, nbytes) |
2304df62 |
541 | Malloc_t mp; |
ee0007ab |
542 | MEM_SIZE nbytes; |
8d063cd8 |
543 | { |
ee0007ab |
544 | register MEM_SIZE onb; |
8d063cd8 |
545 | union overhead *op; |
546 | char *res; |
547 | register int i; |
548 | int was_alloced = 0; |
352d5a3a |
549 | char *cp = (char*)mp; |
8d063cd8 |
550 | |
55497cff |
551 | #ifdef PERL_CORE |
45d8adaa |
552 | #ifdef DEBUGGING |
ee0007ab |
553 | MEM_SIZE size = nbytes; |
45d8adaa |
554 | #endif |
555 | |
55497cff |
556 | #ifdef HAS_64K_LIMIT |
45d8adaa |
557 | if (nbytes > 0xffff) { |
760ac839 |
558 | PerlIO_printf(PerlIO_stderr(), "Reallocation too large: %lx\n", size); |
79072805 |
559 | my_exit(1); |
45d8adaa |
560 | } |
55497cff |
561 | #endif /* HAS_64K_LIMIT */ |
45d8adaa |
562 | if (!cp) |
ee0007ab |
563 | return malloc(nbytes); |
45d8adaa |
564 | #ifdef DEBUGGING |
565 | if ((long)nbytes < 0) |
463ee0b2 |
566 | croak("panic: realloc"); |
45d8adaa |
567 | #endif |
55497cff |
568 | #endif /* PERL_CORE */ |
45d8adaa |
569 | |
cf5c4ad8 |
570 | op = (union overhead *)((caddr_t)cp |
571 | - sizeof (union overhead) * CHUNK_SHIFT); |
572 | i = OV_INDEX(op); |
573 | if (OV_MAGIC(op, i) == MAGIC) { |
55497cff |
574 | was_alloced = 1; |
8d063cd8 |
575 | } else { |
576 | /* |
577 | * Already free, doing "compaction". |
578 | * |
579 | * Search for the old block of memory on the |
580 | * free list. First, check the most common |
581 | * case (last element free'd), then (this failing) |
378cc40b |
582 | * the last ``reall_srchlen'' items free'd. |
8d063cd8 |
583 | * If all lookups fail, then assume the size of |
584 | * the memory block being realloc'd is the |
585 | * smallest possible. |
586 | */ |
587 | if ((i = findbucket(op, 1)) < 0 && |
378cc40b |
588 | (i = findbucket(op, reall_srchlen)) < 0) |
8d063cd8 |
589 | i = 0; |
590 | } |
cf5c4ad8 |
591 | onb = (1L << (i + 3)) - |
592 | #ifdef PACK_MALLOC |
593 | (i <= (MAX_PACKED - 3) ? 0 : M_OVERHEAD) |
594 | #else |
595 | M_OVERHEAD |
596 | #endif |
55497cff |
597 | #ifdef TWO_POT_OPTIMIZE |
598 | + (i >= (FIRST_BIG_TWO_POT - 3) ? PERL_PAGESIZE : 0) |
599 | #endif |
cf5c4ad8 |
600 | ; |
55497cff |
601 | /* |
602 | * avoid the copy if same size block. |
603 | * We are not agressive with boundary cases. Note that it is |
604 | * possible for small number of cases give false negative if |
605 | * both new size and old one are in the bucket for |
606 | * FIRST_BIG_TWO_POT, but the new one is near the lower end. |
607 | */ |
8d063cd8 |
608 | if (was_alloced && |
55497cff |
609 | nbytes <= onb && (nbytes > ( (onb >> 1) - M_OVERHEAD ) |
610 | #ifdef TWO_POT_OPTIMIZE |
611 | || (i == (FIRST_BIG_TWO_POT - 3) |
612 | && nbytes >= LAST_SMALL_BOUND ) |
613 | #endif |
614 | )) { |
a687059c |
615 | #ifdef RCHECK |
616 | /* |
617 | * Record new allocated size of block and |
618 | * bound space with magic numbers. |
619 | */ |
cf5c4ad8 |
620 | if (OV_INDEX(op) <= 13) { |
a687059c |
621 | /* |
622 | * Convert amount of memory requested into |
623 | * closest block size stored in hash buckets |
624 | * which satisfies request. Account for |
625 | * space used per block for accounting. |
626 | */ |
cf5c4ad8 |
627 | nbytes += M_OVERHEAD; |
a687059c |
628 | nbytes = (nbytes + 3) &~ 3; |
629 | op->ov_size = nbytes - 1; |
630 | *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC; |
631 | } |
632 | #endif |
45d8adaa |
633 | res = cp; |
a687059c |
634 | } |
45d8adaa |
635 | else { |
636 | if ((res = (char*)malloc(nbytes)) == NULL) |
637 | return (NULL); |
638 | if (cp != res) /* common optimization */ |
ee0007ab |
639 | Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char); |
45d8adaa |
640 | if (was_alloced) |
641 | free(cp); |
642 | } |
643 | |
55497cff |
644 | #ifdef PERL_CORE |
45d8adaa |
645 | #ifdef DEBUGGING |
a0d0e21e |
646 | if (debug & 128) { |
760ac839 |
647 | PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) rfree\n",(unsigned long)res,an++); |
648 | PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) realloc %ld bytes\n", |
a0d0e21e |
649 | (unsigned long)res,an++,(long)size); |
650 | } |
45d8adaa |
651 | #endif |
55497cff |
652 | #endif /* PERL_CORE */ |
2304df62 |
653 | return ((Malloc_t)res); |
8d063cd8 |
654 | } |
655 | |
656 | /* |
657 | * Search ``srchlen'' elements of each free list for a block whose |
658 | * header starts at ``freep''. If srchlen is -1 search the whole list. |
659 | * Return bucket number, or -1 if not found. |
660 | */ |
ee0007ab |
661 | static int |
8d063cd8 |
662 | findbucket(freep, srchlen) |
663 | union overhead *freep; |
664 | int srchlen; |
665 | { |
666 | register union overhead *p; |
667 | register int i, j; |
668 | |
669 | for (i = 0; i < NBUCKETS; i++) { |
670 | j = 0; |
671 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { |
672 | if (p == freep) |
673 | return (i); |
674 | j++; |
675 | } |
676 | } |
677 | return (-1); |
678 | } |
679 | |
cf5c4ad8 |
680 | Malloc_t |
681 | calloc(elements, size) |
682 | register MEM_SIZE elements; |
683 | register MEM_SIZE size; |
684 | { |
685 | long sz = elements * size; |
686 | Malloc_t p = malloc(sz); |
687 | |
688 | if (p) { |
689 | memset((void*)p, 0, sz); |
690 | } |
691 | return p; |
692 | } |
693 | |
c07a80fd |
694 | #ifdef DEBUGGING_MSTATS |
8d063cd8 |
695 | /* |
696 | * mstats - print out statistics about malloc |
697 | * |
698 | * Prints two lines of numbers, one showing the length of the free list |
699 | * for each size category, the second showing the number of mallocs - |
700 | * frees for each size category. |
701 | */ |
ee0007ab |
702 | void |
c07a80fd |
703 | dump_mstats(s) |
8d063cd8 |
704 | char *s; |
705 | { |
706 | register int i, j; |
707 | register union overhead *p; |
c07a80fd |
708 | int topbucket=0, totfree=0, totused=0; |
709 | u_int nfree[NBUCKETS]; |
8d063cd8 |
710 | |
c07a80fd |
711 | for (i=0; i < NBUCKETS; i++) { |
8d063cd8 |
712 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) |
713 | ; |
c07a80fd |
714 | nfree[i] = j; |
715 | totfree += nfree[i] * (1 << (i + 3)); |
8d063cd8 |
716 | totused += nmalloc[i] * (1 << (i + 3)); |
c07a80fd |
717 | if (nfree[i] || nmalloc[i]) |
718 | topbucket = i; |
719 | } |
720 | if (s) |
760ac839 |
721 | PerlIO_printf(PerlIO_stderr(), "Memory allocation statistics %s (buckets 8..%d)\n", |
c07a80fd |
722 | s, (1 << (topbucket + 3)) ); |
760ac839 |
723 | PerlIO_printf(PerlIO_stderr(), " %7d free: ", totfree); |
c07a80fd |
724 | for (i=0; i <= topbucket; i++) { |
760ac839 |
725 | PerlIO_printf(PerlIO_stderr(), (i<5)?" %5d":" %3d", nfree[i]); |
8d063cd8 |
726 | } |
760ac839 |
727 | PerlIO_printf(PerlIO_stderr(), "\n %7d used: ", totused); |
c07a80fd |
728 | for (i=0; i <= topbucket; i++) { |
760ac839 |
729 | PerlIO_printf(PerlIO_stderr(), (i<5)?" %5d":" %3d", nmalloc[i]); |
c07a80fd |
730 | } |
760ac839 |
731 | PerlIO_printf(PerlIO_stderr(), "\n"); |
cf5c4ad8 |
732 | #ifdef PACK_MALLOC |
733 | if (sbrk_slack || start_slack) { |
760ac839 |
734 | PerlIO_printf(PerlIO_stderr(), "Odd ends: %7d bytes from sbrk(), %7d from malloc.\n", |
cf5c4ad8 |
735 | sbrk_slack, start_slack); |
736 | } |
737 | #endif |
c07a80fd |
738 | } |
739 | #else |
740 | void |
741 | dump_mstats(s) |
742 | char *s; |
743 | { |
8d063cd8 |
744 | } |
745 | #endif |
a687059c |
746 | #endif /* lint */ |
cf5c4ad8 |
747 | |
748 | |
749 | #ifdef USE_PERL_SBRK |
750 | |
760ac839 |
751 | # ifdef NeXT |
752 | # define PERL_SBRK_VIA_MALLOC |
753 | # endif |
754 | |
755 | # ifdef PERL_SBRK_VIA_MALLOC |
756 | # ifdef HIDEMYMALLOC |
757 | # undef malloc |
758 | # else |
759 | # include "Error: -DPERL_SBRK_VIA_MALLOC requires -DHIDEMYMALLOC" |
760 | # endif |
cf5c4ad8 |
761 | |
762 | /* it may seem schizophrenic to use perl's malloc and let it call system */ |
763 | /* malloc, the reason for that is only the 3.2 version of the OS that had */ |
764 | /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */ |
765 | /* end to the cores */ |
766 | |
760ac839 |
767 | # define SYSTEM_ALLOC(a) malloc(a) |
cf5c4ad8 |
768 | |
760ac839 |
769 | # endif /* PERL_SBRK_VIA_MALLOC */ |
cf5c4ad8 |
770 | |
771 | static IV Perl_sbrk_oldchunk; |
772 | static long Perl_sbrk_oldsize; |
773 | |
760ac839 |
774 | # define PERLSBRK_32_K (1<<15) |
775 | # define PERLSBRK_64_K (1<<16) |
cf5c4ad8 |
776 | |
777 | char * |
778 | Perl_sbrk(size) |
779 | int size; |
780 | { |
781 | IV got; |
782 | int small, reqsize; |
783 | |
784 | if (!size) return 0; |
55497cff |
785 | #ifdef PERL_CORE |
cf5c4ad8 |
786 | reqsize = size; /* just for the DEBUG_m statement */ |
787 | #endif |
788 | if (size <= Perl_sbrk_oldsize) { |
789 | got = Perl_sbrk_oldchunk; |
790 | Perl_sbrk_oldchunk += size; |
791 | Perl_sbrk_oldsize -= size; |
792 | } else { |
793 | if (size >= PERLSBRK_32_K) { |
794 | small = 0; |
795 | } else { |
55497cff |
796 | #ifndef PERL_CORE |
cf5c4ad8 |
797 | reqsize = size; |
798 | #endif |
799 | size = PERLSBRK_64_K; |
800 | small = 1; |
801 | } |
802 | got = (IV)SYSTEM_ALLOC(size); |
803 | if (small) { |
804 | /* Chunk is small, register the rest for future allocs. */ |
805 | Perl_sbrk_oldchunk = got + reqsize; |
806 | Perl_sbrk_oldsize = size - reqsize; |
807 | } |
808 | } |
809 | |
55497cff |
810 | #ifdef PERL_CORE |
760ac839 |
811 | DEBUG_m(PerlIO_printf(PerlIO_stderr(), "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n", |
cf5c4ad8 |
812 | size, reqsize, Perl_sbrk_oldsize, got)); |
813 | #endif |
814 | |
815 | return (void *)got; |
816 | } |
817 | |
818 | #endif /* ! defined USE_PERL_SBRK */ |