malloc.c revision 31c6d826a7f7a4ee7d83c8e99f25d82a4a248076
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1988 AT&T
* All Rights Reserved
*/
/*
* Simplified version of malloc(), calloc() and free(), to be linked with
* utilities that use [s]brk() and do not define their own version of the
* routines.
* Each call to mmap() creates a page. The pages are linked in a list.
* Each page is divided in blocks. There is at least one block in a page.
* New memory chunks are allocated on a first-fit basis.
* Freed blocks are joined in larger blocks. Free pages are unmapped.
*/
#include <stdlib.h>
#include <memory.h>
#include "_rtld.h"
#include "msg.h"
struct block {
int status;
void * memstart[1];
};
struct page {
};
#define FREE 0
#define BUSY 1
#if DEBUG
/*
* When built for debugging, scribble a pattern over newly allocated and
* freed memory.
*/
#define NEWMEM 0
#define FREMEM 1
/* LINTED */
};
static void
{
while (memsize--) {
}
}
#endif
/*
* Defragmentation
*/
void
defrag()
{
continue;
}
}
/*
* If a page becomes free, leave it, and save the unmapping
* expense, as we'll probably come back and reclaim the page
* for later malloc activity.
*
* Free the defrag index.
*/
}
}
static void
{
/* LINTED */
}
}
#include <stdio.h>
/*
* Replace both malloc() and lmalloc() (libc's private memory allocator).
* They are both private here.
*/
void *
{
/*
* Try to locate necessary space
*/
goto found;
}
}
/*
* Need to allocate a new page
*/
if (!page) {
MAP_PRIVATE)) == MAP_FAILED)
return (0);
}
#if DEBUG
#endif
}
void *
{
void * mp;
total = 0;
} else {
/* check for overflow */
return (NULL);
}
}
return (NULL);
return (mp);
}
void *
{
void * newptr;
/* LINTED */
/*
* Join block with next one if it is free
*/
}
#if DEBUG
#endif
return (ptr);
}
return (NULL);
/*
* Add the free block to the free APlist for later defragmentation.
* However, this addition can only be achieved if there is room on the
* free APlist. The APlist can't be allowed to grow, as the growth
* requires a realloc(), which would recurse back here, resulting in an
* infinite loop. If the free APlist is full, defrag() now. This
* defragmentation might not be able to collapse any free space, but
* the free APlist will be cleared as part of the processing, ensuring
* room for the addition.
*/
defrag();
return (newptr);
}
/*
* Replace both free() and lfree() (libc's private memory allocator).
* They are both private here.
*/
void
{
return;
/* LINTED */
#if DEBUG
#endif
}
/* ARGSUSED1 */
void
{
}
/*
* We can use any memory after ld.so.1's .bss up until the next page boundary
* as allocatable memory.
*/
void
{
return;
}