Deleted Added
full compact
1/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.36 2002/03/08 17:36:47 christos Exp $ */
1/* $Header: /src/pub/tcsh/tc.alloc.c,v 3.39 2005/01/05 16:06:14 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 $")
43RCSID("$Id: tc.alloc.c,v 3.39 2005/01/05 16:06:14 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
50#ifdef WINNT_NATIVE
51# define malloc fmalloc
52# define free ffree
53# define calloc fcalloc
54# define realloc frealloc
55#endif /* WINNT_NATIVE */
56
57#ifndef SYSMALLOC

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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