0byt3m1n1
Path:
/
home
/
mgatv524
/
php
/
ext
/
zeromq-0.0.2
/
extension
/
[
Home
]
File: context.c
//----------------------------------------------------------------------------- #include "context.h" #include "socket.h" //----------------------------------------------------------------------------- static VALUE context_allocate( VALUE klass ); static void context_free( void* instance ); static VALUE context_initialize( int argc_, VALUE* argv_, VALUE self ); static VALUE context_close( VALUE self ); //----------------------------------------------------------------------------- struct zeromq_context* zeromq_context_create() //-------------------------------------------- { struct zeromq_context* context = NULL; context = ALLOC( struct zeromq_context ); context->context = NULL; context->reference_count = 1; return context; } void zeromq_context_destroy( struct zeromq_context* context ) //----------------------------------------------------------- { assert( context ); if ( context->context != NULL ) { int rc = zmq_ctx_destroy( context->context ); assert( rc == 0 ); } xfree( context ); } void zeromq_context_retain( struct zeromq_context* context ) //---------------------------------------------------------- { assert( context ); assert( context->context ); assert( context->reference_count != 0 ); context->reference_count++; } void zeromq_context_release( struct zeromq_context* context ) //----------------------------------------------------------- { assert( context ); assert( context->context ); assert( context->reference_count != 0 ); context->reference_count--; if ( context->reference_count == 0 ) { zeromq_context_destroy( context ); } } //----------------------------------------------------------------------------- /* * Document-class: ZeroMQ::Context * * The ZeroMQ::Context class ... */ VALUE context_declare( VALUE zeromq_module ) //------------------------------------------ { VALUE klass = rb_define_class_under( zeromq_module, "Context", rb_cObject ); rb_define_singleton_method( klass, "default", context_default, 0 ); rb_define_alloc_func( klass, context_allocate ); rb_define_method( klass, "initialize", context_initialize, -1 ); rb_define_method( klass, "close", context_close, 0 ); return klass; } /* * @!method default * * The +default+ method ... */ VALUE context_default( VALUE self ) //--------------------------------- { static VALUE default_context = Qnil; if ( default_context == Qnil ) { default_context = rb_class_new_instance( 0, NULL, self ); } return default_context; } static VALUE context_allocate( VALUE class ) //------------------------------------------ { return rb_data_object_alloc( class, zeromq_context_create(), 0, context_free ); } static void context_free( void* instance ) //---------------------------------------- { zeromq_context_destroy( ( struct zeromq_context* )instance ); } static VALUE context_initialize( int arg_count, VALUE* args, VALUE self ) //------------------------------------------------------------------------ { VALUE context_options = Qnil; rb_scan_args( arg_count, args, "01", &context_options ); struct zeromq_context* context = NULL; Data_Get_Struct( self, void, context ); assert( context->context == NULL ); context->context = zmq_ctx_new(); if ( !context->context ) { rb_raise( exception_class, "%s", zmq_strerror( zmq_errno() ) ); return Qnil; } return self; } static VALUE context_close( VALUE self ) //-------------------------------------- { struct zeromq_context* context = NULL; Data_Get_Struct( self, void, context ); if ( context->context != NULL ) { zeromq_context_destroy( context ); } return Qnil; }