summaryrefslogtreecommitdiff
path: root/include/ruby/impl/xmalloc.h
blob: ecf9addc9e39dadd69839f73e3c4b3fe65595c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef RUBY3_XMALLOC_H                              /*-*-C++-*-vi:se ft=cpp:*/
#define RUBY3_XMALLOC_H
/**
 * @file
 * @author     Ruby developers <ruby-core@ruby-lang.org>
 * @copyright  This  file  is   a  part  of  the   programming  language  Ruby.
 *             Permission  is hereby  granted,  to  either redistribute  and/or
 *             modify this file, provided that  the conditions mentioned in the
 *             file COPYING are met.  Consult the file for details.
 * @warning    Symbols   prefixed   with   either  `RUBY3`   or   `ruby3`   are
 *             implementation details.   Don't take  them as canon.  They could
 *             rapidly appear then vanish.  The name (path) of this header file
 *             is also an  implementation detail.  Do not expect  it to persist
 *             at the place it is now.  Developers are free to move it anywhere
 *             anytime at will.
 * @note       To  ruby-core:  remember  that   this  header  can  be  possibly
 *             recursively included  from extension  libraries written  in C++.
 *             Do not  expect for  instance `__VA_ARGS__` is  always available.
 *             We assume C99  for ruby itself but we don't  assume languages of
 *             extension libraries. They could be written in C++98.
 * @brief      Declares ::ruby_xmalloc().
 */
#include "ruby/3/config.h"

#ifdef STDC_HEADERS
# include <stddef.h>
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#include "ruby/3/attr/alloc_size.h"
#include "ruby/3/attr/nodiscard.h"
#include "ruby/3/attr/noexcept.h"
#include "ruby/3/attr/restrict.h"
#include "ruby/3/attr/returns_nonnull.h"
#include "ruby/3/dllexport.h"

#ifndef USE_GC_MALLOC_OBJ_INFO_DETAILS
# define USE_GC_MALLOC_OBJ_INFO_DETAILS 0
#endif

#define xmalloc ruby_xmalloc
#define xmalloc2 ruby_xmalloc2
#define xcalloc ruby_xcalloc
#define xrealloc ruby_xrealloc
#define xrealloc2 ruby_xrealloc2
#define xfree ruby_xfree

RUBY3_SYMBOL_EXPORT_BEGIN()

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1))
void *ruby_xmalloc(size_t size)
RUBY3_ATTR_NOEXCEPT(malloc(size))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1,2))
void *ruby_xmalloc2(size_t nelems, size_t elemsiz)
RUBY3_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1,2))
void *ruby_xcalloc(size_t nelems, size_t elemsiz)
RUBY3_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((2))
void *ruby_xrealloc(void *ptr, size_t newsiz)
RUBY3_ATTR_NOEXCEPT(realloc(ptr, newsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((2,3))
void *ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
RUBY3_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
;

void ruby_xfree(void *ptr)
RUBY3_ATTR_NOEXCEPT(free(ptr))
;

#if USE_GC_MALLOC_OBJ_INFO_DETAILS || defined(__DOXYGEN)
# define ruby_xmalloc(s1)            ruby_xmalloc_with_location(s1, __FILE__, __LINE__)
# define ruby_xmalloc2(s1, s2)       ruby_xmalloc2_with_location(s1, s2, __FILE__, __LINE__)
# define ruby_xcalloc(s1, s2)        ruby_xcalloc_with_location(s1, s2, __FILE__, __LINE__)
# define ruby_xrealloc(ptr, s1)      ruby_xrealloc_with_location(ptr, s1, __FILE__, __LINE__)
# define ruby_xrealloc2(ptr, s1, s2) ruby_xrealloc2_with_location(ptr, s1, s2, __FILE__, __LINE__)

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1))
void *ruby_xmalloc_body(size_t size)
RUBY3_ATTR_NOEXCEPT(malloc(size))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1,2))
void *ruby_xmalloc2_body(size_t nelems, size_t elemsiz)
RUBY3_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RESTRICT()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((1,2))
void *ruby_xcalloc_body(size_t nelems, size_t elemsiz)
RUBY3_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((2))
void *ruby_xrealloc_body(void *ptr, size_t newsiz)
RUBY3_ATTR_NOEXCEPT(realloc(ptr, newsiz))
;

RUBY3_ATTR_NODISCARD()
RUBY3_ATTR_RETURNS_NONNULL()
RUBY3_ATTR_ALLOC_SIZE((2,3))
void *ruby_xrealloc2_body(void *ptr, size_t newelems, size_t newsiz)
RUBY3_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
;

RUBY_EXTERN const char *ruby_malloc_info_file;
RUBY_EXTERN int ruby_malloc_info_line;

static inline void *
ruby_xmalloc_with_location(size_t s, const char *file, int line)
{
    void *ptr;
    ruby_malloc_info_file = file;
    ruby_malloc_info_line = line;
    ptr = ruby_xmalloc_body(s);
    ruby_malloc_info_file = NULL;
    return ptr;
}

static inline void *
ruby_xmalloc2_with_location(size_t s1, size_t s2, const char *file, int line)
{
    void *ptr;
    ruby_malloc_info_file = file;
    ruby_malloc_info_line = line;
    ptr = ruby_xmalloc2_body(s1, s2);
    ruby_malloc_info_file = NULL;
    return ptr;
}

static inline void *
ruby_xcalloc_with_location(size_t s1, size_t s2, const char *file, int line)
{
    void *ptr;
    ruby_malloc_info_file = file;
    ruby_malloc_info_line = line;
    ptr = ruby_xcalloc_body(s1, s2);
    ruby_malloc_info_file = NULL;
    return ptr;
}

static inline void *
ruby_xrealloc_with_location(void *ptr, size_t s, const char *file, int line)
{
    void *rptr;
    ruby_malloc_info_file = file;
    ruby_malloc_info_line = line;
    rptr = ruby_xrealloc_body(ptr, s);
    ruby_malloc_info_file = NULL;
    return rptr;
}

static inline void *
ruby_xrealloc2_with_location(void *ptr, size_t s1, size_t s2, const char *file, int line)
{
    void *rptr;
    ruby_malloc_info_file = file;
    ruby_malloc_info_line = line;
    rptr = ruby_xrealloc2_body(ptr, s1, s2);
    ruby_malloc_info_file = NULL;
    return rptr;
}
#endif

RUBY3_SYMBOL_EXPORT_END()

#endif /* RUBY3_XMALLOC_H */