* Copyright (c) 1987 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
* @(#)kern_malloc.c 7.3 (Berkeley) %G%
#include "../machine/pte.h"
struct kmembuckets bucket
[MINBUCKET
+ 16];
struct kmemstats kmemstats
[M_LAST
];
struct kmemusage
*kmemusage
;
* Allocate a block of memory
qaddr_t
malloc(size
, type
, flags
)
register struct kmembuckets
*kbp
;
register struct kmemusage
*kup
;
long indx
, npg
, alloc
, allocsize
, s
;
register struct kmemstats
*ksp
;
if (ksp
->ks_inuse
>= ksp
->ks_limit
)
if (kbp
->kb_next
== NULL
) {
allocsize
= roundup(size
, CLBYTES
);
npg
= clrnd(btoc(allocsize
));
if ((flags
& M_NOWAIT
) && freemem
< npg
) {
alloc
= rmalloc(kmemmap
, npg
);
if (vmemall(&kmempt
[alloc
], npg
, &proc
[0], CSYS
) == 0) {
rmfree(kmemmap
, npg
, alloc
);
va
= (caddr_t
) kmemxtob(alloc
);
vmaccess(&kmempt
[alloc
], va
, npg
);
kbp
->kb_total
+= kbp
->kb_elmpercl
;
if (allocsize
> MAXALLOCSAVE
) {
panic("malloc: allocation too large");
kup
->ku_freecnt
= kbp
->kb_elmpercl
;
kbp
->kb_totalfree
+= kbp
->kb_elmpercl
;
kbp
->kb_next
= va
+ (npg
* NBPG
) - allocsize
;
for(cp
= kbp
->kb_next
; cp
> va
; cp
-= allocsize
)
*(caddr_t
*)cp
= cp
- allocsize
;
kbp
->kb_next
= *(caddr_t
*)va
;
if (kup
->ku_indx
!= indx
)
panic("malloc: wrong bucket");
if (kup
->ku_freecnt
== 0)
panic("malloc: lost data");
if (ksp
->ks_inuse
> ksp
->ks_maxused
)
ksp
->ks_maxused
= ksp
->ks_inuse
;
* Free a block of memory allocated by malloc.
register struct kmembuckets
*kbp
;
register struct kmemusage
*kup
;
if (1 << kup
->ku_indx
> MAXALLOCSAVE
) {
(void) memfree(&kmempt
[alloc
], kup
->ku_pagecnt
, 0);
rmfree(kmemmap
, (long)kup
->ku_pagecnt
, alloc
);
kmemstats
[type
].ks_inuse
--;
kbp
= &bucket
[kup
->ku_indx
];
if (kup
->ku_freecnt
>= kbp
->kb_elmpercl
)
if (kup
->ku_freecnt
> kbp
->kb_elmpercl
)
panic("free: multiple frees");
else if (kbp
->kb_totalfree
> kbp
->kb_highwat
)
kmemstats
[type
].ks_inuse
--;
*(caddr_t
*)addr
= kbp
->kb_next
;
* Initialize the kernel memory allocator
if (!powerof2(MAXALLOCSAVE
))
panic("kmeminit: MAXALLOCSAVE not power of 2");
if (MAXALLOCSAVE
> MINALLOCSIZE
* 32768)
panic("kmeminit: MAXALLOCSAVE too big");
if (MAXALLOCSAVE
< CLBYTES
)
panic("kmeminit: MAXALLOCSAVE too small");
rminit(kmemmap
, ekmempt
- kmempt
, (long)CLSIZE
,
"malloc map", ekmempt
- kmempt
);
for (indx
= 0; indx
< MINBUCKET
+ 16; indx
++) {
if (1 << indx
>= CLBYTES
)
bucket
[indx
].kb_elmpercl
= 1;
bucket
[indx
].kb_elmpercl
= CLBYTES
/ (1 << indx
);
bucket
[indx
].kb_highwat
= 5 * bucket
[indx
].kb_elmpercl
;
for (indx
= 0; indx
< M_LAST
; indx
++)
kmemstats
[indx
].ks_limit
= 0x7fffffff;