/*
- * Copyright (c) 1987 Regents of the University of California.
+ * Copyright (c) 1987, 1991 The Regents of the University of California.
* All rights reserved.
*
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
*
- * @(#)kern_malloc.c 7.22 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)kern_malloc.c 7.25 (Berkeley) 5/8/91
*/
#include "param.h"
-#include "cmap.h"
-#include "time.h"
#include "proc.h"
#include "map.h"
#include "kernel.h"
#include "malloc.h"
-#include "../vm/vm_param.h"
-#include "../vm/vm_map.h"
-#include "../vm/vm_kern.h"
+#include "vm/vm.h"
+#include "vm/vm_kern.h"
struct kmembuckets bucket[MINBUCKET + 16];
struct kmemstats kmemstats[M_LAST];
struct kmemusage *kmemusage;
-char *memname[] = INITKMEMNAMES;
char *kmembase, *kmemlimit;
char *memname[] = INITKMEMNAMES;
-long malloc_reentered;
-#define IN { if (malloc_reentered) panic("malloc reentered");\
- else malloc_reentered = 1;}
-#define OUT (malloc_reentered = 0)
/*
* Allocate a block of memory
*/
-qaddr_t
+void *
malloc(size, type, flags)
unsigned long size;
int type, flags;
register struct kmemusage *kup;
long indx, npg, alloc, allocsize;
int s;
- caddr_t va, cp, rp;
+ caddr_t va, cp, savedlist;
#ifdef KMEMSTATS
register struct kmemstats *ksp = &kmemstats[type];
if (((unsigned long)type) > M_LAST)
panic("malloc - bogus type");
+#endif
indx = BUCKETINDX(size);
kbp = &bucket[indx];
s = splimp();
- IN;
#ifdef KMEMSTATS
while (ksp->ks_memuse >= ksp->ks_limit) {
if (flags & M_NOWAIT) {
- OUT;
splx(s);
- return (0);
+ return ((void *) NULL);
}
if (ksp->ks_limblocks < 65535)
ksp->ks_limblocks++;
- OUT;
tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
- IN;
}
#endif
if (kbp->kb_next == NULL) {
va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
!(flags & M_NOWAIT));
if (va == NULL) {
- OUT;
splx(s);
- return (0);
+ return ((void *) NULL);
}
#ifdef KMEMSTATS
kbp->kb_total += kbp->kb_elmpercl;
* bucket, don't assume the list is still empty.
*/
savedlist = kbp->kb_next;
- rp = kbp->kb_next; /* returned while blocked in vmemall */
kbp->kb_next = va + (npg * NBPG) - allocsize;
- for (cp = kbp->kb_next; cp >= va; cp -= allocsize) {
- ((caddr_t *)cp)[2] = (cp > va ? cp - allocsize : rp);
- if (indx == 7) {
- long *lp = (long *)cp;
- lp[0] = lp[1] = lp[3] = lp[4] = -1;
- }
- }
+ for (cp = kbp->kb_next; cp > va; cp -= allocsize)
+ *(caddr_t *)cp = cp - allocsize;
+ *(caddr_t *)cp = savedlist;
}
va = kbp->kb_next;
- kbp->kb_next = ((caddr_t *)va)[2];
- if (indx == 7) {
- long *lp = (long *)va;
- if (lp[0] != -1 || lp[1] != -1 || lp[3] != -1 || lp[4] != -1)
- panic("malloc meddled");
- }
+ kbp->kb_next = *(caddr_t *)va;
#ifdef KMEMSTATS
kup = btokup(va);
if (kup->ku_indx != indx)
#else
out:
#endif
- OUT;
splx(s);
- return ((qaddr_t)va);
+ return ((void *) va);
}
#ifdef DIAGNOSTIC
};
#endif /* DIAGNOSTIC */
-#ifdef DIAGNOSTIC
-long addrmask[] = { 0x00000000,
- 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
- 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
- 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
- 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
-};
-#endif /* DIAGNOSTIC */
-
/*
* Free a block of memory allocated by malloc.
*/
void
free(addr, type)
- caddr_t addr;
+ void *addr;
int type;
{
register struct kmembuckets *kbp;
addr, size, type, alloc);
panic("free: unaligned addr");
}
-#endif /* DIAGNOSTIC */
- size = 1 << kup->ku_indx;
-#ifdef DIAGNOSTIC
- if (size > NBPG * CLSIZE)
- alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
- else
- alloc = addrmask[kup->ku_indx];
- if (((u_long)addr & alloc) != 0) {
- printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
- addr, size, type, alloc);
- panic("free: unaligned addr");
- }
#endif /* DIAGNOSTIC */
kbp = &bucket[kup->ku_indx];
s = splimp();
- IN;
if (size > MAXALLOCSAVE) {
kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
#ifdef KMEMSTATS
splx(s);
return;
}
- if (size == 128) {
- long *lp = (long *)addr;
- lp[0] = lp[1] = lp[3] = lp[4] = -1;
- }
#ifdef KMEMSTATS
kup->ku_freecnt++;
if (kup->ku_freecnt >= kbp->kb_elmpercl)
wakeup((caddr_t)ksp);
ksp->ks_inuse--;
#endif
- ((caddr_t *)addr)[2] = kbp->kb_next;
+ *(caddr_t *)addr = kbp->kb_next;
kbp->kb_next = addr;
- OUT;
splx(s);
}