2019-11-29 09:18:34 +03:00
|
|
|
#ifndef INTERNAL_STRUCT_H /* -*- C -*- */
|
|
|
|
#define INTERNAL_STRUCT_H
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Internal header for Struct.
|
|
|
|
* @author \@shyouhei
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-12-03 07:18:53 +03:00
|
|
|
#include "internal/gc.h" /* for RB_OBJ_WRITE */
|
|
|
|
#include "internal/stdbool.h" /* for bool */
|
|
|
|
#include "ruby/ruby.h" /* for struct RBasic */
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/gc.h" /* for RB_OBJ_WRITE */
|
2019-11-29 09:18:34 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RSTRUCT_EMBED_LEN_MAX = RVALUE_EMBED_LEN_MAX,
|
|
|
|
RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1),
|
|
|
|
RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
|
|
|
|
RSTRUCT_TRANSIENT_FLAG = FL_USER3,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RStruct {
|
|
|
|
struct RBasic basic;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
long len;
|
|
|
|
const VALUE *ptr;
|
|
|
|
} heap;
|
|
|
|
const VALUE ary[RSTRUCT_EMBED_LEN_MAX];
|
|
|
|
} as;
|
|
|
|
};
|
|
|
|
|
2019-12-03 07:18:53 +03:00
|
|
|
#define RSTRUCT(obj) (R_CAST(RStruct)(obj))
|
2019-12-04 11:16:30 +03:00
|
|
|
|
|
|
|
#ifdef RSTRUCT_LEN
|
|
|
|
# undef RSTRUCT_LEN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RSTRUCT_PTR
|
|
|
|
# undef RSTRUCT_PTR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RSTRUCT_SET
|
|
|
|
# undef RSTRUCT_SET
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RSTRUCT_GET
|
|
|
|
# undef RSTRUCT_GET
|
|
|
|
#endif
|
2019-11-29 09:18:34 +03:00
|
|
|
|
|
|
|
/* struct.c */
|
|
|
|
VALUE rb_struct_init_copy(VALUE copy, VALUE s);
|
|
|
|
VALUE rb_struct_lookup(VALUE s, VALUE idx);
|
|
|
|
VALUE rb_struct_s_keyword_init(VALUE klass);
|
2019-12-03 07:18:53 +03:00
|
|
|
static inline const VALUE *rb_struct_const_heap_ptr(VALUE st);
|
|
|
|
static inline bool RSTRUCT_TRANSIENT_P(VALUE st);
|
|
|
|
static inline void RSTRUCT_TRANSIENT_SET(VALUE st);
|
|
|
|
static inline void RSTRUCT_TRANSIENT_UNSET(VALUE st);
|
|
|
|
static inline long RSTRUCT_EMBED_LEN(VALUE st);
|
|
|
|
static inline long RSTRUCT_LEN(VALUE st);
|
|
|
|
static inline int RSTRUCT_LENINT(VALUE st);
|
|
|
|
static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st);
|
|
|
|
static inline void RSTRUCT_SET(VALUE st, long k, VALUE v);
|
|
|
|
static inline VALUE RSTRUCT_GET(VALUE st, long k);
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
RSTRUCT_TRANSIENT_P(VALUE st)
|
|
|
|
{
|
|
|
|
#if USE_TRANSIENT_HEAP
|
|
|
|
return FL_TEST_RAW(st, RSTRUCT_TRANSIENT_FLAG);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
RSTRUCT_TRANSIENT_SET(VALUE st)
|
|
|
|
{
|
|
|
|
#if USE_TRANSIENT_HEAP
|
|
|
|
FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
RSTRUCT_TRANSIENT_UNSET(VALUE st)
|
|
|
|
{
|
|
|
|
#if USE_TRANSIENT_HEAP
|
|
|
|
FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-29 09:18:34 +03:00
|
|
|
|
|
|
|
static inline long
|
2019-12-03 07:18:53 +03:00
|
|
|
RSTRUCT_EMBED_LEN(VALUE st)
|
2019-11-29 09:18:34 +03:00
|
|
|
{
|
2019-12-03 07:18:53 +03:00
|
|
|
long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
|
|
|
|
ret >>= RSTRUCT_EMBED_LEN_SHIFT;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline long
|
|
|
|
RSTRUCT_LEN(VALUE st)
|
|
|
|
{
|
|
|
|
if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
|
|
|
|
return RSTRUCT_EMBED_LEN(st);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return RSTRUCT(st)->as.heap.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
RSTRUCT_LENINT(VALUE st)
|
|
|
|
{
|
|
|
|
return rb_long2int(RSTRUCT_LEN(st));
|
2019-11-29 09:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const VALUE *
|
2019-12-03 07:18:53 +03:00
|
|
|
RSTRUCT_CONST_PTR(VALUE st)
|
|
|
|
{
|
|
|
|
const struct RStruct *p = RSTRUCT(st);
|
|
|
|
|
|
|
|
if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
|
|
|
|
return p->as.ary;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return p->as.heap.ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
RSTRUCT_SET(VALUE st, long k, VALUE v)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
RSTRUCT_GET(VALUE st, long k)
|
2019-11-29 09:18:34 +03:00
|
|
|
{
|
2019-12-03 07:18:53 +03:00
|
|
|
return RSTRUCT_CONST_PTR(st)[k];
|
2019-11-29 09:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const VALUE *
|
|
|
|
rb_struct_const_heap_ptr(VALUE st)
|
|
|
|
{
|
|
|
|
/* TODO: check embed on debug mode */
|
|
|
|
return RSTRUCT(st)->as.heap.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* INTERNAL_STRUCT_H */
|