ext/nmatrix/nmatrix.h in nmatrix-0.0.9 vs ext/nmatrix/nmatrix.h in nmatrix-0.1.0.rc1

- old
+ new

@@ -7,12 +7,12 @@ // NMatrix was originally inspired by and derived from NArray, by // Masahiro Tanaka: http://narray.rubyforge.org // // == Copyright Information // -// SciRuby is Copyright (c) 2010 - 2013, Ruby Science Foundation -// NMatrix is Copyright (c) 2013, Ruby Science Foundation +// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation +// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation // // Please see LICENSE.txt for additional copyright notices. // // == Contributing // @@ -51,10 +51,12 @@ #include <sys/time.h> #include <sys/resource.h> #endif #endif +#include "nm_memory.h" + /* * Macros */ #define RUBY_ZERO INT2FIX(0) @@ -111,25 +113,31 @@ * * VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) { * return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv); * } */ + +//opening portion -- this allows unregistering any objects in use before returning + #define RETURN_SIZED_ENUMERATOR_PRE do { \ + if (!rb_block_given_p()) { + +//remaining portion #ifdef RUBY_2 #ifndef RETURN_SIZED_ENUMERATOR #undef RETURN_SIZED_ENUMERATOR // Ruby 2.0 and higher has rb_enumeratorize_with_size instead of rb_enumeratorize. // We want to support both in the simplest way possible. - #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \ - if (!rb_block_given_p()) \ - return rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()), (argc), (argv), (size_fn)); \ + #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) \ + return rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()), (argc), (argv), (size_fn)); \ + } \ } while (0) #endif #else #undef RETURN_SIZED_ENUMERATOR - #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \ - if (!rb_block_given_p()) \ - return rb_enumeratorize((obj), ID2SYM(rb_frame_this_func()), (argc), (argv)); \ + #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) \ + return rb_enumeratorize((obj), ID2SYM(rb_frame_this_func()), (argc), (argv)); \ + } \ } while (0) #endif #define NM_DECL_ENUM(enum_type, name) nm::enum_type name #define NM_DECL_STRUCT(type, name) type name; @@ -276,10 +284,22 @@ NM_DEF_STRUCT_PRE(NMATRIX); // struct NMATRIX { NM_DECL_ENUM(stype_t, stype); // stype_t stype; // Method of storage (csc, dense, etc). NM_DECL_STRUCT(STORAGE*, storage); // STORAGE* storage; // Pointer to storage struct. NM_DEF_STRUCT_POST(NMATRIX); // }; +/* Structs for dealing with VALUEs in use so that they don't get GC'd */ + +typedef struct __NM_GC_LL_NODE { + VALUE* val; + size_t n; + __NM_GC_LL_NODE* next; +} nm_gc_ll_node; + +typedef struct __NM_GC_HOLDER { + __NM_GC_LL_NODE* start; +} nm_gc_holder; + #define NM_MAX_RANK 15 #define UnwrapNMatrix(obj,var) Data_Get_Struct(obj, NMATRIX, var) #define NM_STORAGE(val) (NM_STRUCT(val)->storage) @@ -353,19 +373,24 @@ NM_DECL_ENUM(dtype_t, nm_dtype_guess(VALUE)); // (This is a function) NM_DECL_ENUM(dtype_t, nm_dtype_min(VALUE)); // Non-API functions needed by other cpp files. - NMATRIX* nm_create(nm::stype_t stype, STORAGE* storage); - NMATRIX* nm_cast_with_ctype_args(NMATRIX* self, nm::stype_t new_stype, nm::dtype_t new_dtype, void* init_ptr); + NMATRIX* nm_create(NM_DECL_ENUM(stype_t, stype), STORAGE* storage); + NMATRIX* nm_cast_with_ctype_args(NMATRIX* self, NM_DECL_ENUM(stype_t, new_stype), NM_DECL_ENUM(dtype_t, new_dtype), void* init_ptr); VALUE nm_cast(VALUE self, VALUE new_stype_symbol, VALUE new_dtype_symbol, VALUE init); void nm_mark(NMATRIX* mat); void nm_delete(NMATRIX* mat); void nm_delete_ref(NMATRIX* mat); - void nm_mark(NMATRIX* mat); void nm_register_values(VALUE* vals, size_t n); void nm_unregister_values(VALUE* vals, size_t n); - + void nm_register_value(VALUE& val); + void nm_unregister_value(VALUE& val); + void nm_register_storage(nm::stype_t stype, const STORAGE* storage); + void nm_unregister_storage(nm::stype_t stype, const STORAGE* storage); + void nm_register_nmatrix(NMATRIX* nmatrix); + void nm_unregister_nmatrix(NMATRIX* nmatrix); + void nm_completely_unregister_value(VALUE& val); #ifdef __cplusplus } #endif #endif // NMATRIX_H