heap_kmem.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#if 1
#endif
/* #define DEBUG ON */
/*
* Conditions on use:
* kmem_alloc and kmem_free must not be called from interrupt level,
* except from software interrupt level. This is because they are
* not reentrant, and only block out software interrupts. They take
* too long to block any real devices. There is a routine
* kmem_free_intr that can be used to free blocks at interrupt level,
* but only up to splimp, not higher. This is because kmem_free_intr
* only spl's to splimp.
*
* Also, these routines are not that fast, so they should not be used
* in very frequent operations (e.g. operations that happen more often
* than, say, once every few seconds).
*/
/*
* description:
* Yet another memory allocator, this one based on a method
* described in C.J. Stephenson, "Fast Fits", IBM Sys. Journal
*
* The basic data structure is a "Cartesian" binary tree, in which
* nodes are ordered by ascending addresses (thus minimizing free
* list insertion time) and block sizes decrease with depth in the
* tree (thus minimizing search time for a block of a given size).
*
* In other words, for any node s, letting D(s) denote
* the set of descendents of s, we have:
*
* a. addr(D(left(s))) < addr(s) < addr(D(right(s)))
* b. len(D(left(s))) <= len(s) >= len(D(right(s)))
*/
#include <sys/sysmacros.h>
/*
* The node header structure.
*
* To reduce storage consumption, a header block is associated with
* free blocks only, not allocated blocks.
* When a free block is allocated, its header block is put on
* a free header block list.
*
* This creates a header space and a free block space.
* The left pointer of a header blocks is used to chain free header
* blocks together.
*/
typedef enum {false, true} bool;
/*
* Description of a header for a free block
* Only free blocks have such headers.
*/
struct freehdr {
};
#define WORDSIZE sizeof (int)
/*
* Description of a data block.
*/
struct dblk {
};
/*
* weight(x) is the size of a block, in bytes; or 0 if and only if x
* is a null pointer. It is the responsibility of kmem_alloc() and
* kmem_free() to keep zero-length blocks out of the arena.
*/
#define max(a, b) ((a) < (b)? (b): (a))
void *kmem_alloc(size_t, int);
Freehdr getfreehdr(void);
static void check_need_to_free(void);
#ifdef __sparc
extern void resalloc_init(void);
#endif
extern int splnet(void);
extern int splimp(void);
extern void splx(int);
/*
* Structure containing various info about allocated memory.
*/
#define NEED_TO_FREE_SIZE 5
struct kmem_info {
struct need_to_free {
} kmem_info;
#ifdef DEBUG
#endif
/*
* Initialize kernel memory allocator
*/
void
kmem_init(void)
{
int i;
struct need_to_free *ntf;
#ifdef DEBUG
printf("kmem_init entered\n");
#endif
#ifdef __sparc
#endif
for (i = 0; i < NEED_TO_FREE_SIZE; i++) {
}
#ifdef DEBUG
printf("kmem_init returning\n");
#endif
}
/*
* Insert a new node in a cartesian tree or subtree, placing it
* in the correct position with respect to the existing nodes.
*
* algorithm:
* Starting from the root, a binary search is made for the new
* node. If this search were allowed to continue, it would
* eventually fail (since there cannot already be a node at the
* given address); but in fact it stops when it reaches a node in
* the tree which has a length less than that of the new node (or
* when it reaches a null tree pointer). The new node is then
* inserted at the root of the subtree for which the shorter node
* forms the old root (or in place of the null pointer).
*/
void
{
Freehdr x;
x = *tree;
/*
* Search for the first node which has a weight less
* than that of the new node; this will be the
* point at which we insert the new node.
*/
if (p < x->block)
else
x = *tree;
}
/*
* Perform root insertion. The variable x traces a path through
* the tree, and with the help of left_hook and right_hook,
* rewrites all links that cross the territory occupied
* by p. Note that this improves performance under
* paging.
*/
newhdr = getfreehdr();
while (x != NIL) {
/*
* Remark:
* The name 'left_hook' is somewhat confusing, since
* it is always set to the address of a .right link
* field. However, its value is always an address
* below (i.e., to the left of) p. Similarly
* for right_hook. The values of left_hook and
* right_hook converge toward the value of p,
* as in a classical binary search.
*/
if (x->block < p) {
/*
* rewrite link crossing from the left
*/
*left_hook = x;
x = x->right;
} else {
/*
* rewrite link crossing from the right
*/
*right_hook = x;
right_hook = &x->left;
x = x->left;
} /* else */
} /* while */
} /* insert */
/*
* Delete a node from a cartesian tree. p is the address of
* a pointer to the node which is to be deleted.
*
* algorithm:
* The left and right sons of the node to be deleted define two
* subtrees which are to be merged and attached in place of the
* deleted node. Each node on the inside edges of these two
* subtrees is examined and longer nodes are placed above the
* shorter ones.
*
* On entry:
* *p is assumed to be non-null.
*/
void
{
Freehdr x;
x = *p;
left_branch = x->left;
right_branch = x->right;
while (left_branch != right_branch) {
/*
* iterate until left branch and right branch are
* both NIL.
*/
/*
* promote the left branch
*/
*p = left_branch;
p = &left_branch->right;
} else {
/*
* promote the right branch
*/
*p = right_branch;
p = &right_branch->left;
} /* else */
} /* while */
*p = NIL;
freehdr(x);
} /* delete */
/*
* Demote a node in a cartesian tree, if necessary, to establish
* the required vertical ordering.
*
* algorithm:
* The left and right subtrees of the node to be demoted are to
* be partially merged and attached in place of the demoted node.
* The nodes on the inside edges of these two subtrees are
* examined and the longer nodes are placed above the shorter
* ones, until a node is reached which has a length no greater
* than that of the node being demoted (or until a null pointer
* is reached). The node is then attached at this point, and
* the remaining subtrees (if any) become its descendants.
*
* on entry:
* a. All the nodes in the tree, including the one to be demoted,
* must be correctly ordered horizontally;
* b. All the nodes except the one to be demoted must also be
* correctly positioned vertically. The node to be demoted
* may be already correctly positioned vertically, or it may
* have a length which is less than that of one or both of
* its progeny.
* c. *p is non-null
*/
static void
{
Freehdr x; /* addr of node to be demoted */
x = *p;
left_branch = x->left;
right_branch = x->right;
/*
* select a descendant branch for promotion
*/
/*
* promote the left branch
*/
*p = left_branch;
p = &left_branch->right;
left_branch = *p;
} else {
/*
* promote the right branch
*/
*p = right_branch;
p = &right_branch->left;
right_branch = *p;
} /* else */
} /* while */
*p = x; /* attach demoted node here */
x->left = left_branch;
x->right = right_branch;
} /* demote */
/*
* Allocate a block of storage
*
* algorithm:
* The freelist is searched by descending the tree from the root
* so that at each decision point the "better fitting" child node
* is chosen (i.e., the shorter one, if it is long enough, or
* the longer one, otherwise). The descent stops when both
* child nodes are too short.
*
* function result:
* kmem_alloc returns a pointer to the allocated block; a null
* pointer indicates storage could not be allocated.
*/
/*
* We need to return blocks that are on word boundaries so that callers
* that are putting int's into the area will work. Since we allow
* arbitrary free'ing, we need a weight function that considers
* free blocks starting on an odd boundary special. Allocation is
* aligned to 8 byte boundaries (ALIGN).
*/
/*
* If it is empty then weight == 0
* If it is aligned then weight == size
* If it is unaligned
* if not enough room to align then weight == 0
* else weight == aligned size
*/
/*ARGSUSED1*/
void *
{
Freehdr a; /* ptr to node to be allocated */
Freehdr *p; /* address of ptr to node */
char *retblock; /* Address returned to the user */
int s;
#ifdef DEBUG
#endif /* DEBUG */
if (nbytes == 0) {
return (NULL);
}
s = splnet();
if (nbytes < SMALLEST_BLK) {
prom_panic("kmem_alloc");
}
/*
* ensure that at least one block is big enough to satisfy
* the request.
*/
/*
* the largest block is not enough.
*/
prom_panic("kmem_alloc");
}
}
/*
* search down through the tree until a suitable block is
* found. At each decision point, select the better
* fitting node.
*/
a = *p;
if (left_weight <= right_weight) {
if (left_weight >= nbytes) {
p = &a->left;
a = left_son;
} else {
p = &a->right;
a = right_son;
}
} else {
if (right_weight >= nbytes) {
p = &a->right;
a = right_son;
} else {
p = &a->left;
a = left_son;
}
}
} /* while */
/*
* allocate storage from the selected node.
*/
/*
* not big enough to split; must leave at least
* a dblk's worth of space.
*/
delete(p);
} else {
/*
* split the node, allocating nbytes from the top.
* Remember we've already accounted for the
* allocated node's header space.
*/
Freehdr x;
x = getfreehdr();
prom_panic("kmem_alloc: short block allocated");
/*
* the node pointed to by *p has become smaller;
* move it down to its appropriate place in
* the tree.
*/
*p = x;
demote(p);
}
freehdr(a);
} else {
/*
* the node pointed to by *p has become smaller;
* move it down to its appropriate place in
* the tree.
*/
*p = x;
demote(p);
freehdr(a);
}
}
#ifdef DEBUG
#endif
splx(s);
#ifdef DEBUG
#endif
return (retblock);
} /* kmem_alloc */
/*
* Return a block to the free space tree.
*
* algorithm:
* Starting at the root, search for and coalesce free blocks
* adjacent to one given. When the appropriate place in the
* tree is found, insert the given block.
*
* Do some sanity checks to avoid total confusion in the tree.
* If the block has already been freed, prom_panic.
* If the ptr is not from the arena, prom_panic.
*/
void
{
char *neigh_block; /* Ptr to potential neighbor */
int s;
#ifdef DEBUG
#endif
#ifdef lint
#endif
if (nbytes == 0) {
return;
}
if (ptr == 0) {
prom_panic("kmem_free of 0");
}
s = splnet();
/*
* Search the tree for the correct insertion point for this
* node, coalescing adjacent free blocks along the way.
*/
if ((char *)ptr < neigh_block) {
/*
* Absorb and delete right neighbor
*/
nbytes += neigh_size;
/*
* The block being freed overlaps
* another block in the tree. This
* is bad news.
*/
printf("kmem_free: free block overlap %p+%lx"
(void *)neigh_block);
prom_panic("kmem_free: free block overlap");
} else {
/*
* Search to the left
*/
}
} else if ((char *)ptr > neigh_block) {
/*
* Absorb and delete left neighbor
*/
ptr = neigh_block;
nbytes += neigh_size;
/*
* This block has already been freed
*/
prom_panic("kmem_free block already free");
} else {
/*
* search to the right
*/
}
} else {
/*
* This block has already been freed
* as "ptr == neigh_block"
*/
prom_panic("kmem_free: block already free as neighbor");
return;
} /* else */
} /* while */
/*
* Insert the new node into the free space tree
*/
#ifdef DEBUG
printf("exiting kmem_free\n");
#endif
splx(s);
} /* kmem_free */
/*
* Sigh. We include a header file which the kernel
* uses to declare (one of its many) kmem_free prototypes.
* In order not to use the kernel's namespace, then, we must
* define another name here for use by boot.
*/
void *
{
return (kmem_alloc(size, 0));
}
/*
* Boot's kmem_alloc is really kmem_zalloc().
*/
void *
{
return (kmem_alloc(size, 0));
}
void
{
}
static void
check_need_to_free(void)
{
int i;
struct need_to_free *ntf;
int s;
s = splimp();
splx(s);
goto again;
}
for (i = 0; i < NEED_TO_FREE_SIZE; i++) {
splx(s);
goto again;
}
}
splx(s);
}
/*
* Add a block of at least nbytes to the free space tree.
*
* return value:
* true if at least nbytes can be allocated
* false otherwise
*
* remark:
* free space (delimited by the static variable ubound) is
* extended by an amount determined by rounding nbytes up to
* a multiple of the system page size.
*/
static bool
{
#ifdef __sparc
#else
#endif
Dblk p;
#ifdef DEBUG
#endif /* DEBUG */
if (p == 0) {
return (false);
}
#ifdef DEBUG
printf("morecore() returing, p = %p\n", p);
#endif
return (true);
} /* morecore */
/*
* Get a free block header
* There is a list of available free block headers.
* When the list is empty, allocate another pagefull.
*/
getfreehdr(void)
{
Freehdr r;
int n = 0;
#ifdef DEBUG
printf("getfreehdr()\n");
#endif /* DEBUG */
r = kmem_info.free_hdr_list;
} else {
if (r == 0) {
prom_panic("getfreehdr");
}
for (n = 1; n < PAGESIZE / sizeof (*r); n++) {
freehdr(&r[n]);
}
}
#ifdef DEBUG
printf("getfreehdr: freed %x headers\n", n);
printf("getfreehdr: returning %p\n", r);
#endif /* DEBUG */
return (r);
}
/*
* Free a free block header
* Add it to the list of available headers.
*/
void
{
#ifdef DEBUG
printf("freehdr(%p)\n", p);
#endif /* DEBUG */
kmem_info.free_hdr_list = p;
}
#ifdef DEBUG
/*
* Diagnostic routines
*/
static int depth = 0;
static void
{
int n;
if (depth == 0) {
}
if (p != NIL) {
depth++;
depth--;
for (n = 0; n < depth; n++) {
printf(" ");
}
"(%p): (left = %p, right = %p, block = %p, size = %lx)\n",
depth++;
depth--;
}
}
#endif /* DEBUG */