static ctx_t *getctx (flux_t h) { int saved_errno; ctx_t *ctx = (ctx_t *)flux_aux_get (h, "req"); if (!ctx) { ctx = xzmalloc (sizeof (*ctx)); ctx->h = h; ctx->ping_requests = zhash_new (); ctx->clog_requests = zlist_new (); if (!ctx->clog_requests || !ctx->ping_requests) { saved_errno = ENOMEM; goto error; } if (flux_get_rank (h, &ctx->rank) < 0) { saved_errno = errno; goto error; } flux_aux_set (h, "req", ctx, freectx); } return ctx; error: freectx (ctx); errno = saved_errno; return NULL; }
static ctx_t *getctx (flux_t *h) { ctx_t *ctx = (ctx_t *)flux_aux_get (h, "flux::barrier"); if (!ctx) { ctx = xzmalloc (sizeof (*ctx)); if (!(ctx->barriers = zhash_new ())) { errno = ENOMEM; goto error; } if (flux_get_rank (h, &ctx->rank) < 0) { flux_log_error (h, "flux_get_rank"); goto error; } if (!(ctx->timer = flux_timer_watcher_create (flux_get_reactor (h), barrier_reduction_timeout_sec, 0., timeout_cb, ctx) )) { flux_log_error (h, "flux_timer_watacher_create"); goto error; } ctx->h = h; flux_aux_set (h, "flux::barrier", ctx, freectx); } return ctx; error: freectx (ctx); return NULL; }
int app_pa_phone(const struct cli_parsed *parsed, void *context) { PaCtx *ctx; if ((ctx = pa_phone_setup()) == NULL) return -1; freectx(ctx); return 0; }
static PaCtx * pa_phone_setup(void) { PaCtx *ctx; int err, i, srcerr; PaError err2; err = paNoError; err2 = 0; if ((ctx = calloc(1, sizeof(PaCtx))) == NULL) { WHY("Unable to allocate PA context"); err2 = 1; goto error; } /* Init mutex */ if (pthread_mutex_init(&ctx->mtx, NULL) != 0) { WHYF("Unable to init mutex: %s\n", strerror(errno)); err2 = 1; goto error; } /* Allocate FIFOs */ i = IN_FRAMES * 10 * sizeof(int16_t); printf("Allocating %d byte FIFOs\n", i); if ((ctx->incoming = fifo_alloc(i)) == NULL) { WHY("Unable to allocate incoming FIFO\n"); err2 = 1; goto error; } if ((ctx->incrate = fifo_alloc(i)) == NULL) { WHY("Unable to allocate incoming SRC FIFO\n"); err2 = 1; goto error; } if ((ctx->outgoing = fifo_alloc(i)) == NULL) { WHY("Unable to allocate outgoing FIFO\n"); err2 = 1; goto error; } /* Init sample rate converter */ if ((ctx->src = src_new(SRC_SINC_BEST_QUALITY, 1, &srcerr)) == NULL) { WHYF("Unable to init sample rate converter: %d\n", srcerr); err2 = 1; goto error; } /* Init echo canceller */ if ((ctx->echocan = echo_can_init(ECHO_LEN, ADAPT_MODE)) == NULL) { WHY("Unable to init echo canceller\n"); err2 = 1; goto error; } /* Init codec2 */ if ((ctx->codec2 = codec2_create()) == NULL) { WHY("Unable to init codec2\n"); err2 = 1; goto error; } /* Initialize Port Audio library */ if ((err = Pa_Initialize()) != paNoError) goto error; /* Open an audio I/O stream. */ if ((err = Pa_OpenDefaultStream(&ctx->stream, 1, /* input channels */ 1, /* output channels */ paInt16, SAMPLE_RATE, IN_FRAMES, /* frames per buffer */ patestCallback, &ctx)) != paNoError) goto error; /* Start stream */ if ((err = Pa_StartStream(ctx->stream)) != paNoError) goto error; /* Close down stream, PA, etc */ /* XXX: hangs in pthread_join on Ubuntu 10.04 */ #ifndef linux if ((err = Pa_StopStream(ctx->stream)) != paNoError) goto error; #endif /* Do stuff */ if ((err = Pa_CloseStream(ctx->stream)) != paNoError) goto error; error: Pa_Terminate(); /* Free things */ freectx(ctx); if (err != paNoError) WHYF("Port audio error: %s\n", Pa_GetErrorText(err)); return NULL; }
static sqlite_ctx_t *getctx (flux_t *h) { sqlite_ctx_t *ctx = (sqlite_ctx_t *)flux_aux_get (h, "flux::content-sqlite"); const char *dir; const char *tmp; bool cleanup = false; if (!ctx) { if (!(ctx = calloc (1, sizeof (*ctx)))) goto error; if (!(ctx->lzo_buf = calloc (1, lzo_buf_chunksize))) goto error; ctx->lzo_bufsize = lzo_buf_chunksize; ctx->h = h; if (!(ctx->hashfun = flux_attr_get (h, "content.hash"))) { flux_log_error (h, "content.hash"); goto error; } if (!(tmp = flux_attr_get (h, "content.blob-size-limit"))) { flux_log_error (h, "content.blob-size-limit"); goto error; } ctx->blob_size_limit = strtoul (tmp, NULL, 10); if (!(dir = flux_attr_get (h, "persist-directory"))) { if (!(dir = flux_attr_get (h, "broker.rundir"))) { flux_log_error (h, "broker.rundir"); goto error; } cleanup = true; } if (asprintf (&ctx->dbdir, "%s/content", dir) < 0) goto error; if (mkdir (ctx->dbdir, 0755) < 0 && errno != EEXIST) { flux_log_error (h, "mkdir %s", ctx->dbdir); goto error; } if (cleanup) cleanup_push_string (cleanup_directory_recursive, ctx->dbdir); if (asprintf (&ctx->dbfile, "%s/sqlite", ctx->dbdir) < 0) goto error; if (sqlite3_open (ctx->dbfile, &ctx->db) != SQLITE_OK) { flux_log_error (h, "sqlite3_open %s", ctx->dbfile); goto error; } if (sqlite3_exec (ctx->db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL) != SQLITE_OK || sqlite3_exec (ctx->db, "PRAGMA synchronous=OFF", NULL, NULL, NULL) != SQLITE_OK || sqlite3_exec (ctx->db, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, NULL) != SQLITE_OK) { log_sqlite_error (ctx, "setting sqlite pragmas"); goto error_sqlite; } if (sqlite3_exec (ctx->db, sql_create_table, NULL, NULL, NULL) != SQLITE_OK) { log_sqlite_error (ctx, "creating table"); goto error_sqlite; } if (sqlite3_prepare_v2 (ctx->db, sql_load, -1, &ctx->load_stmt, NULL) != SQLITE_OK) { log_sqlite_error (ctx, "preparing load stmt"); goto error_sqlite; } if (sqlite3_prepare_v2 (ctx->db, sql_store, -1, &ctx->store_stmt, NULL) != SQLITE_OK) { log_sqlite_error (ctx, "preparing store stmt"); goto error_sqlite; } if (sqlite3_prepare_v2 (ctx->db, sql_dump, -1, &ctx->dump_stmt, NULL) != SQLITE_OK) { log_sqlite_error (ctx, "preparing dump stmt"); goto error_sqlite; } if (flux_aux_set (h, "flux::content-sqlite", ctx, freectx) < 0) goto error; } return ctx; error_sqlite: set_errno_from_sqlite_error (ctx); error: freectx (ctx); return NULL; }