#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef __ia64__
#include <ucontext.h>
extern unsigned long * __libc_ia64_register_backing_store_base;
#endif
#include "libguile/_scm.h"
#include "libguile/eval.h"
#include "libguile/stime.h"
#include "libguile/stackchk.h"
#include "libguile/struct.h"
#include "libguile/smob.h"
#include "libguile/arrays.h"
#include "libguile/async.h"
#include "libguile/ports.h"
#include "libguile/root.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/weaks.h"
#include "libguile/hashtab.h"
#include "libguile/tags.h"
#include "libguile/validate.h"
#include "libguile/deprecation.h"
#include "libguile/gc.h"
#include "libguile/private-gc.h"
#ifdef GUILE_DEBUG_MALLOC
#include "libguile/debug-malloc.h"
#endif
#include <unistd.h>
#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
#define SCM_DEFAULT_MALLOC_MINYIELD 40
static void*
do_realloc (void *from, size_t new_size)
{
scm_gc_register_allocation (new_size);
return realloc (from, new_size);
}
static void*
do_calloc (size_t n, size_t size)
{
scm_gc_register_allocation (size);
return calloc (n, size);
}
static void*
do_gc_malloc (size_t size, const char *what)
{
return GC_MALLOC (size ? size : sizeof (void *));
}
static void*
do_gc_malloc_atomic (size_t size, const char *what)
{
return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
}
static void*
do_gc_realloc (void *from, size_t size, const char *what)
{
return GC_REALLOC (from, size ? size : sizeof (void *));
}
static void
do_gc_free (void *ptr)
{
GC_FREE (ptr);
}
void *
scm_realloc (void *mem, size_t size)
{
void *ptr;
ptr = do_realloc (mem, size);
if (ptr || size == 0)
return ptr;
#ifdef HAVE_GC_GCOLLECT_AND_UNMAP
GC_gcollect_and_unmap ();
#else
GC_gcollect ();
#endif
ptr = do_realloc (mem, size);
if (ptr)
return ptr;
scm_memory_error ("realloc");
}
void *
scm_malloc (size_t sz)
{
return scm_realloc (NULL, sz);
}
void *
scm_calloc (size_t sz)
{
void * ptr;
ptr = do_calloc (sz, 1);
if (ptr || sz == 0)
return ptr;
ptr = scm_realloc (NULL, sz);
memset (ptr, 0x0, sz);
return ptr;
}
char *
scm_strndup (const char *str, size_t n)
{
char *dst = scm_malloc (n + 1);
memcpy (dst, str, n);
dst[n] = 0;
return dst;
}
char *
scm_strdup (const char *str)
{
return scm_strndup (str, strlen (str));
}
void
scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
{
scm_gc_register_allocation (size);
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_register (mem, what);
#endif
}
void
scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
{
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_unregister (mem);
#endif
}
void *
scm_gc_malloc_pointerless (size_t size, const char *what)
{
return do_gc_malloc_atomic (size, what);
}
void *
scm_gc_malloc (size_t size, const char *what)
{
return do_gc_malloc (size, what);
}
void *
scm_gc_calloc (size_t size, const char *what)
{
return do_gc_malloc (size, what);
}
void *
scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
{
return do_gc_realloc (mem, new_size, what);
}
void
scm_gc_free (void *mem, size_t size, const char *what)
{
do_gc_free (mem);
}
char *
scm_gc_strndup (const char *str, size_t n, const char *what)
{
char *dst = do_gc_malloc_atomic (n + 1, what);
memcpy (dst, str, n);
dst[n] = 0;
return dst;
}
char *
scm_gc_strdup (const char *str, const char *what)
{
return scm_gc_strndup (str, strlen (str), what);
}
#if SCM_ENABLE_DEPRECATED == 1
void *
scm_must_malloc (size_t size, const char *what)
{
scm_c_issue_deprecation_warning
("scm_must_malloc is deprecated. "
"Use scm_gc_malloc and scm_gc_free instead.");
return scm_gc_malloc (size, what);
}
void *
scm_must_realloc (void *where,
size_t old_size,
size_t size,
const char *what)
{
scm_c_issue_deprecation_warning
("scm_must_realloc is deprecated. "
"Use scm_gc_realloc and scm_gc_free instead.");
return scm_gc_realloc (where, old_size, size, what);
}
char *
scm_must_strndup (const char *str, size_t length)
{
scm_c_issue_deprecation_warning
("scm_must_strndup is deprecated. "
"Use scm_gc_strndup and scm_gc_free instead.");
return scm_gc_strndup (str, length, "string");
}
char *
scm_must_strdup (const char *str)
{
scm_c_issue_deprecation_warning
("scm_must_strdup is deprecated. "
"Use scm_gc_strdup and scm_gc_free instead.");
return scm_gc_strdup (str, "string");
}
void
scm_must_free (void *obj)
#define FUNC_NAME "scm_must_free"
{
scm_c_issue_deprecation_warning
("scm_must_free is deprecated. "
"Use scm_gc_malloc and scm_gc_free instead.");
do_gc_free (obj);
}
#undef FUNC_NAME
void
scm_done_malloc (long size)
{
scm_c_issue_deprecation_warning
("scm_done_malloc is deprecated. "
"Use scm_gc_register_collectable_memory instead.");
if (size >= 0)
scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
else
scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
}
void
scm_done_free (long size)
{
scm_c_issue_deprecation_warning
("scm_done_free is deprecated. "
"Use scm_gc_unregister_collectable_memory instead.");
if (size >= 0)
scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
else
scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
}
#endif