Skip to content
Snippets Groups Projects
Commit 243d4ac1 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Implemented reference counting to knot_zone_t.

refs #1976
parent 1a253891
No related branches found
No related tags found
No related merge requests found
......@@ -122,7 +122,6 @@ static int zonedata_destroy(knot_zone_t *zone)
free(zd);
/* Invalidate. */
zone->dtor = 0;
zone->data = 0;
return KNOTD_EOK;
......@@ -181,7 +180,7 @@ static int zonedata_init(conf_zone_t *cfg, knot_zone_t *zone)
/* Set and install destructor. */
zone->data = zd;
zone->dtor = zonedata_destroy;
knot_zone_set_dtor(zone, zonedata_destroy);
/* Set zonefile SOA serial. */
const knot_rrset_t *soa_rrs = 0;
......
......@@ -59,7 +59,7 @@ typedef struct zonedata_t
/*! \brief Zone data lock for exclusive access. */
pthread_mutex_t lock;
/*! \brief Access control lists. */
acl_t *xfr_out; /*!< ACL for xfr-out.*/
acl_t *notify_in; /*!< ACL for notify-in.*/
......
......@@ -33,6 +33,12 @@
#include "hash/cuckoo-hash-table.h"
#include "zone/zone-contents.h"
/*! \brief Adaptor for knot_zone_deep_free() */
static void knot_zone_dtor(struct ref_t *p) {
knot_zone_t *z = (knot_zone_t *)p;
knot_zone_deep_free(&z, 0);
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
......@@ -55,6 +61,13 @@ knot_zone_t *knot_zone_new_empty(knot_dname_t *name)
// save the zone name
dbg_zone("Setting zone name.\n");
zone->name = name;
/* Initialize reference counting. */
ref_init(&zone->ref, knot_zone_dtor);
/* Set reference counter to 1, caller should release it after use. */
knot_zone_retain(zone);
return zone;
}
......@@ -242,3 +255,10 @@ dbg_zone_exec(
free(*zone);
*zone = NULL;
}
void knot_zone_set_dtor(knot_zone_t *zone, int (*dtor)(struct knot_zone *))
{
if (zone != NULL) {
zone->dtor = dtor;
}
}
......@@ -34,6 +34,7 @@
#include "nsec3.h"
#include "zone/dname-table.h"
#include "common/tree.h"
#include "common/ref.h"
#include "hash/cuckoo-hash-table.h"
#include "zone-tree.h"
......@@ -63,6 +64,7 @@ typedef enum knot_zone_retvals knot_zone_retvals_t;
* double-free errors when destroying the zone.
*/
struct knot_zone {
ref_t ref; /*!< Reference counting. */
knot_dname_t *name;
knot_zone_contents_t *contents;
......@@ -147,6 +149,36 @@ void knot_zone_free(knot_zone_t **zone);
*/
void knot_zone_deep_free(knot_zone_t **zone, int destroy_dname_table);
/*!
* \brief Set destructor and initialize reference counter to 1.
*
* \param zone Related zone.
* \param dtor Destructor.
*/
void knot_zone_set_dtor(knot_zone_t *zone, int (*dtor)(struct knot_zone *));
/*!
* \brief Increment reference counter for dname.
*
* \param zone Referenced zone.
*/
static inline void knot_zone_retain(knot_zone_t *zone) {
if (zone != NULL) {
ref_retain(&zone->ref);
}
}
/*!
* \brief Decrement reference counter for dname.
*
* \param zone Referenced zone.
*/
static inline void knot_zone_release(knot_zone_t *zone) {
if (zone != NULL) {
ref_release(&zone->ref);
}
}
#endif
/*! @} */
......@@ -280,7 +280,8 @@ static void delete_zone_from_db(void *node, void *data)
knot_zone_t *zone = (knot_zone_t *)node;
assert(zone);
synchronize_rcu();
knot_zone_deep_free(&zone, 0);
knot_zone_release(zone);
zone = NULL;
}
void knot_zonedb_deep_free(knot_zonedb_t **db)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment