Changeset View
Changeset View
Standalone View
Standalone View
head/libexec/rtld-elf/malloc.c
Show First 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* The array of supported page sizes is provided by the user, i.e., the | * The array of supported page sizes is provided by the user, i.e., the | ||||
* program that calls this storage allocator. That program must initialize | * program that calls this storage allocator. That program must initialize | ||||
* the array before making its first call to allocate storage. The array | * the array before making its first call to allocate storage. The array | ||||
* must contain at least one page size. The page sizes must be stored in | * must contain at least one page size. The page sizes must be stored in | ||||
* increasing order. | * increasing order. | ||||
*/ | */ | ||||
extern size_t *pagesizes; | |||||
void * | void * | ||||
malloc(size_t nbytes) | malloc(size_t nbytes) | ||||
{ | { | ||||
union overhead *op; | union overhead *op; | ||||
int bucket; | int bucket; | ||||
ssize_t n; | ssize_t n; | ||||
size_t amt; | size_t amt; | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | calloc(size_t num, size_t size) | ||||
return (ret); | return (ret); | ||||
} | } | ||||
/* | /* | ||||
* Allocate more memory to the indicated bucket. | * Allocate more memory to the indicated bucket. | ||||
*/ | */ | ||||
static void | static void | ||||
morecore(bucket) | morecore(int bucket) | ||||
int bucket; | |||||
{ | { | ||||
union overhead *op; | union overhead *op; | ||||
int sz; /* size of desired block */ | int sz; /* size of desired block */ | ||||
int amt; /* amount to allocate */ | int amt; /* amount to allocate */ | ||||
int nblks; /* how many blocks we get */ | int nblks; /* how many blocks we get */ | ||||
/* | /* | ||||
* sbrk_size <= 0 only for big, FLUFFY, requests (about | * sbrk_size <= 0 only for big, FLUFFY, requests (about | ||||
Show All 26 Lines | */ | ||||
nextf[bucket] = op; | nextf[bucket] = op; | ||||
while (--nblks > 0) { | while (--nblks > 0) { | ||||
op->ov_next = (union overhead *)((caddr_t)op + sz); | op->ov_next = (union overhead *)((caddr_t)op + sz); | ||||
op = (union overhead *)((caddr_t)op + sz); | op = (union overhead *)((caddr_t)op + sz); | ||||
} | } | ||||
} | } | ||||
void | void | ||||
free(cp) | free(void * cp) | ||||
void *cp; | |||||
{ | { | ||||
int size; | int size; | ||||
union overhead *op; | union overhead *op; | ||||
if (cp == NULL) | if (cp == NULL) | ||||
return; | return; | ||||
op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | ||||
#ifdef MALLOC_DEBUG | #ifdef MALLOC_DEBUG | ||||
Show All 21 Lines | |||||
* this is the last block it freed; occasionally it might be farther | * this is the last block it freed; occasionally it might be farther | ||||
* back. We have to search all the free lists for the block in order | * back. We have to search all the free lists for the block in order | ||||
* to determine its bucket: 1st we make one pass through the lists | * to determine its bucket: 1st we make one pass through the lists | ||||
* checking only the first block in each; if that fails we search | * checking only the first block in each; if that fails we search | ||||
* ``realloc_srchlen'' blocks in each list for a match (the variable | * ``realloc_srchlen'' blocks in each list for a match (the variable | ||||
* is extern so the caller can modify it). If that fails we just copy | * is extern so the caller can modify it). If that fails we just copy | ||||
* however many bytes was given to realloc() and hope it's not huge. | * however many bytes was given to realloc() and hope it's not huge. | ||||
*/ | */ | ||||
int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ | static int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ | ||||
void * | void * | ||||
realloc(cp, nbytes) | realloc(void *cp, size_t nbytes) | ||||
void *cp; | |||||
size_t nbytes; | |||||
{ | { | ||||
u_int onb; | u_int onb; | ||||
int i; | int i; | ||||
union overhead *op; | union overhead *op; | ||||
char *res; | char *res; | ||||
int was_alloced = 0; | int was_alloced = 0; | ||||
if (cp == NULL) | if (cp == NULL) | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/* | /* | ||||
* Search ``srchlen'' elements of each free list for a block whose | * Search ``srchlen'' elements of each free list for a block whose | ||||
* header starts at ``freep''. If srchlen is -1 search the whole list. | * header starts at ``freep''. If srchlen is -1 search the whole list. | ||||
* Return bucket number, or -1 if not found. | * Return bucket number, or -1 if not found. | ||||
*/ | */ | ||||
static int | static int | ||||
findbucket(freep, srchlen) | findbucket(union overhead *freep, int srchlen) | ||||
union overhead *freep; | |||||
int srchlen; | |||||
{ | { | ||||
union overhead *p; | union overhead *p; | ||||
int i, j; | int i, j; | ||||
for (i = 0; i < NBUCKETS; i++) { | for (i = 0; i < NBUCKETS; i++) { | ||||
j = 0; | j = 0; | ||||
for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | ||||
if (p == freep) | if (p == freep) | ||||
return (i); | return (i); | ||||
j++; | j++; | ||||
} | } | ||||
} | } | ||||
return (-1); | return (-1); | ||||
} | } | ||||
#ifdef MSTATS | #ifdef MSTATS | ||||
/* | /* | ||||
* mstats - print out statistics about malloc | * mstats - print out statistics about malloc | ||||
* | * | ||||
* Prints two lines of numbers, one showing the length of the free list | * Prints two lines of numbers, one showing the length of the free list | ||||
* for each size category, the second showing the number of mallocs - | * for each size category, the second showing the number of mallocs - | ||||
* frees for each size category. | * frees for each size category. | ||||
*/ | */ | ||||
mstats(s) | mstats(char * s) | ||||
char *s; | |||||
{ | { | ||||
int i, j; | int i, j; | ||||
union overhead *p; | union overhead *p; | ||||
int totfree = 0, | int totfree = 0, | ||||
totused = 0; | totused = 0; | ||||
fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); | fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); | ||||
for (i = 0; i < NBUCKETS; i++) { | for (i = 0; i < NBUCKETS; i++) { | ||||
Show All 9 Lines | union overhead *p; | ||||
} | } | ||||
fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", | fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", | ||||
totused, totfree); | totused, totfree); | ||||
} | } | ||||
#endif | #endif | ||||
static int | static int | ||||
morepages(n) | morepages(int n) | ||||
int n; | |||||
{ | { | ||||
int fd = -1; | int fd = -1; | ||||
int offset; | int offset; | ||||
if (pagepool_end - pagepool_start > pagesz) { | if (pagepool_end - pagepool_start > pagesz) { | ||||
caddr_t addr = (caddr_t) | caddr_t addr = (caddr_t) | ||||
(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1)); | (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1)); | ||||
if (munmap(addr, pagepool_end - addr) != 0) { | if (munmap(addr, pagepool_end - addr) != 0) { | ||||
Show All 21 Lines |