1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)db_salloc.c 10.14 (Sleepycat) 11/16/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "shqueue.h"
1N/A#include "common_ext.h"
1N/A
1N/A/*
1N/A * Implement shared memory region allocation, using simple first-fit algorithm.
1N/A * The model is that we take a "chunk" of shared memory store and begin carving
1N/A * it up into areas, similarly to how malloc works. We do coalescing on free.
1N/A *
1N/A * The "len" field in the __data struct contains the length of the free region
1N/A * (less the size_t bytes that holds the length). We use the address provided
1N/A * by the caller to find this length, which allows us to free a chunk without
1N/A * requiring that the caller pass in the length of the chunk they're freeing.
1N/A */
1N/ASH_LIST_HEAD(__head);
1N/Astruct __data {
1N/A size_t len;
1N/A SH_LIST_ENTRY links;
1N/A};
1N/A
1N/A/*
1N/A * __db_shalloc_init --
1N/A * Initialize the area as one large chunk.
1N/A *
1N/A * PUBLIC: void __db_shalloc_init __P((void *, size_t));
1N/A */
1N/Avoid
1N/A__db_shalloc_init(area, size)
1N/A void *area;
1N/A size_t size;
1N/A{
1N/A struct __data *elp;
1N/A struct __head *hp;
1N/A
1N/A hp = area;
1N/A SH_LIST_INIT(hp);
1N/A
1N/A elp = (struct __data *)(hp + 1);
1N/A elp->len = size - sizeof(struct __head) - sizeof(elp->len);
1N/A SH_LIST_INSERT_HEAD(hp, elp, links, __data);
1N/A}
1N/A
1N/A/*
1N/A * __db_shalloc --
1N/A * Allocate some space from the shared region.
1N/A *
1N/A * PUBLIC: int __db_shalloc __P((void *, size_t, size_t, void *));
1N/A */
1N/Aint
1N/A__db_shalloc(p, len, align, retp)
1N/A void *p, *retp;
1N/A size_t len, align;
1N/A{
1N/A struct __data *elp;
1N/A size_t *sp;
1N/A void *rp;
1N/A
1N/A /*
1N/A * We never allocate less than the size of a struct __data, align
1N/A * to less than a size_t boundary, or align to something that's not
1N/A * a multiple of a size_t.
1N/A */
1N/A if (len < sizeof(struct __data))
1N/A len = sizeof(struct __data);
1N/A align = align <= sizeof(size_t) ?
1N/A sizeof(size_t) : ALIGN(align, sizeof(size_t));
1N/A
1N/A /* Walk the list, looking for a slot. */
1N/A for (elp = SH_LIST_FIRST((struct __head *)p, __data);
1N/A elp != NULL;
1N/A elp = SH_LIST_NEXT(elp, links, __data)) {
1N/A /*
1N/A * Calculate the value of the returned pointer if we were to
1N/A * use this chunk.
1N/A * + Find the end of the chunk.
1N/A * + Subtract the memory the user wants.
1N/A * + Find the closest previous correctly-aligned address.
1N/A */
1N/A rp = (u_int8_t *)elp + sizeof(size_t) + elp->len;
1N/A rp = (u_int8_t *)rp - len;
1N/A rp = (u_int8_t *)((ALIGNTYPE)rp & ~(align - 1));
1N/A
1N/A /*
1N/A * Rp may now point before elp->links, in which case the chunk
1N/A * was too small, and we have to try again.
1N/A */
1N/A if ((u_int8_t *)rp < (u_int8_t *)&elp->links)
1N/A continue;
1N/A
1N/A *(void **)retp = rp;
1N/A
1N/A#define SHALLOC_FRAGMENT 32
1N/A /*
1N/A * If there are at least SHALLOC_FRAGMENT additional bytes of
1N/A * memory, divide the chunk into two chunks.
1N/A */
1N/A if ((u_int8_t *)rp >=
1N/A (u_int8_t *)&elp->links + SHALLOC_FRAGMENT) {
1N/A sp = rp;
1N/A *--sp = elp->len -
1N/A ((u_int8_t *)rp - (u_int8_t *)&elp->links);
1N/A elp->len -= *sp + sizeof(size_t);
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * Otherwise, we return the entire chunk, wasting some amount
1N/A * of space to keep the list compact. However, because the
1N/A * address we're returning to the user may not be the address
1N/A * of the start of the region for alignment reasons, set the
1N/A * size_t length fields back to the "real" length field to a
1N/A * flag value, so that we can find the real length during free.
1N/A */
1N/A#define ILLEGAL_SIZE 1
1N/A SH_LIST_REMOVE(elp, links, __data);
1N/A for (sp = rp; (u_int8_t *)--sp >= (u_int8_t *)&elp->links;)
1N/A *sp = ILLEGAL_SIZE;
1N/A return (0);
1N/A }
1N/A
1N/A /* Nothing found large enough; need to grow the region. */
1N/A return (ENOMEM);
1N/A}
1N/A
1N/A/*
1N/A * __db_shalloc_free --
1N/A * Free a shared memory allocation.
1N/A *
1N/A * PUBLIC: void __db_shalloc_free __P((void *, void *));
1N/A */
1N/Avoid
1N/A__db_shalloc_free(regionp, ptr)
1N/A void *regionp, *ptr;
1N/A{
1N/A struct __data *elp, *lastp, *newp;
1N/A struct __head *hp;
1N/A size_t free_size, *sp;
1N/A int merged;
1N/A
1N/A /*
1N/A * Step back over flagged length fields to find the beginning of
1N/A * the object and its real size.
1N/A */
1N/A for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
1N/A ;
1N/A ptr = sp;
1N/A
1N/A newp = (struct __data *)((u_int8_t *)ptr - sizeof(size_t));
1N/A free_size = newp->len;
1N/A
1N/A /* Trash the returned memory. */
1N/A#ifdef DIAGNOSTIC
1N/A memset(ptr, 0xdb, free_size);
1N/A#endif
1N/A
1N/A /*
1N/A * Walk the list, looking for where this entry goes.
1N/A *
1N/A * We keep the free list sorted by address so that coalescing is
1N/A * trivial.
1N/A *
1N/A * XXX
1N/A * Probably worth profiling this to see how expensive it is.
1N/A */
1N/A hp = (struct __head *)regionp;
1N/A for (elp = SH_LIST_FIRST(hp, __data), lastp = NULL;
1N/A elp != NULL && (void *)elp < (void *)ptr;
1N/A lastp = elp, elp = SH_LIST_NEXT(elp, links, __data))
1N/A ;
1N/A
1N/A /*
1N/A * Elp is either NULL (we reached the end of the list), or the slot
1N/A * after the one that's being returned. Lastp is either NULL (we're
1N/A * returning the first element of the list) or the element before the
1N/A * one being returned.
1N/A *
1N/A * Check for coalescing with the next element.
1N/A */
1N/A merged = 0;
1N/A if ((u_int8_t *)ptr + free_size == (u_int8_t *)elp) {
1N/A newp->len += elp->len + sizeof(size_t);
1N/A SH_LIST_REMOVE(elp, links, __data);
1N/A if (lastp != NULL)
1N/A SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
1N/A else
1N/A SH_LIST_INSERT_HEAD(hp, newp, links, __data);
1N/A merged = 1;
1N/A }
1N/A
1N/A /* Check for coalescing with the previous element. */
1N/A if (lastp != NULL && (u_int8_t *)lastp +
1N/A lastp->len + sizeof(size_t) == (u_int8_t *)newp) {
1N/A lastp->len += newp->len + sizeof(size_t);
1N/A
1N/A /*
1N/A * If we have already put the new element into the list take
1N/A * it back off again because it's just been merged with the
1N/A * previous element.
1N/A */
1N/A if (merged)
1N/A SH_LIST_REMOVE(newp, links, __data);
1N/A merged = 1;
1N/A }
1N/A
1N/A if (!merged)
1N/A if (lastp == NULL)
1N/A SH_LIST_INSERT_HEAD(hp, newp, links, __data);
1N/A else
1N/A SH_LIST_INSERT_AFTER(lastp, newp, links, __data);
1N/A}
1N/A
1N/A/*
1N/A * __db_shalloc_count --
1N/A * Return the amount of memory on the free list.
1N/A *
1N/A * PUBLIC: size_t __db_shalloc_count __P((void *));
1N/A */
1N/Asize_t
1N/A__db_shalloc_count(addr)
1N/A void *addr;
1N/A{
1N/A struct __data *elp;
1N/A size_t count;
1N/A
1N/A count = 0;
1N/A for (elp = SH_LIST_FIRST((struct __head *)addr, __data);
1N/A elp != NULL;
1N/A elp = SH_LIST_NEXT(elp, links, __data))
1N/A count += elp->len;
1N/A
1N/A return (count);
1N/A}
1N/A
1N/A/*
1N/A * __db_shsizeof --
1N/A * Return the size of a shalloc'd piece of memory.
1N/A *
1N/A * PUBLIC: size_t __db_shsizeof __P((void *));
1N/A */
1N/Asize_t
1N/A__db_shsizeof(ptr)
1N/A void *ptr;
1N/A{
1N/A struct __data *elp;
1N/A size_t *sp;
1N/A
1N/A /*
1N/A * Step back over flagged length fields to find the beginning of
1N/A * the object and its real size.
1N/A */
1N/A for (sp = (size_t *)ptr; sp[-1] == ILLEGAL_SIZE; --sp)
1N/A ;
1N/A
1N/A elp = (struct __data *)((u_int8_t *)sp - sizeof(size_t));
1N/A return (elp->len);
1N/A}
1N/A
1N/A/*
1N/A * __db_shalloc_dump --
1N/A *
1N/A * PUBLIC: void __db_shalloc_dump __P((void *, FILE *));
1N/A */
1N/Avoid
1N/A__db_shalloc_dump(addr, fp)
1N/A void *addr;
1N/A FILE *fp;
1N/A{
1N/A struct __data *elp;
1N/A
1N/A /* Make it easy to call from the debugger. */
1N/A if (fp == NULL)
1N/A fp = stderr;
1N/A
1N/A fprintf(fp, "%s\nMemory free list\n", DB_LINE);
1N/A
1N/A for (elp = SH_LIST_FIRST((struct __head *)addr, __data);
1N/A elp != NULL;
1N/A elp = SH_LIST_NEXT(elp, links, __data))
1N/A fprintf(fp, "%#lx: %lu\t", (u_long)elp, (u_long)elp->len);
1N/A fprintf(fp, "\n");
1N/A}