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

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

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
49#ifndef USE_GC_MALLOC_OBJ_INFO_DETAILS
50# define USE_GC_MALLOC_OBJ_INFO_DETAILS 0
51#endif
52
53#define xmalloc ruby_xmalloc
54#define xmalloc2 ruby_xmalloc2
55#define xcalloc ruby_xcalloc
56#define xrealloc ruby_xrealloc
57#define xrealloc2 ruby_xrealloc2
58#define xfree ruby_xfree
59
60 RBIMPL_SYMBOL_EXPORT_BEGIN()
61
62 RBIMPL_ATTR_NODISCARD()
63 RBIMPL_ATTR_RESTRICT()
64 RBIMPL_ATTR_RETURNS_NONNULL()
65 RBIMPL_ATTR_ALLOC_SIZE((1))
86void *ruby_xmalloc(size_t size)
87 RBIMPL_ATTR_NOEXCEPT(malloc(size))
88;
89
90 RBIMPL_ATTR_NODISCARD()
91 RBIMPL_ATTR_RESTRICT()
92 RBIMPL_ATTR_RETURNS_NONNULL()
93 RBIMPL_ATTR_ALLOC_SIZE((1,2))
117void *ruby_xmalloc2(size_t nelems, size_t elemsiz)
118 RBIMPL_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
119;
120
121 RBIMPL_ATTR_NODISCARD()
122 RBIMPL_ATTR_RESTRICT()
123 RBIMPL_ATTR_RETURNS_NONNULL()
124 RBIMPL_ATTR_ALLOC_SIZE((1,2))
147void *ruby_xcalloc(size_t nelems, size_t elemsiz)
148 RBIMPL_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
149;
150
151 RBIMPL_ATTR_NODISCARD()
152 RBIMPL_ATTR_RETURNS_NONNULL()
153 RBIMPL_ATTR_ALLOC_SIZE((2))
193void *ruby_xrealloc(void *ptr, size_t newsiz)
194 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newsiz))
195;
196
197 RBIMPL_ATTR_NODISCARD()
198 RBIMPL_ATTR_RETURNS_NONNULL()
199 RBIMPL_ATTR_ALLOC_SIZE((2,3))
250void *ruby_xrealloc2(void *ptr, size_t newelems, size_t newsiz)
251 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
252;
253
282void ruby_xfree(void *ptr)
283 RBIMPL_ATTR_NOEXCEPT(free(ptr))
284;
285
286#if USE_GC_MALLOC_OBJ_INFO_DETAILS
287# define ruby_xmalloc(s1) ruby_xmalloc_with_location(s1, __FILE__, __LINE__)
288# define ruby_xmalloc2(s1, s2) ruby_xmalloc2_with_location(s1, s2, __FILE__, __LINE__)
289# define ruby_xcalloc(s1, s2) ruby_xcalloc_with_location(s1, s2, __FILE__, __LINE__)
290# define ruby_xrealloc(ptr, s1) ruby_xrealloc_with_location(ptr, s1, __FILE__, __LINE__)
291# define ruby_xrealloc2(ptr, s1, s2) ruby_xrealloc2_with_location(ptr, s1, s2, __FILE__, __LINE__)
292
293 RBIMPL_ATTR_NODISCARD()
294 RBIMPL_ATTR_RESTRICT()
295 RBIMPL_ATTR_RETURNS_NONNULL()
296 RBIMPL_ATTR_ALLOC_SIZE((1))
297void *ruby_xmalloc_body(size_t size)
298 RBIMPL_ATTR_NOEXCEPT(malloc(size))
299;
300
301 RBIMPL_ATTR_NODISCARD()
302 RBIMPL_ATTR_RESTRICT()
303 RBIMPL_ATTR_RETURNS_NONNULL()
304 RBIMPL_ATTR_ALLOC_SIZE((1,2))
305void *ruby_xmalloc2_body(size_t nelems, size_t elemsiz)
306 RBIMPL_ATTR_NOEXCEPT(malloc(nelems * elemsiz))
307;
308
309 RBIMPL_ATTR_NODISCARD()
310 RBIMPL_ATTR_RESTRICT()
311 RBIMPL_ATTR_RETURNS_NONNULL()
312 RBIMPL_ATTR_ALLOC_SIZE((1,2))
313void *ruby_xcalloc_body(size_t nelems, size_t elemsiz)
314 RBIMPL_ATTR_NOEXCEPT(calloc(nelems, elemsiz))
315;
316
317 RBIMPL_ATTR_NODISCARD()
318 RBIMPL_ATTR_RETURNS_NONNULL()
319 RBIMPL_ATTR_ALLOC_SIZE((2))
320void *ruby_xrealloc_body(void *ptr, size_t newsiz)
321 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newsiz))
322;
323
324 RBIMPL_ATTR_NODISCARD()
325 RBIMPL_ATTR_RETURNS_NONNULL()
326 RBIMPL_ATTR_ALLOC_SIZE((2,3))
327void *ruby_xrealloc2_body(void *ptr, size_t newelems, size_t newsiz)
328 RBIMPL_ATTR_NOEXCEPT(realloc(ptr, newelems * newsiz))
329;
330
331 RUBY_EXTERN const char *ruby_malloc_info_file;
332 RUBY_EXTERN int ruby_malloc_info_line;
333
334static inline void *
335ruby_xmalloc_with_location(size_t s, const char *file, int line)
336{
337 void *ptr;
338 ruby_malloc_info_file = file;
339 ruby_malloc_info_line = line;
340 ptr = ruby_xmalloc_body(s);
341 ruby_malloc_info_file = NULL;
342 return ptr;
343}
344
345static inline void *
346ruby_xmalloc2_with_location(size_t s1, size_t s2, const char *file, int line)
347{
348 void *ptr;
349 ruby_malloc_info_file = file;
350 ruby_malloc_info_line = line;
351 ptr = ruby_xmalloc2_body(s1, s2);
352 ruby_malloc_info_file = NULL;
353 return ptr;
354}
355
356static inline void *
357ruby_xcalloc_with_location(size_t s1, size_t s2, const char *file, int line)
358{
359 void *ptr;
360 ruby_malloc_info_file = file;
361 ruby_malloc_info_line = line;
362 ptr = ruby_xcalloc_body(s1, s2);
363 ruby_malloc_info_file = NULL;
364 return ptr;
365}
366
367static inline void *
368ruby_xrealloc_with_location(void *ptr, size_t s, const char *file, int line)
369{
370 void *rptr;
371 ruby_malloc_info_file = file;
372 ruby_malloc_info_line = line;
373 rptr = ruby_xrealloc_body(ptr, s);
374 ruby_malloc_info_file = NULL;
375 return rptr;
376}
377
378static inline void *
379ruby_xrealloc2_with_location(void *ptr, size_t s1, size_t s2, const char *file, int line)
380{
381 void *rptr;
382 ruby_malloc_info_file = file;
383 ruby_malloc_info_line = line;
384 rptr = ruby_xrealloc2_body(ptr, s1, s2);
385 ruby_malloc_info_file = NULL;
386 return rptr;
387}
388#endif
389
390 RBIMPL_SYMBOL_EXPORT_END()
391
392#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
dllexport.h
Tweaking visibility of C variables/functions.
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
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
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:66
restrict.h
Defines RBIMPL_ATTR_RESTRICT.
RBIMPL_ATTR_RESTRICT
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Definition restrict.h:41
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