static client_t *create_client(server_t *s, mrp_transport_t *lt) { mrp_transport_t *t; client_t *c; c = mrp_allocz(sizeof(*c)); if (c != NULL) { mrp_list_init(&c->hook); c->s = s; c->id = s->next_id++; c->t = mrp_transport_accept(lt, c, MRP_TRANSPORT_REUSEADDR); if (c->t != NULL) { mrp_list_append(&s->clients, &c->hook); return c; } mrp_free(c); } else { t = mrp_transport_accept(lt, NULL, MRP_TRANSPORT_REUSEADDR); mrp_transport_destroy(t); } return NULL; }
mrp_mainloop_t *glib_mainloop_create(test_config_t *cfg) { glib_config_t *glib; mrp_mainloop_t *ml; glib = mrp_allocz(sizeof(*glib)); if (glib != NULL) { glib->gml = g_main_loop_new(NULL, FALSE); ml = mrp_mainloop_glib_get(glib->gml); if (ml != NULL) { cfg->glib = glib; cfg->ml = ml; return ml; } else { g_main_loop_unref(glib->gml); mrp_free(glib); } } return NULL; }
int clients_register_player(context_t *ctx, const char *name, const char *service, const char *object) { clients_t *clients; player_t *player; if (!ctx || !name || !(clients = ctx->clients)) return -1; if (!(player = mrp_allocz(sizeof(player_t)))) return -1; player->ctx = ctx; player->name = mrp_strdup(name); player->service = service ? mrp_strdup(service) : NULL; player->object = object ? mrp_strdup(object) : NULL; mrp_htbl_insert(clients->player.name, (void*)player->name, (void*)player); mrp_log_info("Mpris2 player '%s' (service '%s' object '%s') registered", player->name, player->service ? player->service : "none", player->object ? player->object : "none"); if (!clients->deflt) { clients->current = clients->deflt = player; mrp_log_info("'%s' become the default player", player->name); } return 0; }
int clients_create(context_t *ctx) { clients_t *clients; mrp_htbl_config_t cfg; if (!ctx) return -1; if (!(clients = mrp_allocz(sizeof(clients_t)))) return -1; memset(&cfg, 0, sizeof(cfg)); cfg.nentry = 10; cfg.comp = mrp_string_comp; cfg.hash = mrp_string_hash; cfg.free = player_free; cfg.nbucket = cfg.nentry; clients->player.name = mrp_htbl_create(&cfg); cfg.free = NULL; clients->player.addr = mrp_htbl_create(&cfg); clients->current = NULL; ctx->clients = clients; return 0; }
mrp_attr_t *mrp_resource_set_get_attribute_by_name( mrp_resource_set_t *resource_set, const char *resource_name, const char *attribute_name) { mrp_attr_t *attr = NULL, *attrs; uint32_t res_id; mrp_attr_t attr_buf[128]; uint32_t attr_idx = 0; memset(attr_buf, 0, sizeof(attr_buf)); res_id = mrp_resource_definition_get_resource_id_by_name(resource_name); attrs = mrp_resource_definition_read_all_attributes(res_id, 128, attr_buf); if (!attrs) return NULL; while (attrs->name != NULL) { if (strcmp(attrs->name, attribute_name) == 0) { mrp_attr_t *buf = mrp_allocz(sizeof(mrp_attr_t)); mrp_resource_set_read_attribute(resource_set, resource_name, attr_idx, buf); attr = buf; break; } attr_idx++; attrs++; } return attr; }
mrp_res_string_array_t * mrp_res_list_attribute_names(mrp_res_context_t *cx, const mrp_res_resource_t *res) { int i; mrp_res_string_array_t *ret; if (!cx || !res) return NULL; ret = mrp_allocz(sizeof(mrp_res_string_array_t)); if (!ret) return NULL; ret->num_strings = res->priv->num_attributes; ret->strings = mrp_allocz_array(const char *, res->priv->num_attributes); if (!ret->strings) { mrp_free(ret); return NULL; } for (i = 0; i < res->priv->num_attributes; i++) { ret->strings[i] = mrp_strdup(res->priv->attrs[i].name); if (!ret->strings[i]) { ret->num_strings = i; mrp_res_free_string_array(ret); return NULL; } } return ret; }
static client_t *create_client(const char *argv0) { client_t *c = mrp_allocz(sizeof(*c)); if (c != NULL) set_client_defaults(c, argv0); return c; }
mrp_res_attribute_t *mrp_attribute_array_dup(uint32_t dim, mrp_res_attribute_t *arr) { size_t size; uint32_t i; mrp_res_attribute_t *sattr, *dattr; mrp_res_attribute_t *dup; int err; size = (sizeof(mrp_res_attribute_t) * (dim + 1)); if (!(dup = mrp_allocz(size))) { err = ENOMEM; goto failed; } for (i = 0; i < dim; i++) { sattr = arr + i; dattr = dup + i; if (!(dattr->name = mrp_strdup(sattr->name))) { err = ENOMEM; goto failed; } switch ((dattr->type = sattr->type)) { case mrp_string: if (!(dattr->string = mrp_strdup(sattr->string))) { err = ENOMEM; goto failed; } break; case mrp_int32: dattr->integer = sattr->integer; break; case mrp_uint32: dattr->type = mrp_uint32; dattr->unsignd = sattr->unsignd; break; case mrp_double: dattr->type = mrp_double; dattr->floating = sattr->floating; break; default: err = EINVAL; goto failed; } } return dup; failed: mrp_attribute_array_free(dup, dim); errno = err; return NULL; }
static int basic_tests(int n) { void **ptrs; char buf[1024], *p; int i; mrp_mm_config(MRP_MM_DEBUG); ptrs = mrp_allocz(n * sizeof(*ptrs)); if (ptrs == NULL) fatal("Failed to allocate pointer table."); for (i = 0; i < n; i++) { snprintf(buf, sizeof(buf), "#%d: message number %d (0x%x)", i, i, i); p = ptrs[i] = mrp_strdup(buf); if (p != NULL) { if (!strcmp(buf, p)) { printf("'%s' was duplicated as '%s'\n", buf, p); } else { printf("'%s' was incorrectly duplicated as '%s'\n", buf, p); return FALSE; } } else { printf("failed to duplicate '%s'\n", buf); return FALSE; } } mrp_mm_check(stdout); for (i = 0; i < n; i += 2) { mrp_free(ptrs[i]); ptrs[i] = NULL; } mrp_mm_check(stdout); for (i = 0; i < n; i++) { mrp_free(ptrs[i]); ptrs[i] = NULL; } mrp_mm_check(stdout); mrp_free(ptrs); mrp_mm_check(stdout); return TRUE; }
static int cdpa_open(rnc_dev_t *dev, const char *device) { cdpa_t *cdpa; char *msg; mrp_debug("opening device '%s' with cdparanoia", device); msg = NULL; cdpa = dev->data = mrp_allocz(sizeof(*cdpa)); if (cdpa == NULL) goto fail; cdpa->cdio = cdio_open(device, DRIVER_LINUX); if (cdpa->cdio == NULL) { msg = "failed open device"; goto fail; } cdpa->cdda = cdio_cddap_identify_cdio(cdpa->cdio, CDDA_MESSAGE_LOGIT, &msg); if (cdpa->cdda == NULL) goto fail; cdio_cddap_free_messages(msg); msg = NULL; if (cdio_cddap_open(cdpa->cdda) < 0) { msg = "failed to CDDAP-open device"; goto fail; } cdpa->cdpa = cdio_paranoia_init(cdpa->cdda); if (cdpa->cdpa == NULL) { msg = "failed to initialize cd-paranoia"; goto fail; } cdpa->ctrack = -1; return 0; fail: if (cdpa != NULL && msg != NULL) cdpa->errmsg = mrp_strdup(msg); return -1; }
static rnc_buf_t *buf_alloc(const char *name, buf_api_t *api, size_t size) { rnc_buf_t *b; if ((b = mrp_allocz(size)) == NULL || (b->name = mrp_strdup(name)) == NULL) goto nomem; b->api = api; return b; nomem: mrp_free(b); return NULL; }
mrp_fragbuf_t *mrp_fragbuf_create(int framed, size_t pre_alloc) { mrp_fragbuf_t *buf; buf = mrp_allocz(sizeof(*buf)); if (buf != NULL) { if (fragbuf_init(buf, framed, pre_alloc)) return buf; mrp_free(buf); } return NULL; }
static int create_dbusif(srs_plugin_t *plugin) { dbusif_t *bus; mrp_debug("creating D-Bus client interface plugin"); bus = mrp_allocz(sizeof(*bus)); if (bus != NULL) { bus->self = plugin; plugin->plugin_data = bus; return TRUE; } else return FALSE; }
static playlist_t *playlist_dup(size_t nlist, playlist_t *lists) { playlist_t *dup = mrp_allocz(sizeof(playlist_t) * (nlist + 1)); playlist_t *src, *dst; size_t i; for (i = 0; i < nlist; i++) { src = lists + i; dst = dup + i; dst->id = mrp_strdup(src->id); dst->name = mrp_strdup(src->name); } return dup; }
mrp_attr_t *mrp_attribute_get_all_values(uint32_t nvalue, mrp_attr_t *values, uint32_t nattr, mrp_attr_def_t *defs, mrp_attr_value_t *attrs) { mrp_attr_def_t *adef; mrp_attr_t *vdst, *vend; uint32_t i; MRP_ASSERT((!nvalue || (nvalue > 0 && values)) && (!nattr || (nattr > 0 && defs)), "invalid argument"); if (nvalue) nvalue--; else { for (i = 0; i < nattr; i++) { if (!attrs || (attrs && (defs[i].access & MRP_RESOURCE_READ))) nvalue++; } if (!(values = mrp_allocz(sizeof(mrp_attr_t) * (nvalue + 1)))) { mrp_log_error("Memory alloc failure. Can't get attributes"); return NULL; } } vend = (vdst = values) + nvalue; for (i = 0; i < nattr && vdst < vend; i++) { adef = defs + i; if (!(adef->access && MRP_RESOURCE_READ)) continue; vdst->name = adef->name; vdst->type = adef->type; vdst->value = attrs ? attrs[i] : adef->value; vdst++; } memset(vdst, 0, sizeof(*vdst)); return values; }
pdp_t *create_domain_control(mrp_context_t *ctx, const char *extaddr, const char *intaddr, const char *wrtaddr, const char *httpdir) { pdp_t *pdp; pdp = mrp_allocz(sizeof(*pdp)); if (pdp != NULL) { pdp->ctx = ctx; pdp->address = extaddr; if (init_proxies(pdp) && init_tables(pdp)) { if (extaddr && *extaddr) pdp->extt = create_transport(pdp, extaddr); if (intaddr && *intaddr) pdp->intt = create_transport(pdp, intaddr); if (wrtaddr && *wrtaddr) { pdp->wrtt = create_transport(pdp, wrtaddr); if (pdp->wrtt != NULL) { const char *sm_opt = MRP_WSCK_OPT_SENDMODE; const char *sm_val = MRP_WSCK_SENDMODE_TEXT; const char *hd_opt = MRP_WSCK_OPT_HTTPDIR; const char *hd_val = httpdir; mrp_transport_setopt(pdp->wrtt, sm_opt, sm_val); mrp_transport_setopt(pdp->wrtt, hd_opt, hd_val); } } if ((!extaddr || !*extaddr || pdp->extt != NULL) && (!intaddr || !*intaddr || pdp->intt != NULL) && (!wrtaddr || !*wrtaddr || pdp->wrtt != NULL)) return pdp; } destroy_domain_control(pdp); } return NULL; }
static int create_wrtc(srs_plugin_t *plugin) { wrtc_t *wrtc; mrp_debug("creating WRT media client plugin"); wrtc = mrp_allocz(sizeof(*wrtc)); if (wrtc != NULL) { wrtc->self = plugin; wrtc->srs = plugin->srs; plugin->plugin_data = wrtc; return TRUE; } else return FALSE; }
static int create_input(srs_plugin_t *plugin) { context_t *ctx; struct udev *udev; mrp_log_info("creating input plugin"); if ((ctx = mrp_allocz(sizeof(context_t))) && (udev = udev_new())) { ctx->plugin = plugin; ctx->udev = udev; plugin->plugin_data = ctx; return TRUE; } return FALSE; }
int dbusif_create(context_t *ctx, mrp_mainloop_t *ml) { dbusif_t *dbusif = NULL; if (!(dbusif = mrp_allocz(sizeof(dbusif_t)))) return -1; dbusif->bustype = mrp_strdup("session"); dbusif->dbus = mrp_dbus_get(ml, dbusif->bustype, NULL); if (!dbusif->dbus) { mrp_log_error("mpris2 plugin: failed to obtain DBus"); mrp_free(dbusif); return -1; } ctx->dbusif = dbusif; return 0; }
static void set_client_defaults(client_t *c, const char *argv0) { int i; c->app_class = "player"; c->app_name = strrchr(argv0, '/'); if (c->app_name != NULL) c->app_name++; else c->app_name = argv0; c->commands = mrp_allocz(sizeof(default_commands)); c->ncommand = MRP_ARRAY_SIZE(default_commands); for (i = 0; i < c->ncommand; i++) { c->commands[i] = mrp_strdup(default_commands[i]); if (c->commands[i] == NULL) { print(c, "Failed to initialize default command set."); exit(1); } } }
static rnc_meta_t *tracklist_lookup(rnc_metadb_t *db, int track) { tracklist_t *t = (tracklist_t *)db->data; rnc_meta_t *m; if (t == NULL) goto invalid; track--; if (track < 0 || track >= t->ntrack) goto noentry; m = mrp_allocz(sizeof(*m)); if (m == NULL) goto nomem; m->track = track; m->title = mrp_strdup(t->tracks[track]); m->album = mrp_strdup(t->album); m->artist = mrp_strdup(t->artist); m->genre = mrp_strdup(t->genre); m->date.tm_year = t->year; return m; invalid: errno = EINVAL; return NULL; noentry: errno = ENOENT; nomem: return NULL; }
static int create_synth(srs_plugin_t *plugin) { srs_context_t *srs = plugin->srs; mrp_mainloop_t *ml = srs->ml; synth_t *synth; mrp_debug("creating simple voice plugin"); synth = mrp_allocz(sizeof(*synth)); if (synth != NULL) { synth->sigh = mrp_add_sighandler(ml, SIGCHLD, sighandler, synth); if (synth->sigh != NULL) { plugin->plugin_data = synth; return TRUE; } mrp_free(synth); } return FALSE; }
mrp_res_resource_set_t *resource_query_response(mrp_msg_t *msg, void **pcursor) { int status; uint32_t dim, i; resource_def_t rdef[RESOURCE_MAX]; mrp_res_attribute_t attrs[ATTRIBUTE_MAX + 1]; resource_def_t *src; mrp_res_resource_set_t *arr = NULL; if (!fetch_status(msg, pcursor, &status)) goto failed; if (status != 0) mrp_log_error("Resource query failed (%u): %s", status, strerror(status)); else { dim = 0; while (fetch_resource_name(msg, pcursor, &rdef[dim].name)) { int n_attrs = fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX+1, attrs); if (n_attrs < 0) goto failed; if (!(rdef[dim].attrs = mrp_attribute_array_dup(n_attrs, attrs))) { mrp_log_error("failed to duplicate attributes"); goto failed; } rdef[dim].num_attrs = n_attrs; dim++; } arr = mrp_allocz(sizeof(mrp_res_resource_set_t)); if (!arr) goto failed; arr->priv = mrp_allocz(sizeof(mrp_res_resource_set_private_t)); if (!arr->priv) goto failed; arr->application_class = NULL; arr->state = MRP_RES_RESOURCE_LOST; arr->priv->num_resources = dim; arr->priv->resources = mrp_allocz_array(mrp_res_resource_t *, dim); if (!arr->priv->resources) goto failed; for (i = 0; i < dim; i++) { src = rdef + i; arr->priv->resources[i] = mrp_allocz(sizeof(mrp_res_resource_t)); if (!arr->priv->resources[i]) { arr->priv->num_resources = i; goto failed; } arr->priv->resources[i]->priv = mrp_allocz(sizeof(mrp_res_resource_private_t)); if (!arr->priv->resources[i]->priv) { mrp_free(arr->priv->resources[i]); arr->priv->num_resources = i; goto failed; } priv_res_to_mrp_res(i, src, arr->priv->resources[i]); } } return arr; failed: mrp_log_error("malformed reply to resource query"); free_resource_set(arr); return NULL; }
static tracklist_t *tracklist_parse(const char *path) { #define CHECK_STRTAG(_tag, _member) \ if (!strcasecmp(tag, _tag)) { \ mrp_free(t->_member); \ t->_member = mrp_strdup(value); \ continue; \ } #define CHECK_INTTAG(_tag, _member) \ if (!strcasecmp(tag, _tag)) { \ t->_member = (int)strtoul(value, &e, 10); \ if (e && *e) \ goto invalid; \ continue; \ } tracklist_t *t; char buf[16 * 1024], *tag, *sep, *value, *p, *e; int fd, n; fd = open(path, O_RDONLY); if (fd < 0) goto failed; n = read(fd, buf, sizeof(buf) - 1); close(fd); if (n < 0) goto failed; if (n >= (int)sizeof(buf) - 1) goto nospace; buf[n] = '\0'; t = mrp_allocz(sizeof(*t)); if (t == NULL) goto nomem; p = buf; while (p && *p) { while (*p == ' ' || *p == '\t') p++; if (*p == '\n') { p++; continue; } tag = p; while (*p && *p != ':' && *p != '.' && *p != '\n') p++; if (*p == '\n') goto invalid; sep = p; p++; while (*p == ' ' || *p == '\t') p++; value = p; while (*p && *p != '\n') p++; if (p != '\0') *p++ = '\0'; e = p - 1; while (e > value && (*e == ' ' || *e == '\t')) *e-- = '\0'; if (*sep == ':') { *sep = '\0'; CHECK_STRTAG("Album" , album); CHECK_STRTAG("Artist", artist); CHECK_STRTAG("Genre" , genre); CHECK_INTTAG("Year" , year); mrp_log_warning("Ignoring unknown tag '%s'...", tag); continue; } if (*sep == '.') { *sep = '\0'; n = strtoul(tag, &e, 10); if (e && *e) goto invalid; if (n < 1 || n > 99) /* max. CDDA tracks */ goto invalid; if (n > t->ntrack) { if (!mrp_reallocz(t->tracks, t->ntrack, n)) goto nomem; t->ntrack = n; } mrp_free(t->tracks[n - 1]); t->tracks[n - 1] = mrp_strdup(value); } } return t; nospace: errno = ENOBUFS; failed: nomem: return NULL; invalid: errno = EINVAL; tracklist_free(t); return NULL; }
int options_create(context_t *ctx, int ncfg, srs_cfg_t *cfgs) { options_t *opts; srs_cfg_t *cfg; const char *key; const char *value; char *e; bool verbose; int i; int sts; size_t pfxlen; size_t ndec; options_decoder_t *decs; char buf[65536]; if (!ctx) { errno = EINVAL; return -1; } pfxlen = strlen(SPHINX_PREFIX); if (!(opts = mrp_allocz(sizeof(options_t))) || !(decs = mrp_allocz(sizeof(options_decoder_t)))) return -1; ndec = 1; decs->name = mrp_strdup("default"); decs->hmm = mrp_strdup(DEFAULT_HMM); decs->lm = mrp_strdup(DEFAULT_LM); decs->dict = mrp_strdup(DEFAULT_DICT); decs->fsg = NULL; opts->srcnam = NULL; opts->audio = NULL; opts->logfn = mrp_strdup("/dev/null"); opts->topn = 12; opts->rate = 16000; opts->silen = 1.0; verbose = false; sts = 0; for (i = 0; i < ncfg; i++) { cfg = cfgs + i; key = cfg->key + pfxlen; value = cfg->value; if (!strncmp(cfg->key, SPHINX_PREFIX, pfxlen)) { switch (key[0]) { case 'd': if (!strcmp(key, "dict")) { mrp_free((void *)decs->dict); decs->dict = mrp_strdup(value); } else if (!strncmp(key, "decoder", 7)) { add_decoder(ncfg, cfgs, value, &ndec, &decs); } break; case 'f': if (!strcmp(key, "fsg")) { mrp_free((void *)decs->fsg); decs->fsg = mrp_strdup(value); } break; case 'h': if (!strcmp(key, "hmm")) { mrp_free((void *)decs->hmm); decs->hmm = mrp_strdup(value); } break; case 'l': if (!strcmp(key, "lm")) { mrp_free((void *)decs->lm); decs->lm = mrp_strdup(value); } break; case 'p': if (!strcmp(key, "pulsesrc")) { mrp_free((void *)opts->srcnam); opts->srcnam = mrp_strdup(value); } break; case 'r': if (!strcmp(key, "record")) { mrp_free((void *)opts->audio); opts->audio = mrp_strdup(value); } break; case 's': if (!strcmp(key, "samplerate")) { opts->rate = strtoul(value, &e, 10); if (e[0] || e == value || opts->rate < 8000 || opts->rate > 4800) { mrp_log_error("invalid value %s for samplerate",value); sts = -1; } } break; case 't': if (!strcmp(key, "topn")) { opts->topn = strtoul(value, &e, 10); if (e[0] || e == value || opts->topn < 1 || opts->topn > 100) { mrp_log_error("invalid value %s for topn", value); sts = -1; } } break; default: // cfg->used = FALSE; break; } /* switch key */ } } /* for cfg */ opts->ndec = ndec; opts->decs = decs; if (sts == 0) { print_decoders(opts->ndec, opts->decs, sizeof(buf), buf); mrp_log_info("topn: %u\n" " pulseaudio source name: %s\n" " sample rate: %.1lf KHz\n" " audio recording file: %s\n" "%s", opts->topn, opts->srcnam ? opts->srcnam : "<default-source>", (double)opts->rate / 1000.0, opts->audio, buf); } ctx->opts = opts; ctx->verbose = verbose; return sts; }
static int glib_pump_setup(mrp_mainloop_t *ml) { static mrp_subloop_ops_t glib_ops = { .prepare = glib_prepare, .query = glib_query, .check = glib_check, .dispatch = glib_dispatch }; GMainContext *main_context; GMainLoop *main_loop; if (sizeof(GPollFD) != sizeof(struct pollfd)) { mrp_log_error("sizeof(GPollFD:%zd) != sizeof(struct pollfd:%zd)\n", sizeof(GPollFD), sizeof(struct pollfd)); return FALSE; } main_context = NULL; main_loop = NULL; glib_glue = NULL; if ((main_context = g_main_context_default()) != NULL && (main_loop = g_main_loop_new(main_context, FALSE)) != NULL && (glib_glue = mrp_allocz(sizeof(*glib_glue))) != NULL) { glib_glue->mc = main_context; glib_glue->ml = main_loop; glib_glue->sl = mrp_add_subloop(ml, &glib_ops, glib_glue); { const char *domains[] = { "GLib", "GLib-GObject", "GLib-GIO", "GLib-GRegex", "GThread", "GModule", NULL }, *d; int i; for (d = domains[i=0]; d; d = domains[++i]) g_log_set_handler(d, G_LOG_LEVEL_MASK, glib_log, NULL); g_log_set_handler(NULL, G_LOG_LEVEL_MASK, glib_log, NULL); } if (glib_glue->sl != NULL) return TRUE; else mrp_log_error("glib-pump failed to register subloop."); } /* all of these handle a NULL argument gracefully... */ g_main_loop_unref(main_loop); g_main_context_unref(main_context); mrp_free(glib_glue); glib_glue = NULL; return FALSE; }
resctl_t *resctl_init(void) { resctl_t *ctl; mrp_resource_client_t *client; mrp_resource_set_t *set; const char *zone = "driver"; const char *play = "audio_playback"; const char *rec = "audio_recording"; const char *cls = "phone"; bool ar = false; uint32_t prio = 1; /* what is this ? */ ctl = NULL; client = NULL; set = NULL; ctl = mrp_allocz(sizeof(*ctl)); if (ctl == NULL) { mrp_log_error("Failed to initialize telephony resource control."); goto fail; } client = mrp_resource_client_create("telephony", ctl); if (client == NULL) { mrp_log_error("Failed to create telephony resource client."); goto fail; } set = mrp_resource_set_create(client, ar, prio, event_cb, ctl); if (set == NULL) { mrp_log_error("Failed to create telephony resource set."); goto fail; } if (mrp_resource_set_add_resource(set, play, false, NULL, true) < 0 || mrp_resource_set_add_resource(set, rec , false, NULL, true) < 0) { mrp_log_error("Failed to initialize telephony resource set."); goto fail; } if (mrp_application_class_add_resource_set(cls, zone, set, 0) != 0) { mrp_log_error("Failed to assign telephony resource set with class."); goto fail; } ctl->client = client; ctl->set = set; ctl->seqno = 1; return ctl; fail: if (set != NULL) mrp_resource_set_destroy(set); if (client != NULL) mrp_resource_client_destroy(client); mrp_free(ctl); return NULL; }
static int pool_tests(void) { mrp_objpool_config_t cfg; mrp_objpool_t *pool; obj_t **ptrs; int limit, prealloc, i, max; int success; limit = 0; prealloc = 512; max = 8382; ptrs = mrp_allocz(max * sizeof(obj_t)); if (ptrs == NULL) { error("Failed to allocate check pointer table."); return FALSE; } cfg.name = "test pool"; cfg.limit = limit; cfg.objsize = sizeof(obj_t); cfg.prealloc = prealloc; cfg.setup = obj_setup; cfg.cleanup = obj_cleanup; cfg.poison = POISON; cfg.flags = MRP_OBJPOOL_FLAG_POISON; info("Creating object pool..."); pool = mrp_objpool_create(&cfg); if (pool == NULL) { error("Failed to create test object pool."); return FALSE; } info("Allocating objects..."); for (i = 0; i < max; i++) { ptrs[i] = mrp_objpool_alloc(pool); if (ptrs[i] == NULL) { error("Failed to allocate test object #%d.", i); success = FALSE; goto out; } if (!obj_check(ptrs[i], TRUE)) { error("Object check failed for %p.", ptrs[i]); success = FALSE; } } info("Freeing objects..."); for (i = 0; i < max; i += 2) { mrp_objpool_free(ptrs[i]); obj_check(ptrs[i], FALSE); ptrs[i] = NULL; } info("Reallocating objects..."); for (i = 0; i < max; i += 2) { ptrs[i] = mrp_objpool_alloc(pool); if (ptrs[i] == NULL) { error("Failed to re-allocate test object #%d.", i); success = FALSE; goto out; } if (!obj_check(ptrs[i], TRUE)) { error("Object check failed for %p.", ptrs[i]); success = FALSE; } } info("Freeing objects..."); for (i = 0; i < max; i++) { mrp_objpool_free(ptrs[i]); ptrs[i] = NULL; } info("Reallocating again objects..."); for (i = 0; i < max; i++) { ptrs[i] = mrp_objpool_alloc(pool); if (ptrs[i] == NULL) { error("Failed to re-allocate test object #%d.", i); success = FALSE; goto out; } if (!obj_check(ptrs[i], TRUE)) { error("Object check failed for %p.", ptrs[i]); success = FALSE; } } out: mrp_free(ptrs); info("Destroying object pool..."); mrp_objpool_destroy(pool); return success; }