1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-11 05:17:44 +03:00

dhcp: stop using refcnt.h

No need to invole atomic ops in single-threaded APIs, let's simplify
this.
This commit is contained in:
Lennart Poettering 2015-08-26 21:05:53 +02:00
parent 57de20dd8f
commit 3733eec3e2
8 changed files with 120 additions and 73 deletions

View File

@ -25,7 +25,6 @@
#include <stdint.h>
#include <linux/if_packet.h>
#include "refcnt.h"
#include "util.h"
#include "list.h"
@ -48,7 +47,7 @@ struct sd_dhcp_raw_option {
};
struct sd_dhcp_lease {
RefCount n_ref;
int n_ref;
int32_t time_offset;
uint32_t t1;

View File

@ -26,7 +26,6 @@
#include "sd-dhcp-server.h"
#include "hashmap.h"
#include "refcnt.h"
#include "util.h"
#include "log.h"
@ -47,7 +46,7 @@ typedef struct DHCPLease {
} DHCPLease;
struct sd_dhcp_server {
RefCount n_ref;
unsigned n_ref;
sd_event *event;
int event_priority;

View File

@ -24,13 +24,11 @@
#include <stdint.h>
#include "refcnt.h"
#include "sd-dhcp6-lease.h"
#include "dhcp6-internal.h"
struct sd_dhcp6_lease {
RefCount n_ref;
unsigned n_ref;
uint8_t *serverid;
size_t serverid_len;

View File

@ -27,7 +27,6 @@
#include <sys/ioctl.h>
#include "util.h"
#include "refcnt.h"
#include "random-util.h"
#include "async.h"
@ -41,7 +40,7 @@
#define MAX_MAC_ADDR_LEN CONST_MAX(INFINIBAND_ALEN, ETH_ALEN)
struct sd_dhcp_client {
RefCount n_ref;
unsigned n_ref;
DHCPState state;
sd_event *event;
@ -1676,30 +1675,41 @@ sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
}
sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client) {
if (client)
assert_se(REFCNT_INC(client->n_ref) >= 2);
if (!client)
return NULL;
assert(client->n_ref >= 1);
client->n_ref++;
return client;
}
sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
if (client && REFCNT_DEC(client->n_ref) == 0) {
log_dhcp_client(client, "FREE");
client_initialize(client);
if (!client)
return NULL;
client->receive_message =
sd_event_source_unref(client->receive_message);
assert(client->n_ref >= 1);
client->n_ref--;
sd_dhcp_client_detach_event(client);
if (client->n_ref > 0)
return NULL;
sd_dhcp_lease_unref(client->lease);
log_dhcp_client(client, "FREE");
free(client->req_opts);
free(client->hostname);
free(client->vendor_class_identifier);
free(client);
}
client_initialize(client);
client->receive_message = sd_event_source_unref(client->receive_message);
sd_dhcp_client_detach_event(client);
sd_dhcp_lease_unref(client->lease);
free(client->req_opts);
free(client->hostname);
free(client->vendor_class_identifier);
free(client);
return NULL;
}
@ -1713,7 +1723,7 @@ int sd_dhcp_client_new(sd_dhcp_client **ret) {
if (!client)
return -ENOMEM;
client->n_ref = REFCNT_INIT;
client->n_ref = 1;
client->state = DHCP_STATE_INIT;
client->index = -1;
client->fd = -1;

View File

@ -195,32 +195,45 @@ int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const uint8_t **data
}
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
if (lease)
assert_se(REFCNT_INC(lease->n_ref) >= 2);
if (!lease)
return NULL;
assert(lease->n_ref >= 1);
lease->n_ref++;
return lease;
}
sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
if (lease && REFCNT_DEC(lease->n_ref) == 0) {
while (lease->private_options) {
struct sd_dhcp_raw_option *option = lease->private_options;
LIST_REMOVE(options, lease->private_options, option);
if (!lease)
return NULL;
free(option->data);
free(option);
}
free(lease->hostname);
free(lease->domainname);
free(lease->dns);
free(lease->ntp);
free(lease->static_route);
free(lease->client_id);
free(lease->vendor_specific);
free(lease);
assert(lease->n_ref >= 1);
lease->n_ref--;
if (lease->n_ref > 0)
return NULL;
while (lease->private_options) {
struct sd_dhcp_raw_option *option = lease->private_options;
LIST_REMOVE(options, lease->private_options, option);
free(option->data);
free(option);
}
free(lease->hostname);
free(lease->domainname);
free(lease->dns);
free(lease->ntp);
free(lease->static_route);
free(lease->client_id);
free(lease->vendor_specific);
free(lease);
return NULL;
}
@ -643,7 +656,7 @@ int dhcp_lease_new(sd_dhcp_lease **ret) {
return -ENOMEM;
lease->router = INADDR_ANY;
lease->n_ref = REFCNT_INIT;
lease->n_ref = 1;
LIST_HEAD_INIT(lease->private_options);
*ret = lease;

View File

@ -73,8 +73,12 @@ bool sd_dhcp_server_is_running(sd_dhcp_server *server) {
}
sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) {
if (server)
assert_se(REFCNT_INC(server->n_ref) >= 2);
if (!server)
return NULL;
assert(server->n_ref >= 1);
server->n_ref++;
return server;
}
@ -127,7 +131,10 @@ sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) {
if (!server)
return NULL;
if (REFCNT_DEC(server->n_ref) > 0)
assert(server->n_ref >= 1);
server->n_ref--;
if (server->n_ref > 0)
return NULL;
log_dhcp_server(server, "UNREF");
@ -158,7 +165,7 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
if (!server)
return -ENOMEM;
server->n_ref = REFCNT_INIT;
server->n_ref = 1;
server->fd_raw = -1;
server->fd = -1;
server->address = htobe32(INADDR_ANY);

View File

@ -27,7 +27,6 @@
#include "udev.h"
#include "udev-util.h"
#include "util.h"
#include "refcnt.h"
#include "random-util.h"
#include "network-internal.h"
@ -40,7 +39,7 @@
#define MAX_MAC_ADDR_LEN INFINIBAND_ALEN
struct sd_dhcp6_client {
RefCount n_ref;
unsigned n_ref;
enum DHCP6State state;
sd_event *event;
@ -1222,26 +1221,36 @@ sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) {
}
sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) {
if (client)
assert_se(REFCNT_INC(client->n_ref) >= 2);
if (!client)
return NULL;
assert(client->n_ref >= 1);
client->n_ref++;
return client;
}
sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
if (client && REFCNT_DEC(client->n_ref) == 0) {
client_reset(client);
sd_dhcp6_client_detach_event(client);
sd_dhcp6_lease_unref(client->lease);
free(client->req_opts);
free(client);
if (!client)
return NULL;
}
return client;
assert(client->n_ref >= 1);
client->n_ref--;
if (client->n_ref > 0)
return NULL;
client_reset(client);
sd_dhcp6_client_detach_event(client);
sd_dhcp6_lease_unref(client->lease);
free(client->req_opts);
free(client);
return NULL;
}
int sd_dhcp6_client_new(sd_dhcp6_client **ret)
@ -1255,7 +1264,7 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret)
if (!client)
return -ENOMEM;
client->n_ref = REFCNT_INIT;
client->n_ref = 1;
client->ia_na.type = DHCP6_OPTION_IA_NA;

View File

@ -361,26 +361,38 @@ int sd_dhcp6_lease_get_ntp_fqdn(sd_dhcp6_lease *lease, char ***ntp_fqdn) {
}
sd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease) {
if (lease)
assert_se(REFCNT_INC(lease->n_ref) >= 2);
if (!lease)
return NULL;
assert(lease->n_ref >= 1);
lease->n_ref++;
return lease;
}
sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease) {
if (lease && REFCNT_DEC(lease->n_ref) == 0) {
free(lease->serverid);
dhcp6_lease_free_ia(&lease->ia);
free(lease->dns);
if (!lease)
return NULL;
lease->domains = strv_free(lease->domains);
assert(lease->n_ref >= 1);
lease->n_ref--;
free(lease->ntp);
if (lease->n_ref > 0)
return NULL;
lease->ntp_fqdn = strv_free(lease->ntp_fqdn);
free(lease);
}
free(lease->serverid);
dhcp6_lease_free_ia(&lease->ia);
free(lease->dns);
lease->domains = strv_free(lease->domains);
free(lease->ntp);
lease->ntp_fqdn = strv_free(lease->ntp_fqdn);
free(lease);
return NULL;
}
@ -392,7 +404,7 @@ int dhcp6_lease_new(sd_dhcp6_lease **ret) {
if (!lease)
return -ENOMEM;
lease->n_ref = REFCNT_INIT;
lease->n_ref = 1;
LIST_HEAD_INIT(lease->ia.addresses);