Ruby: include/ruby/internal/xmalloc.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

xmalloc.h
Go to the documentation of this file.
1#ifndef RBIMPL_XMALLOC_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_XMALLOC_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#ifdef HAVE_STDLIB_H
30# include <stdlib.h>
31#endif
32
33#include "ruby/internal/attr/alloc_size.h"
34#include "ruby/internal/attr/nodiscard.h"
35#include "ruby/internal/attr/noexcept.h"
36#include "ruby/internal/attr/restrict.h"
37#include "ruby/internal/attr/returns_nonnull.h"
38#include "ruby/internal/dllexport.h"
39
40#ifndef USE_GC_MALLOC_OBJ_INFO_DETAILS
41# define USE_GC_MALLOC_OBJ_INFO_DETAILS 0
42#endif
43
44#define xmalloc ruby_xmalloc
45#define xmalloc2 ruby_xmalloc2
46#define xcalloc ruby_xcalloc
47#define xrealloc ruby_xrealloc
48#define xrealloc2 ruby_xrealloc2
49#define xfree ruby_xfree
50
51 RBIMPL_SYMBOL_EXPORT_BEGIN()
52
53 RBIMPL_ATTR_NODISCARD()
54 RBIMPL_ATTR_RESTRICT()
55 RBIMPL_ATTR_RETURNS_NONNULL()
56 RBIMPL_ATTR_ALLOC_SIZE((1))
77void *ruby_xmalloc(size_t size)
78 RBIMPL_ATTR_NOEXCEPT(malloc(size))
79;
80
81 RBIMPL_ATTR_NODISCARD()
82 RBIMPL_ATTR_RESTRICT()
83 RBIMPL_ATTR_RETURNS_NONNULL()
84 RBIMPL_ATTR_ALLOC_SIZE((1,2))
108void *ruby_xmalloc2(size_t nelems, size_t elemsiz)
109 RBIMPL_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
110;
111
112 RBIMPL_ATTR_NODISCARD()
113 RBIMPL_ATTR_RESTRICT()
114 RBIMPL_ATTR_RETURNS_NONNULL()
115 RBIMPL_ATTR_ALLOC_SIZE((1,2))
137void *ruby_xcalloc(size_t nelems, size_t elemsiz)
138 RBIMPL_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
139;
140
141 RBIMPL_ATTR_NODISCARD()
142 RBIMPL_ATTR_RETURNS_NONNULL()
143 RBIMPL_ATTR_ALLOC_SIZE((2))
177void *ruby_xrealloc(void *ptr, size_t newsiz)
178 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newsiz))
179;
180
181 RBIMPL_ATTR_NODISCARD()
182 RBIMPL_ATTR_RETURNS_NONNULL()
183 RBIMPL_ATTR_ALLOC_SIZE((2,3))
229void *ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
230 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
231;
232
252void ruby_xfree(void *ptr)
253 RBIMPL_ATTR_NOEXCEPT(free(ptr))
254;
255
256#if USE_GC_MALLOC_OBJ_INFO_DETAILS || defined(__DOXYGEN)
257# define ruby_xmalloc(s1) ruby_xmalloc_with_location(s1, __FILE__, __LINE__)
258# define ruby_xmalloc2(s1, s2) ruby_xmalloc2_with_location(s1, s2, __FILE__, __LINE__)
259# define ruby_xcalloc(s1, s2) ruby_xcalloc_with_location(s1, s2, __FILE__, __LINE__)
260# define ruby_xrealloc(ptr, s1) ruby_xrealloc_with_location(ptr, s1, __FILE__, __LINE__)
261# define ruby_xrealloc2(ptr, s1, s2) ruby_xrealloc2_with_location(ptr, s1, s2, __FILE__, __LINE__)
262
263 RBIMPL_ATTR_NODISCARD()
264 RBIMPL_ATTR_RESTRICT()
265 RBIMPL_ATTR_RETURNS_NONNULL()
266 RBIMPL_ATTR_ALLOC_SIZE((1))
267void *ruby_xmalloc_body(size_t size)
268 RBIMPL_ATTR_NOEXCEPT(malloc(size))
269;
270
271 RBIMPL_ATTR_NODISCARD()
272 RBIMPL_ATTR_RESTRICT()
273 RBIMPL_ATTR_RETURNS_NONNULL()
274 RBIMPL_ATTR_ALLOC_SIZE((1,2))
275void *ruby_xmalloc2_body(size_t nelems, size_t elemsiz)
276 RBIMPL_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
277;
278
279 RBIMPL_ATTR_NODISCARD()
280 RBIMPL_ATTR_RESTRICT()
281 RBIMPL_ATTR_RETURNS_NONNULL()
282 RBIMPL_ATTR_ALLOC_SIZE((1,2))
283void *ruby_xcalloc_body(size_t nelems, size_t elemsiz)
284 RBIMPL_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
285;
286
287 RBIMPL_ATTR_NODISCARD()
288 RBIMPL_ATTR_RETURNS_NONNULL()
289 RBIMPL_ATTR_ALLOC_SIZE((2))
290void *ruby_xrealloc_body(void *ptr, size_t newsiz)
291 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newsiz))
292;
293
294 RBIMPL_ATTR_NODISCARD()
295 RBIMPL_ATTR_RETURNS_NONNULL()
296 RBIMPL_ATTR_ALLOC_SIZE((2,3))
297void *ruby_xrealloc2_body(void *ptr, size_t newelems, size_t newsiz)
298 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
299;
300
301 RUBY_EXTERN const char *ruby_malloc_info_file;
302 RUBY_EXTERN int ruby_malloc_info_line;
303
304static inline void *
305ruby_xmalloc_with_location(size_t s, const char *file, int line)
306{
307 void *ptr;
308 ruby_malloc_info_file = file;
309 ruby_malloc_info_line = line;
310 ptr = ruby_xmalloc_body(s);
311 ruby_malloc_info_file = NULL;
312 return ptr;
313}
314
315static inline void *
316ruby_xmalloc2_with_location(size_t s1, size_t s2, const char *file, int line)
317{
318 void *ptr;
319 ruby_malloc_info_file = file;
320 ruby_malloc_info_line = line;
321 ptr = ruby_xmalloc2_body(s1, s2);
322 ruby_malloc_info_file = NULL;
323 return ptr;
324}
325
326static inline void *
327ruby_xcalloc_with_location(size_t s1, size_t s2, const char *file, int line)
328{
329 void *ptr;
330 ruby_malloc_info_file = file;
331 ruby_malloc_info_line = line;
332 ptr = ruby_xcalloc_body(s1, s2);
333 ruby_malloc_info_file = NULL;
334 return ptr;
335}
336
337static inline void *
338ruby_xrealloc_with_location(void *ptr, size_t s, const char *file, int line)
339{
340 void *rptr;
341 ruby_malloc_info_file = file;
342 ruby_malloc_info_line = line;
343 rptr = ruby_xrealloc_body(ptr, s);
344 ruby_malloc_info_file = NULL;
345 return rptr;
346}
347
348static inline void *
349ruby_xrealloc2_with_location(void *ptr, size_t s1, size_t s2, const char *file, int line)
350{
351 void *rptr;
352 ruby_malloc_info_file = file;
353 ruby_malloc_info_line = line;
354 rptr = ruby_xrealloc2_body(ptr, s1, s2);
355 ruby_malloc_info_file = NULL;
356 return rptr;
357}
358#endif
359
360 RBIMPL_SYMBOL_EXPORT_END()
361
362#endif /* RBIMPL_XMALLOC_H */
alloc_size.h
Defines RBIMPL_ATTR_ALLOC_SIZE.
RBIMPL_ATTR_ALLOC_SIZE
#define RBIMPL_ATTR_ALLOC_SIZE(tuple)
Wraps (or simulates) __attribute__((alloc_size))
Definition: alloc_size.h:29
ptr
struct RIMemo * ptr
Definition: debug.c:88
dllexport.h
Tewaking visibility of C variables/functions.
RUBY_EXTERN
#define RUBY_EXTERN
Definition: dllexport.h:36
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
free
#define free(x)
Definition: dln.c:52
ruby_xmalloc2_body
void * ruby_xmalloc2_body(size_t n, size_t size)
Definition: gc.c:10846
ruby_xrealloc_body
void * ruby_xrealloc_body(void *ptr, size_t new_size)
Definition: gc.c:10881
ruby_xmalloc_body
void * ruby_xmalloc_body(size_t size)
Definition: gc.c:10829
ruby_xcalloc_body
void * ruby_xcalloc_body(size_t n, size_t size)
Definition: gc.c:10862
ruby_xrealloc2_body
void * ruby_xrealloc2_body(void *ptr, size_t n, size_t size)
Definition: gc.c:10897
config.h
Thin wrapper to ruby/config.h.
size
voidpf void uLong size
Definition: ioapi.h:138
nodiscard.h
Defines RBIMPL_ATTR_NODISCARD.
RBIMPL_ATTR_NODISCARD
#define RBIMPL_ATTR_NODISCARD()
Wraps (or simulates) [[nodiscard]].
Definition: nodiscard.h:42
noexcept.h
Defines RBIMPL_ATTR_NOEXCEPT.
RBIMPL_ATTR_NOEXCEPT
#define RBIMPL_ATTR_NOEXCEPT(_)
Wraps (or simulates) C++11 noexcept
Definition: noexcept.h:85
NULL
#define NULL
Definition: regenc.h:69
restrict.h
Defines RBIMPL_ATTR_RESTRICT.
RBIMPL_ATTR_RESTRICT
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Definition: restrict.h:42
returns_nonnull.h
Defines RBIMPL_ATTR_RETURNS_NONNULL.
RBIMPL_ATTR_RETURNS_NONNULL
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
Definition: returns_nonnull.h:34
realloc
#define realloc
Definition: st.c:172
calloc
#define calloc
Definition: st.c:171
malloc
#define malloc
Definition: st.c:170
const
#define const
Definition: strftime.c:108
file
Definition: gzappend.c:170
ruby_xrealloc
void * ruby_xrealloc(void *ptr, size_t newsiz)
Resize the storage instance.
Definition: gc.c:12825
ruby_xrealloc2
void * ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes...
Definition: gc.c:12835
ruby_xfree
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition: gc.c:10914
ruby_xmalloc2
void * ruby_xmalloc2(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc(), except it allocates nelems * elemsiz bytes.
Definition: gc.c:12805
ruby_xmalloc
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
Definition: gc.c:12795
ruby_xcalloc
void * ruby_xcalloc(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc2(), except it zero-fills the region before it returns.
Definition: gc.c:12815