Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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>
  • Loading branch information
mcb30 committed Jun 22, 2010
1 parent f4faa27 commit 4bfd5b5
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/core/downloader.c
Expand Up @@ -264,7 +264,7 @@ int create_downloader ( struct job_interface *job, struct image *image,
downloader = zalloc ( sizeof ( *downloader ) );
if ( ! downloader )
return -ENOMEM;
downloader->refcnt.free = downloader_free;
ref_init ( &downloader->refcnt, downloader_free );
job_init ( &downloader->job, &downloader_job_operations,
&downloader->refcnt );
xfer_init ( &downloader->xfer, &downloader_xfer_operations,
Expand Down
1 change: 1 addition & 0 deletions src/core/hw.c
Expand Up @@ -59,6 +59,7 @@ static int hw_open ( struct xfer_interface *xfer, struct uri *uri __unused ) {
hw = zalloc ( sizeof ( *hw ) );
if ( ! hw )
return -ENOMEM;
ref_init ( &hw->refcnt, NULL );
xfer_init ( &hw->xfer, &hw_xfer_operations, &hw->refcnt );
process_init ( &hw->process, hw_step, &hw->refcnt );

Expand Down
2 changes: 1 addition & 1 deletion src/core/image.c
Expand Up @@ -64,7 +64,7 @@ struct image * alloc_image ( void ) {

image = zalloc ( sizeof ( *image ) );
if ( image ) {
image->refcnt.free = free_image;
ref_init ( &image->refcnt, free_image );
}
return image;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/posix_io.c
Expand Up @@ -198,7 +198,7 @@ int open ( const char *uri_string ) {
file = zalloc ( sizeof ( *file ) );
if ( ! file )
return -ENOMEM;
file->refcnt.free = posix_file_free;
ref_init ( &file->refcnt, posix_file_free );
file->fd = fd;
file->rc = -EINPROGRESS;
xfer_init ( &file->xfer, &posix_file_xfer_operations,
Expand Down
3 changes: 3 additions & 0 deletions src/core/resolv.c
Expand Up @@ -121,6 +121,7 @@ static int numeric_resolv ( struct resolv_interface *resolv,
numeric = zalloc ( sizeof ( *numeric ) );
if ( ! numeric )
return -ENOMEM;
ref_init ( &numeric->refcnt, NULL );
resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
process_init ( &numeric->process, numeric_step, &numeric->refcnt );
memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
Expand Down Expand Up @@ -256,6 +257,7 @@ int resolv ( struct resolv_interface *resolv, const char *name,
mux = zalloc ( sizeof ( *mux ) + name_len );
if ( ! mux )
return -ENOMEM;
ref_init ( &mux->refcnt, NULL );
resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
mux->resolver = table_start ( RESOLVERS );
Expand Down Expand Up @@ -389,6 +391,7 @@ int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
named = zalloc ( sizeof ( *named ) );
if ( ! named )
return -ENOMEM;
ref_init ( &named->refcnt, NULL );
xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
named->semantics = semantics;
Expand Down
1 change: 1 addition & 0 deletions src/drivers/block/srp.c
Expand Up @@ -482,6 +482,7 @@ int srp_attach ( struct scsi_device *scsi, const char *root_path ) {
rc = -ENOMEM;
goto err_alloc;
}
ref_init ( &srp->refcnt, NULL );
xfer_init ( &srp->socket, &srp_xfer_operations, &srp->refcnt );
srp->transport = transport;
DBGC ( srp, "SRP %p using %s\n", srp, root_path );
Expand Down
2 changes: 1 addition & 1 deletion src/image/embedded.c
Expand Up @@ -41,7 +41,7 @@ EMBED_ALL
/* Image structures for all embedded images */
#undef EMBED
#define EMBED( _index, _path, _name ) { \
.refcnt = { .free = embedded_image_free, }, \
.refcnt = REF_INIT ( embedded_image_free ), \
.name = _name, \
.data = ( userptr_t ) ( embedded_image_ ## _index ## _data ), \
.len = ( size_t ) embedded_image_ ## _index ## _len, \
Expand Down
35 changes: 35 additions & 0 deletions src/include/ipxe/refcnt.h
Expand Up @@ -40,6 +40,41 @@ struct refcnt {
void ( * free ) ( struct refcnt *refcnt );
};

/**
* Initialise a reference counter
*
* @v refcnt Reference counter
* @v free Freeing function
*/
static inline __attribute__ (( always_inline )) void
ref_init ( struct refcnt *refcnt,
void ( * free ) ( struct refcnt *refcnt ) ) {
refcnt->free = free;
}

/**
* Initialise a reference counter
*
* @v refcnt Reference counter
* @v free Free containing object
*/
#define ref_init( refcnt, free ) do { \
if ( __builtin_constant_p ( (free) ) && ( (free) == NULL ) ) { \
/* Skip common case of no initialisation required */ \
} else { \
ref_init ( (refcnt), (free) ); \
} \
} while ( 0 )

/**
* Initialise a static reference counter
*
* @v free Free containing object
*/
#define REF_INIT( free ) { \
.free = free, \
}

extern struct refcnt * ref_get ( struct refcnt *refcnt );
extern void ref_put ( struct refcnt *refcnt );

Expand Down
2 changes: 1 addition & 1 deletion src/net/aoe.c
Expand Up @@ -438,7 +438,7 @@ int aoe_attach ( struct ata_device *ata, struct net_device *netdev,
aoe = zalloc ( sizeof ( *aoe ) );
if ( ! aoe )
return -ENOMEM;
aoe->refcnt.free = aoe_free;
ref_init ( &aoe->refcnt, aoe_free );
aoe->netdev = netdev_get ( netdev );
memcpy ( aoe->target, netdev->ll_broadcast, sizeof ( aoe->target ) );
aoe->tag = AOE_TAG_MAGIC;
Expand Down
1 change: 1 addition & 0 deletions src/net/dhcppkt.c
Expand Up @@ -271,6 +271,7 @@ static struct settings_operations dhcppkt_settings_operations = {
*/
void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct dhcphdr *data,
size_t len ) {
ref_init ( &dhcppkt->refcnt, NULL );
dhcppkt->dhcphdr = data;
dhcppkt->max_len = len;
dhcpopt_init ( &dhcppkt->options, &dhcppkt->dhcphdr->options,
Expand Down
1 change: 1 addition & 0 deletions src/net/infiniband/ib_cmrc.c
Expand Up @@ -383,6 +383,7 @@ int ib_cmrc_open ( struct xfer_interface *xfer, struct ib_device *ibdev,
rc = -ENOMEM;
goto err_alloc;
}
ref_init ( &cmrc->refcnt, NULL );
xfer_init ( &cmrc->xfer, &ib_cmrc_xfer_operations, &cmrc->refcnt );
cmrc->ibdev = ibdev;
memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );
Expand Down
2 changes: 1 addition & 1 deletion src/net/netdevice.c
Expand Up @@ -328,7 +328,7 @@ struct net_device * alloc_netdev ( size_t priv_size ) {
total_len = ( sizeof ( *netdev ) + priv_size );
netdev = zalloc ( total_len );
if ( netdev ) {
netdev->refcnt.free = free_netdev;
ref_init ( &netdev->refcnt, free_netdev );
netdev->link_rc = -EUNKNOWN_LINK_STATUS;
INIT_LIST_HEAD ( &netdev->tx_queue );
INIT_LIST_HEAD ( &netdev->rx_queue );
Expand Down
1 change: 1 addition & 0 deletions src/net/tcp.c
Expand Up @@ -224,6 +224,7 @@ static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
if ( ! tcp )
return -ENOMEM;
DBGC ( tcp, "TCP %p allocated\n", tcp );
ref_init ( &tcp->refcnt, NULL );
xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
tcp->prev_tcp_state = TCP_CLOSED;
tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
Expand Down
2 changes: 1 addition & 1 deletion src/net/tcp/ftp.c
Expand Up @@ -491,7 +491,7 @@ static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
ftp = zalloc ( sizeof ( *ftp ) );
if ( ! ftp )
return -ENOMEM;
ftp->refcnt.free = ftp_free;
ref_init ( &ftp->refcnt, ftp_free );
xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
ftp->uri = uri_get ( uri );
xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
Expand Down
2 changes: 1 addition & 1 deletion src/net/tcp/http.c
Expand Up @@ -547,7 +547,7 @@ int http_open_filter ( struct xfer_interface *xfer, struct uri *uri,
http = zalloc ( sizeof ( *http ) );
if ( ! http )
return -ENOMEM;
http->refcnt.free = http_free;
ref_init ( &http->refcnt, http_free );
xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
http->uri = uri_get ( uri );
xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );
Expand Down
2 changes: 1 addition & 1 deletion src/net/tcp/iscsi.c
Expand Up @@ -1792,7 +1792,7 @@ int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
iscsi = zalloc ( sizeof ( *iscsi ) );
if ( ! iscsi )
return -ENOMEM;
iscsi->refcnt.free = iscsi_free;
ref_init ( &iscsi->refcnt, iscsi_free );
xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );

Expand Down
2 changes: 1 addition & 1 deletion src/net/tls.c
Expand Up @@ -1731,7 +1731,7 @@ int add_tls ( struct xfer_interface *xfer, struct xfer_interface **next ) {
if ( ! tls )
return -ENOMEM;
memset ( tls, 0, sizeof ( *tls ) );
tls->refcnt.free = free_tls;
ref_init ( &tls->refcnt, free_tls );
filter_init ( &tls->plainstream, &tls_plainstream_operations,
&tls->cipherstream, &tls_cipherstream_operations,
&tls->refcnt );
Expand Down
1 change: 1 addition & 0 deletions src/net/udp.c
Expand Up @@ -110,6 +110,7 @@ static int udp_open_common ( struct xfer_interface *xfer,
if ( ! udp )
return -ENOMEM;
DBGC ( udp, "UDP %p allocated\n", udp );
ref_init ( &udp->refcnt, NULL );
xfer_init ( &udp->xfer, &udp_xfer_operations, &udp->refcnt );
if ( st_peer )
memcpy ( &udp->peer, st_peer, sizeof ( udp->peer ) );
Expand Down
4 changes: 2 additions & 2 deletions src/net/udp/dhcp.c
Expand Up @@ -1439,7 +1439,7 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
dhcp = zalloc ( sizeof ( *dhcp ) );
if ( ! dhcp )
return -ENOMEM;
dhcp->refcnt.free = dhcp_free;
ref_init ( &dhcp->refcnt, dhcp_free );
job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
dhcp->netdev = netdev_get ( netdev );
Expand Down Expand Up @@ -1542,7 +1542,7 @@ int start_pxebs ( struct job_interface *job, struct net_device *netdev,
sizeof ( *ip ) /* terminator */ );
if ( ! dhcp )
return -ENOMEM;
dhcp->refcnt.free = dhcp_free;
ref_init ( &dhcp->refcnt, dhcp_free );
job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
dhcp->netdev = netdev_get ( netdev );
Expand Down
1 change: 1 addition & 0 deletions src/net/udp/dns.c
Expand Up @@ -503,6 +503,7 @@ static int dns_resolv ( struct resolv_interface *resolv,
rc = -ENOMEM;
goto err_alloc_dns;
}
ref_init ( &dns->refcnt, NULL );
resolv_init ( &dns->resolv, &null_resolv_ops, &dns->refcnt );
xfer_init ( &dns->socket, &dns_socket_operations, &dns->refcnt );
dns->timer.expired = dns_timer_expired;
Expand Down
2 changes: 1 addition & 1 deletion src/net/udp/slam.c
Expand Up @@ -748,7 +748,7 @@ static int slam_open ( struct xfer_interface *xfer, struct uri *uri ) {
slam = zalloc ( sizeof ( *slam ) );
if ( ! slam )
return -ENOMEM;
slam->refcnt.free = slam_free;
ref_init ( &slam->refcnt, slam_free );
xfer_init ( &slam->xfer, &slam_xfer_operations, &slam->refcnt );
xfer_init ( &slam->socket, &slam_socket_operations, &slam->refcnt );
xfer_init ( &slam->mc_socket, &slam_mc_socket_operations,
Expand Down
2 changes: 1 addition & 1 deletion src/net/udp/tftp.c
Expand Up @@ -1134,7 +1134,7 @@ static int tftp_core_open ( struct xfer_interface *xfer, struct uri *uri,
tftp = zalloc ( sizeof ( *tftp ) );
if ( ! tftp )
return -ENOMEM;
tftp->refcnt.free = tftp_free;
ref_init ( &tftp->refcnt, tftp_free );
xfer_init ( &tftp->xfer, &tftp_xfer_operations, &tftp->refcnt );
tftp->uri = uri_get ( uri );
xfer_init ( &tftp->socket, &tftp_socket_operations, &tftp->refcnt );
Expand Down

0 comments on commit 4bfd5b5

Please sign in to comment.