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