static void test_refcounting(int refs) { void *obj = pn_class_new(PN_OBJECT, 0); assert(pn_refcount(obj) == 1); for (int i = 0; i < refs; i++) { pn_incref(obj); assert(pn_refcount(obj) == i + 2); } assert(pn_refcount(obj) == refs + 1); for (int i = 0; i < refs; i++) { pn_decref(obj); assert(pn_refcount(obj) == refs - i); } assert(pn_refcount(obj) == 1); pn_free(obj); }
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); }
void pni_iocpdesc_map_push(iocpdesc_t *iocpd) { pn_hash_put(iocpd->iocp->iocpdesc_map, iocpd->socket, iocpd); pn_decref(iocpd); assert(pn_refcount(iocpd) == 1); }
static void test_map(void) { void *one = pn_class_new(PN_OBJECT, 0); void *two = pn_class_new(PN_OBJECT, 0); void *three = pn_class_new(PN_OBJECT, 0); pn_map_t *map = pn_map(PN_OBJECT, PN_OBJECT, 4, 0.75); assert(pn_map_size(map) == 0); pn_string_t *key = pn_string("key"); pn_string_t *dup = pn_string("key"); pn_string_t *key1 = pn_string("key1"); pn_string_t *key2 = pn_string("key2"); assert(!pn_map_put(map, key, one)); assert(pn_map_size(map) == 1); assert(!pn_map_put(map, key1, two)); assert(pn_map_size(map) == 2); assert(!pn_map_put(map, key2, three)); assert(pn_map_size(map) == 3); assert(pn_map_get(map, dup) == one); assert(!pn_map_put(map, dup, one)); assert(pn_map_size(map) == 3); assert(!pn_map_put(map, dup, two)); assert(pn_map_size(map) == 3); assert(pn_map_get(map, dup) == two); assert(pn_refcount(key) == 2); assert(pn_refcount(dup) == 1); assert(pn_refcount(key1) == 2); assert(pn_refcount(key2) == 2); assert(pn_refcount(one) == 1); assert(pn_refcount(two) == 3); assert(pn_refcount(three) == 2); pn_map_del(map, key1); assert(pn_map_size(map) == 2); assert(pn_refcount(key) == 2); assert(pn_refcount(dup) == 1); assert(pn_refcount(key1) == 1); assert(pn_refcount(key2) == 2); assert(pn_refcount(one) == 1); assert(pn_refcount(two) == 2); assert(pn_refcount(three) == 2); pn_decref(one); pn_decref(two); pn_decref(three); pn_decref(key); pn_decref(dup); pn_decref(key1); pn_decref(key2); pn_decref(map); }
static void test_list_refcount(size_t capacity) { void *one = pn_class_new(PN_OBJECT, 0); void *two = pn_class_new(PN_OBJECT, 0); void *three = pn_class_new(PN_OBJECT, 0); void *four = pn_class_new(PN_OBJECT, 0); pn_list_t *list = pn_list(PN_OBJECT, 0); assert(!pn_list_add(list, one)); assert(!pn_list_add(list, two)); assert(!pn_list_add(list, three)); assert(!pn_list_add(list, four)); assert(pn_list_get(list, 0) == one); assert(pn_list_get(list, 1) == two); assert(pn_list_get(list, 2) == three); assert(pn_list_get(list, 3) == four); assert(pn_list_size(list) == 4); assert(pn_refcount(one) == 2); assert(pn_refcount(two) == 2); assert(pn_refcount(three) == 2); assert(pn_refcount(four) == 2); pn_list_del(list, 1, 2); assert(pn_list_size(list) == 2); assert(pn_refcount(one) == 2); assert(pn_refcount(two) == 1); assert(pn_refcount(three) == 1); assert(pn_refcount(four) == 2); assert(pn_list_get(list, 0) == one); assert(pn_list_get(list, 1) == four); assert(!pn_list_add(list, one)); assert(pn_list_size(list) == 3); assert(pn_refcount(one) == 3); pn_decref(list); assert(pn_refcount(one) == 1); assert(pn_refcount(two) == 1); assert(pn_refcount(three) == 1); assert(pn_refcount(four) == 1); pn_decref(one); pn_decref(two); pn_decref(three); pn_decref(four); }