1 /* Utility definitions for C code of SXEmacs for handling memory
3 Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc.
4 Copyright (C) 1993-1996 Richard Mlynarik.
5 Copyright (C) 1995, 1996, 2000 Ben Wing.
6 Copyright (C) 2004 Steve Youngs.
7 Copyright (C) 2011 Nelson Ferreira.
9 This file is part of SXEmacs
11 SXEmacs is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 SXEmacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* NOT synched with FSF */
25 #ifndef INCLUDED_sxe_memory_h_
26 #define INCLUDED_sxe_memory_h_
29 /* generally useful */
30 #define xnew(type) ((type *) xmalloc (sizeof (type)))
31 #define xnew_atomic(type) ((type *) xmalloc_atomic (sizeof (type)))
32 #define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type)))
33 #define xnew_atomic_array(type, len) \
34 ((type*)xmalloc_atomic((len) * sizeof(type)))
35 #define xnew_and_zero(type) ((type *) xmalloc_and_zero (sizeof (type)))
36 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
37 #define xnew_array_and_zero(type, len) \
38 ((type*)xmalloc_and_zero ((len) * sizeof (type)))
39 #define xrealloc_array(ptr, type, len) \
40 ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
41 #define XREALLOC_ARRAY xrealloc_array
42 #define alloca_array(type, len) ((type *) alloca ((len) * sizeof (type)))
44 /************************************************************************/
46 /************************************************************************/
48 #ifdef ALL_DEBUG_FLAGS
53 #if !defined GC_DEBUG_FLAG
54 # define SXE_DEBUG_GC(args...)
56 # define SXE_DEBUG_GC(args...) __SXE_DEBUG__("[gc] " args)
58 #define SXE_DEBUG_GC_PT(args...) SXE_DEBUG_GC("[pthread]: " args)
59 #define SXE_CRITICAL_GC(args...) __SXE_DEBUG__("[gc] CRITICAL: " args)
61 void malloc_warning(const char *);
63 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
64 # if defined HAVE_THREADS
65 # if !defined GC_PTHREADS
66 # define GC_PTHREADS 1
67 # endif /* !GC_PTHREADS */
68 # if !defined GC_THREADS
70 # endif /* !GC_THREADS */
71 # endif /* HAVE_THREADS */
74 # if defined GC_DEBUG_FLAG
76 # endif /* GC_DEBUG_FLAG */
77 # if defined HAVE_GC_GC_H
79 # elif defined HAVE_GC_H
82 # error "Take less of those pills!"
85 # if defined GC_DEBUG_FLAG
86 # define zmalloc GC_MALLOC_IGNORE_OFF_PAGE
87 # define zcalloc(n, m) GC_MALLOC((n)*(m))
88 # define zmalloc_atomic GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE
89 # define zmalloc_and_zero GC_MALLOC
90 # define zrealloc GC_REALLOC
91 # define zstrdup GC_STRDUP
93 # define zfree(x) GC_FREE(x)
94 # else /* !GC_DEBUG_FLAG */
95 # define zmalloc GC_malloc_ignore_off_page
96 # define zcalloc(n, m) GC_malloc((n)*(m))
97 # define zmalloc_atomic GC_malloc_atomic_ignore_off_page
98 # define zmalloc_and_zero GC_malloc
99 # define zrealloc GC_realloc
100 # define zstrdup GC_strdup
103 # endif /* GC_DEBUG_FLAG */
108 #define zmalloc_atomic F&^!
109 #define zmalloc_and_zero F&^!
110 #define zrealloc F&^!
111 #define zstrdrup F&^!
114 /* also define libc mem funs */
115 #define ymalloc malloc
116 #define ycalloc(n, m) calloc(n, m)
117 #define ymalloc_atomic(n) ycalloc(1, n)
118 #define ymalloc_and_zero(x) ycalloc(1, x)
119 #define yrealloc realloc
120 #define ystrdup strdup
121 #define yfree(x) free(x)
122 /* and their convenience companions */
123 #define ynew(type) ((type*)ymalloc(sizeof(type)))
124 #define ynew_array(type, len) ((type*)ymalloc((len) * sizeof(type)))
125 #define ynew_and_zero(type) ((type*)ymalloc_and_zero(sizeof(type)))
126 #define ynew_array_and_zero(type, len) \
127 ((type*)ymalloc_and_zero((len) * sizeof(type)))
128 #define YREALLOC_ARRAY(ptr, type, len) \
129 ((void)(ptr = (type *)yrealloc(ptr, (len) * sizeof (type))))
131 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
132 /* and now the x* series */
133 # define xmalloc zmalloc
134 # define xcalloc zcalloc
135 # define xmalloc_atomic zmalloc_atomic
136 # define xmalloc_and_zero zmalloc_and_zero
137 # define xrealloc zrealloc
138 # define xstrdup zstrdup
139 # if defined ERROR_CHECK_MALLOC
140 # define xfree(args...) zfree(args)
141 # else /* !ERROR_CHECK_MALLOC */
142 # define xfree(args...)
143 # endif /* ERROR_CHECK_MALLOC */
146 void *xmalloc(size_t size);
147 void *xmalloc_atomic(size_t size);
148 void *xmalloc_and_zero(size_t size);
149 void *xrealloc(void *, size_t size);
150 char *xstrdup(const char *);
151 # if defined ERROR_CHECK_MALLOC
152 # if SIZEOF_VOID_P == 4
153 # define xfree(lvalue) \
155 void *volatile *xfree_ptr = \
157 ((volatile void*)&(lvalue)); \
158 assert(*xfree_ptr != (void*)0xB00BB4BE); \
160 *xfree_ptr = (void*)0xB00BB4BE; \
162 # elif SIZEOF_VOID_P == 8
163 # define xfree(lvalue) \
165 void *volatile *xfree_ptr = \
167 ((volatile void*)&(lvalue)); \
168 assert(*xfree_ptr != (void*)0xCAFEBABEDEADBEEF); \
170 *xfree_ptr = (void*)0xCAFEBABEDEADBEEF; \
173 # error "Strange-arse system detected. Watch a movie, it\'s more fun!"
175 # else /* !ERROR_CHECK_MALLOC */
176 # define xfree(args...) yfree(args)
177 # endif /* ERROR_CHECK_MALLOC */