123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "arrays.h"
- #include "async.h"
- #ifdef GUILE_DEBUG_MALLOC
- #include "debug-malloc.h"
- #endif
- #include "deprecation.h"
- #include "eval.h"
- #include "hashtab.h"
- #include "ports.h"
- #include "smob.h"
- #include "stackchk.h"
- #include "stime.h"
- #include "strings.h"
- #include "struct.h"
- #include "vectors.h"
- #include "gc.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;
-
- GC_gcollect_and_unmap ();
- ptr = do_realloc (mem, size);
- if (ptr)
- return ptr;
- scm_report_out_of_memory ();
-
- return NULL;
- }
- 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);
- }
|