Ruby: include/ruby/internal/memory.h File Reference

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

Data Structures | Macros | Functions
memory.h File Reference

Memory management stuff. More...

`#include "ruby/internal/config.h"`
`#include "ruby/internal/attr/alloc_size.h"`
`#include "ruby/internal/attr/const.h"`
`#include "ruby/internal/attr/constexpr.h"`
`#include "ruby/internal/attr/noalias.h"`
`#include "ruby/internal/attr/nonnull.h"`
`#include "ruby/internal/attr/noreturn.h"`
`#include "ruby/internal/attr/restrict.h"`
`#include "ruby/internal/attr/returns_nonnull.h"`
`#include "ruby/internal/cast.h"`
`#include "ruby/internal/dllexport.h"`
`#include "ruby/internal/has/builtin.h"`
`#include "ruby/internal/stdalign.h"`
`#include "ruby/internal/stdbool.h"`
`#include "ruby/internal/xmalloc.h"`
`#include "ruby/backward/2/limits.h"`
`#include "ruby/backward/2/long_long.h"`
`#include "ruby/backward/2/assume.h"`
`#include "ruby/defines.h"`

Go to the source code of this file.

## Data Structures
struct | rbimpl_size_mul_overflow_tag

## Macros
#define | DSIZE_T unsigned LONG_LONG
#define | RUBY_ALLOCV_LIMIT 1024
#define | RB_GC_GUARD(v)
#define | RB_ALLOC_N(type, n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
#define | RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
#define | RB_ZALLOC_N(type, n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
#define | RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
#define | RB_REALLOC_N(var, type, n) ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
#define | ALLOCA_N(type, n) RBIMPL_CAST((type *)(!(n) ? NULL : alloca(rbimpl_size_mul_or_raise(sizeof(type), (n)))))
#define | RB_ALLOCV(v, n)
#define | RB_ALLOCV_N(type, v, n)
#define | RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
#define | MEMZERO(p, type, n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
#define | MEMCPY(p1, p2, type, n) memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
#define | MEMMOVE(p1, p2, type, n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
#define | MEMCMP(p1, p2, type, n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
#define | ALLOC_N RB_ALLOC_N
#define | ALLOC RB_ALLOC
#define | ZALLOC_N RB_ZALLOC_N
#define | ZALLOC RB_ZALLOC
#define | REALLOC_N RB_REALLOC_N
#define | ALLOCV RB_ALLOCV
#define | ALLOCV_N RB_ALLOCV_N
#define | ALLOCV_END RB_ALLOCV_END
#define | memcpy ruby_nonempty_memcpy

## Functions
void * | alloca (size_t)
void * | rb_alloc_tmp_buffer (volatile VALUE *store, long len)
void * | rb_alloc_tmp_buffer_with_count (volatile VALUE *store, size_t len, size_t count)
void | rb_free_tmp_buffer (volatile VALUE *store)
void | ruby_malloc_size_overflow (size_t, size_t)

## Detailed Description

Memory management stuff.

Author
  Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.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 `RBIMPL` or `rbimpl` 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.

Definition in file memory.h.

## Macro Definition Documentation

## ◆ ALLOC

#define ALLOC RB_ALLOC

Definition at line 134 of file memory.h.

## ◆ ALLOC_N

#define ALLOC_N RB_ALLOC_N

Definition at line 133 of file memory.h.

## ◆ ALLOCA_N

#define ALLOCA_N | ( |  | type,
|  |  | n
| ) |  | RBIMPL_CAST((type *)(!(n) ? NULL : alloca(rbimpl_size_mul_or_raise(sizeof(type), (n)))))

Definition at line 112 of file memory.h.

## ◆ ALLOCV

#define ALLOCV RB_ALLOCV

Definition at line 138 of file memory.h.

## ◆ ALLOCV_END

#define ALLOCV_END RB_ALLOCV_END

Definition at line 140 of file memory.h.

## ◆ ALLOCV_N

#define ALLOCV_N RB_ALLOCV_N

Definition at line 139 of file memory.h.

## ◆ DSIZE_T

#define DSIZE_T unsigned LONG_LONG

Definition at line 81 of file memory.h.

## ◆ MEMCMP

#define MEMCMP | ( |  | p1,
|  |  | p2,
|  |  | type,
|  |  | n
| ) |  | memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))

Definition at line 131 of file memory.h.

## ◆ MEMCPY

#define MEMCPY | ( |  | p1,
|  |  | p2,
|  |  | type,
|  |  | n
| ) |  | memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))

Definition at line 129 of file memory.h.

## ◆ memcpy

#define memcpy ruby_nonempty_memcpy

Definition at line 278 of file memory.h.

## ◆ MEMMOVE

#define MEMMOVE | ( |  | p1,
|  |  | p2,
|  |  | type,
|  |  | n
| ) |  | memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))

Definition at line 130 of file memory.h.

## ◆ MEMZERO

#define MEMZERO | ( |  | p,
|  |  | type,
|  |  | n
| ) |  | memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))

Definition at line 128 of file memory.h.

## ◆ RB_ALLOC

#define RB_ALLOC | ( |  | type | ) | RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))

Definition at line 106 of file memory.h.

## ◆ RB_ALLOC_N

#define RB_ALLOC_N | ( |  | type,
|  |  | n
| ) |  | RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))

Definition at line 105 of file memory.h.

## ◆ RB_ALLOCV

#define RB_ALLOCV | ( |  | v,
|  |  | n
| ) |  |

Value:
((n) < RUBY_ALLOCV_LIMIT ? \
((v) = 0, !(n) ? NULL : alloca(n)) : \
rb_alloc_tmp_buffer(&(v), (n)))
alloca
#define alloca
Definition: ffi_common.h:27
rb_alloc_tmp_buffer
void * rb_alloc_tmp_buffer(volatile VALUE *store, long len)
Definition: gc.c:11007
RUBY_ALLOCV_LIMIT
#define RUBY_ALLOCV_LIMIT
Definition: memory.h:87
NULL
#define NULL
Definition: regenc.h:69

Definition at line 117 of file memory.h.

## ◆ RB_ALLOCV_END

#define RB_ALLOCV_END | ( |  | v | ) | rb_free_tmp_buffer(&(v))

Definition at line 126 of file memory.h.

## ◆ RB_ALLOCV_N

#define RB_ALLOCV_N | ( |  | type,
|  |  | v,
|  |  | n
| ) |  |

Value:
RBIMPL_CAST((type *) \
(((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
((v) = 0, !(n) ? NULL : alloca((n) * sizeof(type))) : \
rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))

Definition at line 121 of file memory.h.

## ◆ RB_GC_GUARD

#define RB_GC_GUARD | ( |  | v | ) |

Value:
(*__extension__ ({ \
volatile VALUE *rb_gc_guarded_ptr = &(v); \
__asm__("" : : "m"(rb_gc_guarded_ptr)); \
rb_gc_guarded_ptr; \
}))
VALUE
unsigned long VALUE
Definition: value.h:38

Definition at line 91 of file memory.h.

## ◆ RB_REALLOC_N

#define RB_REALLOC_N | ( |  | var,
|  |  | type,
|  |  | n
| ) |  | ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))

Definition at line 109 of file memory.h.

## ◆ RB_ZALLOC

#define RB_ZALLOC | ( |  | type | ) | (RB_ZALLOC_N(type, 1))

Definition at line 108 of file memory.h.

## ◆ RB_ZALLOC_N

#define RB_ZALLOC_N | ( |  | type,
|  |  | n
| ) |  | RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))

Definition at line 107 of file memory.h.

## ◆ REALLOC_N

#define REALLOC_N RB_REALLOC_N

Definition at line 137 of file memory.h.

## ◆ RUBY_ALLOCV_LIMIT

#define RUBY_ALLOCV_LIMIT 1024

Definition at line 87 of file memory.h.

## ◆ ZALLOC

#define ZALLOC RB_ZALLOC

Definition at line 136 of file memory.h.

## ◆ ZALLOC_N

#define ZALLOC_N RB_ZALLOC_N

Definition at line 135 of file memory.h.

## Function Documentation

## ◆ alloca()

void * alloca | ( | size_t |  | ) |

## ◆ rb_alloc_tmp_buffer()

void * rb_alloc_tmp_buffer | ( | volatile VALUE * | store,
|  | long | len
| ) |  |

Definition at line 11007 of file gc.c.

References cnt, len, rb_alloc_tmp_buffer_with_count(), rb_eArgError, rb_raise(), and roomof.

## ◆ rb_alloc_tmp_buffer_with_count()

void * rb_alloc_tmp_buffer_with_count | ( | volatile VALUE * | store,
|  | size_t | len,
|  | size_t | count
| ) |  |

Definition at line 10989 of file gc.c.

References rb_imemo_tmpbuf_struct::cnt, cnt, imemo, NULL, ptr, and rb_imemo_tmpbuf_struct::ptr.

Referenced by rb_alloc_tmp_buffer().

## ◆ rb_free_tmp_buffer()

void rb_free_tmp_buffer | ( | volatile VALUE * | store | ) |

Definition at line 11019 of file gc.c.

References ATOMIC_PTR_EXCHANGE, ATOMIC_VALUE_EXCHANGE, rb_imemo_tmpbuf_struct::cnt, ptr, rb_imemo_tmpbuf_struct::ptr, and ruby_xfree().

## ◆ ruby_malloc_size_overflow()

void ruby_malloc_size_overflow | ( | size_t | count,
|  | size_t | elsize
| ) |  |

Definition at line 10838 of file gc.c.

References count, PRIuSIZE, rb_eArgError, and rb_raise().