Deleted Added
sdiff udiff text old ( 100616 ) new ( 145479 )
full compact
1/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.36 2002/03/08 17:36:47 christos Exp $ */
2/*
3 * tc.alloc.c (Caltech) 2/21/82
4 * Chris Kingsley, kingsley@cit-20.
5 *
6 * This is a very fast storage allocator. It allocates blocks of a small
7 * number of different sizes, and keeps free lists of each size. Blocks that
8 * don't exactly fit are passed up to the next larger size. In this
9 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.

--- 25 unchanged lines hidden (view full) ---

35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41#include "sh.h"
42
43RCSID("$Id: tc.alloc.c,v 3.36 2002/03/08 17:36:47 christos Exp $")
44
45static char *memtop = NULL; /* PWP: top of current memory */
46static char *membot = NULL; /* PWP: bottom of allocatable memory */
47
48int dont_free = 0;
49
50#if defined(_VMS_POSIX) || defined(_AMIGA_MEMORY)
51# define NO_SBRK
52#endif
53
54#ifdef WINNT_NATIVE
55# define malloc fmalloc
56# define free ffree
57# define calloc fcalloc
58# define realloc frealloc
59#endif /* WINNT_NATIVE */
60
61#ifndef SYSMALLOC

--- 94 unchanged lines hidden (view full) ---

156 xprintf(str, p); \
157 xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \
158 return; \
159 }
160#endif
161
162memalign_t
163malloc(nbytes)
164 register size_t nbytes;
165{
166#ifndef lint
167 register union overhead *p;
168 register int bucket = 0;
169 register unsigned shiftr;
170
171 /*
172 * Convert amount of memory requested into closest block size stored in
173 * hash buckets which satisfies request. Account for space used per block
174 * for accounting.
175 */
176#ifdef SUNOS4
177 /*

--- 54 unchanged lines hidden (view full) ---

232}
233
234#ifndef lint
235/*
236 * Allocate more memory to the indicated bucket.
237 */
238static void
239morecore(bucket)
240 register int bucket;
241{
242 register union overhead *op;
243 register int rnu; /* 2^rnu bytes will be requested */
244 register int nblks; /* become nblks blocks of the desired size */
245 register int siz;
246
247 if (nextf[bucket])
248 return;
249 /*
250 * Insure memory is allocated on a page boundary. Should make getpageize
251 * call?
252 */
253 op = (union overhead *) sbrk(0);

--- 36 unchanged lines hidden (view full) ---

290
291#endif
292
293void
294free(cp)
295 ptr_t cp;
296{
297#ifndef lint
298 register int size;
299 register union overhead *op;
300
301 /*
302 * the don't free flag is there so that we avoid os bugs in routines
303 * that free invalid pointers!
304 */
305 if (cp == NULL || dont_free)
306 return;
307 CHECK(!memtop || !membot,

--- 25 unchanged lines hidden (view full) ---

333#endif
334}
335
336memalign_t
337calloc(i, j)
338 size_t i, j;
339{
340#ifndef lint
341 register char *cp, *scp;
342
343 i *= j;
344 scp = cp = (char *) xmalloc((size_t) i);
345 if (i != 0)
346 do
347 *cp++ = 0;
348 while (--i);
349

--- 23 unchanged lines hidden (view full) ---

373#endif /* lint */
374
375memalign_t
376realloc(cp, nbytes)
377 ptr_t cp;
378 size_t nbytes;
379{
380#ifndef lint
381 register U_int onb;
382 union overhead *op;
383 ptr_t res;
384 register int i;
385 int was_alloced = 0;
386
387 if (cp == NULL)
388 return (malloc(nbytes));
389 op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
390 if (op->ov_magic == MAGIC) {
391 was_alloced++;
392 i = op->ov_index;

--- 56 unchanged lines hidden (view full) ---

449 * header starts at ``freep''. If srchlen is -1 search the whole list.
450 * Return bucket number, or -1 if not found.
451 */
452static int
453findbucket(freep, srchlen)
454 union overhead *freep;
455 int srchlen;
456{
457 register union overhead *p;
458 register int i, j;
459
460 for (i = 0; i < NBUCKETS; i++) {
461 j = 0;
462 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
463 if (p == freep)
464 return (i);
465 j++;
466 }

--- 21 unchanged lines hidden (view full) ---

488memalign_t
489smalloc(n)
490 size_t n;
491{
492 ptr_t ptr;
493
494 n = n ? n : 1;
495
496#ifndef NO_SBRK
497 if (membot == NULL)
498 membot = (char*) sbrk(0);
499#endif /* !NO_SBRK */
500
501 if ((ptr = malloc(n)) == (ptr_t) 0) {
502 child++;
503 stderror(ERR_NOMEM);
504 }
505#ifdef NO_SBRK
506 if (memtop < ((char *) ptr) + n)
507 memtop = ((char *) ptr) + n;
508 if (membot == NULL)
509 membot = (char*) ptr;
510#endif /* NO_SBRK */
511 return ((memalign_t) ptr);
512}
513
514memalign_t
515srealloc(p, n)
516 ptr_t p;
517 size_t n;
518{
519 ptr_t ptr;
520
521 n = n ? n : 1;
522
523#ifndef NO_SBRK
524 if (membot == NULL)
525 membot = (char*) sbrk(0);
526#endif /* NO_SBRK */
527
528 if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) {
529 child++;
530 stderror(ERR_NOMEM);
531 }
532#ifdef NO_SBRK
533 if (memtop < ((char *) ptr) + n)
534 memtop = ((char *) ptr) + n;
535 if (membot == NULL)
536 membot = (char*) ptr;
537#endif /* NO_SBRK */
538 return ((memalign_t) ptr);
539}
540
541memalign_t
542scalloc(s, n)
543 size_t s, n;
544{
545 char *sptr;
546 ptr_t ptr;
547
548 n *= s;
549 n = n ? n : 1;
550
551#ifndef NO_SBRK
552 if (membot == NULL)
553 membot = (char*) sbrk(0);
554#endif /* NO_SBRK */
555
556 if ((ptr = malloc(n)) == (ptr_t) 0) {
557 child++;
558 stderror(ERR_NOMEM);
559 }
560
561 sptr = (char *) ptr;
562 if (n != 0)
563 do
564 *sptr++ = 0;
565 while (--n);
566
567#ifdef NO_SBRK
568 if (memtop < ((char *) ptr) + n)
569 memtop = ((char *) ptr) + n;
570 if (membot == NULL)
571 membot = (char*) ptr;
572#endif /* NO_SBRK */
573
574 return ((memalign_t) ptr);
575}
576
577void
578sfree(p)
579 ptr_t p;
580{

--- 12 unchanged lines hidden (view full) ---

593 */
594/*ARGSUSED*/
595void
596showall(v, c)
597 Char **v;
598 struct command *c;
599{
600#ifndef SYSMALLOC
601 register int i, j;
602 register union overhead *p;
603 int totfree = 0, totused = 0;
604
605 xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
606 for (i = 0; i < NBUCKETS; i++) {
607 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
608 continue;
609 xprintf(" %4d", j);
610 totfree += j * (1 << (i + 3));

--- 5 unchanged lines hidden (view full) ---

616 }
617 xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
618 totused, totfree);
619 xprintf(CGETS(19, 11,
620 "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"),
621 (unsigned long) membot, (unsigned long) memtop,
622 (unsigned long) sbrk(0));
623#else
624#ifndef NO_SBRK
625 memtop = (char *) sbrk(0);
626#endif /* !NO_SBRK */
627 xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
628 (unsigned long) membot, (unsigned long) memtop,
629 (unsigned long) (memtop - membot));
630#endif /* SYSMALLOC */
631 USE(c);
632 USE(v);
633}