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