Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TypedData_Wrap_Struct() macro to suppress deprecated messages #359

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 71 additions & 22 deletions ext/ox/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@

#define MAX_DEPTH 128

static void builder_free(void *ptr);

static const rb_data_type_t ox_builder_type = {
"Ox/builder",
{
NULL,
builder_free,
NULL,
},
0,
0,
};

typedef struct _element {
char *name;
char buf[64];
Expand Down Expand Up @@ -353,14 +366,14 @@ static VALUE builder_new(int argc, VALUE *argv, VALUE self) {
init(b, 0, indent, buf_size);

if (rb_block_given_p()) {
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);

rb_yield(rb);
bclose(b);

return to_s(b);
} else {
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
}
}

Expand Down Expand Up @@ -408,12 +421,12 @@ static VALUE builder_file(int argc, VALUE *argv, VALUE self) {
init(b, fileno(f), indent, buf_size);

if (rb_block_given_p()) {
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
rb_yield(rb);
bclose(b);
return Qnil;
} else {
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
}
}

Expand Down Expand Up @@ -461,12 +474,12 @@ static VALUE builder_io(int argc, VALUE *argv, VALUE self) {
init(b, fd, indent, buf_size);

if (rb_block_given_p()) {
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
rb_yield(rb);
bclose(b);
return Qnil;
} else {
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
}
}

Expand All @@ -478,8 +491,9 @@ static VALUE builder_io(int argc, VALUE *argv, VALUE self) {
* - +options+ - (Hash) version or encoding
*/
static VALUE builder_instruct(int argc, VALUE *argv, VALUE self) {
Builder b = (Builder)DATA_PTR(self);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
i_am_a_child(b, false);
append_indent(b);
if (0 == argc) {
Expand Down Expand Up @@ -548,11 +562,13 @@ static VALUE builder_instruct(int argc, VALUE *argv, VALUE self) {
* - +attributes+ - (Hash) of the element
*/
static VALUE builder_element(int argc, VALUE *argv, VALUE self) {
Builder b = (Builder)DATA_PTR(self);
Builder b;
Element e;
const char *name;
long len;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

if (1 > argc) {
rb_raise(ox_arg_error_class, "missing element name");
}
Expand Down Expand Up @@ -608,10 +624,12 @@ static VALUE builder_element(int argc, VALUE *argv, VALUE self) {
* - +attributes+ - (Hash) of the element
*/
static VALUE builder_void_element(int argc, VALUE *argv, VALUE self) {
Builder b = (Builder)DATA_PTR(self);
Builder b;
const char *name;
long len;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

if (1 > argc) {
rb_raise(ox_arg_error_class, "missing element name");
}
Expand Down Expand Up @@ -649,8 +667,9 @@ static VALUE builder_void_element(int argc, VALUE *argv, VALUE self) {
* - +text+ - (String) contents of the comment
*/
static VALUE builder_comment(VALUE self, VALUE text) {
Builder b = (Builder)DATA_PTR(self);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
rb_check_type(text, T_STRING);
i_am_a_child(b, false);
append_indent(b);
Expand All @@ -671,8 +690,9 @@ static VALUE builder_comment(VALUE self, VALUE text) {
* - +text+ - (String) contents of the doctype
*/
static VALUE builder_doctype(VALUE self, VALUE text) {
Builder b = (Builder)DATA_PTR(self);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
rb_check_type(text, T_STRING);
i_am_a_child(b, false);
append_indent(b);
Expand All @@ -694,10 +714,12 @@ static VALUE builder_doctype(VALUE self, VALUE text) {
* - +strip_invalid_chars+ - [true|false] strips any characters invalid for XML, defaults to false
*/
static VALUE builder_text(int argc, VALUE *argv, VALUE self) {
Builder b = (Builder)DATA_PTR(self);
Builder b;
volatile VALUE v;
volatile VALUE strip_invalid_chars;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

if ((0 == argc) || (argc > 2)) {
rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..2)", argc);
}
Expand All @@ -721,13 +743,15 @@ static VALUE builder_text(int argc, VALUE *argv, VALUE self) {
* - +data+ - (String) contents of the CDATA element
*/
static VALUE builder_cdata(VALUE self, VALUE data) {
Builder b = (Builder)DATA_PTR(self);
Builder b;
volatile VALUE v = data;
const char *str;
const char *s;
const char *end;
int len;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

v = rb_String(v);
str = StringValuePtr(v);
len = (int)RSTRING_LEN(v);
Expand Down Expand Up @@ -761,13 +785,14 @@ static VALUE builder_cdata(VALUE self, VALUE data) {
* - +text+ - (String) contents to be added
*/
static VALUE builder_raw(VALUE self, VALUE text) {
Builder b = (Builder)DATA_PTR(self);
Builder b;
volatile VALUE v = text;
const char *str;
const char *s;
const char *end;
int len;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
v = rb_String(v);
str = StringValuePtr(v);
len = (int)RSTRING_LEN(v);
Expand All @@ -792,15 +817,21 @@ static VALUE builder_raw(VALUE self, VALUE text) {
* Returns the JSON document string in what ever state the construction is at.
*/
static VALUE builder_to_s(VALUE self) {
return to_s((Builder)DATA_PTR(self));
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
return to_s(b);
}

/* call-seq: line()
*
* Returns the current line in the output. The first line is line 1.
*/
static VALUE builder_line(VALUE self) {
return LONG2NUM(((Builder)DATA_PTR(self))->line);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
return LONG2NUM(b->line);
}

/* call-seq: column()
Expand All @@ -809,15 +840,21 @@ static VALUE builder_line(VALUE self) {
* column 1.
*/
static VALUE builder_column(VALUE self) {
return LONG2NUM(((Builder)DATA_PTR(self))->col);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
return LONG2NUM(b->col);
}

/* call-seq: indent()
*
* Returns the indentation level
*/
static VALUE builder_get_indent(VALUE self) {
return INT2NUM(((Builder)DATA_PTR(self))->indent);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
return INT2NUM(b->indent);
}

/* call-seq: indent=(indent)
Expand All @@ -827,11 +864,15 @@ static VALUE builder_get_indent(VALUE self) {
* - +indent+ (Fixnum) indentaion level, negative values excludes terminating newline
*/
static VALUE builder_set_indent(VALUE self, VALUE indent) {
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

if (rb_cInteger != rb_obj_class(indent)) {
rb_raise(ox_parse_error_class, "indent must be a fixnum.\n");
}

((Builder)DATA_PTR(self))->indent = NUM2INT(indent);
b->indent = NUM2INT(indent);
return Qnil;
}

Expand All @@ -840,15 +881,20 @@ static VALUE builder_set_indent(VALUE self, VALUE indent) {
* Returns the number of bytes written.
*/
static VALUE builder_pos(VALUE self) {
return LONG2NUM(((Builder)DATA_PTR(self))->pos);
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
return LONG2NUM(b->pos);
}

/* call-seq: pop()
*
* Closes the current element.
*/
static VALUE builder_pop(VALUE self) {
pop((Builder)DATA_PTR(self));
Builder b;
TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
pop(b);

return Qnil;
}
Expand All @@ -858,7 +904,10 @@ static VALUE builder_pop(VALUE self) {
* Closes the all elements and the document.
*/
static VALUE builder_close(VALUE self) {
bclose((Builder)DATA_PTR(self));
Builder b;

TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
bclose(b);

return Qnil;
}
Expand Down
17 changes: 15 additions & 2 deletions ext/ox/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ typedef struct _cache {
bool mark;
} *Cache;

const rb_data_type_t ox_cache_type = {
"Ox/Cache",
{
ox_cache_mark,
ox_cache_free,
NULL,
},
0,
0,
};

static uint64_t hash_calc(const uint8_t *key, size_t len) {
const uint8_t *end = key + len;
const uint8_t *endless = key + (len & 0xFFFFFFFC);
Expand Down Expand Up @@ -263,7 +274,8 @@ Cache ox_cache_create(size_t size, VALUE (*form)(const char *str, size_t len), b
return c;
}

void ox_cache_free(Cache c) {
void ox_cache_free(void *ptr) {
Cache c = (Cache)ptr;
uint64_t i;

for (i = 0; i < c->size; i++) {
Expand All @@ -279,7 +291,8 @@ void ox_cache_free(Cache c) {
free(c);
}

void ox_cache_mark(Cache c) {
void ox_cache_mark(void *ptr) {
Cache c = (Cache)ptr;
uint64_t i;

#if !HAVE_PTHREAD_MUTEX_INIT
Expand Down
6 changes: 4 additions & 2 deletions ext/ox/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

struct _cache;

extern const rb_data_type_t ox_cache_type;

extern struct _cache *ox_cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
extern void ox_cache_free(struct _cache *c);
extern void ox_cache_mark(struct _cache *c);
extern void ox_cache_free(void *ptr);
extern void ox_cache_mark(void *ptr);
extern VALUE ox_cache_intern(struct _cache *c, const char *key, size_t len, const char **keyp);

#endif /* OX_CACHE_H */
8 changes: 4 additions & 4 deletions ext/ox/intern.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ void ox_hash_init() {
#endif

ox_str_cache = ox_cache_create(0, form_str, true, false);
ox_str_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_str_cache);
ox_str_cache_obj = TypedData_Wrap_Struct(cache_class, &ox_cache_type, ox_str_cache);
rb_gc_register_address(&ox_str_cache_obj);

ox_sym_cache = ox_cache_create(0, form_sym, true, false);
ox_sym_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_sym_cache);
ox_sym_cache_obj = TypedData_Wrap_Struct(cache_class, &ox_cache_type, ox_sym_cache);
rb_gc_register_address(&ox_sym_cache_obj);

ox_attr_cache = ox_cache_create(0, form_attr, false, false);
ox_attr_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_attr_cache);
ox_attr_cache_obj = TypedData_Wrap_Struct(cache_class, &ox_cache_type, ox_attr_cache);
rb_gc_register_address(&ox_attr_cache_obj);

ox_id_cache = ox_cache_create(0, form_id, false, false);
ox_id_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_id_cache);
ox_id_cache_obj = TypedData_Wrap_Struct(cache_class, &ox_cache_type, ox_id_cache);
rb_gc_register_address(&ox_id_cache_obj);
}

Expand Down
14 changes: 13 additions & 1 deletion ext/ox/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ruby.h"
#include "special.h"

static void mark_pi_cb(void *ptr);
static void read_instruction(PInfo pi);
static void read_doctype(PInfo pi);
static void read_comment(PInfo pi);
Expand All @@ -33,6 +34,17 @@ static char *read_coded_chars(PInfo pi, char *text);
static void next_non_white(PInfo pi);
static int collapse_special(PInfo pi, char *str);

static const rb_data_type_t ox_wrap_type = {
"Object",
{
mark_pi_cb,
NULL,
NULL,
},
0,
0,
};

/* This XML parser is a single pass, destructive, callback parser. It is a
* single pass parse since it only make one pass over the characters in the
* XML document string. It is destructive because it re-uses the content of
Expand Down Expand Up @@ -140,7 +152,7 @@ ox_parse(char *xml, size_t len, ParseCallbacks pcb, char **endp, Options options
// initialize parse info
helper_stack_init(&pi.helpers);
// Protect against GC
wrap = Data_Wrap_Struct(rb_cObject, mark_pi_cb, NULL, &pi);
wrap = TypedData_Wrap_Struct(rb_cObject, &ox_wrap_type, &pi);

err_init(&pi.err);
pi.str = xml;
Expand Down
Loading