Beispiel #1
0
int mk_iov_realloc(struct mk_iov *mk_io, int new_size)
{
    void **new_buf;
    struct iovec *new_io;

    new_io  = mk_mem_realloc(mk_io->io, sizeof(struct iovec) * new_size) ;
    new_buf = mk_mem_realloc(mk_io->buf_to_free, sizeof(void *) * new_size);

    if (!new_io || !new_buf) {
        MK_TRACE("could not reallocate IOV");
        mk_mem_free(new_io);
        mk_mem_free(new_buf);
        return -1;
    }

    /* update data */
    mk_io->io = new_io;
    mk_io->buf_to_free = new_buf;

    mk_io->size = new_size;

    return 0;
}
Beispiel #2
0
/*
 * @METHOD_NAME: printf
 * @METHOD_DESC: It format and enqueue a buffer of data to be send to the HTTP client as response body.
 * The buffer allocated is freed internally when the data is flushed completely.
 * @METHOD_PARAM: dr the request context information hold by a duda_request_t type
 * @METHOD_PARAM: format Specifies the subsequent arguments to be formatted
 * @METHOD_RETURN: Upon successful completion it returns 0, on error returns -1.
 */
int duda_response_printf(duda_request_t *dr, const char *format, ...)
{
    int ret;
    int n, size = 128;
    char *p, *np;
    va_list ap;

    if ((p = mk_mem_alloc(size)) == NULL) {
        return -1;
    }

    /* Try to print in the allocated space. */
    while (1) {
        va_start(ap, format);
        n = vsnprintf(p, size, format, ap);

        /* If that worked, return the string. */
        if (n > -1 && n < size)
            break;

        size *= 2;  /* twice the old size */
        if ((np = mk_mem_realloc(p, size)) == NULL) {
            mk_mem_free(p);
            return - 1;
        } else {
            p = np;
        }
    }
    va_end(ap);

    ret = _print(dr, p, n, MK_TRUE);
    if (ret == -1) {
        mk_mem_free(p);
    }

    return ret;
}