Esempio n. 1
0
static xml_element *
xmlElementNew(xmlrpc_env * const envP,
              const char * const name) {
/*----------------------------------------------------------------------------
  Create a new xml_element. This routine isn't exported, because the
  arguments are implementation-dependent.
-----------------------------------------------------------------------------*/

    xml_element * retval;
    bool nameIsValid;
    bool cdataIsValid;
    bool childrenAreValid;

    XMLRPC_ASSERT_ENV_OK(envP);
    assert(name != NULL);

    /* Set up our error-handling preconditions. */
    retval = NULL;
    nameIsValid = cdataIsValid = childrenAreValid = false;

    MALLOCVAR(retval);
    XMLRPC_FAIL_IF_NULL(retval, envP, XMLRPC_INTERNAL_ERROR,
                        "Couldn't allocate memory for XML element");

    retval->parentP = NULL;
    
    /* Copy over the element name. */
    retval->name = strdup(name);
    XMLRPC_FAIL_IF_NULL(retval->name, envP, XMLRPC_INTERNAL_ERROR,
                        "Couldn't allocate memory for XML element");
    nameIsValid = true;

    /* Initialize a block to hold our CDATA. */
    XMLRPC_TYPED_MEM_BLOCK_INIT(char, envP, &retval->cdata, 0);
    XMLRPC_FAIL_IF_FAULT(envP);
    cdataIsValid = true;

    /* Initialize a block to hold our child elements. */
    XMLRPC_TYPED_MEM_BLOCK_INIT(xml_element *, envP, &retval->children, 0);
    XMLRPC_FAIL_IF_FAULT(envP);
    childrenAreValid = true;

cleanup:
    if (envP->fault_occurred) {
        if (retval) {
            if (nameIsValid)
                xmlrpc_strfree(retval->name);
            if (cdataIsValid)
                xmlrpc_mem_block_clean(&retval->cdata);
            if (childrenAreValid)
                xmlrpc_mem_block_clean(&retval->children);
            free(retval);
        }
        retval = NULL;
    }
    return retval;
}
xmlrpc_registry *
xmlrpc_registry_new(xmlrpc_env *env) {

    xmlrpc_value *methods;
    xmlrpc_registry *registry;
    int registry_valid;
    
    XMLRPC_ASSERT_ENV_OK(env);
    
    /* Error-handling preconditions. */
    methods = NULL;
    registry = NULL;
    registry_valid = 0;

    /* Allocate our memory. */
    methods = xmlrpc_struct_new(env);
    XMLRPC_FAIL_IF_FAULT(env);
    registry = (xmlrpc_registry*) malloc(sizeof(xmlrpc_registry));
    XMLRPC_FAIL_IF_NULL(registry, env, XMLRPC_INTERNAL_ERROR,
                        "Could not allocate memory for registry");
    
    /* Set everything up. */
    registry->_introspection_enabled = 1;
    registry->_methods = methods;
    registry->_default_method = NULL;
    registry->_preinvoke_method = NULL;
    registry_valid = 1;

    /* Install our system methods. */
    install_system_methods(env, registry);
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:
    if (env->fault_occurred) {
        if (registry_valid) {
            xmlrpc_registry_free(registry);
        } else {
            if (methods)
                xmlrpc_DECREF(methods);
            if (registry)
                free(registry);
        }
        return NULL;
    }
    return registry;
}
Esempio n. 3
0
/* Resize an xmlrpc_mem_block, preserving as much of the contents as
   possible.
*/
void 
xmlrpc_mem_block_resize (xmlrpc_env *       const envP,
                         xmlrpc_mem_block * const blockP,
                         size_t             const size) {

    size_t proposed_alloc;
    void* new_block;

    XMLRPC_ASSERT_ENV_OK(envP);
    XMLRPC_ASSERT(blockP != NULL);

    /* Check to see if we already have enough space. Maybe we'll get lucky. */
    if (size <= blockP->_allocated) {
        blockP->_size = size;
        return;
    }

    /* Calculate a new allocation size. */
#ifdef EFENCE
    proposed_alloc = size;
#else
    proposed_alloc = blockP->_allocated;
    while (proposed_alloc < size && proposed_alloc <= BLOCK_ALLOC_MAX)
        proposed_alloc *= 2;
#endif /* DEBUG_MEM_ERRORS */

    if (proposed_alloc > BLOCK_ALLOC_MAX)
        XMLRPC_FAIL(envP, XMLRPC_INTERNAL_ERROR, "Memory block too large");

    /* Allocate our new memory block. */
    new_block = (void*) malloc(proposed_alloc);
    XMLRPC_FAIL_IF_NULL(new_block, envP, XMLRPC_INTERNAL_ERROR,
                        "Can't resize memory block");

    /* Copy over our data and update the xmlrpc_mem_block struct. */
    memcpy(new_block, blockP->_block, blockP->_size);
    free(blockP->_block);
    blockP->_block     = new_block;
    blockP->_size      = size;
    blockP->_allocated = proposed_alloc;

 cleanup:
    return;
}