Browse Source

[refcnt] Add ref_init() wrapper function

Standardise on using ref_init() to initialise an embedded reference
count, to match the coding style used by other embedded objects.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
4bfd5b52c1

+ 1
- 1
src/core/downloader.c View File

@@ -264,7 +264,7 @@ int create_downloader ( struct job_interface *job, struct image *image,
264 264
 	downloader = zalloc ( sizeof ( *downloader ) );
265 265
 	if ( ! downloader )
266 266
 		return -ENOMEM;
267
-	downloader->refcnt.free = downloader_free;
267
+	ref_init ( &downloader->refcnt, downloader_free );
268 268
 	job_init ( &downloader->job, &downloader_job_operations,
269 269
 		   &downloader->refcnt );
270 270
 	xfer_init ( &downloader->xfer, &downloader_xfer_operations,

+ 1
- 0
src/core/hw.c View File

@@ -59,6 +59,7 @@ static int hw_open ( struct xfer_interface *xfer, struct uri *uri __unused ) {
59 59
 	hw = zalloc ( sizeof ( *hw ) );
60 60
 	if ( ! hw )
61 61
 		return -ENOMEM;
62
+	ref_init ( &hw->refcnt, NULL );
62 63
 	xfer_init ( &hw->xfer, &hw_xfer_operations, &hw->refcnt );
63 64
 	process_init ( &hw->process, hw_step, &hw->refcnt );
64 65
 

+ 1
- 1
src/core/image.c View File

@@ -64,7 +64,7 @@ struct image * alloc_image ( void ) {
64 64
 
65 65
 	image = zalloc ( sizeof ( *image ) );
66 66
 	if ( image ) {
67
-		image->refcnt.free = free_image;
67
+		ref_init ( &image->refcnt, free_image );
68 68
 	}
69 69
 	return image;
70 70
 }

+ 1
- 1
src/core/posix_io.c View File

@@ -198,7 +198,7 @@ int open ( const char *uri_string ) {
198 198
 	file = zalloc ( sizeof ( *file ) );
199 199
 	if ( ! file )
200 200
 		return -ENOMEM;
201
-	file->refcnt.free = posix_file_free;
201
+	ref_init ( &file->refcnt, posix_file_free );
202 202
 	file->fd = fd;
203 203
 	file->rc = -EINPROGRESS;
204 204
 	xfer_init ( &file->xfer, &posix_file_xfer_operations,

+ 3
- 0
src/core/resolv.c View File

@@ -121,6 +121,7 @@ static int numeric_resolv ( struct resolv_interface *resolv,
121 121
 	numeric = zalloc ( sizeof ( *numeric ) );
122 122
 	if ( ! numeric )
123 123
 		return -ENOMEM;
124
+	ref_init ( &numeric->refcnt, NULL );
124 125
 	resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
125 126
 	process_init ( &numeric->process, numeric_step, &numeric->refcnt );
126 127
 	memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
@@ -256,6 +257,7 @@ int resolv ( struct resolv_interface *resolv, const char *name,
256 257
 	mux = zalloc ( sizeof ( *mux ) + name_len );
257 258
 	if ( ! mux )
258 259
 		return -ENOMEM;
260
+	ref_init ( &mux->refcnt, NULL );
259 261
 	resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
260 262
 	resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
261 263
 	mux->resolver = table_start ( RESOLVERS );
@@ -389,6 +391,7 @@ int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
389 391
 	named = zalloc ( sizeof ( *named ) );
390 392
 	if ( ! named )
391 393
 		return -ENOMEM;
394
+	ref_init ( &named->refcnt, NULL );
392 395
 	xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
393 396
 	resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
394 397
 	named->semantics = semantics;

+ 1
- 0
src/drivers/block/srp.c View File

@@ -482,6 +482,7 @@ int srp_attach ( struct scsi_device *scsi, const char *root_path ) {
482 482
 		rc = -ENOMEM;
483 483
 		goto err_alloc;
484 484
 	}
485
+	ref_init ( &srp->refcnt, NULL );
485 486
 	xfer_init ( &srp->socket, &srp_xfer_operations, &srp->refcnt );
486 487
 	srp->transport = transport;
487 488
 	DBGC ( srp, "SRP %p using %s\n", srp, root_path );

+ 1
- 1
src/image/embedded.c View File

@@ -41,7 +41,7 @@ EMBED_ALL
41 41
 /* Image structures for all embedded images */
42 42
 #undef EMBED
43 43
 #define EMBED( _index, _path, _name ) {					\
44
-	.refcnt = { .free = embedded_image_free, },			\
44
+	.refcnt = REF_INIT ( embedded_image_free ),			\
45 45
 	.name = _name,							\
46 46
 	.data = ( userptr_t ) ( embedded_image_ ## _index ## _data ),	\
47 47
 	.len = ( size_t ) embedded_image_ ## _index ## _len,		\

+ 35
- 0
src/include/ipxe/refcnt.h View File

@@ -40,6 +40,41 @@ struct refcnt {
40 40
 	void ( * free ) ( struct refcnt *refcnt );
41 41
 };
42 42
 
43
+/**
44
+ * Initialise a reference counter
45
+ *
46
+ * @v refcnt		Reference counter
47
+ * @v free		Freeing function
48
+ */
49
+static inline __attribute__ (( always_inline )) void
50
+ref_init ( struct refcnt *refcnt,
51
+	   void ( * free ) ( struct refcnt *refcnt ) ) {
52
+	refcnt->free = free;
53
+}
54
+
55
+/**
56
+ * Initialise a reference counter
57
+ *
58
+ * @v refcnt		Reference counter
59
+ * @v free		Free containing object
60
+ */
61
+#define ref_init( refcnt, free ) do {					\
62
+	if ( __builtin_constant_p ( (free) ) && ( (free) == NULL ) ) {	\
63
+		/* Skip common case of no initialisation required */	\
64
+	} else {							\
65
+		ref_init ( (refcnt), (free) );				\
66
+	}								\
67
+	} while ( 0 )
68
+
69
+/**
70
+ * Initialise a static reference counter
71
+ *
72
+ * @v free		Free containing object
73
+ */
74
+#define REF_INIT( free ) {						\
75
+		.free = free,						\
76
+	}
77
+
43 78
 extern struct refcnt * ref_get ( struct refcnt *refcnt );
44 79
 extern void ref_put ( struct refcnt *refcnt );
45 80
 

+ 1
- 1
src/net/aoe.c View File

@@ -438,7 +438,7 @@ int aoe_attach ( struct ata_device *ata, struct net_device *netdev,
438 438
 	aoe = zalloc ( sizeof ( *aoe ) );
439 439
 	if ( ! aoe )
440 440
 		return -ENOMEM;
441
-	aoe->refcnt.free = aoe_free;
441
+	ref_init ( &aoe->refcnt, aoe_free );
442 442
 	aoe->netdev = netdev_get ( netdev );
443 443
 	memcpy ( aoe->target, netdev->ll_broadcast, sizeof ( aoe->target ) );
444 444
 	aoe->tag = AOE_TAG_MAGIC;

+ 1
- 0
src/net/dhcppkt.c View File

@@ -271,6 +271,7 @@ static struct settings_operations dhcppkt_settings_operations = {
271 271
  */
272 272
 void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct dhcphdr *data,
273 273
 		    size_t len ) {
274
+	ref_init ( &dhcppkt->refcnt, NULL );
274 275
 	dhcppkt->dhcphdr = data;
275 276
 	dhcppkt->max_len = len;
276 277
 	dhcpopt_init ( &dhcppkt->options, &dhcppkt->dhcphdr->options,

+ 1
- 0
src/net/infiniband/ib_cmrc.c View File

@@ -383,6 +383,7 @@ int ib_cmrc_open ( struct xfer_interface *xfer, struct ib_device *ibdev,
383 383
 		rc = -ENOMEM;
384 384
 		goto err_alloc;
385 385
 	}
386
+	ref_init ( &cmrc->refcnt, NULL );
386 387
 	xfer_init ( &cmrc->xfer, &ib_cmrc_xfer_operations, &cmrc->refcnt );
387 388
 	cmrc->ibdev = ibdev;
388 389
 	memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );

+ 1
- 1
src/net/netdevice.c View File

@@ -328,7 +328,7 @@ struct net_device * alloc_netdev ( size_t priv_size ) {
328 328
 	total_len = ( sizeof ( *netdev ) + priv_size );
329 329
 	netdev = zalloc ( total_len );
330 330
 	if ( netdev ) {
331
-		netdev->refcnt.free = free_netdev;
331
+		ref_init ( &netdev->refcnt, free_netdev );
332 332
 		netdev->link_rc = -EUNKNOWN_LINK_STATUS;
333 333
 		INIT_LIST_HEAD ( &netdev->tx_queue );
334 334
 		INIT_LIST_HEAD ( &netdev->rx_queue );

+ 1
- 0
src/net/tcp.c View File

@@ -224,6 +224,7 @@ static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
224 224
 	if ( ! tcp )
225 225
 		return -ENOMEM;
226 226
 	DBGC ( tcp, "TCP %p allocated\n", tcp );
227
+	ref_init ( &tcp->refcnt, NULL );
227 228
 	xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
228 229
 	tcp->prev_tcp_state = TCP_CLOSED;
229 230
 	tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );

+ 1
- 1
src/net/tcp/ftp.c View File

@@ -491,7 +491,7 @@ static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
491 491
 	ftp = zalloc ( sizeof ( *ftp ) );
492 492
 	if ( ! ftp )
493 493
 		return -ENOMEM;
494
-	ftp->refcnt.free = ftp_free;
494
+	ref_init ( &ftp->refcnt, ftp_free );
495 495
 	xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
496 496
 	ftp->uri = uri_get ( uri );
497 497
 	xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );

+ 1
- 1
src/net/tcp/http.c View File

@@ -547,7 +547,7 @@ int http_open_filter ( struct xfer_interface *xfer, struct uri *uri,
547 547
 	http = zalloc ( sizeof ( *http ) );
548 548
 	if ( ! http )
549 549
 		return -ENOMEM;
550
-	http->refcnt.free = http_free;
550
+	ref_init ( &http->refcnt, http_free );
551 551
 	xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
552 552
        	http->uri = uri_get ( uri );
553 553
 	xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );

+ 1
- 1
src/net/tcp/iscsi.c View File

@@ -1792,7 +1792,7 @@ int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
1792 1792
 	iscsi = zalloc ( sizeof ( *iscsi ) );
1793 1793
 	if ( ! iscsi )
1794 1794
 		return -ENOMEM;
1795
-	iscsi->refcnt.free = iscsi_free;
1795
+	ref_init ( &iscsi->refcnt, iscsi_free );
1796 1796
 	xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
1797 1797
 	process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
1798 1798
 

+ 1
- 1
src/net/tls.c View File

@@ -1731,7 +1731,7 @@ int add_tls ( struct xfer_interface *xfer, struct xfer_interface **next ) {
1731 1731
 	if ( ! tls )
1732 1732
 		return -ENOMEM;
1733 1733
 	memset ( tls, 0, sizeof ( *tls ) );
1734
-	tls->refcnt.free = free_tls;
1734
+	ref_init ( &tls->refcnt, free_tls );
1735 1735
 	filter_init ( &tls->plainstream, &tls_plainstream_operations,
1736 1736
 		      &tls->cipherstream, &tls_cipherstream_operations,
1737 1737
 		      &tls->refcnt );

+ 1
- 0
src/net/udp.c View File

@@ -110,6 +110,7 @@ static int udp_open_common ( struct xfer_interface *xfer,
110 110
 	if ( ! udp )
111 111
 		return -ENOMEM;
112 112
 	DBGC ( udp, "UDP %p allocated\n", udp );
113
+	ref_init ( &udp->refcnt, NULL );
113 114
 	xfer_init ( &udp->xfer, &udp_xfer_operations, &udp->refcnt );
114 115
 	if ( st_peer )
115 116
 		memcpy ( &udp->peer, st_peer, sizeof ( udp->peer ) );

+ 2
- 2
src/net/udp/dhcp.c View File

@@ -1439,7 +1439,7 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1439 1439
 	dhcp = zalloc ( sizeof ( *dhcp ) );
1440 1440
 	if ( ! dhcp )
1441 1441
 		return -ENOMEM;
1442
-	dhcp->refcnt.free = dhcp_free;
1442
+	ref_init ( &dhcp->refcnt, dhcp_free );
1443 1443
 	job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1444 1444
 	xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1445 1445
 	dhcp->netdev = netdev_get ( netdev );
@@ -1542,7 +1542,7 @@ int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1542 1542
 			sizeof ( *ip ) /* terminator */ );
1543 1543
 	if ( ! dhcp )
1544 1544
 		return -ENOMEM;
1545
-	dhcp->refcnt.free = dhcp_free;
1545
+	ref_init ( &dhcp->refcnt, dhcp_free );
1546 1546
 	job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1547 1547
 	xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1548 1548
 	dhcp->netdev = netdev_get ( netdev );

+ 1
- 0
src/net/udp/dns.c View File

@@ -503,6 +503,7 @@ static int dns_resolv ( struct resolv_interface *resolv,
503 503
 		rc = -ENOMEM;
504 504
 		goto err_alloc_dns;
505 505
 	}
506
+	ref_init ( &dns->refcnt, NULL );
506 507
 	resolv_init ( &dns->resolv, &null_resolv_ops, &dns->refcnt );
507 508
 	xfer_init ( &dns->socket, &dns_socket_operations, &dns->refcnt );
508 509
 	dns->timer.expired = dns_timer_expired;

+ 1
- 1
src/net/udp/slam.c View File

@@ -748,7 +748,7 @@ static int slam_open ( struct xfer_interface *xfer, struct uri *uri ) {
748 748
 	slam = zalloc ( sizeof ( *slam ) );
749 749
 	if ( ! slam )
750 750
 		return -ENOMEM;
751
-	slam->refcnt.free = slam_free;
751
+	ref_init ( &slam->refcnt, slam_free );
752 752
 	xfer_init ( &slam->xfer, &slam_xfer_operations, &slam->refcnt );
753 753
 	xfer_init ( &slam->socket, &slam_socket_operations, &slam->refcnt );
754 754
 	xfer_init ( &slam->mc_socket, &slam_mc_socket_operations,

+ 1
- 1
src/net/udp/tftp.c View File

@@ -1134,7 +1134,7 @@ static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
1134 1134
 	tftp = zalloc ( sizeof ( *tftp ) );
1135 1135
 	if ( ! tftp )
1136 1136
 		return -ENOMEM;
1137
-	tftp->refcnt.free = tftp_free;
1137
+	ref_init ( &tftp->refcnt, tftp_free );
1138 1138
 	xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
1139 1139
 	tftp->uri = uri_get ( uri );
1140 1140
 	xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );

Loading…
Cancel
Save