#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/debug.h>
#include <memory.h>
#include "_rtld.h"
#include "msg.h"
struct block {
size_t size;
struct page *page;
int status;
struct block *next;
void * memstart[1];
};
struct page {
size_t size;
struct page *next;
struct block block[1];
};
#define FREE 0
#define BUSY 1
#define HDR_BLOCK (sizeof (struct block) - sizeof (void *))
#define HDR_PAGE (sizeof (struct page) - sizeof (void *))
static struct page *memstart;
#if DEBUG
#define NEWMEM 0
#define FREMEM 1
const ulong_t patterns[] = {
(ulong_t)0xbaddcafebaddcafeULL, (ulong_t)0xdeadbeefdeadbeefULL
};
static void
scribble(ulong_t *membgn, int pattern, size_t size)
{
size_t memsize = size / sizeof (ulong_t);
while (memsize--) {
if (pattern == FREMEM)
ASSERT(*membgn != patterns[pattern]);
*membgn++ = patterns[pattern];
}
}
#endif
void
defrag()
{
struct page *page;
Aliste idx;
for (APLIST_TRAVERSE(free_alp, idx, page)) {
struct block *block;
for (block = page->block; block; block = block->next) {
struct block *block2;
if (block->status == BUSY)
continue;
for (block2 = block->next; block2 &&
block2->status == FREE; block2 = block2->next) {
block->next = block2->next;
block->size += block2->size + HDR_BLOCK;
}
}
aplist_delete(free_alp, &idx);
}
}
static void
split(struct block *block, size_t size)
{
if (block->size > size + sizeof (struct block)) {
struct block *newblock;
newblock = (struct block *)
((char *)block + HDR_BLOCK + size);
newblock->next = block->next;
block->next = newblock;
newblock->status = FREE;
newblock->page = block->page;
newblock->size = block->size - size - HDR_BLOCK;
block->size = size;
}
}
#include <stdio.h>
#pragma weak lmalloc = malloc
void *
malloc(size_t size)
{
struct block *block;
struct page *page;
size = S_DROUND(size);
for (page = memstart; page; page = page->next) {
for (block = page->block; block; block = block->next) {
if ((block->status == FREE) && (block->size >= size))
goto found;
}
}
found:
if (!page) {
size_t totsize = size + HDR_PAGE;
size_t totpage = S_ROUND(totsize, syspagsz);
if ((page = dz_map(0, 0, totpage,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE)) == MAP_FAILED)
return (0);
page->next = memstart;
memstart = page;
page->size = totpage;
block = page->block;
block->next = 0;
block->status = FREE;
block->size = totpage - HDR_PAGE;
block->page = page;
}
split(block, size);
#if DEBUG
scribble((ulong_t *)&block->memstart, NEWMEM, block->size);
#endif
block->status = BUSY;
return (&block->memstart);
}
void *
calloc(size_t num, size_t size)
{
void * mp;
size_t total;
if (num == 0 || size == 0) {
total = 0;
} else {
total = num * size;
if ((total / num) != size) {
errno = ENOMEM;
return (NULL);
}
}
if ((mp = malloc(total)) == NULL)
return (NULL);
(void) memset(mp, 0, total);
return (mp);
}
void *
realloc(void *ptr, size_t size)
{
struct block *block;
size_t osize;
void * newptr;
if (ptr == NULL)
return (malloc(size));
block = (struct block *)((char *)ptr - HDR_BLOCK);
size = S_DROUND(size);
osize = block->size;
if (block->next && block->next->status == FREE) {
block->size += block->next->size + HDR_BLOCK;
block->next = block->next->next;
}
if (size <= block->size) {
split(block, size);
#if DEBUG
if (block->size > osize)
scribble((ulong_t *)((char *)ptr + osize), NEWMEM,
(block->size - osize));
#endif
return (ptr);
}
if ((newptr = malloc(size)) == NULL)
return (NULL);
(void) memcpy(newptr, ptr, osize);
block->status = FREE;
if (free_alp && (aplist_nitems(free_alp) >= aplist_arritems(free_alp)))
defrag();
(void) aplist_test(&free_alp, block->page, AL_CNT_FREELIST);
return (newptr);
}
void
free(void *ptr)
{
struct block *block;
if (ptr == NULL)
return;
block = (struct block *)((char *)ptr - HDR_BLOCK);
block->status = FREE;
#if DEBUG
scribble((ulong_t *)&block->memstart, FREMEM, block->size);
#endif
(void) aplist_test(&free_alp, block->page, AL_CNT_FREELIST);
}
void
lfree(void *ptr, size_t size)
{
free(ptr);
}
void
addfree(void *ptr, size_t bytes)
{
struct block *block;
struct page *page;
if (bytes <= sizeof (struct page))
return;
page = ptr;
page->next = memstart;
memstart = page;
page->size = bytes;
block = page->block;
block->next = 0;
block->status = FREE;
block->size = bytes - HDR_PAGE;
block->page = page;
}