static pn_map_t *build_map(size_t capacity, float load_factor, ...) { pn_map_t *result = pn_map(PN_OBJECT, PN_OBJECT, capacity, load_factor); va_list ap; void *prev = NULL; va_start(ap, load_factor); int count = 0; while (true) { void *arg = va_arg(ap, void *); bool last = arg == END; if (arg == END) { arg = NULL; } if (count % 2) { pn_map_put(result, prev, arg); pn_class_decref(PN_OBJECT, prev); pn_class_decref(PN_OBJECT, arg); } else { prev = arg; } if (last) { break; } count++; } va_end(ap); return result; }
static void test_class(const pn_class_t *clazz, size_t size) { void *a = pn_class_new(clazz, size); void *b = pn_class_new(clazz, size); assert(!pn_class_equals(clazz, a, b)); assert(pn_class_equals(clazz, a, a)); assert(pn_class_equals(clazz, b, b)); assert(!pn_class_equals(clazz, a, NULL)); assert(!pn_class_equals(clazz, NULL, a)); int rca = pn_class_refcount(clazz, a); int rcb = pn_class_refcount(clazz, b); assert(rca == -1 || rca == 1); assert(rcb == -1 || rcb == 1); pn_class_incref(clazz, a); rca = pn_class_refcount(clazz, a); assert(rca == -1 || rca == 2); pn_class_decref(clazz, a); rca = pn_class_refcount(clazz, a); assert(rca == -1 || rca == 1); pn_class_free(clazz, a); pn_class_free(clazz, b); }
void pn_list_set(pn_list_t *list, int index, void *value) { assert(list); assert(list->size); void *old = list->elements[index % list->size]; pn_class_decref(list->clazz, old); list->elements[index % list->size] = value; pn_class_incref(list->clazz, value); }
static void pn_list_finalize(void *object) { assert(object); pn_list_t *list = (pn_list_t *) object; for (size_t i = 0; i < list->size; i++) { pn_class_decref(list->clazz, pn_list_get(list, i)); } free(list->elements); }
pn_subscription_t *pn_subscription(pn_messenger_t *messenger, const char *scheme, const char *host, const char *port) { static const pn_class_t clazz = PN_CLASS(pn_subscription); pn_subscription_t *sub = (pn_subscription_t *) pn_class_new(&clazz, sizeof(pn_subscription_t)); sub->messenger = messenger; pn_string_set(sub->scheme, scheme); pn_string_set(sub->host, host); pn_string_set(sub->port, port); pni_messenger_add_subscription(messenger, sub); pn_class_decref(PN_OBJECT, sub); return sub; }
void pn_list_del(pn_list_t *list, int index, int n) { assert(list); index %= list->size; for (int i = 0; i < n; i++) { pn_class_decref(list->clazz, list->elements[index + i]); } size_t slide = list->size - (index + n); for (size_t i = 0; i < slide; i++) { list->elements[index + i] = list->elements[index + n + i]; } list->size -= n; }
static pn_list_t *build_list(size_t capacity, ...) { pn_list_t *result = pn_list(PN_OBJECT, capacity); va_list ap; va_start(ap, capacity); while (true) { void *arg = va_arg(ap, void *); if (arg == END) { break; } pn_list_add(result, arg); pn_class_decref(PN_OBJECT, arg); } va_end(ap); return result; }
static void pn_event_finalize(pn_event_t *event) { // decref before adding to the free list if (event->clazz && event->context) { pn_class_decref(event->clazz, event->context); } pn_list_t *pool = event->pool; if (pool && pn_refcount(pool) > 1) { event->pool = NULL; event->type = PN_EVENT_NONE; event->clazz = NULL; event->context = NULL; event->next = NULL; pn_record_clear(event->attachments); pn_list_add(pool, event); } else { pn_decref(event->attachments); } pn_decref(pool); }