Example #1
0
int do_block(object player) {
   if (sizeof(balance_check(player)) != 2) {
      write("You found not balance yet, hero.");
      return 1;
   }

   return 0;
}
Example #2
0
int do_set_parent( dict *d, location *loc, void *key, void *val, set_spec *spec, rstat *stat ) {
    node *new_node = do_set_create( d, loc->epoch, key, val, CREATE_NODE, spec );
    if ( new_node == NULL ) {
        *stat = rstat_mem;
        return 0; //do not try again
    }

    while ( 1 ) {
        node * volatile *branch = NULL;
        uint8_t error = 0;
        int dir = d->methods.cmp( loc->set->settings.meta, key, loc->parent->key->value, &error);
        if (error) {
            *stat = error( 1, 0, error + 100, "Application error in compare method" );
            return 0;
        }

        switch( dir ) {
            case -1:
                branch = &(loc->parent->left);
            break;
            case 1:
                branch = &(loc->parent->right);
            break;
            case 0:
                dispose( d, (trash *)new_node );
                *stat = error( 1, 0, DICT_UNKNOWN, "This should not be possible unless a nodes key has changed, which is not permitted." );
                return 0;
            break;
            default:
                dispose( d, (trash *)new_node );
                *stat = error( 1, 0, DICT_API_MISUSE, "The Compare method must return 1, 0, or -1" );
                return 0;
            break;
        }

        // Insert the new node!
        if ( __sync_bool_compare_and_swap( branch, NULL, new_node )) {
            size_t count = __sync_add_and_fetch( &(loc->slot->item_count), 1 );
            __sync_add_and_fetch( &(d->item_count), 1 );

            loc->node  = new_node;
            loc->usref = loc->node->usref;
            loc->sref  = loc->usref->sref;
            if ( blocked_null( loc->sref )) loc->sref = NULL;
            loc->xtrn  = loc->sref ? loc->sref->xtrn : NULL;

            loc->height++;

            *stat = balance_check( d, loc, count );

            return 0;
        }

        // Prepare to try again...
        *stat = locate_key( d, key, &loc );
        if ( stat->num ) {
            dispose( d, (trash *)new_node );
            return 0;
        }

        // Matching node has been inserted :-( all that work for nothing,
        // retry...
        if ( loc->node ) {
            dispose( d, (trash *)new_node );
            return 1;
        }
    }
}