2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: alloc.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: allocating space *
|
2002-12-03 06:06:44 +00:00
|
|
|
* version:$Id: alloc.c,v 1.30 2002-12-03 06:06:43 vsc Exp $ *
|
2001-04-09 20:54:03 +01:00
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2002-05-19 20:04:33 +01:00
|
|
|
#if HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_MEMORY_H
|
|
|
|
#include <memory.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#if __simplescalar__
|
|
|
|
#ifdef USE_MMAP
|
|
|
|
#undef USE_MMAP
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SBRK
|
|
|
|
#undef USE_SBRK
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-10-17 02:37:46 +01:00
|
|
|
#if HAVE_SNPRINTF
|
|
|
|
#define snprintf3(A,B,C) snprintf(A,B,C)
|
|
|
|
#define snprintf4(A,B,C,D) snprintf(A,B,C,D)
|
|
|
|
#define snprintf5(A,B,C,D,E) snprintf(A,B,C,D,E)
|
|
|
|
#else
|
|
|
|
#define snprintf3(A,B,C) sprintf(A,C)
|
|
|
|
#define snprintf4(A,B,C,D) sprintf(A,C,D)
|
|
|
|
#define snprintf5(A,B,C,D,E) sprintf(A,C,D,E)
|
2002-10-10 06:58:49 +01:00
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(void FreeBlock, (BlockHeader *));
|
|
|
|
STATIC_PROTO(BlockHeader *GetBlock, (unsigned int));
|
|
|
|
STATIC_PROTO(char *AllocHeap, (unsigned int));
|
|
|
|
STATIC_PROTO(void RemoveFromFreeList, (BlockHeader *));
|
|
|
|
STATIC_PROTO(void AddToFreeList, (BlockHeader *));
|
|
|
|
|
|
|
|
#ifdef LIGHT
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define K ((Int) 1024)
|
|
|
|
|
|
|
|
#define MinHGap 256*K
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* Yap workspace management */
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_SizeOfBlock(CODEADDR p)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
BlockHeader *b = (BlockHeader *) (p - sizeof(YAP_SEG_SIZE));
|
|
|
|
YAP_SEG_SIZE s = (b->b_size) & ~InUseFlag;
|
|
|
|
return ((s - 1) * sizeof(YAP_SEG_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RemoveFromFreeList(BlockHeader *b)
|
|
|
|
{
|
|
|
|
BlockHeader *p;
|
|
|
|
|
|
|
|
p = b->b_next_size;
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed += (b->b_size + 1) * sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
|
|
|
|
if (p && b->b_size == p->b_size) {
|
|
|
|
b = b->b_next;
|
|
|
|
p->b_next = b;
|
|
|
|
if (b)
|
|
|
|
b->b_next_size = p;
|
|
|
|
}
|
|
|
|
else {
|
2001-06-08 14:39:07 +01:00
|
|
|
BlockHeader **q = &FreeBlocks;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
while ((*q) != b)
|
|
|
|
q = &((*q)->b_next_size);
|
|
|
|
if (b->b_next) {
|
|
|
|
p = b->b_next;
|
|
|
|
*q = p;
|
|
|
|
p->b_next_size = b->b_next_size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*q = b->b_next_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddToFreeList(BlockHeader *b)
|
|
|
|
{
|
|
|
|
BlockHeader **q, *p;
|
|
|
|
YAP_SEG_SIZE *sp;
|
|
|
|
|
|
|
|
/* insert on list of free blocks */
|
2001-06-08 14:39:07 +01:00
|
|
|
q = &FreeBlocks;
|
2001-04-09 20:54:03 +01:00
|
|
|
sp = &(b->b_size) + b->b_size;
|
|
|
|
*sp = b->b_size;
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed -= (b->b_size + 1) * sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
|
|
|
|
while ((p = *q) && p->b_size < b->b_size)
|
|
|
|
q = &p->b_next_size;
|
|
|
|
if (p && p->b_size == b->b_size) {
|
|
|
|
b->b_next = p;
|
|
|
|
b->b_next_size = p->b_next_size;
|
|
|
|
p->b_next_size = b;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
b->b_next = NIL;
|
|
|
|
b->b_next_size = p;
|
|
|
|
}
|
|
|
|
*q = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
long int call_counter;
|
|
|
|
|
|
|
|
static void
|
|
|
|
FreeBlock(BlockHeader *b)
|
|
|
|
{
|
|
|
|
BlockHeader *p;
|
|
|
|
YAP_SEG_SIZE *sp;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
sp = &(b->b_size) + (b->b_size & ~InUseFlag);
|
|
|
|
if (*sp != b->b_size) {
|
|
|
|
#if !SHORT_INTS
|
2002-11-18 18:18:05 +00:00
|
|
|
fprintf(Yap_stderr, "** sanity check failed in FreeBlock %p %x %x\n",
|
2001-04-09 20:54:03 +01:00
|
|
|
b, b->b_size, Unsigned(*sp));
|
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
fprintf(Yap_stderr, "** sanity check failed in FreeBlock %p %lx %lx\n",
|
2001-04-09 20:54:03 +01:00
|
|
|
b, b->b_size, *sp);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
b->b_size &= ~InUseFlag;
|
|
|
|
LOCK(FreeBlocksLock);
|
|
|
|
/* check if we can collapse with other blocsks */
|
|
|
|
/* check previous */
|
|
|
|
sp = &(b->b_size) - 1;
|
|
|
|
if (!(*sp & InUseFlag)) { /* previous block is free */
|
|
|
|
p = (BlockHeader *) (sp - *sp);
|
|
|
|
RemoveFromFreeList(p);
|
|
|
|
p->b_size += b->b_size + 1;
|
|
|
|
b = p;
|
|
|
|
}
|
|
|
|
/* check following */
|
|
|
|
sp = &(b->b_size) + b->b_size + 1;
|
|
|
|
if (!(*sp & InUseFlag)) { /* following block is free */
|
|
|
|
p = (BlockHeader *) sp;
|
|
|
|
RemoveFromFreeList(p);
|
|
|
|
b->b_size += p->b_size + 1;
|
|
|
|
}
|
2002-03-08 06:33:16 +00:00
|
|
|
/* check if we are the HeapTop */
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
LOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
if (sp == (YAP_SEG_SIZE *)HeapTop) {
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed -= (b->b_size + 1) * sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
HeapTop = (ADDR)b;
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* insert on list of free blocks */
|
2002-03-08 06:33:16 +00:00
|
|
|
AddToFreeList(b);
|
|
|
|
}
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
UNLOCK(FreeBlocksLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockHeader *
|
|
|
|
GetBlock(unsigned int n)
|
|
|
|
{ /* get free block with size at least n */
|
|
|
|
register BlockHeader **p, *b, *r;
|
|
|
|
|
|
|
|
if (FreeBlocks == NIL)
|
|
|
|
return (NIL);
|
2001-06-08 14:39:07 +01:00
|
|
|
p = &FreeBlocks;
|
2001-04-09 20:54:03 +01:00
|
|
|
while (((b = *p) != NIL) && b->b_size < n)
|
|
|
|
p = &b->b_next_size;
|
|
|
|
if (b == NIL || b->b_size < n)
|
|
|
|
return (NIL);
|
|
|
|
if ((r = b->b_next) == NIL)
|
|
|
|
*p = b->b_next_size;
|
|
|
|
else {
|
|
|
|
r->b_next_size = b->b_next_size;
|
|
|
|
*p = r;
|
|
|
|
}
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed += (b->b_size + 1) * sizeof(YAP_SEG_SIZE);
|
|
|
|
if (HeapUsed > HeapMax)
|
|
|
|
HeapMax = HeapUsed;
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
return (b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
AllocHeap(unsigned int size)
|
|
|
|
{
|
|
|
|
BlockHeader *b, *n;
|
|
|
|
YAP_SEG_SIZE *sp;
|
|
|
|
|
|
|
|
#if SIZEOF_INT_P==4
|
|
|
|
size = (((size + 7) & 0xffffff8) >> 2) + 2; /* size in dwords + 2 */
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INT_P==8
|
|
|
|
size = (((size + 7) & 0xffffff8) >> 3) + 2; /* size in dwords + 2 */
|
|
|
|
#endif
|
|
|
|
if (size < 6)
|
|
|
|
size = 6;
|
|
|
|
LOCK(FreeBlocksLock);
|
|
|
|
if ((b = GetBlock(size))) {
|
|
|
|
if (b->b_size >= size + 6 + 1) {
|
|
|
|
n = (BlockHeader *) (((YAP_SEG_SIZE *) b) + size + 1);
|
|
|
|
n->b_size = b->b_size - size - 1;
|
|
|
|
b->b_size = size;
|
|
|
|
AddToFreeList(n);
|
|
|
|
}
|
|
|
|
sp = &(b->b_size) + b->b_size;
|
|
|
|
*sp = b->b_size | InUseFlag;
|
|
|
|
b->b_size |= InUseFlag;
|
|
|
|
UNLOCK(FreeBlocksLock);
|
|
|
|
return (Addr(b) + sizeof(YAP_SEG_SIZE));
|
|
|
|
}
|
|
|
|
UNLOCK(FreeBlocksLock);
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
LOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
b = (BlockHeader *) HeapTop;
|
|
|
|
HeapTop += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
2002-11-18 18:18:05 +00:00
|
|
|
if (HeapTop > Addr(Yap_GlobalBase) - MinHeapGap) {
|
2001-04-09 20:54:03 +01:00
|
|
|
abort_optyap("No heap left in function AllocHeap");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (HeapTop > Addr(AuxSp) - MinHeapGap) {
|
|
|
|
HeapTop -= size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
HeapUsed -= size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
if (HeapTop > Addr(AuxSp)) {
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
/* we destroyed the stack */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR, TermNil, "Stack Crashed against Heap...");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
} else {
|
|
|
|
if (HeapTop + size * sizeof(CELL) + sizeof(YAP_SEG_SIZE) < Addr(AuxSp)) {
|
|
|
|
/* small allocations, we can wait */
|
|
|
|
HeapTop += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
HeapUsed += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
2002-03-08 06:33:16 +00:00
|
|
|
CreepFlag = Unsigned(LCL0+1);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
if (size > SizeOfOverflow)
|
|
|
|
SizeOfOverflow = size*sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
/* big allocations, the caller must handle the problem */
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
|
|
|
if (HeapUsed > HeapMax)
|
|
|
|
HeapMax = HeapUsed;
|
|
|
|
HeapPlus = HeapTop + MinHGap / CellSize;
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
b->b_size = size | InUseFlag;
|
|
|
|
sp = &(b->b_size) + size;
|
|
|
|
*sp = b->b_size;
|
|
|
|
if (!HEAPTOP_OWNER(worker_id)) {
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
return (Addr(b) + sizeof(YAP_SEG_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If you need to dinamically allocate space from the heap, this is
|
|
|
|
* the macro you should use */
|
|
|
|
ADDR
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PreAllocCodeSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
LOCK(HeapTopLock);
|
|
|
|
HEAPTOP_OWN(worker_id);
|
|
|
|
return (Addr(HeapTop) + sizeof(YAP_SEG_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
/* Grabbing the HeapTop is an excellent idea for a sequential system,
|
|
|
|
but does work as well in parallel systems. Anyway, this will do for now */
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReleasePreAllocCodeSpace(ADDR ptr)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
HEAPTOP_DISOWN(worker_id);
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If you need to dinamically allocate space from the heap, this is
|
|
|
|
* the macro you should use */
|
2002-11-11 17:38:10 +00:00
|
|
|
static void
|
2001-04-09 20:54:03 +01:00
|
|
|
FreeCodeSpace(char *p)
|
|
|
|
{
|
|
|
|
FreeBlock(((BlockHeader *) (p - sizeof(YAP_SEG_SIZE))));
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
/* If you need to dinamically allocate space from the heap, this is
|
|
|
|
* the macro you should use */
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeCodeSpace(char *p)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
FreeCodeSpace(p);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
char *
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_AllocAtomSpace(unsigned int size)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return (AllocHeap(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeAtomSpace(char *p)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
FreeCodeSpace(p);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static char *
|
2001-04-09 20:54:03 +01:00
|
|
|
AllocCodeSpace(unsigned int size)
|
|
|
|
{
|
|
|
|
if (size < SmallSize + 2 * OpCodeSize + 3 * CellSize)
|
|
|
|
return (AllocHeap(SmallSize + 2 * OpCodeSize + 3 * CellSize));
|
|
|
|
return (AllocHeap(size));
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
char *
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_AllocCodeSpace(unsigned int size)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return AllocCodeSpace(size);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/************************************************************************/
|
|
|
|
/* Workspace allocation */
|
|
|
|
/* */
|
|
|
|
/* We provide four alternatives for workspace allocation. */
|
|
|
|
/* - use 'mmap' */
|
|
|
|
/* - use 'shmat' */
|
|
|
|
/* - use 'sbrk' and provide a replacement to the 'malloc' library */
|
|
|
|
/* - use 'malloc' */
|
|
|
|
/* */
|
|
|
|
/* In any of the alternatives the interface is through the following */
|
|
|
|
/* functions: */
|
|
|
|
/* void *InitWorkSpace(int s) - initial workspace allocation */
|
|
|
|
/* int ExtendWorkSpace(int s) - extend workspace */
|
2002-11-18 18:18:05 +00:00
|
|
|
/* int Yap_FreeWorkSpace() - release workspace */
|
2001-04-09 20:54:03 +01:00
|
|
|
/************************************************************************/
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
#if defined(_WIN32)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
#define BASE_ADDRESS ((LPVOID) MMAP_ADDR)
|
2002-10-17 02:37:46 +01:00
|
|
|
/* #define MAX_WORKSPACE 0x40000000L */
|
|
|
|
#define MAX_WORKSPACE 0x20000000L
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static LPVOID brk;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ExtendWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
LPVOID b;
|
2002-11-18 18:18:05 +00:00
|
|
|
prolog_exec_mode OldPrologMode = Yap_PrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
|
|
|
s = ((s-1)/Yap_page_size+1)*Yap_page_size;
|
2001-04-09 20:54:03 +01:00
|
|
|
b = VirtualAlloc(brk, s, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (b) {
|
|
|
|
brk = (LPVOID) ((Int) brk + s);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"VirtualAlloc could not commit %ld bytes",
|
2002-10-17 02:37:46 +01:00
|
|
|
(long int)s);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static MALLOC_T
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
LPVOID b;
|
|
|
|
|
|
|
|
GetSystemInfo(&si);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_page_size = si.dwPageSize;
|
2001-04-09 20:54:03 +01:00
|
|
|
b = VirtualAlloc(BASE_ADDRESS, MAX_WORKSPACE, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (b==NULL) {
|
|
|
|
b = VirtualAlloc(0x0, MAX_WORKSPACE, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (b == NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR,TermNil,"VirtualAlloc failed");
|
2002-10-10 06:58:49 +01:00
|
|
|
return(0);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-11 17:38:10 +00:00
|
|
|
fprintf(stderr,"[ Warning: YAP reserving space at variable address %p ]\n", b);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
brk = BASE_ADDRESS;
|
|
|
|
|
|
|
|
if (ExtendWorkSpace(s)) {
|
|
|
|
return BASE_ADDRESS;
|
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR,TermNil,"VirtualAlloc Failed");
|
2002-10-10 06:58:49 +01:00
|
|
|
return(0);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeWorkSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif USE_MMAP
|
|
|
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MMAP_ADDR
|
|
|
|
#define USE_FIXED 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static MALLOC_T WorkSpaceTop;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static MALLOC_T
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T a;
|
|
|
|
#if !defined(_AIX) && !defined(__APPLE__) && !__hpux
|
|
|
|
int fd;
|
|
|
|
#endif
|
2001-05-03 19:48:06 +01:00
|
|
|
#if defined(_AIX)
|
2001-04-09 20:54:03 +01:00
|
|
|
a = mmap(0, (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_VARIABLE, -1, 0);
|
|
|
|
#elif __hpux
|
|
|
|
a = mmap(((void *)MMAP_ADDR), (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
2001-05-03 18:13:18 +01:00
|
|
|
if (a != (MALLOC_T)MMAP_ADDR) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not map ANON at %p, got %p", (void *)MMAP_ADDR, a);
|
2002-02-22 06:12:18 +00:00
|
|
|
return(NULL);
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif defined(__APPLE__)
|
2001-05-03 18:13:18 +01:00
|
|
|
a = mmap(((void *)MMAP_ADDR), (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
|
|
|
|
if (a != (MALLOC_T)MMAP_ADDR) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not map ANON at %p, got %p", (void *)MMAP_ADDR,a );
|
2002-02-22 06:12:18 +00:00
|
|
|
return(NULL);
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
fd = open("/dev/zero", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
#if HAVE_MKSTEMP
|
|
|
|
char file[256];
|
|
|
|
strncpy(file,"/tmp/YAP.TMPXXXXXX", 256);
|
|
|
|
if (mkstemp(file) == -1) {
|
|
|
|
#if HAVE_STRERROR
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mkstemp could not create temporary file %s (%s)", file, strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mkstemp could not create temporary file %s", file);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if HAVE_TMPNAM
|
|
|
|
char *file = tmpnam(NULL);
|
|
|
|
#else
|
|
|
|
char file[YAP_FILENAME_MAX];
|
|
|
|
strcpy(file,"/tmp/mapfile");
|
|
|
|
itos(getpid(), &file[12]);
|
|
|
|
#endif /* HAVE_TMPNAM */
|
|
|
|
#endif /* HAVE_MKSTEMP */
|
|
|
|
fd = open(file, O_CREAT|O_RDWR);
|
|
|
|
if (fd < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not open %s", file);
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (lseek(fd, s, SEEK_SET) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not lseek in mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (write(fd, "", 1) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not write in mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (unlink(file) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR,TermNil, "mmap could not unlink mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if USE_FIXED
|
|
|
|
a = mmap(((void *)MMAP_ADDR), (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_FIXED, fd, 0);
|
2001-05-03 18:13:18 +01:00
|
|
|
if (a != (MALLOC_T)MMAP_ADDR) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap could not map at %p, got %p", (void *)MMAP_ADDR, a);
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
a = mmap(0, (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE, fd, 0);
|
|
|
|
if ((CELL)a & YAP_PROTECTED_MASK) {
|
|
|
|
close(fd);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmapped address %p collides with YAP tags", a);
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (close(fd) == -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "while closing mmaped file");
|
2002-02-22 06:12:18 +00:00
|
|
|
return NULL;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if
|
|
|
|
#ifdef MMAP_FAILED
|
|
|
|
(a == (MALLOC_T) MMAP_FAILED)
|
|
|
|
#else
|
|
|
|
(a == (MALLOC_T) - 1)
|
|
|
|
#endif
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "mmap cannot allocate memory ***");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
WorkSpaceTop = (char *) a + s;
|
|
|
|
return (void *) a;
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ExtendWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
#ifdef YAPOR
|
|
|
|
abort_optyap("function ExtendWorkSpace called");
|
2001-05-21 21:00:05 +01:00
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
MALLOC_T a;
|
2002-11-18 18:18:05 +00:00
|
|
|
prolog_exec_mode OldPrologMode = Yap_PrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if defined(_AIX) || defined(__hpux)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
a = mmap(WorkSpaceTop, (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
a = mmap(WorkSpaceTop, (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
2001-05-03 18:13:18 +01:00
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
int fd;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
fd = open("/dev/zero", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
#if HAVE_MKSTEMP
|
|
|
|
char file[256];
|
|
|
|
strncpy(file,"/tmp/YAP.TMPXXXXXX",256);
|
|
|
|
if (mkstemp(file) == -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf5(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mkstemp could not create temporary file %s (%s)",
|
|
|
|
file, strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mkstemp could not create temporary file %s", file);
|
2001-05-03 18:23:30 +01:00
|
|
|
#endif /* HAVE_STRERROR */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if HAVE_TMPNAM
|
|
|
|
char *file = tmpnam(NULL);
|
|
|
|
#else
|
|
|
|
char file[YAP_FILENAME_MAX];
|
|
|
|
strcpy(file,"/tmp/mapfile");
|
|
|
|
itos(getpid(), &file[12]);
|
|
|
|
#endif /* HAVE_TMPNAM */
|
|
|
|
#endif /* HAVE_MKSTEMP */
|
|
|
|
fd = open(file, O_CREAT|O_RDWR);
|
|
|
|
if (fd < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not open %s", file);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (lseek(fd, s, SEEK_SET) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not lseek in mmapped file %s", file);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (write(fd, "", 1) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not write in mmapped file %s", file);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (unlink(file) < 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not unlink mmapped file %s", file);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-05-07 14:53:19 +01:00
|
|
|
a = mmap(WorkSpaceTop, (size_t) s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
2002-10-10 06:58:49 +01:00
|
|
|
MAP_PRIVATE
|
2002-11-11 17:38:10 +00:00
|
|
|
#if !defined(__linux)
|
2002-10-10 06:58:49 +01:00
|
|
|
/* use MAP_FIXED, otherwise God knows where you will be placed */
|
|
|
|
|MAP_FIXED
|
|
|
|
#endif
|
|
|
|
, fd, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (close(fd) == -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not close file (%s) ]\n", strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf3(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not close file ]\n");
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (a == (MALLOC_T) - 1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf5(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not allocate %d bytes (%s)", (int)s, strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-18 18:18:05 +00:00
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not allocate %d bytes", (int)s);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-05-03 18:13:18 +01:00
|
|
|
if (a != WorkSpaceTop) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf5(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"mmap could not grow memory at %p, got %p", WorkSpaceTop, a );
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-05-03 18:23:30 +01:00
|
|
|
return FALSE;
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
|
|
|
|
2001-05-03 18:23:30 +01:00
|
|
|
WorkSpaceTop = (char *) a + s;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
2001-05-21 21:00:05 +01:00
|
|
|
#endif /* YAPOR */
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeWorkSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif USE_SHM
|
|
|
|
|
|
|
|
#if HAVE_SYS_SHM_H
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MMAP_ADDR
|
|
|
|
#define MMAP_ADDR 0x0L
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static MALLOC_T WorkSpaceTop;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static MALLOC_T
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T ptr;
|
|
|
|
int shm_id;
|
|
|
|
|
|
|
|
/* mapping heap area */
|
|
|
|
if((shm_id = shmget(IPC_PRIVATE, (size_t)s, SHM_R|SHM_W)) == -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "could not shmget %d bytes", s);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
if((ptr = (MALLOC_T)shmat(shm_id, (void *) MMAP_ADDR, 0)) == (MALLOC_T) -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "could not shmat at %p", MMAP_ADDR);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
if (shmctl(shm_id, IPC_RMID, 0) != 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "could not remove shm segment", shm_id);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
WorkSpaceTop = (char *) ptr + s;
|
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ExtendWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T ptr;
|
|
|
|
int shm_id;
|
2002-12-03 06:06:44 +00:00
|
|
|
prolog_exec_mode OldPrologMode = Yap_PrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* mapping heap area */
|
|
|
|
if((shm_id = shmget(IPC_PRIVATE, (size_t)s, SHM_R|SHM_W)) == -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not shmget %d bytes", s);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if((ptr = (MALLOC_T)shmat(shm_id, WorkSpaceTop, 0)) == (MALLOC_T) -1) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not shmat at %p", MMAP_ADDR);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (shmctl(shm_id, IPC_RMID, 0) != 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not remove shm segment", shm_id);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
WorkSpaceTop = (char *) ptr + s;
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeWorkSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif USE_SBRK
|
|
|
|
|
|
|
|
/***********************************************************************\
|
|
|
|
* Worspace allocation based on 'sbrk' *
|
|
|
|
* We have to provide a replacement for the 'malloc' functions. *
|
|
|
|
* The situation is further complicated by the need to provide *
|
|
|
|
* temporary 'malloc' space when restoring a previously saved state. *
|
|
|
|
\***********************************************************************/
|
|
|
|
|
|
|
|
#ifdef _AIX
|
|
|
|
char *STD_PROTO(sbrk, (int));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int in_limbo; /* non-zero when restoring a saved state */
|
|
|
|
|
|
|
|
#ifndef LIMBO_SIZE
|
|
|
|
#define LIMBO_SIZE 32*K
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char limbo_space[LIMBO_SIZE]; /* temporary malloc space */
|
|
|
|
static char *limbo_p = limbo_space, *limbo_pp = 0;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static MALLOC_T
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T ptr = (MALLOC_T)sbrk(s);
|
|
|
|
|
|
|
|
if (ptr == ((MALLOC_T) - 1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "could not allocate %d bytes", s);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ExtendWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T ptr = (MALLOC_T)sbrk(s);
|
2002-12-03 06:06:44 +00:00
|
|
|
prolog_exec_mode OldPrologMode = Yap_PrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ptr == ((MALLOC_T) - 1)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-17 02:37:46 +01:00
|
|
|
"could not expand stacks over %d bytes", s);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeWorkSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_T
|
|
|
|
malloc(size_t size)
|
|
|
|
{
|
|
|
|
if (in_limbo) {
|
|
|
|
limbo_pp = limbo_p;
|
|
|
|
limbo_p += (size + 7) & 0xffff8;
|
|
|
|
if (limbo_p >= &limbo_space[LIMBO_SIZE])
|
|
|
|
return(NULL);
|
|
|
|
return (limbo_pp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CODEADDR codep = (CODEADDR)AllocCodeSpace(size + 2*sizeof(void *));
|
|
|
|
if (codep == NIL)
|
|
|
|
return(NIL);
|
|
|
|
else
|
|
|
|
return(codep + 2*sizeof(void *));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free(MALLOC_T ptr)
|
|
|
|
{
|
|
|
|
BlockHeader *b = (BlockHeader *) (((char *) ptr) - 2*sizeof(void *) - sizeof(YAP_SEG_SIZE));
|
|
|
|
|
|
|
|
if (ptr == limbo_pp) {
|
|
|
|
limbo_p = limbo_pp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
2002-11-18 18:18:05 +00:00
|
|
|
if ((char *) ptr < Yap_HeapBase || (char *) ptr > HeapTop)
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
if (!(b->b_size & InUseFlag))
|
|
|
|
return;
|
|
|
|
FreeCodeSpace((char *) ptr - 2*sizeof(void *));
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_T
|
|
|
|
realloc(MALLOC_T ptr, size_t size)
|
|
|
|
{
|
|
|
|
MALLOC_T new = malloc(size);
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
memcpy(new, ptr, size);
|
|
|
|
free(ptr);
|
|
|
|
return (new);
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_T
|
|
|
|
calloc(size_t n, size_t e)
|
|
|
|
{
|
|
|
|
unsigned k = n * e;
|
|
|
|
MALLOC_T p = malloc(k);
|
|
|
|
|
|
|
|
memset(p, 0, k);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef M_MXFAST
|
|
|
|
int
|
|
|
|
mallopt(cmd, value)
|
|
|
|
{
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mallinfo xmall;
|
|
|
|
|
|
|
|
struct mallinfo
|
|
|
|
mallinfo(void)
|
|
|
|
{
|
|
|
|
return (xmall);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* use malloc to initiliase memory */
|
|
|
|
|
|
|
|
/* user should ask for a lot of memory first */
|
|
|
|
|
2002-07-17 21:25:30 +01:00
|
|
|
#ifdef __linux
|
|
|
|
#define MAX_SPACE 420*1024*1024
|
|
|
|
#else
|
2002-01-26 05:37:32 +00:00
|
|
|
#define MAX_SPACE 128*1024*1024
|
2002-07-17 21:25:30 +01:00
|
|
|
#endif
|
2002-01-26 05:37:32 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static int total_space;
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static MALLOC_T
|
2001-04-09 20:54:03 +01:00
|
|
|
InitWorkSpace(Int s)
|
|
|
|
{
|
2002-05-19 20:04:33 +01:00
|
|
|
MALLOC_T ptr;
|
|
|
|
|
|
|
|
#ifdef M_MMAP_MAX
|
|
|
|
mallopt(M_MMAP_MAX, 0);
|
|
|
|
#endif
|
2002-07-23 16:49:06 +01:00
|
|
|
ptr = (MALLOC_T)calloc(MAX_SPACE,1);
|
2001-04-09 20:54:03 +01:00
|
|
|
total_space = s;
|
|
|
|
|
2002-07-17 21:25:30 +01:00
|
|
|
if (ptr == NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(FATAL_ERROR, TermNil, "could not allocate %d bytes", s);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
ExtendWorkSpace(Int s)
|
|
|
|
{
|
|
|
|
MALLOC_T ptr;
|
2002-12-03 06:06:44 +00:00
|
|
|
prolog_exec_mode OldPrologMode = Yap_PrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = ExtendStackMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
total_space += s;
|
2002-01-26 05:37:32 +00:00
|
|
|
if (total_space < MAX_SPACE) return(TRUE);
|
2002-11-18 18:18:05 +00:00
|
|
|
ptr = (MALLOC_T)realloc((void *)Yap_HeapBase, total_space);
|
2002-07-17 21:25:30 +01:00
|
|
|
if (ptr == NULL) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not allocate %d bytes", s);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
if (ptr != (MALLOC_T)Yap_HeapBase) {
|
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-10 06:58:49 +01:00
|
|
|
"could not expand contiguous stacks %d bytes", s);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
return(FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-09-24 15:35:58 +01:00
|
|
|
if ((CELL)ptr & MBIT) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ErrorMessage = Yap_ErrorSay;
|
|
|
|
snprintf5(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2002-10-23 21:55:37 +01:00
|
|
|
"memory at %p conflicts with MBIT %lx", ptr, (unsigned long)MBIT);
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
return(FALSE);
|
2001-09-24 15:35:58 +01:00
|
|
|
}
|
2002-12-03 06:06:44 +00:00
|
|
|
Yap_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_FreeWorkSpace(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static void
|
|
|
|
InitHeap(void *heap_addr)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* allocate space */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_HeapBase = heap_addr;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* reserve space for specially allocated functors and atoms so that
|
|
|
|
their values can be known statically */
|
2002-11-18 18:18:05 +00:00
|
|
|
HeapTop = Yap_HeapBase + AdjustSize(sizeof(all_heap_codes));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
HeapMax = HeapUsed = HeapTop-Yap_HeapBase;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
|
|
|
HeapTop = HeapTop + sizeof(YAP_SEG_SIZE);
|
|
|
|
*((YAP_SEG_SIZE *) HeapTop) = InUseFlag;
|
|
|
|
|
|
|
|
HeapPlus = HeapTop + MinHGap / CellSize;
|
|
|
|
FreeBlocks = NIL;
|
|
|
|
HEAPTOP_DISOWN(worker_id);
|
|
|
|
|
|
|
|
#if defined(YAPOR) || defined(TABLING)
|
|
|
|
#ifdef USE_HEAP
|
|
|
|
/* Try to make the system to crash */
|
|
|
|
BaseAllocArea = NULL;
|
|
|
|
TopAllocArea = BaseAllocArea;
|
|
|
|
#else
|
|
|
|
BaseAllocArea = AllocCodeSpace(OPT_CHUNK_SIZE);
|
|
|
|
TopAllocArea = BaseAllocArea;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LOCAL = REMOTE; /* point to the first area */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitHeap(void *heap_addr)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
InitHeap(heap_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitMemory(int Trail, int Heap, int Stack)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Int pm, sa, ta;
|
|
|
|
|
|
|
|
Trail = AdjustPageSize(Trail * K);
|
|
|
|
Stack = AdjustPageSize(Stack * K);
|
|
|
|
Heap = AdjustPageSize(Heap * K);
|
|
|
|
|
|
|
|
pm = (Trail + Heap + Stack); /* memory to be
|
|
|
|
* requested */
|
|
|
|
sa = Stack; /* stack area size */
|
|
|
|
ta = Trail; /* trail area size */
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
InitHeap(InitWorkSpace(pm));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_TrailTop = Yap_HeapBase + pm;
|
|
|
|
Yap_LocalBase = Yap_TrailTop - ta;
|
|
|
|
Yap_TrailBase = Yap_LocalBase + sizeof(CELL);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_GlobalBase = Yap_LocalBase - sa;
|
|
|
|
AuxTop = Yap_GlobalBase - CellSize; /* avoid confusions while
|
2001-04-09 20:54:03 +01:00
|
|
|
* * restoring */
|
|
|
|
AuxSp = (CELL *) AuxTop;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#if SIZEOF_INT_P!=SIZEOF_INT
|
2002-11-19 17:10:45 +00:00
|
|
|
if (Yap_output_msg) {
|
2001-05-07 20:56:02 +01:00
|
|
|
fprintf(stderr, "HeapBase = %p GlobalBase = %p\n LocalBase = %p TrailTop = %p\n",
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_HeapBase, Yap_GlobalBase, Yap_LocalBase, Yap_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2002-11-19 17:10:45 +00:00
|
|
|
if (Yap_output_msg) {
|
2001-05-07 20:56:02 +01:00
|
|
|
fprintf(stderr, "HeapBase = %x GlobalBase = %x\n LocalBase = %x TrailTop = %x\n",
|
2002-11-18 18:18:05 +00:00
|
|
|
(UInt) Yap_HeapBase, (UInt) Yap_GlobalBase,
|
|
|
|
(UInt) Yap_LocalBase, (UInt) Yap_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !SHORT_INTS
|
2001-05-07 20:56:02 +01:00
|
|
|
fprintf(stderr, "Heap+Aux: %d\tLocal+Global: %d\tTrail: %d\n",
|
2001-04-09 20:54:03 +01:00
|
|
|
pm - sa - ta, sa, ta);
|
|
|
|
#else /* SHORT_INTS */
|
2001-05-07 20:56:02 +01:00
|
|
|
fprintf(stderr, "Heap+Aux: %ld\tLocal+Global: %ld\tTrail: %ld\n",
|
2001-04-09 20:54:03 +01:00
|
|
|
pm - sa - ta, sa, ta);
|
|
|
|
#endif /* SHORT_INTS */
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ExtendWorkSpace(Int s)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return ExtendWorkSpace(s);
|
|
|
|
}
|