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 *
|
2008-05-11 00:24:13 +01:00
|
|
|
* version:$Id: alloc.c,v 1.95 2008-05-10 23:24:11 vsc Exp $ *
|
2001-04-09 20:54:03 +01:00
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2016-08-02 01:14:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "alloc.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2016-08-01 23:48:25 +01:00
|
|
|
#include "Yatom.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#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
|
2004-10-28 21:12:23 +01:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
#include "dlmalloc.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
|
2008-11-26 09:56:18 +00:00
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <stdio.h>
|
2016-08-01 23:48:25 +01:00
|
|
|
#include <stdlib.h>
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if __simplescalar__
|
2014-03-07 23:07:46 +00:00
|
|
|
#ifdef USE_SYSTEM_MMAP
|
|
|
|
#undef USE_SYSTEM_MMAP
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SBRK
|
|
|
|
#undef USE_SBRK
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* Yap workspace management */
|
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
#define MASK 0x968e00
|
2009-05-23 00:35:24 +01:00
|
|
|
#if USE_SYSTEM_MALLOC
|
2016-07-31 05:07:06 +01:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
|
|
|
#undef free
|
|
|
|
#undef malloc
|
|
|
|
#undef realloc
|
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
int write_malloc = 0;
|
2016-07-31 05:07:06 +01:00
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
void *my_malloc(size_t sz) {
|
2016-02-11 14:00:56 +00:00
|
|
|
void *p;
|
2016-07-31 05:07:06 +01:00
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
p = malloc(sz);
|
2016-02-11 14:00:56 +00:00
|
|
|
// Yap_DebugPuts(stderr,"gof\n");
|
2016-08-01 23:48:25 +01:00
|
|
|
if (sz > 500 && write_malloc++ > 0)
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "YAPDroid ", "+ %d %p", write_malloc,
|
|
|
|
p);
|
2016-02-11 14:00:56 +00:00
|
|
|
return p;
|
|
|
|
}
|
2016-07-31 05:07:06 +01:00
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
void *my_realloc(void *ptr, size_t sz) {
|
2016-07-31 05:07:06 +01:00
|
|
|
void *p;
|
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
p = realloc(ptr, sz);
|
2016-02-11 14:00:56 +00:00
|
|
|
// Yap_DebugPuts(stderr,"gof\n");
|
2016-08-01 23:48:25 +01:00
|
|
|
if (sz > 500 && write_malloc++ > 0)
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "YAPDroid ", "* %d %p", write_malloc,
|
|
|
|
p);
|
2016-07-31 05:07:06 +01:00
|
|
|
return p;
|
2016-02-11 14:00:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 23:48:25 +01:00
|
|
|
void my_free(void *p) {
|
|
|
|
// printf("f %p\n",p);
|
|
|
|
if (write_malloc && write_malloc++ > 0)
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "YAPDroid ", "- %d %p", write_malloc,
|
|
|
|
p);
|
2016-07-31 05:07:06 +01:00
|
|
|
|
|
|
|
free(p);
|
|
|
|
// Yap_DebugPuts(stderr,"gof\n");
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:00:56 +00:00
|
|
|
#endif
|
2016-07-31 05:07:06 +01:00
|
|
|
|
|
|
|
#else // USE_SYSTEM_MALLOC
|
|
|
|
|
2009-05-23 00:35:24 +01:00
|
|
|
#define my_malloc(sz) Yap_dlmalloc(sz)
|
2016-07-31 05:07:06 +01:00
|
|
|
#define my_realloc(ptr, sz) Yap_dlrealloc(ptr, sz)
|
2009-05-23 00:35:24 +01:00
|
|
|
#define my_free(sz) Yap_dlfree(sz)
|
|
|
|
|
2016-07-31 05:07:06 +01:00
|
|
|
static char *my_reallocl(char *ptr, UInt sz, UInt osz, int safe) {
|
2009-05-23 00:35:24 +01:00
|
|
|
char *nptr;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
restart:
|
2009-05-23 00:35:24 +01:00
|
|
|
/* simple case */
|
|
|
|
if (ptr < Yap_HeapBase || ptr > HeapTop) {
|
|
|
|
/* we have enough room */
|
|
|
|
nptr = Yap_dlmalloc(sz);
|
|
|
|
if (nptr) {
|
|
|
|
memmove(nptr, ptr, osz);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nptr = Yap_dlrealloc(ptr, sz);
|
|
|
|
}
|
|
|
|
if (nptr) {
|
|
|
|
return nptr;
|
|
|
|
}
|
|
|
|
/* we do not have enough room */
|
|
|
|
if (safe) {
|
|
|
|
if (Yap_growheap(FALSE, sz, NULL)) {
|
|
|
|
/* now, we have room */
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no room in Heap, gosh */
|
|
|
|
if (ptr < Yap_HeapBase || ptr > HeapTop) {
|
|
|
|
/* try expanding outside the heap */
|
|
|
|
nptr = realloc(ptr, sz);
|
|
|
|
if (nptr) {
|
|
|
|
memmove(nptr, ptr, osz);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* try calling the outside world for help */
|
|
|
|
nptr = malloc(sz);
|
|
|
|
if (!nptr)
|
|
|
|
return NULL;
|
|
|
|
memmove(nptr, ptr, osz);
|
|
|
|
Yap_dlfree(ptr);
|
|
|
|
}
|
|
|
|
/* did we suceed? at this point we could not care less */
|
|
|
|
return nptr;
|
2006-05-19 15:31:32 +01:00
|
|
|
}
|
2004-10-28 21:12:23 +01:00
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
#if USE_SYSTEM_MALLOC || USE_DL_MALLOC
|
2004-11-04 18:22:36 +00:00
|
|
|
|
2005-11-08 13:51:15 +00:00
|
|
|
long long unsigned int mallocs, reallocs, frees;
|
|
|
|
long long unsigned int tmalloc;
|
|
|
|
|
2010-04-15 22:23:54 +01:00
|
|
|
#undef INSTRUMENT_MALLOC
|
2005-11-08 13:51:15 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static inline char *call_malloc(size_t size) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2005-12-17 03:25:39 +00:00
|
|
|
char *out;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2005-11-08 13:51:15 +00:00
|
|
|
#if INSTRUMENT_MALLOC
|
|
|
|
mallocs++;
|
|
|
|
tmalloc += size;
|
2010-04-15 11:37:15 +01:00
|
|
|
size += sizeof(CELL);
|
2005-11-08 13:51:15 +00:00
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= MallocMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
out = (char *)my_malloc(size);
|
2010-04-15 11:37:15 +01:00
|
|
|
#if INSTRUMENT_MALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
*(CELL *)out = size - sizeof(CELL);
|
2010-04-15 11:37:15 +01:00
|
|
|
out += sizeof(CELL);
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2005-12-17 03:25:39 +00:00
|
|
|
return out;
|
2005-11-23 03:01:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void *Yap_AllocCodeSpace(size_t size) {
|
2011-04-15 23:04:23 +01:00
|
|
|
size = AdjustSize(size);
|
2015-09-25 10:57:26 +01:00
|
|
|
return call_malloc(size);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static inline char *call_realloc(char *p, size_t size) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2009-11-27 13:31:03 +00:00
|
|
|
char *out;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2009-11-27 13:31:03 +00:00
|
|
|
#if INSTRUMENT_MALLOC
|
2010-04-15 11:37:15 +01:00
|
|
|
reallocs++;
|
2009-11-27 13:31:03 +00:00
|
|
|
tmalloc += size;
|
2010-04-15 11:37:15 +01:00
|
|
|
size += sizeof(CELL);
|
|
|
|
p -= sizeof(CELL);
|
2015-09-25 10:57:26 +01:00
|
|
|
tmalloc -= *(CELL *)p;
|
2009-11-27 13:31:03 +00:00
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= MallocMode;
|
2016-07-31 05:07:06 +01:00
|
|
|
out = (char *)my_realloc(p, size);
|
2010-04-15 11:37:15 +01:00
|
|
|
#if INSTRUMENT_MALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
*(CELL *)out = size - sizeof(CELL);
|
2010-04-15 11:37:15 +01:00
|
|
|
out += sizeof(CELL);
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2009-11-27 13:31:03 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void *Yap_ReallocCodeSpace(void *p, size_t size) {
|
2011-04-15 23:04:23 +01:00
|
|
|
size = AdjustSize(size);
|
2015-09-25 10:57:26 +01:00
|
|
|
return call_realloc(p, size);
|
2009-11-27 13:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_FreeCodeSpace(void *p) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= MallocMode;
|
2005-11-08 13:51:15 +00:00
|
|
|
#if INSTRUMENT_MALLOC
|
2010-04-15 11:37:15 +01:00
|
|
|
p -= sizeof(CELL);
|
2015-09-25 10:57:26 +01:00
|
|
|
tmalloc -= *(CELL *)p;
|
2005-11-08 13:51:15 +00:00
|
|
|
frees++;
|
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
my_free(p);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void *Yap_AllocAtomSpace(size_t size) {
|
2011-04-15 23:04:23 +01:00
|
|
|
size = AdjustSize(size);
|
2005-11-23 03:01:33 +00:00
|
|
|
return call_malloc(size);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_FreeAtomSpace(void *p) {
|
2011-05-25 16:40:36 +01:00
|
|
|
CACHE_REGS
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= MallocMode;
|
2005-11-08 13:51:15 +00:00
|
|
|
#if INSTRUMENT_MALLOC
|
2010-04-15 11:37:15 +01:00
|
|
|
p -= sizeof(CELL);
|
2015-09-25 10:57:26 +01:00
|
|
|
tmalloc -= *(CELL *)p;
|
2005-11-08 13:51:15 +00:00
|
|
|
frees++;
|
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
my_free(p);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 23:28:41 +01:00
|
|
|
#endif
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
/* If you need to dinamically allocate space from the heap, this is
|
|
|
|
* the macro you should use */
|
2015-09-25 10:57:26 +01:00
|
|
|
ADDR Yap_InitPreAllocCodeSpace(int wid) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-01-23 02:23:51 +00:00
|
|
|
char *ptr;
|
2012-12-13 18:12:50 +00:00
|
|
|
UInt sz = REMOTE_ScratchPad(wid).msz;
|
|
|
|
|
|
|
|
if (REMOTE_ScratchPad(wid).ptr == NULL) {
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_PrologMode(wid) |= MallocMode;
|
2010-04-15 11:37:15 +01:00
|
|
|
#if INSTRUMENT_MALLOC
|
|
|
|
mallocs++;
|
|
|
|
tmalloc += sz;
|
|
|
|
sz += sizeof(CELL);
|
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
while (!(ptr =
|
2013-10-16 09:44:34 +01:00
|
|
|
#ifdef YAPOR_COPY
|
2015-09-25 10:57:26 +01:00
|
|
|
malloc(sz)
|
2013-10-16 09:44:34 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
my_malloc(sz)
|
2013-10-16 09:44:34 +01:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
)) {
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_PrologMode(wid) &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
if (!Yap_growheap(FALSE, LOCAL_Error_Size, NULL)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
|
|
|
|
return (NULL);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
2010-04-15 11:37:15 +01:00
|
|
|
#if INSTRUMENT_MALLOC
|
|
|
|
sz -= sizeof(CELL);
|
2015-09-25 10:57:26 +01:00
|
|
|
*(CELL *)ptr = sz;
|
2010-04-15 11:37:15 +01:00
|
|
|
ptr += sizeof(CELL);
|
|
|
|
#endif
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_PrologMode(wid) |= MallocMode;
|
2005-12-17 03:25:39 +00:00
|
|
|
}
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_PrologMode(wid) &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_ScratchPad(wid).ptr = ptr;
|
2004-01-23 02:23:51 +00:00
|
|
|
} else {
|
2012-12-13 18:12:50 +00:00
|
|
|
ptr = REMOTE_ScratchPad(wid).ptr;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
2009-05-23 00:35:24 +01:00
|
|
|
AuxBase = (ADDR)(ptr);
|
2015-09-25 10:57:26 +01:00
|
|
|
AuxSp = (CELL *)(AuxTop = AuxBase + REMOTE_ScratchPad(wid).sz);
|
2004-01-23 02:23:51 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
ADDR Yap_ExpandPreAllocCodeSpace(UInt sz0, void *cip, int safe) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-01-23 02:23:51 +00:00
|
|
|
char *ptr;
|
2015-11-05 15:24:03 +00:00
|
|
|
UInt sz;
|
2004-02-05 16:57:02 +00:00
|
|
|
if (sz0 < SCRATCH_INC_SIZE)
|
|
|
|
sz0 = SCRATCH_INC_SIZE;
|
2011-05-04 10:11:41 +01:00
|
|
|
if (sz0 < LOCAL_ScratchPad.sz)
|
2015-09-25 10:57:26 +01:00
|
|
|
sz = LOCAL_ScratchPad.sz + sz0;
|
|
|
|
else
|
2008-01-23 17:57:56 +00:00
|
|
|
sz = sz0;
|
2015-09-25 10:57:26 +01:00
|
|
|
sz = AdjustLargePageSize(sz + sz / 4);
|
2004-02-05 16:57:02 +00:00
|
|
|
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
LOCK(DLMallocLock);
|
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode |= MallocMode;
|
2005-11-08 13:51:15 +00:00
|
|
|
#if INSTRUMENT_MALLOC
|
|
|
|
reallocs++;
|
2011-05-04 10:11:41 +01:00
|
|
|
tmalloc -= LOCAL_ScratchPad.sz;
|
2010-04-15 11:37:15 +01:00
|
|
|
tmalloc += sz;
|
2005-11-08 13:51:15 +00:00
|
|
|
#endif
|
2016-08-01 23:48:25 +01:00
|
|
|
if (!(ptr = my_realloc(LOCAL_ScratchPad.ptr, sz))) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
return NULL;
|
2004-10-28 21:12:23 +01:00
|
|
|
}
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode &= ~MallocMode;
|
2010-02-03 22:54:47 +00:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
UNLOCK(DLMallocLock);
|
|
|
|
#endif
|
2011-05-04 10:11:41 +01:00
|
|
|
LOCAL_ScratchPad.sz = LOCAL_ScratchPad.msz = sz;
|
|
|
|
LOCAL_ScratchPad.ptr = ptr;
|
2009-05-23 00:35:24 +01:00
|
|
|
AuxBase = ptr;
|
2015-09-25 10:57:26 +01:00
|
|
|
AuxSp = (CELL *)(AuxTop = ptr + sz);
|
2004-01-23 02:23:51 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2004-10-28 21:12:23 +01:00
|
|
|
#if USE_SYSTEM_MALLOC
|
|
|
|
|
2004-12-02 06:06:47 +00:00
|
|
|
struct various_codes *Yap_heap_regs;
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void InitHeap(void) {
|
|
|
|
Yap_heap_regs =
|
|
|
|
(struct various_codes *)calloc(1, sizeof(struct various_codes));
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitHeap(void *heap_addr) {
|
2004-01-23 02:23:51 +00:00
|
|
|
InitHeap();
|
2008-01-23 17:57:56 +00:00
|
|
|
Yap_HoleSize = 0;
|
|
|
|
HeapMax = 0;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void InitExStacks(int wid, int Trail, int Stack) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2006-03-24 16:26:31 +00:00
|
|
|
UInt pm, sa;
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
/* sanity checking for data areas */
|
|
|
|
if (Trail < MinTrailSpace)
|
|
|
|
Trail = MinTrailSpace;
|
|
|
|
if (Stack < MinStackSpace)
|
|
|
|
Stack = MinStackSpace;
|
2015-09-25 10:57:26 +01:00
|
|
|
pm = (Trail + Stack) * K; /* memory to be
|
|
|
|
* requested */
|
|
|
|
sa = Stack * K; /* stack area size */
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2008-04-02 18:37:07 +01:00
|
|
|
#ifdef THREADS
|
2015-09-25 10:57:26 +01:00
|
|
|
if (wid)
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_GlobalBase(wid) = (ADDR)REMOTE_ThreadHandle(wid).stack_address;
|
|
|
|
else
|
|
|
|
AuxSp = NULL;
|
2008-04-02 18:37:07 +01:00
|
|
|
#endif
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_TrailTop(wid) = REMOTE_GlobalBase(wid) + pm;
|
|
|
|
REMOTE_LocalBase(wid) = REMOTE_GlobalBase(wid) + sa;
|
|
|
|
REMOTE_TrailBase(wid) = REMOTE_LocalBase(wid) + sizeof(CELL);
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_ScratchPad(wid).ptr = NULL;
|
|
|
|
REMOTE_ScratchPad(wid).sz = REMOTE_ScratchPad(wid).msz = SCRATCH_START_SIZE;
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2013-11-16 00:27:02 +00:00
|
|
|
#if DEBUG
|
2011-06-20 14:49:24 +01:00
|
|
|
if (Yap_output_msg) {
|
2006-03-24 17:15:20 +00:00
|
|
|
UInt ta;
|
2006-03-24 16:26:31 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"HeapBase = %p GlobalBase = %p\n LocalBase = %p TrailTop = %p\n",
|
|
|
|
Yap_HeapBase, REMOTE_GlobalBase(wid), REMOTE_LocalBase(wid),
|
|
|
|
REMOTE_TrailTop(wid));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
ta = Trail * K; /* trail area size */
|
2004-02-05 16:57:02 +00:00
|
|
|
fprintf(stderr, "Heap+Aux: %lu\tLocal+Global: %lu\tTrail: %lu\n",
|
2015-09-25 10:57:26 +01:00
|
|
|
(long unsigned)(pm - sa - ta), (long unsigned)sa,
|
|
|
|
(long unsigned)ta);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitExStacks(int wid, int Trail, int Stack) {
|
2012-12-13 18:12:50 +00:00
|
|
|
InitExStacks(wid, Trail, Stack);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 00:24:13 +01:00
|
|
|
#if defined(THREADS)
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_KillStacks(int wid) {
|
2011-05-09 20:19:49 +01:00
|
|
|
ADDR gb = REMOTE_ThreadHandle(wid).stack_address;
|
2004-02-19 19:24:46 +00:00
|
|
|
if (gb) {
|
|
|
|
free(gb);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).stack_address = NULL;
|
2004-02-19 19:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_KillStacks(int wid) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_GlobalBase) {
|
|
|
|
free(LOCAL_GlobalBase);
|
|
|
|
LOCAL_GlobalBase = NULL;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-19 19:24:46 +00:00
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitMemory(UInt Trail, UInt Heap, UInt Stack) { InitHeap(); }
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_ExtendWorkSpace(Int s) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-07-23 00:59:57 +01:00
|
|
|
void *basebp = LOCAL_GlobalBase, *nbp;
|
2015-09-25 10:57:26 +01:00
|
|
|
UInt s0 = LOCAL_TrailTop - LOCAL_GlobalBase;
|
|
|
|
nbp = realloc(basebp, s + s0);
|
|
|
|
if (nbp == NULL)
|
2004-02-05 16:57:02 +00:00
|
|
|
return FALSE;
|
2008-05-11 00:24:13 +01:00
|
|
|
#if defined(THREADS)
|
2011-05-09 19:55:06 +01:00
|
|
|
LOCAL_ThreadHandle.stack_address = (char *)nbp;
|
2008-05-11 00:24:13 +01:00
|
|
|
#endif
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_GlobalBase = (char *)nbp;
|
2004-02-05 16:57:02 +00:00
|
|
|
return TRUE;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
size_t Yap_ExtendWorkSpaceThroughHole(size_t s) { return 0; }
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_AllocHole(UInt actual_request, UInt total_size) {}
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2006-05-18 17:33:05 +01:00
|
|
|
#if HAVE_MALLINFO
|
2015-09-25 10:57:26 +01:00
|
|
|
UInt Yap_givemallinfo(void) {
|
2006-05-18 17:33:05 +01:00
|
|
|
struct mallinfo mi = mallinfo();
|
|
|
|
return mi.uordblks;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
#else
|
|
|
|
|
2002-10-17 02:37:46 +01:00
|
|
|
#if HAVE_SNPRINTF
|
2015-09-25 10:57:26 +01:00
|
|
|
#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)
|
2002-10-17 02:37:46 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
#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
|
|
|
|
|
2004-10-28 21:12:23 +01:00
|
|
|
#ifdef LIGHT
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !USE_DL_MALLOC
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void FreeBlock(BlockHeader *);
|
|
|
|
static BlockHeader *GetBlock(unsigned long int);
|
|
|
|
static char *AllocHeap(unsigned long int);
|
|
|
|
static void RemoveFromFreeList(BlockHeader *);
|
|
|
|
static void AddToFreeList(BlockHeader *);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
#define MinHGap 256 * K
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void RemoveFromFreeList(BlockHeader *b) {
|
2001-04-09 20:54:03 +01:00
|
|
|
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;
|
2015-09-25 10:57:26 +01:00
|
|
|
} 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;
|
2015-09-25 10:57:26 +01:00
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
*q = b->b_next_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void AddToFreeList(BlockHeader *b) {
|
2001-04-09 20:54:03 +01:00
|
|
|
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;
|
2015-09-25 10:57:26 +01:00
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
b->b_next = NIL;
|
|
|
|
b->b_next_size = p;
|
|
|
|
}
|
|
|
|
*q = b;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static void FreeBlock(BlockHeader *b) {
|
2001-04-09 20:54:03 +01:00
|
|
|
BlockHeader *p;
|
|
|
|
YAP_SEG_SIZE *sp;
|
|
|
|
|
2003-11-12 12:33:31 +00:00
|
|
|
/* {
|
|
|
|
static long long int vsc_free_ops;
|
2015-09-25 10:57:26 +01:00
|
|
|
|
2003-11-12 12:33:31 +00:00
|
|
|
vsc_free_ops++;
|
|
|
|
BlockHeader *q = FreeBlocks;
|
|
|
|
while (q) q = q->b_next_size;
|
|
|
|
}*/
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* sanity check */
|
|
|
|
sp = &(b->b_size) + (b->b_size & ~InUseFlag);
|
2004-09-28 19:37:05 +01:00
|
|
|
if (!(b->b_size & InUseFlag) || *sp != b->b_size) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#if !SHORT_INTS
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"%% YAP INTERNAL ERROR: sanity check failed in FreeBlock %p %x %x\n", b,
|
|
|
|
b->b_size, Unsigned(*sp));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"%% YAP INTERNAL ERROR: sanity check failed in FreeBlock %p %lx %lx\n",
|
|
|
|
b, b->b_size, *sp);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
b->b_size &= ~InUseFlag;
|
|
|
|
LOCK(FreeBlocksLock);
|
|
|
|
/* check if we can collapse with other blocsks */
|
|
|
|
/* check previous */
|
|
|
|
sp = &(b->b_size) - 1;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (!(*sp & InUseFlag)) { /* previous block is free */
|
|
|
|
p = (BlockHeader *)(sp - *sp);
|
2001-04-09 20:54:03 +01:00
|
|
|
RemoveFromFreeList(p);
|
|
|
|
p->b_size += b->b_size + 1;
|
|
|
|
b = p;
|
|
|
|
}
|
|
|
|
/* check following */
|
|
|
|
sp = &(b->b_size) + b->b_size + 1;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (!(*sp & InUseFlag)) { /* following block is free */
|
|
|
|
p = (BlockHeader *)sp;
|
2001-04-09 20:54:03 +01:00
|
|
|
RemoveFromFreeList(p);
|
|
|
|
b->b_size += p->b_size + 1;
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
LOCK(HeapTopLock);
|
2002-03-08 06:33:16 +00:00
|
|
|
if (sp == (YAP_SEG_SIZE *)HeapTop) {
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed -= (b->b_size + 1) * sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
HeapTop = (ADDR)b;
|
2015-09-25 10:57:26 +01:00
|
|
|
*((YAP_SEG_SIZE *)HeapTop) = InUseFlag;
|
2002-03-08 06:33:16 +00:00
|
|
|
} else {
|
2015-09-25 10:57:26 +01:00
|
|
|
/* insert on list of free blocks */
|
2002-03-08 06:33:16 +00:00
|
|
|
AddToFreeList(b);
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
UNLOCK(HeapTopLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
UNLOCK(FreeBlocksLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockHeader *
|
2015-09-25 10:57:26 +01:00
|
|
|
GetBlock(unsigned long int n) { /* get free block with size at least n */
|
2001-04-09 20:54:03 +01:00
|
|
|
register BlockHeader **p, *b, *r;
|
|
|
|
|
|
|
|
if (FreeBlocks == NIL)
|
|
|
|
return (NIL);
|
2005-05-31 20:42:28 +01:00
|
|
|
/* check for bugs */
|
|
|
|
p = &FreeBlocks;
|
|
|
|
/* end check for bugs */
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-31 05:07:06 +01:00
|
|
|
static char *AllocHeap(size_t size) {
|
2001-04-09 20:54:03 +01:00
|
|
|
BlockHeader *b, *n;
|
|
|
|
YAP_SEG_SIZE *sp;
|
2004-09-03 04:11:09 +01:00
|
|
|
UInt align, extra;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2003-11-12 12:33:31 +00:00
|
|
|
/* {
|
|
|
|
static long long int vsc_alloc_ops;
|
|
|
|
vsc_alloc_ops++;
|
|
|
|
BlockHeader *q = FreeBlocks;
|
|
|
|
while (q) q = q->b_next_size;
|
|
|
|
}*/
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
extra = size / 16;
|
|
|
|
#if SIZEOF_INT_P == 4
|
|
|
|
align = 2 * sizeof(CELL); /* size in dwords + 2 */
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
#if SIZEOF_INT_P == 8
|
2004-09-03 04:11:09 +01:00
|
|
|
align = sizeof(CELL);
|
2003-11-28 01:26:53 +00:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
while (align < extra)
|
|
|
|
align *= 2;
|
|
|
|
size = ALIGN_SIZE(size, align);
|
2004-09-03 04:11:09 +01:00
|
|
|
if (size < sizeof(BlockHeader))
|
|
|
|
size = sizeof(BlockHeader);
|
|
|
|
size += sizeof(YAP_SEG_SIZE);
|
|
|
|
/* change units to cells */
|
2015-09-25 10:57:26 +01:00
|
|
|
size = size / sizeof(CELL);
|
2001-04-09 20:54:03 +01:00
|
|
|
LOCK(FreeBlocksLock);
|
|
|
|
if ((b = GetBlock(size))) {
|
2015-09-25 10:57:26 +01:00
|
|
|
if (b->b_size >= size + 24 + 1) {
|
|
|
|
n = (BlockHeader *)(((YAP_SEG_SIZE *)b) + size + 1);
|
2001-04-09 20:54:03 +01:00
|
|
|
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));
|
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
LOCK(HeapTopLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
UNLOCK(FreeBlocksLock);
|
2015-09-25 10:57:26 +01:00
|
|
|
b = (BlockHeader *)HeapTop;
|
2001-04-09 20:54:03 +01:00
|
|
|
HeapTop += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
LOCK(HeapUsedLock);
|
|
|
|
HeapUsed += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
|
|
|
|
#ifdef YAPOR
|
2011-05-23 16:19:47 +01:00
|
|
|
if (HeapTop > Addr(LOCAL_GlobalBase) - MinHeapGap)
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "no heap left (AllocHeap)");
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2004-01-23 02:23:51 +00:00
|
|
|
if (HeapTop > HeapLim - MinHeapGap) {
|
2001-04-09 20:54:03 +01:00
|
|
|
HeapTop -= size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
HeapUsed -= size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
2004-01-23 02:23:51 +00:00
|
|
|
if (HeapTop > HeapLim) {
|
2001-04-09 20:54:03 +01:00
|
|
|
UNLOCK(HeapUsedLock);
|
2004-01-23 02:23:51 +00:00
|
|
|
UNLOCK(HeapTopLock);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* we destroyed the stack */
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "Stack Crashed against Heap...");
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2004-01-23 02:23:51 +00:00
|
|
|
if (HeapTop + size * sizeof(CELL) + sizeof(YAP_SEG_SIZE) < HeapLim) {
|
2015-09-25 10:57:26 +01:00
|
|
|
/* small allocations, we can wait */
|
|
|
|
HeapTop += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
HeapUsed += size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
Yap_signal(YAP_CDOVF_SIGNAL);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2015-09-25 10:57:26 +01:00
|
|
|
if (size > GLOBAL_SizeOfOverflow)
|
|
|
|
GLOBAL_SizeOfOverflow = size * sizeof(CELL) + sizeof(YAP_SEG_SIZE);
|
|
|
|
/* big allocations, the caller must handle the problem */
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
UNLOCK(HeapTopLock);
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* YAPOR */
|
2015-09-25 10:57:26 +01:00
|
|
|
*((YAP_SEG_SIZE *)HeapTop) = InUseFlag;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (HeapUsed > HeapMax)
|
|
|
|
HeapMax = HeapUsed;
|
|
|
|
UNLOCK(HeapUsedLock);
|
|
|
|
b->b_size = size | InUseFlag;
|
|
|
|
sp = &(b->b_size) + size;
|
|
|
|
*sp = b->b_size;
|
|
|
|
UNLOCK(HeapTopLock);
|
2004-01-23 02:23:51 +00:00
|
|
|
return (Addr(b) + sizeof(YAP_SEG_SIZE));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If you need to dinamically allocate space from the heap, this is
|
|
|
|
* the macro you should use */
|
2015-09-25 10:57:26 +01:00
|
|
|
static void FreeCodeSpace(char *p) {
|
|
|
|
FreeBlock(((BlockHeader *)(p - sizeof(YAP_SEG_SIZE))));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static char *AllocCodeSpace(unsigned long int size) {
|
2004-10-28 21:12:23 +01:00
|
|
|
if (size < SmallSize + 2 * OpCodeSize + 3 * CellSize)
|
|
|
|
return (AllocHeap(SmallSize + 2 * OpCodeSize + 3 * CellSize));
|
|
|
|
return (AllocHeap(size));
|
|
|
|
}
|
|
|
|
|
2004-10-27 16:56:34 +01:00
|
|
|
#if DEBUG_ALLOC
|
|
|
|
int vsc_mem_trace;
|
|
|
|
#endif
|
|
|
|
|
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 */
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_FreeCodeSpace(char *p) {
|
2004-10-27 16:56:34 +01:00
|
|
|
#if DEBUG_ALLOC
|
|
|
|
if (vsc_mem_trace)
|
2015-09-25 10:57:26 +01:00
|
|
|
printf("-%p\n", p);
|
2004-10-27 16:56:34 +01:00
|
|
|
#endif
|
2002-11-11 17:38:10 +00:00
|
|
|
FreeCodeSpace(p);
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
char *Yap_AllocAtomSpace(unsigned long int size) {
|
2004-10-27 16:56:34 +01:00
|
|
|
char *out = AllocHeap(size);
|
|
|
|
#if DEBUG_ALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
if (vsc_mem_trace)
|
|
|
|
printf("+%p/%d\n", out, size);
|
2004-10-27 16:56:34 +01:00
|
|
|
#endif
|
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_FreeAtomSpace(char *p) {
|
2004-10-27 16:56:34 +01:00
|
|
|
#if DEBUG_ALLOC
|
|
|
|
if (vsc_mem_trace)
|
2015-09-25 10:57:26 +01:00
|
|
|
printf("-%p\n", p);
|
2004-10-27 16:56:34 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
FreeCodeSpace(p);
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
char *Yap_AllocCodeSpace(unsigned long int size) {
|
2004-10-27 16:56:34 +01:00
|
|
|
char *out = AllocCodeSpace(size);
|
|
|
|
#if DEBUG_ALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
if (vsc_mem_trace)
|
|
|
|
printf("+%p/%d\n", out, size);
|
2004-10-27 16:56:34 +01:00
|
|
|
#endif
|
|
|
|
return out;
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2004-10-28 21:12:23 +01:00
|
|
|
#endif
|
2004-02-05 16:57:02 +00:00
|
|
|
|
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
|
|
|
/************************************************************************/
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
|
2011-03-22 09:31:37 +00:00
|
|
|
#undef DEBUG_WIN32_ALLO
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
static LPVOID brk;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static int ExtendWorkSpace(Int s, int fixed_allocation) {
|
2004-07-28 23:09:02 +01:00
|
|
|
LPVOID b = brk;
|
2011-05-25 16:40:36 +01:00
|
|
|
prolog_exec_mode OldPrologMode = LOCAL_PrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = ExtendStackMode;
|
2006-05-16 19:37:31 +01:00
|
|
|
|
|
|
|
#if DEBUG_WIN32_ALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "trying: %p (" Int_FORMAT "K) %d\n", b, s / 1024,
|
|
|
|
fixed_allocation);
|
2006-05-16 19:37:31 +01:00
|
|
|
#endif
|
2004-10-26 21:16:18 +01:00
|
|
|
if (fixed_allocation) {
|
2004-08-11 17:14:55 +01:00
|
|
|
b = VirtualAlloc(b, s, MEM_RESERVE, PAGE_NOACCESS);
|
2004-10-26 21:16:18 +01:00
|
|
|
} else {
|
2004-08-11 17:14:55 +01:00
|
|
|
b = VirtualAlloc(NULL, s, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (b && b < brk) {
|
|
|
|
return ExtendWorkSpace(s, fixed_allocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!b) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2006-05-16 19:37:31 +01:00
|
|
|
#if DEBUG_WIN32_ALLOC
|
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2015-09-25 10:57:26 +01:00
|
|
|
NULL, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256, NULL);
|
|
|
|
fprintf(stderr, "NOT OK1: %p %p %s\n", brk, b, msg);
|
2006-05-16 19:37:31 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-08-11 17:14:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-07-28 23:09:02 +01:00
|
|
|
b = VirtualAlloc(b, s, MEM_COMMIT, PAGE_READWRITE);
|
2004-08-11 17:14:55 +01:00
|
|
|
if (!b) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"VirtualAlloc could not commit %ld bytes", (long int)s);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2006-05-16 19:37:31 +01:00
|
|
|
#if DEBUG_WIN32_ALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "NOT OK2: %p--%p\n", b, brk);
|
2006-05-16 19:37:31 +01:00
|
|
|
#endif
|
2004-08-11 17:14:55 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
brk = (LPVOID)((Int)b + s);
|
2006-05-16 19:37:31 +01:00
|
|
|
#if DEBUG_WIN32_ALLOC
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "OK: %p--%p " Int_FORMAT "\n", b, brk, s);
|
2006-05-16 19:37:31 +01:00
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2004-08-11 17:14:55 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T InitWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
SYSTEM_INFO si;
|
2010-05-06 15:00:44 +01:00
|
|
|
Int psz;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
GetSystemInfo(&si);
|
2010-05-06 15:00:44 +01:00
|
|
|
psz = Yap_page_size = si.dwPageSize;
|
|
|
|
brk = (LPVOID)psz;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (!ExtendWorkSpace(s, 0))
|
2006-01-08 23:01:47 +00:00
|
|
|
return FALSE;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (MALLOC_T)brk - s;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_FreeWorkSpace(void) { return TRUE; }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-03-07 23:07:46 +00:00
|
|
|
#elif USE_SYSTEM_MMAP
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2003-10-28 01:16:03 +00:00
|
|
|
#ifndef MAP_FIXED
|
|
|
|
#define MAP_FIXED 1
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static MALLOC_T WorkSpaceTop;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T InitWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T a;
|
2015-09-25 10:57:26 +01:00
|
|
|
#if !defined(_AIX) && !defined(__APPLE__) && !__hpux
|
2001-04-09 20:54:03 +01:00
|
|
|
int fd;
|
|
|
|
#endif
|
2001-05-03 19:48:06 +01:00
|
|
|
#if defined(_AIX)
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(0, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_VARIABLE, -1, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif __hpux
|
2015-09-25 10:57:26 +01:00
|
|
|
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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil,
|
|
|
|
"mmap could not map ANON at %p, got %p", (void *)MMAP_ADDR, a);
|
|
|
|
return (NULL);
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#elif defined(__APPLE__)
|
2010-01-25 12:29:27 +00:00
|
|
|
#ifdef MMAP_ADDR
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(((void *)MMAP_ADDR), (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
|
2001-05-03 18:13:18 +01:00
|
|
|
if (a != (MALLOC_T)MMAP_ADDR) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil,
|
|
|
|
"mmap could not map ANON at %p, got %p", (void *)MMAP_ADDR, a);
|
|
|
|
return (NULL);
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2010-01-25 12:29:27 +00:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(NULL, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANON, -1, 0);
|
2010-01-25 12:29:27 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2008-11-26 09:56:18 +00:00
|
|
|
fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
#if HAVE_MKSTEMP
|
|
|
|
char file[256];
|
2015-09-25 10:57:26 +01:00
|
|
|
strncpy(file, "/tmp/YAP.TMPXXXXXX", 256);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (mkstemp(file) == -1) {
|
|
|
|
#if HAVE_STRERROR
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil,
|
|
|
|
"mkstemp could not create temporary file %s (%s)", file,
|
|
|
|
strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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];
|
2015-09-25 10:57:26 +01:00
|
|
|
strcpy(file, "/tmp/mapfile");
|
2001-04-09 20:54:03 +01:00
|
|
|
itos(getpid(), &file[12]);
|
|
|
|
#endif /* HAVE_TMPNAM */
|
|
|
|
#endif /* HAVE_MKSTEMP */
|
2015-09-25 10:57:26 +01:00
|
|
|
fd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fd < 0) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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
|
2015-09-25 10:57:26 +01:00
|
|
|
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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(0, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd,
|
|
|
|
0);
|
2001-04-09 20:54:03 +01:00
|
|
|
if ((CELL)a & YAP_PROTECTED_MASK) {
|
|
|
|
close(fd);
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, 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
|
2015-09-25 10:57:26 +01:00
|
|
|
(a == (MALLOC_T)MMAP_FAILED)
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
(a == (MALLOC_T)-1)
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
{
|
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "mmap cannot allocate memory ***");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
WorkSpaceTop = (char *)a + s;
|
|
|
|
return (void *)a;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
#ifndef YAPOR
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T mmap_extension(Int s, MALLOC_T base, int fixed_allocation) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T a;
|
2009-04-29 14:22:20 +01:00
|
|
|
|
2005-07-06 16:10:18 +01:00
|
|
|
#if !defined(_AIX) && !defined(__hpux) && !defined(__APPLE__)
|
2015-09-25 10:57:26 +01:00
|
|
|
int fd;
|
2003-11-12 12:33:31 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
#if defined(_AIX) || defined(__hpux)
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(base, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(base, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE | MAP_ANON | fixed_allocation, -1, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
fd = open("/dev/zero", O_RDWR);
|
|
|
|
if (fd < 0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_MKSTEMP
|
2015-09-25 10:57:26 +01:00
|
|
|
char file[256];
|
|
|
|
strncpy(file, "/tmp/YAP.TMPXXXXXX", 256);
|
|
|
|
if (mkstemp(file) == -1) {
|
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2015-09-25 10:57:26 +01:00
|
|
|
snprintf5(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
|
|
|
"mkstemp could not create temporary file %s (%s)", file,
|
|
|
|
strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
|
|
|
"mkstemp could not create temporary file %s", file);
|
2001-05-03 18:23:30 +01:00
|
|
|
#endif /* HAVE_STRERROR */
|
2015-09-25 10:57:26 +01:00
|
|
|
return (MALLOC_T)-1;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
#if HAVE_TMPNAM
|
2015-09-25 10:57:26 +01:00
|
|
|
char *file = tmpnam(NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
char file[YAP_FILENAME_MAX];
|
|
|
|
strcpy(file, "/tmp/mapfile");
|
|
|
|
itos(getpid(), &file[12]);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* HAVE_TMPNAM */
|
|
|
|
#endif /* HAVE_MKSTEMP */
|
2015-09-25 10:57:26 +01:00
|
|
|
fd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fd < 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not open %s", file);
|
2009-04-29 14:22:20 +01:00
|
|
|
return (MALLOC_T)-1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (lseek(fd, s, SEEK_SET) < 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not lseek in mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2009-04-29 14:22:20 +01:00
|
|
|
return (MALLOC_T)-1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (write(fd, "", 1) < 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not write in mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2009-04-29 14:22:20 +01:00
|
|
|
return (MALLOC_T)-1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (unlink(file) < 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not unlink mmapped file %s", file);
|
2002-10-10 06:58:49 +01:00
|
|
|
close(fd);
|
2009-04-29 14:22:20 +01:00
|
|
|
return (MALLOC_T)-1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
a = mmap(
|
|
|
|
base, (size_t)s, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
|
|
MAP_PRIVATE
|
2002-11-11 17:38:10 +00:00
|
|
|
#if !defined(__linux)
|
2015-09-25 10:57:26 +01:00
|
|
|
/* use MAP_FIXED, otherwise God knows where you will be placed */
|
2016-08-01 23:48:25 +01:00
|
|
|
| fixed_allocation
|
2002-10-10 06:58:49 +01:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
,
|
|
|
|
fd, 0);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (close(fd) == -1) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not close file (%s) ]\n", strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf3(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not close file ]\n");
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2009-04-29 14:22:20 +01:00
|
|
|
return (MALLOC_T)-1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-04-29 14:22:20 +01:00
|
|
|
return a;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
#endif /* !YAPOR */
|
|
|
|
|
2012-06-01 16:39:06 +01:00
|
|
|
/*
|
2015-09-25 10:57:26 +01:00
|
|
|
s= how much memory we need;
|
2012-06-01 16:39:06 +01:00
|
|
|
fixed_alloc = do we need to contiguously extend stack
|
|
|
|
returns TRUE or FALSE
|
|
|
|
|
|
|
|
updates WorkSpaceTop
|
|
|
|
*/
|
2015-09-25 10:57:26 +01:00
|
|
|
static int ExtendWorkSpace(Int s, int fixed_allocation) {
|
2009-04-29 14:22:20 +01:00
|
|
|
#ifdef YAPOR
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
|
|
|
"cannot extend stacks (ExtendWorkSpace)");
|
|
|
|
return (FALSE);
|
2009-04-29 14:22:20 +01:00
|
|
|
#else
|
|
|
|
MALLOC_T a;
|
2011-05-25 16:40:36 +01:00
|
|
|
prolog_exec_mode OldPrologMode = LOCAL_PrologMode;
|
2009-04-29 14:22:20 +01:00
|
|
|
MALLOC_T base = WorkSpaceTop;
|
|
|
|
|
|
|
|
if (fixed_allocation == MAP_FIXED)
|
|
|
|
base = WorkSpaceTop;
|
|
|
|
else
|
|
|
|
base = 0L;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = ExtendStackMode;
|
2009-04-29 14:22:20 +01:00
|
|
|
a = mmap_extension(s, base, fixed_allocation);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (a == (MALLOC_T)-1) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_STRERROR
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf5(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not allocate %d bytes (%s)", (int)s, strerror(errno));
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-05-23 16:19:47 +01:00
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not allocate %d bytes", (int)s);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-10-28 01:16:03 +00:00
|
|
|
if (fixed_allocation) {
|
|
|
|
if (a != WorkSpaceTop) {
|
2003-10-30 11:31:05 +00:00
|
|
|
munmap((void *)a, (size_t)s);
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf5(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"mmap could not grow memory at %p, got %p", WorkSpaceTop, a);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2003-10-28 01:16:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else if (a < WorkSpaceTop) {
|
2004-07-22 22:32:23 +01:00
|
|
|
/* try again */
|
2004-07-23 20:02:09 +01:00
|
|
|
int res = ExtendWorkSpace(s, fixed_allocation);
|
|
|
|
/* release memory back to system */
|
|
|
|
munmap(a, s);
|
|
|
|
return res;
|
2001-05-03 18:13:18 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop = (char *)a + s;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_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
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_FreeWorkSpace(void) { return 1; }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-03-07 23:07:46 +00:00
|
|
|
#elif USE_SYSTEM_SHM
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if HAVE_SYS_SHM_H
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MMAP_ADDR
|
|
|
|
#define MMAP_ADDR 0x0L
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static MALLOC_T WorkSpaceTop;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T InitWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T ptr;
|
|
|
|
int shm_id;
|
|
|
|
|
|
|
|
/* mapping heap area */
|
2015-09-25 10:57:26 +01:00
|
|
|
if ((shm_id = shmget(IPC_PRIVATE, (size_t)s, SHM_R | SHM_W)) == -1) {
|
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "could not shmget %d bytes", s);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if ((ptr = (MALLOC_T)shmat(shm_id, (void *)MMAP_ADDR, 0)) == (MALLOC_T)-1) {
|
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "could not shmat at %p", MMAP_ADDR);
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (shmctl(shm_id, IPC_RMID, 0) != 0) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "could not remove shm segment",
|
|
|
|
shm_id);
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop = (char *)ptr + s;
|
|
|
|
return (ptr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static int ExtendWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T ptr;
|
|
|
|
int shm_id;
|
2011-05-25 16:40:36 +01:00
|
|
|
prolog_exec_mode OldPrologMode = LOCAL_PrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = ExtendStackMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* mapping heap area */
|
2015-09-25 10:57:26 +01:00
|
|
|
if ((shm_id = shmget(IPC_PRIVATE, (size_t)s, SHM_R | SHM_W)) == -1) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not shmget %d bytes", s);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if ((ptr = (MALLOC_T)shmat(shm_id, WorkSpaceTop, 0)) == (MALLOC_T)-1) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
2015-09-25 10:57:26 +01:00
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE, "could not shmat at %p",
|
|
|
|
MMAP_ADDR);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (shmctl(shm_id, IPC_RMID, 0) != 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not remove shm segment", shm_id);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop = (char *)ptr + s;
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_FreeWorkSpace(void) { return TRUE; }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#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
|
2013-04-25 23:15:04 +01:00
|
|
|
char *sbrk(int);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int in_limbo; /* non-zero when restoring a saved state */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifndef LIMBO_SIZE
|
2015-09-25 10:57:26 +01:00
|
|
|
#define LIMBO_SIZE 32 * K
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static char limbo_space[LIMBO_SIZE]; /* temporary malloc space */
|
2001-04-09 20:54:03 +01:00
|
|
|
static char *limbo_p = limbo_space, *limbo_pp = 0;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T InitWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T ptr = (MALLOC_T)sbrk(s);
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
if (ptr == ((MALLOC_T)-1)) {
|
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "could not allocate %d bytes", s);
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
return (ptr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static int ExtendWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T ptr = (MALLOC_T)sbrk(s);
|
2011-05-25 16:40:36 +01:00
|
|
|
prolog_exec_mode OldPrologMode = LOCAL_PrologMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = ExtendStackMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
if (ptr == ((MALLOC_T)-1)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not expand stacks over %d bytes", s);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2015-09-25 10:57:26 +01:00
|
|
|
return (FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_FreeWorkSpace(void) { return TRUE; }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
MALLOC_T
|
2015-09-25 10:57:26 +01:00
|
|
|
malloc(size_t size) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (in_limbo) {
|
|
|
|
limbo_pp = limbo_p;
|
|
|
|
limbo_p += (size + 7) & 0xffff8;
|
|
|
|
if (limbo_p >= &limbo_space[LIMBO_SIZE])
|
2015-09-25 10:57:26 +01:00
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
return (limbo_pp);
|
2015-09-25 10:57:26 +01:00
|
|
|
} else {
|
|
|
|
CODEADDR codep = (CODEADDR)AllocCodeSpace(size + 2 * sizeof(void *));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (codep == NIL)
|
2015-09-25 10:57:26 +01:00
|
|
|
return (NIL);
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
2015-09-25 10:57:26 +01:00
|
|
|
return (codep + 2 * sizeof(void *));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void free(MALLOC_T ptr) {
|
|
|
|
BlockHeader *b = (BlockHeader *)(((char *)ptr) - 2 * sizeof(void *) -
|
|
|
|
sizeof(YAP_SEG_SIZE));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (ptr == limbo_pp) {
|
|
|
|
limbo_p = limbo_pp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
2015-09-25 10:57:26 +01:00
|
|
|
if ((char *)ptr < Yap_HeapBase || (char *)ptr > HeapTop)
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
if (!(b->b_size & InUseFlag))
|
|
|
|
return;
|
2015-09-25 10:57:26 +01:00
|
|
|
FreeCodeSpace((char *)ptr - 2 * sizeof(void *));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_T
|
2015-09-25 10:57:26 +01:00
|
|
|
XX realloc(MALLOC_T ptr, size_t size) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T new = malloc(size);
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
memcpy(new, ptr, size);
|
|
|
|
free(ptr);
|
|
|
|
return (new);
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_T
|
2015-09-25 10:57:26 +01:00
|
|
|
calloc(size_t n, size_t e) {
|
2001-04-09 20:54:03 +01:00
|
|
|
unsigned k = n * e;
|
|
|
|
MALLOC_T p = malloc(k);
|
|
|
|
|
|
|
|
memset(p, 0, k);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef M_MXFAST
|
2015-09-25 10:57:26 +01:00
|
|
|
int mallopt(cmd, value) { return (value); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static struct mallinfo xmall;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
struct mallinfo mallinfo(void) {
|
2001-04-09 20:54:03 +01:00
|
|
|
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
|
2015-09-25 10:57:26 +01:00
|
|
|
#define MAX_SPACE 420 * 1024 * 1024
|
2002-07-17 21:25:30 +01:00
|
|
|
#else
|
2015-09-25 10:57:26 +01: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;
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static MALLOC_T InitWorkSpace(Int s) {
|
2002-05-19 20:04:33 +01:00
|
|
|
MALLOC_T ptr;
|
|
|
|
|
|
|
|
#ifdef M_MMAP_MAX
|
|
|
|
mallopt(M_MMAP_MAX, 0);
|
|
|
|
#endif
|
2015-09-25 10:57:26 +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) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "could not allocate %d bytes", s);
|
|
|
|
return (NULL);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2015-09-25 10:57:26 +01:00
|
|
|
return (ptr);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
static int ExtendWorkSpace(Int s) {
|
2001-04-09 20:54:03 +01:00
|
|
|
MALLOC_T ptr;
|
2011-05-25 16:40:36 +01:00
|
|
|
prolog_exec_mode OldPrologMode = LOCAL_PrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = ExtendStackMode;
|
2002-10-10 06:58:49 +01:00
|
|
|
total_space += s;
|
2015-09-25 10:57:26 +01: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) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not allocate %d bytes", s);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2003-10-28 01:16:03 +00: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) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf4(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"could not expand contiguous stacks %d bytes", s);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2003-10-28 01:16:03 +00:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2016-07-31 05:07:06 +01:00
|
|
|
#if MBIT
|
2001-09-24 15:35:58 +01:00
|
|
|
if ((CELL)ptr & MBIT) {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = LOCAL_ErrorSay;
|
|
|
|
snprintf5(LOCAL_ErrorMessage, MAX_ERROR_MSG_SIZE,
|
2015-09-25 10:57:26 +01:00
|
|
|
"memory at %p conflicts with MBIT %lx", ptr, (unsigned long)MBIT);
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2003-10-28 01:16:03 +00:00
|
|
|
return FALSE;
|
2001-09-24 15:35:58 +01:00
|
|
|
}
|
2016-07-31 05:07:06 +01:00
|
|
|
#endif
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_PrologMode = OldPrologMode;
|
2001-04-09 20:54:03 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_FreeWorkSpace(void) { return TRUE; }
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-25 10:57:26 +01: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));
|
2006-05-19 15:31:32 +01:00
|
|
|
Yap_HoleSize = 0;
|
2004-10-28 21:12:23 +01:00
|
|
|
#if USE_DL_MALLOC
|
|
|
|
Yap_initdlmalloc();
|
|
|
|
#else
|
2015-09-25 10:57:26 +01:00
|
|
|
HeapMax = HeapUsed = HeapTop - Yap_HeapBase;
|
2003-11-28 01:26:53 +00:00
|
|
|
/* notice that this forces odd addresses */
|
2015-09-25 10:57:26 +01:00
|
|
|
*((YAP_SEG_SIZE *)HeapTop) = InUseFlag;
|
2001-04-09 20:54:03 +01:00
|
|
|
HeapTop = HeapTop + sizeof(YAP_SEG_SIZE);
|
2015-09-25 10:57:26 +01:00
|
|
|
*((YAP_SEG_SIZE *)HeapTop) = InUseFlag;
|
2004-10-28 21:12:23 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
FreeBlocks = NIL;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitHeap(void *heap_addr) { InitHeap(heap_addr); }
|
2002-11-11 17:38:10 +00:00
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitMemory(UInt Trail, UInt Heap, UInt Stack) {
|
2009-03-27 14:45:40 +00:00
|
|
|
UInt pm, sa, ta;
|
2009-11-18 23:32:00 +00:00
|
|
|
void *addr;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-03-21 14:17:54 +00:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
2015-09-25 10:57:26 +01:00
|
|
|
Stack = ((Stack + (YAP_ALLOC_SIZE - 1)) / YAP_ALLOC_SIZE) * YAP_ALLOC_SIZE;
|
|
|
|
Heap = ((Heap + (YAP_ALLOC_SIZE - 1)) / YAP_ALLOC_SIZE) * YAP_ALLOC_SIZE;
|
|
|
|
Trail = ((Trail + (YAP_ALLOC_SIZE - 1)) / YAP_ALLOC_SIZE) * YAP_ALLOC_SIZE;
|
2011-03-21 14:17:54 +00:00
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
pm = (Trail + Heap + Stack); /* memory to be
|
|
|
|
* requested */
|
|
|
|
sa = Stack; /* stack area size */
|
|
|
|
ta = Trail; /* trail area size */
|
2009-11-18 23:32:00 +00:00
|
|
|
|
|
|
|
#if RANDOMIZE_START_ADDRESS
|
|
|
|
srand(time(NULL));
|
2015-09-25 10:57:26 +01:00
|
|
|
UInt x = (rand() % 100) * YAP_ALLOC_SIZE;
|
2009-11-18 23:32:00 +00:00
|
|
|
pm += x;
|
|
|
|
#endif
|
|
|
|
addr = InitWorkSpace(pm);
|
|
|
|
#if RANDOMIZE_START_ADDRESS
|
2015-09-25 10:57:26 +01:00
|
|
|
addr = (char *)addr + x;
|
2009-11-18 23:32:00 +00:00
|
|
|
pm -= x;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
InitHeap(addr);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_TrailTop = Yap_HeapBase + pm;
|
|
|
|
LOCAL_LocalBase = LOCAL_TrailTop - ta;
|
|
|
|
LOCAL_TrailBase = LOCAL_LocalBase + sizeof(CELL);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_GlobalBase = LOCAL_LocalBase - sa;
|
2015-09-25 10:57:26 +01:00
|
|
|
HeapLim = LOCAL_GlobalBase; /* avoid confusions while
|
|
|
|
* * restoring */
|
2004-10-28 21:12:23 +01:00
|
|
|
#if !USE_DL_MALLOC
|
2011-05-23 16:19:47 +01:00
|
|
|
AuxTop = (ADDR)(AuxSp = (CELL *)LOCAL_GlobalBase);
|
2004-10-28 21:12:23 +01:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-11-16 00:27:02 +00:00
|
|
|
#if DEBUG
|
2015-09-25 10:57:26 +01:00
|
|
|
#if SIZEOF_INT_P != SIZEOF_INT
|
2011-06-20 14:49:24 +01:00
|
|
|
if (Yap_output_msg) {
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"HeapBase = %p GlobalBase = %p\n LocalBase = %p TrailTop = %p\n",
|
|
|
|
Yap_HeapBase, LOCAL_GlobalBase, LOCAL_LocalBase, LOCAL_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2011-06-20 14:49:24 +01:00
|
|
|
if (Yap_output_msg) {
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"HeapBase = %x GlobalBase = %x\n LocalBase = %x TrailTop = %x\n",
|
|
|
|
(UInt)Yap_HeapBase, (UInt)LOCAL_GlobalBase, (UInt)LOCAL_LocalBase,
|
|
|
|
(UInt)LOCAL_TrailTop);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
fprintf(stderr, "Heap+Aux: " UInt_FORMAT "\tLocal+Global: " UInt_FORMAT
|
|
|
|
"\tTrail: " UInt_FORMAT "\n",
|
|
|
|
pm - sa - ta, sa, ta);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_InitExStacks(int wid, int Trail, int Stack) {
|
2004-10-28 21:12:23 +01:00
|
|
|
#if USE_DL_MALLOC
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_ScratchPad(wid).ptr = NULL;
|
|
|
|
REMOTE_ScratchPad(wid).sz = REMOTE_ScratchPad(wid).msz = SCRATCH_START_SIZE;
|
2004-10-28 21:12:23 +01:00
|
|
|
AuxSp = NULL;
|
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
2004-08-11 17:14:55 +01:00
|
|
|
#define WorkSpaceTop brk
|
|
|
|
#define MAP_FIXED 1
|
|
|
|
#endif
|
|
|
|
|
2009-05-23 00:35:24 +01:00
|
|
|
#if !USE_DL_MALLOC
|
|
|
|
/* dead code */
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_add_memory_hole(ADDR Start, ADDR End) { Yap_HoleSize += Start - End; }
|
2009-05-23 00:35:24 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
int Yap_ExtendWorkSpace(Int s) {
|
2014-03-07 23:07:46 +00:00
|
|
|
#if USE_SYSTEM_MMAP
|
2003-10-28 01:16:03 +00:00
|
|
|
return ExtendWorkSpace(s, MAP_FIXED);
|
2004-08-11 17:14:55 +01:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
return ExtendWorkSpace(s, MAP_FIXED);
|
2003-10-30 22:52:46 +00:00
|
|
|
#else
|
|
|
|
return ExtendWorkSpace(s);
|
|
|
|
#endif
|
2003-10-28 01:16:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
size_t Yap_ExtendWorkSpaceThroughHole(size_t s) {
|
2014-03-07 23:07:46 +00:00
|
|
|
#if USE_SYSTEM_MMAP || defined(_WIN32) || defined(__CYGWIN__)
|
2003-10-28 01:16:03 +00:00
|
|
|
MALLOC_T WorkSpaceTop0 = WorkSpaceTop;
|
2015-09-25 10:57:26 +01:00
|
|
|
#if SIZEOF_INT_P == 4
|
2004-08-11 17:14:55 +01:00
|
|
|
while (WorkSpaceTop < (MALLOC_T)0xc0000000L) {
|
|
|
|
/* progress 1 MB */
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop += 512 * 1024;
|
2004-08-11 17:14:55 +01:00
|
|
|
if (ExtendWorkSpace(s, MAP_FIXED)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_add_memory_hole((ADDR)WorkSpaceTop0, (ADDR)WorkSpaceTop - s);
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_ErrorMessage = NULL;
|
2015-09-25 10:57:26 +01:00
|
|
|
return WorkSpaceTop - WorkSpaceTop0;
|
2004-08-11 17:14:55 +01:00
|
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* 487 happens when you step over someone else's memory */
|
|
|
|
if (GetLastError() != 487) {
|
2004-10-26 21:16:18 +01:00
|
|
|
WorkSpaceTop = WorkSpaceTop0;
|
2014-09-10 05:50:43 +01:00
|
|
|
return 0;
|
2004-08-11 17:14:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
2015-09-25 10:57:26 +01:00
|
|
|
#elif SIZEOF_INT_P == 8
|
2011-01-20 17:59:54 +00:00
|
|
|
{
|
2015-09-25 10:57:26 +01:00
|
|
|
int n = 1024 * 1024;
|
2011-10-21 22:05:28 +01:00
|
|
|
while (n--) {
|
2011-01-20 17:59:54 +00:00
|
|
|
/* progress 1 MB */
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop += 512 * 1024;
|
2011-01-20 17:59:54 +00:00
|
|
|
if (ExtendWorkSpace(s, MAP_FIXED)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_add_memory_hole((ADDR)WorkSpaceTop0, (ADDR)WorkSpaceTop - s);
|
|
|
|
LOCAL_ErrorMessage = NULL;
|
|
|
|
return WorkSpaceTop - WorkSpaceTop0;
|
2011-01-20 17:59:54 +00:00
|
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* 487 happens when you step over someone else's memory */
|
|
|
|
if (GetLastError() != 487) {
|
2015-09-25 10:57:26 +01:00
|
|
|
WorkSpaceTop = WorkSpaceTop0;
|
|
|
|
return 0;
|
2011-01-20 17:59:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2004-08-11 17:14:55 +01:00
|
|
|
#endif
|
2004-10-26 21:16:18 +01:00
|
|
|
}
|
2011-01-20 17:59:54 +00:00
|
|
|
WorkSpaceTop = WorkSpaceTop0;
|
2003-10-30 22:52:46 +00:00
|
|
|
#endif
|
2014-09-10 05:50:43 +01:00
|
|
|
return 0;
|
2003-10-28 01:16:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:57:26 +01:00
|
|
|
void Yap_AllocHole(UInt actual_request, UInt total_size) {
|
|
|
|
#if (USE_SYSTEM_MMAP || defined(_WIN32) || defined(__CYGWIN__)) && \
|
|
|
|
!USE_DL_MALLOC
|
2003-10-28 01:16:03 +00:00
|
|
|
/* where we were when the hole was created,
|
|
|
|
also where is the hole store */
|
2015-09-25 10:57:26 +01:00
|
|
|
ADDR WorkSpaceTop0 = WorkSpaceTop - total_size;
|
2003-10-28 01:16:03 +00:00
|
|
|
BlockHeader *newb = (BlockHeader *)HeapTop;
|
2015-09-25 10:57:26 +01:00
|
|
|
BlockHeader *endb = (BlockHeader *)(WorkSpaceTop0 - sizeof(YAP_SEG_SIZE));
|
|
|
|
YAP_SEG_SIZE bsiz = (WorkSpaceTop0 - HeapTop) / sizeof(CELL) -
|
|
|
|
2 * sizeof(YAP_SEG_SIZE) / sizeof(CELL);
|
2003-10-28 01:16:03 +00:00
|
|
|
|
|
|
|
/* push HeapTop to after hole */
|
2015-09-25 10:57:26 +01:00
|
|
|
HeapTop = WorkSpaceTop - (actual_request - sizeof(YAP_SEG_SIZE));
|
|
|
|
((YAP_SEG_SIZE *)HeapTop)[0] = InUseFlag;
|
2003-10-28 01:16:03 +00:00
|
|
|
/* now simulate a block */
|
2015-09-25 10:57:26 +01:00
|
|
|
((YAP_SEG_SIZE *)HeapTop)[-1] = endb->b_size =
|
|
|
|
(HeapTop - WorkSpaceTop0) / sizeof(YAP_SEG_SIZE) | InUseFlag;
|
2003-10-28 01:16:03 +00:00
|
|
|
newb->b_size = bsiz;
|
|
|
|
AddToFreeList(newb);
|
2003-11-12 12:33:31 +00:00
|
|
|
#endif
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
#endif /* USE_SYSTEM_MALLOC */
|