Cleanup utilities. Introduce sxe-memory.h
[sxemacs] / src / sxe-utils.h
index 83569d9..2a6d166 100644 (file)
@@ -1,4 +1,4 @@
-/* Utility definitions for C code of SXEmacs 
+/* Utility definitions for C code of SXEmacs
 
    Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc.
    Copyright (C) 1993-1996 Richard Mlynarik.
@@ -46,13 +46,17 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 \f
 /* goodies */
-#ifdef UNUSED
+#ifdef SXE_UNUSED
 #elif defined(__GNUC__)
-# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
+# define SXE_UNUSED(x) UNUSED_ ## x __attribute__((unused))
 #elif defined(__LCLINT__)
-# define UNUSED(x) /*@unused@*/ x
+# define SXE_UNUSED(x) /*@unused@*/ x
 #else
-# define UNUSED(x) x
+# define SXE_UNUSED(x) x
+#endif
+#ifdef UNUSED
+#undef UNUSED
+#define UNUSED(x) SXE_UNUSED(x)
 #endif
 
 #ifdef WEAK_EXTERN
@@ -80,14 +84,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #endif
 
 \f
-
-#if defined SXE_STATIC_EXTERN_INLINE
-# define extern_inline static inline
-#elif !defined __GNUC_STDC_INLINE__ && !defined SXE_INLINE_NO_EXTERN
-# define extern_inline extern inline
-#else
-# define extern_inline  inline
-#endif
+#define extern_inline  static inline
 
 #ifdef ALL_DEBUG_FLAGS
 #undef SXE_DEBUG_FLAG
@@ -146,21 +143,7 @@ void assert_failed(const char *, int, const char *);
 #endif
 
 \f
-/* 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);
@@ -170,7 +153,7 @@ extern char *strdup(const char *s);
 #ifndef PRINTF_ARGS
 # if defined (__GNUC__) && (__GNUC__ >= 2)
 #  define PRINTF_ARGS(string_index,first_to_check) \
-          __attribute__ ((format (printf, string_index, first_to_check)))
+         __attribute__ ((format (printf, string_index, first_to_check)))
 # else
 #  define PRINTF_ARGS(string_index,first_to_check)
 # endif                                /* GNUC */
@@ -181,23 +164,23 @@ extern char *strdup(const char *s);
 #  if ((__GNUC__ > 2) || (__GNUC__ == 2) && (__GNUC_MINOR__ >= 5))
 #   define DOESNT_RETURN void
 #   define DECLARE_DOESNT_RETURN(decl) \
-           extern void decl __attribute__ ((noreturn))
+          extern void decl __attribute__ ((noreturn))
 #   define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
      /* Should be able to state multiple independent __attribute__s, but  \
-        the losing syntax doesn't work that way, and screws losing cpp */ \
-           extern void decl \
-                  __attribute__ ((noreturn, format (printf, str, idx)))
+       the losing syntax doesn't work that way, and screws losing cpp */ \
+          extern void decl \
+                 __attribute__ ((noreturn, format (printf, str, idx)))
 #  else
 #   define DOESNT_RETURN void volatile
 #   define DECLARE_DOESNT_RETURN(decl) extern void volatile decl
 #   define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
-           extern void volatile decl PRINTF_ARGS(str,idx)
+          extern void volatile decl PRINTF_ARGS(str,idx)
 #  endif                       /* GNUC 2.5 */
 # else
 #  define DOESNT_RETURN void
 #  define DECLARE_DOESNT_RETURN(decl) extern void decl
 #  define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
-          extern void decl PRINTF_ARGS(str,idx)
+         extern void decl PRINTF_ARGS(str,idx)
 # endif                                /* GNUC */
 #endif
 
@@ -258,140 +241,118 @@ template < typename T > struct alignment_trick {
 #endif
 
 \f
-/************************************************************************/
-/*                               Memory                                */
-/************************************************************************/
-
-#ifdef ALL_DEBUG_FLAGS
-#undef GC_DEBUG_FLAG
-#define GC_DEBUG_FLAG
-#endif
-
-#if !defined GC_DEBUG_FLAG
-# define SXE_DEBUG_GC(args...)
+/* str funs */
+#define xstrlen                strlen
+#define xstrcmp                strcmp
+#define xstrncmp       strncmp
+#define xstrncat       strncat
+
+extern_inline char*
+xstrncpy(char* target, const char*source, size_t len)
+       __attribute__((always_inline));
+extern_inline char*
+xstrncpy(char* target, const char*source, size_t len)
+{
+       *target ='\0';
+       return strncat(target,source,len-1);
+}
+
+#if !defined(FORBID_STRCPY)
+#  define xstrcat              strcat
+#  define xstrcpy              strcpy
+#  if defined(HAVE_STPCPY)
+#     define xstpcpy   stpcpy
+#   else
+extern_inline char*
+xstpcpy(char *target, const char *source)
+       __attribute__((always_inline));
+extern_inline char*
+xstpcpy(char *target, const char *source)
+{
+       char *p = target;
+       size_t len = xstrlen(source);
+
+       strcpy(target, source);
+       p += len;
+       return p;
+}
+#   endif
 #else
-# define SXE_DEBUG_GC(args...)         __SXE_DEBUG__("[gc] " args)
+#  if defined(strcpy)
+#    undef strcpy
+#  endif
+#  define strcpy  no_strcpy
+extern_inline char*
+no_strcpy(char*,const char*)
+       __attribute__((always_inline));
+extern_inline char*
+no_strcpy(char * SXE_UNUSED(target),const char * SXE_UNUSED(source))
+{
+       assert(0);
+       return NULL;
+}
+#  if defined(strcat)
+#    undef strcat
+#  endif
+#  define strcat  no_strcat
+extern_inline char*
+no_strcat(char*,const char*)
+       __attribute__((always_inline));
+extern_inline char*
+no_strcat(char * SXE_UNUSED(target), const char* SXE_UNUSED(source))
+{
+       assert(0);
+       return NULL;
+}
+#  if defined(stpcpy)
+#    undef stpcpy
+#  endif
+#  define stpcpy       no_stpcpy
+#  define xstpcpy      no_stpcpy
+extern_inline char*
+no_stpcpy(char*,const char*)
+       __attribute__((always_inline));
+extern_inline char*
+no_stpcpy(char* SXE_UNUSED(target),const char* SXE_UNUSED(source))
+{
+       assert(0);
+       return NULL;
+}
 #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 <gc/gc.h>
-# elif defined HAVE_GC_H
-#  include <gc.h>
-# 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 */
+
+#if defined HAVE_STPNCPY
+# define xstpncpy      stpncpy
+#else
+extern_inline char*
+xstpncpy(char *target, const char *source, size_t len)
+       __attribute__((always_inline));
+extern_inline char*
+xstpncpy(char *target, const char *source, size_t len)
+{
+       char *p = target;
+       xstrncpy(target, source, len);
+       p += len;
+       return p;
+}
+#endif /* !HAVE_STPNCPY */
+
+#define xmemcmp                memcmp
+#define xmemcpy                memcpy
+
+
+
+extern_inline size_t
+xmin_size_t(size_t a, size_t b)
+       __attribute__((always_inline));
+extern_inline size_t
+xmin_size_t(size_t a, size_t b)
+{
+       if (a < b) {
+               return a;
+       } else {
+               return b;
+       }
+}
 
 #endif