iPXE
Data Structures | Macros | Functions | Variables
refcnt.h File Reference

Reference counting. More...

#include <stddef.h>
#include <assert.h>

Go to the source code of this file.

Data Structures

struct  refcnt
 A reference counter. More...
 

Macros

#define ref_init(refcnt, free)
 Initialise a reference counter. More...
 
#define REF_INIT(free_fn)
 Initialise a static reference counter. More...
 
#define ref_get(refcnt)
 Get additional reference to object. More...
 
#define ref_put(refcnt)
 Drop reference to object. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static __attribute__ ((always_inline)) void ref_init(struct refcnt *refcnt
 Initialise a reference counter. More...
 
void ref_increment (struct refcnt *refcnt)
 Increment reference count. More...
 
void ref_decrement (struct refcnt *refcnt)
 Decrement reference count. More...
 
void ref_no_free (struct refcnt *refcnt)
 

Variables

static void(* free )(struct refcnt *refcnt))
 

Detailed Description

Reference counting.

Definition in file refcnt.h.

Macro Definition Documentation

◆ ref_init

#define ref_init (   refcnt,
  free 
)
Value:
do { \
if ( __builtin_constant_p ( (free) ) && ( (free) == NULL ) ) { \
/* Skip common case of no initialisation required */ \
} else { \
ref_init ( (refcnt), (free) ); \
} \
} while ( 0 )
A reference counter.
Definition: refcnt.h:26
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

Initialise a reference counter.

Parameters
refcntReference counter
freeFree containing object

Definition at line 64 of file refcnt.h.

◆ REF_INIT

#define REF_INIT (   free_fn)
Value:
{ \
.free = free_fn, \
}

Initialise a static reference counter.

Parameters
free_fnFree containing object

Definition at line 77 of file refcnt.h.

◆ ref_get

#define ref_get (   refcnt)
Value:
( { \
if ( refcnt ) \
assert ( (refcnt)->count >= 0 ); \
ref_increment ( refcnt ); \
(refcnt); } )
static void(*) struct refcnt refcnt)
Definition: pool.h:62
A reference counter.
Definition: refcnt.h:26
uint16_t count
Number of entries.
Definition: ena.h:22

Get additional reference to object.

Parameters
refcntReference counter, or NULL
Return values
refcntReference counter

If refcnt is NULL, no action is taken.

Definition at line 92 of file refcnt.h.

◆ ref_put

#define ref_put (   refcnt)
Value:
do { \
if ( refcnt ) \
assert ( (refcnt)->count >= 0 ); \
ref_decrement ( refcnt ); \
} while ( 0 )
A reference counter.
Definition: refcnt.h:26
uint16_t count
Number of entries.
Definition: ena.h:22

Drop reference to object.

Parameters
refcntReference counter, or NULL
Return values
refcntReference counter

If refcnt is NULL, no action is taken.

Definition at line 106 of file refcnt.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

static __attribute__ ( (always_inline)  )
inlinestatic

Initialise a reference counter.

Parameters
refcntReference counter
freeFreeing function

◆ ref_increment()

void ref_increment ( struct refcnt refcnt)

Increment reference count.

Parameters
refcntReference counter, or NULL

If refcnt is NULL, no action is taken.

Definition at line 42 of file refcnt.c.

42  {
43 
44  if ( refcnt ) {
45  refcnt->count++;
46  DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
47  refcnt, refcnt->count );
48  }
49 }
A reference counter.
Definition: refcnt.h:26
int count
Current reference count.
Definition: refcnt.h:32
#define DBGC2(...)
Definition: compiler.h:522

References refcnt::count, and DBGC2.

◆ ref_decrement()

void ref_decrement ( struct refcnt refcnt)

Decrement reference count.

Parameters
refcntReference counter, or NULL

If the reference count decreases below zero, the object's free() method will be called.

If refcnt is NULL, no action is taken.

Definition at line 61 of file refcnt.c.

61  {
62 
63  if ( ! refcnt )
64  return;
65 
66  refcnt->count--;
67  DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
68  refcnt, refcnt->count );
69 
70  if ( refcnt->count >= 0 )
71  return;
72 
73  if ( refcnt->count < -1 ) {
74  DBGC ( refcnt, "REFCNT %p decremented too far (%d)!\n",
75  refcnt, refcnt->count );
76  /* Avoid multiple calls to free(), which typically
77  * result in memory corruption that is very hard to
78  * track down.
79  */
80  return;
81  }
82 
83  if ( refcnt->free ) {
84  DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
85  refcnt, refcnt->free );
86  refcnt->free ( refcnt );
87  } else {
88  DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
89  free ( refcnt );
90  }
91 }
#define DBGC(...)
Definition: compiler.h:505
A reference counter.
Definition: refcnt.h:26
int count
Current reference count.
Definition: refcnt.h:32
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define DBGC2(...)
Definition: compiler.h:522
void(* free)(struct refcnt *refcnt)
Free containing object.
Definition: refcnt.h:43

References refcnt::count, DBGC, DBGC2, refcnt::free, and free.

◆ ref_no_free()

void ref_no_free ( struct refcnt refcnt)

Variable Documentation

◆ free

void( * free) (struct refcnt *refcnt))

Definition at line 54 of file refcnt.h.

54  {
55  refcnt->free = free;
56 }
A reference counter.
Definition: refcnt.h:26
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void(* free)(struct refcnt *refcnt)
Free containing object.
Definition: refcnt.h:43

Referenced by aes_unwrap(), aes_wrap(), alloc_usb_bus(), aoecmd_free(), aoedev_free(), apply_dns_search(), apply_dns_servers(), apply_peerdisc_settings(), apply_syslog_settings(), apply_syslogs_settings(), ar9300_eeprom_restore_internal(), arbel_alloc(), arbel_create_cq(), arbel_create_qp(), arbel_destroy_cq(), arbel_destroy_qp(), arbel_free(), asn1_grow(), asn1_okx(), atacmd_free(), ath5k_desc_free(), ath5k_eeprom_free_pcal_info(), ath5k_hw_attach(), ath5k_hw_detach(), ath5k_probe(), ath5k_remove(), ath9k_deinit_softc(), ath9k_init_softc(), ath_descdma_cleanup(), atl1e_free_ring_resources(), autovivified_settings_free(), bitmap_free(), certstore_apply_settings(), chap_finish(), cmdline_image_free(), cms_free(), copy_encap_settings(), deflate_test_exec(), del_ipv4_miniroute(), delwin(), destroy_menu(), dhcp_create_request(), dhcp_free(), dhcpv6_free(), dhe_key(), downloader_free(), eap_rx_identity(), eap_rx_md5(), eap_rx_mschapv2_request(), eap_tx_response(), echo_exec(), efi_aoe_path(), efi_autoexec_filesystem(), efi_autoexec_tftp(), efi_block_describe(), efi_block_exec(), efi_block_hook(), efi_block_install(), efi_block_label(), efi_block_unhook(), efi_cmdline_free(), efi_download_start(), efi_driver_start(), efi_driver_stop(), efi_file_free(), efi_ifr_free(), efi_image_exec(), efi_iscsi_path(), efi_local_check_volume_name(), efi_local_len(), efi_local_open_path(), efi_locate_device(), efi_pxe_free(), efi_shim_cmdline(), efi_snp_hii_fetch(), efi_snp_hii_install(), efi_snp_hii_process(), efi_snp_hii_store(), efi_snp_hii_uninstall(), efi_snp_probe(), efi_snp_remove(), efi_usb_free_all(), efi_usb_install(), efi_usb_probe(), efi_usb_remove(), efi_usb_uninstall(), efi_veto(), efipci_start(), efipci_stop(), efivars_fetch(), efivars_find(), ehci_endpoint_close(), ehci_endpoint_open(), ehci_probe(), ehci_remove(), ehci_ring_alloc(), ehci_ring_free(), eisabus_probe(), eisabus_remove(), empty_line_buffer(), eoib_discard(), eoib_flush_peers(), exanic_probe(), exanic_remove(), expand_settings(), expand_tokens(), fc_els_free(), fc_ns_query_free(), fc_ulp_free(), fc_ulp_logout(), fc_xchg_free(), fcpcmd_free(), fetch_next_server_and_filename(), fetch_root_path(), fetch_san_filename(), fetchf_setting(), fetchn_setting(), flexboot_nodnic_create_cq(), flexboot_nodnic_create_qp(), flexboot_nodnic_destroy_cq(), flexboot_nodnic_destroy_qp(), flexboot_nodnic_eth_open(), flexboot_nodnic_probe(), flexboot_nodnic_remove(), fragment_expired(), fragment_reassemble(), free_image(), free_iob(), free_mlx_utils(), free_netdev(), free_parameters(), free_pixbuf(), free_tls(), free_tls_session(), free_tokens(), free_usb(), free_usb_bus(), free_usb_hub(), ftp_free(), generic_settings_clear(), generic_settings_store(), golan_create_cq(), golan_create_qp_aux(), golan_destroy_cq(), golan_destroy_qp(), golan_probe_normal(), golan_remove_normal(), guestinfo_fetch_type(), hermon_alloc(), hermon_create_cq(), hermon_create_qp(), hermon_destroy_cq(), hermon_destroy_qp(), hermon_free(), history_append(), history_cleanup(), history_free(), history_store(), http_conn_free(), http_format_ntlm_auth(), http_free(), http_open_uri(), http_parse_ntlm_auth(), hub_probe(), hub_remove(), hv_probe(), hv_remove(), hvm_map_xenstore(), hvm_probe(), hvm_remove(), ib_create_conn(), ib_create_cq(), ib_create_mi(), ib_create_path(), ib_create_qp(), ib_destroy_conn(), ib_destroy_cq(), ib_destroy_madx(), ib_destroy_mi(), ib_destroy_path(), ib_destroy_qp(), ib_mcast_attach(), ib_mcast_detach(), ib_srp_free(), ib_srp_parse_root_path(), ibft_install(), icert_cert(), icert_certs(), icert_encode(), image_set_cmdline(), image_set_name(), image_x509(), imgdownload(), imgsingle_exec(), imgverify(), init_mlx_utils(), initrd_reshuffle(), initrd_swap(), initrd_swap_any(), int13_hook(), ipair_free(), ipair_rx_pubkey(), ipair_tx_pair(), ipoib_discard_remac(), ipoib_flush_remac(), ipv6_del_miniroute(), ipv6_sock_aton(), ipv6conf_free(), ipxe(), isabus_probe(), isabus_remove(), isapnpbus_probe(), isapnpbus_remove(), iscsi_fetch_settings(), iscsi_free(), iscsi_handle_chap_c_value(), iscsi_handle_chap_r_value(), iscsi_handle_targetaddress_value(), iscsi_parse_root_path(), iscsi_rx_buffered_data_done(), iscsi_scsi_done(), item_exec(), jme_free_rx_resources(), jme_free_tx_resources(), keymap_apply(), linda_create_send_wq(), linda_destroy_send_wq(), linux_args_cleanup(), lldp_remove(), lldp_rx(), loopback_test(), mcabus_probe(), mcabus_remove(), memcpy_test_speed(), menu_exec(), mlx_memory_free_priv(), neighbour_free(), net80211_autoassociate(), net80211_free(), net80211_free_wlan(), net80211_free_wlanlist(), net80211_mgmt_dequeue(), net80211_netdev_close(), net80211_prepare_assoc(), net80211_probe_finish_all(), net80211_probe_finish_best(), net80211_step_associate(), netboot(), netfront_read_mac(), nfs_deliver(), nfs_free(), nfs_open(), nfs_uri_free(), nfs_uri_init(), nfs_uri_next_mountpoint(), nfs_uri_symlink(), nii_map(), nii_pci_close(), nii_unmap(), ntlm_authenticate_okx(), ocsp_free(), ocsp_response(), ocsp_uri_string(), param_exec(), parse_settings_name(), pci_vpd_resize(), pcibridge_probe(), pcibridge_remove(), pcibus_probe(), pcibus_remove(), peerblk_decrypt(), peerblk_free(), peerblk_reset(), peerdisc_discovered(), peerdisc_free(), peerdisc_socket_tx(), peermux_free(), pem_asn1(), png_pixbuf(), posix_file_free(), privkey_apply_settings(), privkey_free(), process_script(), prompt_exec(), pxe_menu_boot(), qib7322_destroy_send_bufs(), qib7322_destroy_send_wq(), qib7322_probe(), qib7322_remove(), rc80211_free(), read_value(), readline_history(), ref_decrement(), resolve_path(), resolve_uri(), rsa_free(), rtl818x_probe(), sandev_free(), sandev_parse_iso9660(), scsicmd_free(), sec80211_install(), set_core_exec(), shell(), show_exec(), sis190_free_phy(), skge_free(), skge_probe(), skge_remove(), sky2_free_rings(), sky2_probe(), sky2_remove(), slam_free(), slam_parse_multicast_address(), srpcmd_free(), storef_setting(), storen_setting(), string_test_exec(), system(), t509bus_probe(), t509bus_remove(), tftp_free(), tftp_uri(), tg3_free_consistent(), tls_clear_cipher(), tls_clear_handshake(), tls_new_finished(), tls_new_server_key_exchange(), tls_new_session_ticket(), tls_send_certificate(), tls_send_client_key_exchange_dhe(), tls_send_plaintext(), txnic_bgx_probe(), txnic_bgx_remove(), txnic_pf_probe(), txnic_pf_remove(), ucode_exec(), uhci_dequeue(), uhci_endpoint_close(), uhci_endpoint_open(), uhci_enqueue(), uhci_probe(), uhci_remove(), undipci_probe(), undipci_remove(), undirom_probe(), uri_churi_okx(), uri_format_okx(), uri_free(), uri_resolve_okx(), uri_resolve_path_okx(), usb_autoconfigure(), usb_config_descriptor(), usb_get_string_descriptor(), usb_probe_all(), usb_remove_all(), usbio_config(), usbio_endpoint_close(), usbio_endpoint_open(), usbio_interfaces(), usbio_interrupt_close(), usbio_interrupt_open(), usbio_path(), usbio_start(), usbio_stop(), usbkbd_probe(), usbkbd_remove(), validator_free(), validator_start_download(), vesafb_init(), virtnet_free_virtqueues(), vmbus_close(), vmbus_open(), vmbus_probe(), vmbus_probe_channels(), vmbus_remove(), vmbus_remove_channels(), vxge_hw_device_terminate(), wpa_stop(), x509_free(), x509_free_chain(), x509_truncate(), xcm_free(), xenbus_probe(), xenbus_probe_device(), xenbus_probe_type(), xenbus_remove_device(), xenstore_dump(), xenstore_message(), xenstore_read_num(), xenstore_response(), xfer_vprintf(), xhci_bus_close(), xhci_bus_open(), xhci_device_close(), xhci_device_open(), xhci_endpoint_close(), xhci_endpoint_open(), xhci_probe(), xhci_remove(), xhci_ring_alloc(), xhci_ring_free(), xsigo_free(), xve_create(), xve_destroy(), and zlib_deflate().