Cleanup utilities. Introduce sxe-memory.h
[sxemacs] / src / sxe-utils.h
index 7f6bfe0..2a6d166 100644 (file)
@@ -143,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);
@@ -255,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