From b84e8f01881114d77ad01ed16e50e561494a3c91 Mon Sep 17 00:00:00 2001 From: Nelson Ferreira Date: Sun, 27 Sep 2015 17:24:04 -0400 Subject: [PATCH] Cleanup utilities. Introduce sxe-memory.h Move all the memory related utilities to sxe-memory.h so that sxe-utils.h can be included in contexts where the memory utils are not welcome. * src/sxemacs.h: Remove include for sxe-utils.h, lisp.h already does it. * src/lisp.h: Include new sxe-memory.h * src/sxe-utils.h: Remove xnew, xnew_atomic, xnew_array, xnew_atomic_array, xnew_and_zero, xzero, xnew_array_and_zero, xrealloc_array, alloca_array, xstrdup * src/sxe-memory.h: Add definitions removed from sxe-utils. Signed-off-by: Nelson Ferreira --- src/lisp.h | 1 + src/sxe-memory.h | 181 +++++++++++++++++++++++++++++++++++++++++++++++ src/sxe-utils.h | 151 --------------------------------------- src/sxemacs.h | 2 +- 4 files changed, 183 insertions(+), 152 deletions(-) create mode 100644 src/sxe-memory.h diff --git a/src/lisp.h b/src/lisp.h index 43f7536..e60bc14 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -31,6 +31,7 @@ along with this program. If not, see . */ /* the old SXEmacs general includes and utility macros moved here: */ #include "sxe-utils.h" +#include "sxe-memory.h" /* ------------------------ dynamic arrays ------------------- */ diff --git a/src/sxe-memory.h b/src/sxe-memory.h new file mode 100644 index 0000000..41840e4 --- /dev/null +++ b/src/sxe-memory.h @@ -0,0 +1,181 @@ +/* Utility definitions for C code of SXEmacs for handling memory + + Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc. + Copyright (C) 1993-1996 Richard Mlynarik. + Copyright (C) 1995, 1996, 2000 Ben Wing. + Copyright (C) 2004 Steve Youngs. + Copyright (C) 2011 Nelson Ferreira. + +This file is part of SXEmacs + +SXEmacs is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +SXEmacs is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . */ + +/* NOT synched with FSF */ +#ifndef INCLUDED_sxe_memory_h_ +#define INCLUDED_sxe_memory_h_ + + +/* generally useful */ +#define xnew(type) ((type *) xmalloc (sizeof (type))) +#define xnew_atomic(type) ((type *) xmalloc_atomic (sizeof (type))) +#define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type))) +#define xnew_atomic_array(type, len) \ + ((type*)xmalloc_atomic((len) * sizeof(type))) +#define xnew_and_zero(type) ((type *) xmalloc_and_zero (sizeof (type))) +#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue))) +#define xnew_array_and_zero(type, len) \ + ((type*)xmalloc_and_zero ((len) * sizeof (type))) +#define xrealloc_array(ptr, type, len) \ + ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type)))) +#define XREALLOC_ARRAY xrealloc_array +#define alloca_array(type, len) ((type *) alloca ((len) * sizeof (type))) + +/************************************************************************/ +/* Memory */ +/************************************************************************/ + +#ifdef ALL_DEBUG_FLAGS +#undef GC_DEBUG_FLAG +#define GC_DEBUG_FLAG +#endif + +#if !defined GC_DEBUG_FLAG +# define SXE_DEBUG_GC(args...) +#else +# define SXE_DEBUG_GC(args...) __SXE_DEBUG__("[gc] " args) +#endif +#define SXE_DEBUG_GC_PT(args...) SXE_DEBUG_GC("[pthread]: " args) +#define SXE_CRITICAL_GC(args...) __SXE_DEBUG__("[gc] CRITICAL: " args) + +void malloc_warning(const char *); + +#if defined HAVE_BDWGC && defined EF_USE_BDWGC +# if defined HAVE_THREADS +# if !defined GC_PTHREADS +# define GC_PTHREADS 1 +# endif /* !GC_PTHREADS */ +# if !defined GC_THREADS +# define GC_THREADS 1 +# endif /* !GC_THREADS */ +# endif /* HAVE_THREADS */ + +# undef GC_DEBUG +# if defined GC_DEBUG_FLAG +# define GC_DEBUG 1 +# endif /* GC_DEBUG_FLAG */ +# if defined HAVE_GC_GC_H +# include +# elif defined HAVE_GC_H +# include +# else +# error "Take less of those pills!" +# endif + +# if defined GC_DEBUG_FLAG +# define zmalloc GC_MALLOC_IGNORE_OFF_PAGE +# define zcalloc(n, m) GC_MALLOC((n)*(m)) +# define zmalloc_atomic GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE +# define zmalloc_and_zero GC_MALLOC +# define zrealloc GC_REALLOC +# define zstrdup GC_STRDUP +# undef zfree +# define zfree(x) GC_FREE(x) +# else /* !GC_DEBUG_FLAG */ +# define zmalloc GC_malloc_ignore_off_page +# define zcalloc(n, m) GC_malloc((n)*(m)) +# define zmalloc_atomic GC_malloc_atomic_ignore_off_page +# define zmalloc_and_zero GC_malloc +# define zrealloc GC_realloc +# define zstrdup GC_strdup +# undef zfree +# define zfree(x) +# endif /* GC_DEBUG_FLAG */ + +#else /* !BDWGC */ +#define zmalloc F&^! +#define zcalloc F&^! +#define zmalloc_atomic F&^! +#define zmalloc_and_zero F&^! +#define zrealloc F&^! +#define zstrdrup F&^! +#endif /* BDWGC */ + +/* also define libc mem funs */ +#define ymalloc malloc +#define ycalloc(n, m) calloc(n, m) +#define ymalloc_atomic(n) ycalloc(1, n) +#define ymalloc_and_zero(x) ycalloc(1, x) +#define yrealloc realloc +#define ystrdup strdup +#define yfree(x) free(x) +/* and their convenience companions */ +#define ynew(type) ((type*)ymalloc(sizeof(type))) +#define ynew_array(type, len) ((type*)ymalloc((len) * sizeof(type))) +#define ynew_and_zero(type) ((type*)ymalloc_and_zero(sizeof(type))) +#define ynew_array_and_zero(type, len) \ + ((type*)ymalloc_and_zero((len) * sizeof(type))) +#define YREALLOC_ARRAY(ptr, type, len) \ + ((void)(ptr = (type *)yrealloc(ptr, (len) * sizeof (type)))) + +#if defined HAVE_BDWGC && defined EF_USE_BDWGC +/* and now the x* series */ +# define xmalloc zmalloc +# define xcalloc zcalloc +# define xmalloc_atomic zmalloc_atomic +# define xmalloc_and_zero zmalloc_and_zero +# define xrealloc zrealloc +# define xstrdup zstrdup +# if defined ERROR_CHECK_MALLOC +# define xfree(args...) zfree(args) +# else /* !ERROR_CHECK_MALLOC */ +# define xfree(args...) +# endif /* ERROR_CHECK_MALLOC */ + +#else /* !BDWGC */ +void *xmalloc(size_t size); +void *xmalloc_atomic(size_t size); +void *xmalloc_and_zero(size_t size); +void *xrealloc(void *, size_t size); +char *xstrdup(const char *); +# if defined ERROR_CHECK_MALLOC +# if SIZEOF_VOID_P == 4 +# define xfree(lvalue) \ + do { \ + void *volatile *xfree_ptr = \ + (void *volatile*) \ + ((volatile void*)&(lvalue)); \ + assert(*xfree_ptr != (void*)0xB00BB4BE); \ + yfree(*xfree_ptr); \ + *xfree_ptr = (void*)0xB00BB4BE; \ + } while (0) +# elif SIZEOF_VOID_P == 8 +# define xfree(lvalue) \ + do { \ + void *volatile *xfree_ptr = \ + (void *volatile*) \ + ((volatile void*)&(lvalue)); \ + assert(*xfree_ptr != (void*)0xCAFEBABEDEADBEEF); \ + yfree(*xfree_ptr); \ + *xfree_ptr = (void*)0xCAFEBABEDEADBEEF; \ + } while (0) +# else /* huh? */ +# error "Strange-arse system detected. Watch a movie, it\'s more fun!" +# endif +# else /* !ERROR_CHECK_MALLOC */ +# define xfree(args...) yfree(args) +# endif /* ERROR_CHECK_MALLOC */ +#endif /* BDWGC */ + + +#endif diff --git a/src/sxe-utils.h b/src/sxe-utils.h index 8bc985f..2a6d166 100644 --- a/src/sxe-utils.h +++ b/src/sxe-utils.h @@ -143,21 +143,7 @@ void assert_failed(const char *, int, const char *); #endif -/* generally useful */ #define countof(x) ((int) (sizeof(x)/sizeof((x)[0]))) -#define xnew(type) ((type *) xmalloc (sizeof (type))) -#define xnew_atomic(type) ((type *) xmalloc_atomic (sizeof (type))) -#define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type))) -#define xnew_atomic_array(type, len) \ - ((type*)xmalloc_atomic((len) * sizeof(type))) -#define xnew_and_zero(type) ((type *) xmalloc_and_zero (sizeof (type))) -#define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue))) -#define xnew_array_and_zero(type, len) \ - ((type*)xmalloc_and_zero ((len) * sizeof (type))) -#define xrealloc_array(ptr, type, len) \ - ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type)))) -#define XREALLOC_ARRAY xrealloc_array -#define alloca_array(type, len) ((type *) alloca ((len) * sizeof (type))) #if !defined HAVE_DECL_STRDUP extern char *strdup(const char *s); @@ -254,143 +240,6 @@ template < typename T > struct alignment_trick { #define DECLARE_NOTHING struct nosuchstruct #endif - -/************************************************************************/ -/* Memory */ -/************************************************************************/ - -#ifdef ALL_DEBUG_FLAGS -#undef GC_DEBUG_FLAG -#define GC_DEBUG_FLAG -#endif - -#if !defined GC_DEBUG_FLAG -# define SXE_DEBUG_GC(args...) -#else -# define SXE_DEBUG_GC(args...) __SXE_DEBUG__("[gc] " args) -#endif -#define SXE_DEBUG_GC_PT(args...) SXE_DEBUG_GC("[pthread]: " args) -#define SXE_CRITICAL_GC(args...) __SXE_DEBUG__("[gc] CRITICAL: " args) - -void malloc_warning(const char *); - -#if defined HAVE_BDWGC && defined EF_USE_BDWGC -# if defined HAVE_THREADS -# if !defined GC_PTHREADS -# define GC_PTHREADS 1 -# endif /* !GC_PTHREADS */ -# if !defined GC_THREADS -# define GC_THREADS 1 -# endif /* !GC_THREADS */ -# endif /* HAVE_THREADS */ - -# undef GC_DEBUG -# if defined GC_DEBUG_FLAG -# define GC_DEBUG 1 -# endif /* GC_DEBUG_FLAG */ -# if defined HAVE_GC_GC_H -# include -# elif defined HAVE_GC_H -# include -# else -# error "Take less of those pills!" -# endif - -# if defined GC_DEBUG_FLAG -# define zmalloc GC_MALLOC_IGNORE_OFF_PAGE -# define zcalloc(n, m) GC_MALLOC((n)*(m)) -# define zmalloc_atomic GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE -# define zmalloc_and_zero GC_MALLOC -# define zrealloc GC_REALLOC -# define zstrdup GC_STRDUP -# undef zfree -# define zfree(x) GC_FREE(x) -# else /* !GC_DEBUG_FLAG */ -# define zmalloc GC_malloc_ignore_off_page -# define zcalloc(n, m) GC_malloc((n)*(m)) -# define zmalloc_atomic GC_malloc_atomic_ignore_off_page -# define zmalloc_and_zero GC_malloc -# define zrealloc GC_realloc -# define zstrdup GC_strdup -# undef zfree -# define zfree(x) -# endif /* GC_DEBUG_FLAG */ - -#else /* !BDWGC */ -#define zmalloc F&^! -#define zcalloc F&^! -#define zmalloc_atomic F&^! -#define zmalloc_and_zero F&^! -#define zrealloc F&^! -#define zstrdrup F&^! -#endif /* BDWGC */ - -/* also define libc mem funs */ -#define ymalloc malloc -#define ycalloc(n, m) calloc(n, m) -#define ymalloc_atomic(n) ycalloc(1, n) -#define ymalloc_and_zero(x) ycalloc(1, x) -#define yrealloc realloc -#define ystrdup strdup -#define yfree(x) free(x) -/* and their convenience companions */ -#define ynew(type) ((type*)ymalloc(sizeof(type))) -#define ynew_array(type, len) ((type*)ymalloc((len) * sizeof(type))) -#define ynew_and_zero(type) ((type*)ymalloc_and_zero(sizeof(type))) -#define ynew_array_and_zero(type, len) \ - ((type*)ymalloc_and_zero((len) * sizeof(type))) -#define YREALLOC_ARRAY(ptr, type, len) \ - ((void)(ptr = (type *)yrealloc(ptr, (len) * sizeof (type)))) - -#if defined HAVE_BDWGC && defined EF_USE_BDWGC -/* and now the x* series */ -# define xmalloc zmalloc -# define xcalloc zcalloc -# define xmalloc_atomic zmalloc_atomic -# define xmalloc_and_zero zmalloc_and_zero -# define xrealloc zrealloc -# define xstrdup zstrdup -# if defined ERROR_CHECK_MALLOC -# define xfree(args...) zfree(args) -# else /* !ERROR_CHECK_MALLOC */ -# define xfree(args...) -# endif /* ERROR_CHECK_MALLOC */ - -#else /* !BDWGC */ -void *xmalloc(size_t size); -void *xmalloc_atomic(size_t size); -void *xmalloc_and_zero(size_t size); -void *xrealloc(void *, size_t size); -char *xstrdup(const char *); -# if defined ERROR_CHECK_MALLOC -# if SIZEOF_VOID_P == 4 -# define xfree(lvalue) \ - do { \ - void *volatile *xfree_ptr = \ - (void *volatile*) \ - ((volatile void*)&(lvalue)); \ - assert(*xfree_ptr != (void*)0xB00BB4BE); \ - yfree(*xfree_ptr); \ - *xfree_ptr = (void*)0xB00BB4BE; \ - } while (0) -# elif SIZEOF_VOID_P == 8 -# define xfree(lvalue) \ - do { \ - void *volatile *xfree_ptr = \ - (void *volatile*) \ - ((volatile void*)&(lvalue)); \ - assert(*xfree_ptr != (void*)0xCAFEBABEDEADBEEF); \ - yfree(*xfree_ptr); \ - *xfree_ptr = (void*)0xCAFEBABEDEADBEEF; \ - } while (0) -# else /* huh? */ -# error "Strange-arse system detected. Watch a movie, it\'s more fun!" -# endif -# else /* !ERROR_CHECK_MALLOC */ -# define xfree(args...) yfree(args) -# endif /* ERROR_CHECK_MALLOC */ -#endif /* BDWGC */ - /* str funs */ #define xstrlen strlen diff --git a/src/sxemacs.h b/src/sxemacs.h index 9cec2e3..f551f27 100644 --- a/src/sxemacs.h +++ b/src/sxemacs.h @@ -81,7 +81,7 @@ #if defined USE_SXEMACS_CONFIG_H # include "config.h" #endif -#include "sxe-utils.h" + #include "lisp.h" #include "sysfile.h" -- 2.25.1