#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)db_salloc.c 10.14 (Sleepycat) 11/16/98";
#endif
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#endif
#include "db_int.h"
#include "shqueue.h"
#include "common_ext.h"
SH_LIST_HEAD(__head);
struct __data {
size_t len;
SH_LIST_ENTRY links;
};
void
__db_shalloc_init(area, size)
void *area;
size_t size;
{
struct __data *elp;
struct __head *hp;
hp = area;
SH_LIST_INIT(hp);
elp = (struct __data *)(hp + 1);
elp->len = size - sizeof(struct __head) - sizeof(elp->len);
SH_LIST_INSERT_HEAD(hp, elp, links, __data);
}
int
__db_shalloc(p, len, align, retp)
void *p, *retp;
size_t len, align;
{
struct __data *elp;
size_t *sp;
void *rp;
if (len < sizeof(struct __data))
len = sizeof(struct __data);
align = align <= sizeof(size_t) ?
sizeof(size_t) : ALIGN(align, sizeof(size_t));
for (elp = SH_LIST_FIRST((struct __head *)p, __data);
elp != NULL;
elp = SH_LIST_NEXT(elp, links, __data)) {
rp = (u_int8_t *)elp + sizeof(size_t) + elp->len;
rp = (u_int8_t *)rp - len;
rp = (u_int8_t *)((ALIGNTYPE)rp & ~(align - 1));
if ((u_int8_t *)rp < (u_int8_t *)&elp->links)
continue;
*(void **)retp = rp;
#define SHALLOC_FRAGMENT 32
if ((u_int8_t *)rp >=
(u_int8_t *)&elp->links + SHALLOC_FRAGMENT) {
sp = rp;
*--sp = elp->len -
((u_int8_t *)rp - (u_int8_t *)&elp->links);
elp->len -= *sp + sizeof(size_t);
return (0);
}
#define ILLEGAL_SIZE 1
SH_LIST_REMOVE(elp, links, __data);
for (sp = rp; (u_int8_t *)--sp >= (u_int8_t *)&elp->links;)
*sp = ILLEGAL_SIZE;
return (0);
}
return (ENOMEM);
}
void
__db_shalloc_free(regionp, ptr)
void *regionp, *ptr;
{
struct __data *elp, *lastp, *newp;
struct __head *hp;
size_t free_size, *sp;
int merged;
for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
;
ptr = sp;
newp = (struct __data *)((u_int8_t *)ptr - sizeof(size_t));
free_size = newp->len;
#ifdef DIAGNOSTIC
memset(ptr, 0xdb, free_size);
#endif
hp = (struct __head *)regionp;
for (elp = SH_LIST_FIRST(hp, __data), lastp = NULL;
elp != NULL && (void *)elp < (void *)ptr;
lastp = elp, elp = SH_LIST_NEXT(elp, links, __data))
;
merged = 0;
if ((u_int8_t *)ptr + free_size == (u_int8_t *)elp) {
newp->len += elp->len + sizeof(size_t);
SH_LIST_REMOVE(elp, links, __data);
if (lastp != NULL)
SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
else
SH_LIST_INSERT_HEAD(hp, newp, links, __data);
merged = 1;
}
if (lastp != NULL && (u_int8_t *)lastp +
lastp->len + sizeof(size_t) == (u_int8_t *)newp) {
lastp->len += newp->len + sizeof(size_t);
if (merged)
SH_LIST_REMOVE(newp, links, __data);
merged = 1;
}
if (!merged)
if (lastp == NULL)
SH_LIST_INSERT_HEAD(hp, newp, links, __data);
else
SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
}
size_t
__db_shalloc_count(addr)
void *addr;
{
struct __data *elp;
size_t count;
count = 0;
for (elp = SH_LIST_FIRST((struct __head *)addr, __data);
elp != NULL;
elp = SH_LIST_NEXT(elp, links, __data))
count += elp->len;
return (count);
}
size_t
__db_shsizeof(ptr)
void *ptr;
{
struct __data *elp;
size_t *sp;
for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
;
elp = (struct __data *)((u_int8_t *)sp - sizeof(size_t));
return (elp->len);
}
void
__db_shalloc_dump(addr, fp)
void *addr;
FILE *fp;
{
struct __data *elp;
if (fp == NULL)
fp = stderr;
fprintf(fp, "%s\nMemory free list\n", DB_LINE);
for (elp = SH_LIST_FIRST((struct __head *)addr, __data);
elp != NULL;
elp = SH_LIST_NEXT(elp, links, __data))
fprintf(fp, "%#lx: %lu\t", (u_long)elp, (u_long)elp->len);
fprintf(fp, "\n");
}