mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
56c0baa523
For everything we declare that starts with _, make it end with _ instead.
This is a machine-generated patch. To make it, start by getting the
list of reserved identifiers using:
git ls-tree -r --name-only HEAD | grep '\.[ch]$' | \
xargs ctags --c-kinds=defglmpstuvx -o - | grep '^_' | \
cut -f 1 | sort| uniq
You might need gnu ctags.
Then pipe the output through this script:
==============================
use strict;
BEGIN { print "#!/usr/bin/perl -w -i -p\n\n"; }
chomp;
next if (
/^__attribute__/ or
/^__func__/ or
/^_FILE_OFFSET_BITS/ or
/^_FORTIFY_SOURCE/ or
/^_GNU_SOURCE/ or
/^_WIN32/ or
/^_DARWIN_UNLIMITED/ or
/^_FILE_OFFSET_BITS/ or
/^_LARGEFILE64_SOURCE/ or
/^_LFS64_LARGEFILE/ or
/^__cdecl/ or
/^__attribute__/ or
/^__func__/ or
/^_WIN32_WINNT/);
my $ident = $_;
my $better = $ident;
$better =~ s/^_//;
$better = "${better}_";
print "s/(?<![A-Za-z0-9_])$ident(?![A-Za-z0-9_])/$better/g;\n";
==============================
Then run the resulting script on all the files you want to change.
(That is, all the C except that in src/ext.) The resulting script was:
==============================
s/(?<![A-Za-z0-9_])_address(?![A-Za-z0-9_])/address_/g;
s/(?<![A-Za-z0-9_])_aes_fill_buf(?![A-Za-z0-9_])/aes_fill_buf_/g;
s/(?<![A-Za-z0-9_])_AllowInvalid(?![A-Za-z0-9_])/AllowInvalid_/g;
s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MAX(?![A-Za-z0-9_])/AP_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MIN(?![A-Za-z0-9_])/AP_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_assert_cache_ok(?![A-Za-z0-9_])/assert_cache_ok_/g;
s/(?<![A-Za-z0-9_])_A_UNKNOWN(?![A-Za-z0-9_])/A_UNKNOWN_/g;
s/(?<![A-Za-z0-9_])_base(?![A-Za-z0-9_])/base_/g;
s/(?<![A-Za-z0-9_])_BridgePassword_AuthDigest(?![A-Za-z0-9_])/BridgePassword_AuthDigest_/g;
s/(?<![A-Za-z0-9_])_buffer_stats_compare_entries(?![A-Za-z0-9_])/buffer_stats_compare_entries_/g;
s/(?<![A-Za-z0-9_])_chan_circid_entries_eq(?![A-Za-z0-9_])/chan_circid_entries_eq_/g;
s/(?<![A-Za-z0-9_])_chan_circid_entry_hash(?![A-Za-z0-9_])/chan_circid_entry_hash_/g;
s/(?<![A-Za-z0-9_])_check_no_tls_errors(?![A-Za-z0-9_])/check_no_tls_errors_/g;
s/(?<![A-Za-z0-9_])_c_hist_compare(?![A-Za-z0-9_])/c_hist_compare_/g;
s/(?<![A-Za-z0-9_])_circ(?![A-Za-z0-9_])/circ_/g;
s/(?<![A-Za-z0-9_])_circuit_get_global_list(?![A-Za-z0-9_])/circuit_get_global_list_/g;
s/(?<![A-Za-z0-9_])_circuit_mark_for_close(?![A-Za-z0-9_])/circuit_mark_for_close_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_C_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_C_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MIN_/g;
s/(?<![A-Za-z0-9_])_cmp_int_strings(?![A-Za-z0-9_])/cmp_int_strings_/g;
s/(?<![A-Za-z0-9_])_compare_cached_resolves_by_expiry(?![A-Za-z0-9_])/compare_cached_resolves_by_expiry_/g;
s/(?<![A-Za-z0-9_])_compare_digests(?![A-Za-z0-9_])/compare_digests_/g;
s/(?<![A-Za-z0-9_])_compare_digests256(?![A-Za-z0-9_])/compare_digests256_/g;
s/(?<![A-Za-z0-9_])_compare_dir_src_ents_by_authority_id(?![A-Za-z0-9_])/compare_dir_src_ents_by_authority_id_/g;
s/(?<![A-Za-z0-9_])_compare_duration_idx(?![A-Za-z0-9_])/compare_duration_idx_/g;
s/(?<![A-Za-z0-9_])_compare_int(?![A-Za-z0-9_])/compare_int_/g;
s/(?<![A-Za-z0-9_])_compare_networkstatus_v2_published_on(?![A-Za-z0-9_])/compare_networkstatus_v2_published_on_/g;
s/(?<![A-Za-z0-9_])_compare_old_routers_by_identity(?![A-Za-z0-9_])/compare_old_routers_by_identity_/g;
s/(?<![A-Za-z0-9_])_compare_orports(?![A-Za-z0-9_])/compare_orports_/g;
s/(?<![A-Za-z0-9_])_compare_pairs(?![A-Za-z0-9_])/compare_pairs_/g;
s/(?<![A-Za-z0-9_])_compare_routerinfo_by_id_digest(?![A-Za-z0-9_])/compare_routerinfo_by_id_digest_/g;
s/(?<![A-Za-z0-9_])_compare_routerinfo_by_ip_and_bw(?![A-Za-z0-9_])/compare_routerinfo_by_ip_and_bw_/g;
s/(?<![A-Za-z0-9_])_compare_signed_descriptors_by_age(?![A-Za-z0-9_])/compare_signed_descriptors_by_age_/g;
s/(?<![A-Za-z0-9_])_compare_string_ptrs(?![A-Za-z0-9_])/compare_string_ptrs_/g;
s/(?<![A-Za-z0-9_])_compare_strings_for_pqueue(?![A-Za-z0-9_])/compare_strings_for_pqueue_/g;
s/(?<![A-Za-z0-9_])_compare_strs(?![A-Za-z0-9_])/compare_strs_/g;
s/(?<![A-Za-z0-9_])_compare_tor_version_str_ptr(?![A-Za-z0-9_])/compare_tor_version_str_ptr_/g;
s/(?<![A-Za-z0-9_])_compare_vote_rs(?![A-Za-z0-9_])/compare_vote_rs_/g;
s/(?<![A-Za-z0-9_])_compare_votes_by_authority_id(?![A-Za-z0-9_])/compare_votes_by_authority_id_/g;
s/(?<![A-Za-z0-9_])_compare_without_first_ch(?![A-Za-z0-9_])/compare_without_first_ch_/g;
s/(?<![A-Za-z0-9_])_connection_free(?![A-Za-z0-9_])/connection_free_/g;
s/(?<![A-Za-z0-9_])_connection_mark_and_flush(?![A-Za-z0-9_])/connection_mark_and_flush_/g;
s/(?<![A-Za-z0-9_])_connection_mark_for_close(?![A-Za-z0-9_])/connection_mark_for_close_/g;
s/(?<![A-Za-z0-9_])_connection_mark_unattached_ap(?![A-Za-z0-9_])/connection_mark_unattached_ap_/g;
s/(?<![A-Za-z0-9_])_connection_write_to_buf_impl(?![A-Za-z0-9_])/connection_write_to_buf_impl_/g;
s/(?<![A-Za-z0-9_])_ConnLimit(?![A-Za-z0-9_])/ConnLimit_/g;
s/(?<![A-Za-z0-9_])_CONN_TYPE_MAX(?![A-Za-z0-9_])/CONN_TYPE_MAX_/g;
s/(?<![A-Za-z0-9_])_CONN_TYPE_MIN(?![A-Za-z0-9_])/CONN_TYPE_MIN_/g;
s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MAX(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MIN(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MAX(?![A-Za-z0-9_])/CPUWORKER_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MIN(?![A-Za-z0-9_])/CPUWORKER_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_crypto_dh_get_dh(?![A-Za-z0-9_])/crypto_dh_get_dh_/g;
s/(?<![A-Za-z0-9_])_crypto_global_initialized(?![A-Za-z0-9_])/crypto_global_initialized_/g;
s/(?<![A-Za-z0-9_])_crypto_new_pk_from_rsa(?![A-Za-z0-9_])/crypto_new_pk_from_rsa_/g;
s/(?<![A-Za-z0-9_])_crypto_pk_get_evp_pkey(?![A-Za-z0-9_])/crypto_pk_get_evp_pkey_/g;
s/(?<![A-Za-z0-9_])_crypto_pk_get_rsa(?![A-Za-z0-9_])/crypto_pk_get_rsa_/g;
s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MAX(?![A-Za-z0-9_])/DIR_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MIN(?![A-Za-z0-9_])/DIR_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MAX(?![A-Za-z0-9_])/DIR_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MIN(?![A-Za-z0-9_])/DIR_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_dirreq_map_get(?![A-Za-z0-9_])/dirreq_map_get_/g;
s/(?<![A-Za-z0-9_])_dirreq_map_put(?![A-Za-z0-9_])/dirreq_map_put_/g;
s/(?<![A-Za-z0-9_])_dns_randfn(?![A-Za-z0-9_])/dns_randfn_/g;
s/(?<![A-Za-z0-9_])_dummy(?![A-Za-z0-9_])/dummy_/g;
s/(?<![A-Za-z0-9_])_edge(?![A-Za-z0-9_])/edge_/g;
s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MAX(?![A-Za-z0-9_])/END_CIRC_REASON_MAX_/g;
s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MIN(?![A-Za-z0-9_])/END_CIRC_REASON_MIN_/g;
s/(?<![A-Za-z0-9_])_EOF(?![A-Za-z0-9_])/EOF_/g;
s/(?<![A-Za-z0-9_])_ERR(?![A-Za-z0-9_])/ERR_/g;
s/(?<![A-Za-z0-9_])_escaped_val(?![A-Za-z0-9_])/escaped_val_/g;
s/(?<![A-Za-z0-9_])_evdns_log(?![A-Za-z0-9_])/evdns_log_/g;
s/(?<![A-Za-z0-9_])_evdns_nameserver_add_impl(?![A-Za-z0-9_])/evdns_nameserver_add_impl_/g;
s/(?<![A-Za-z0-9_])_EVENT_MAX(?![A-Za-z0-9_])/EVENT_MAX_/g;
s/(?<![A-Za-z0-9_])_EVENT_MIN(?![A-Za-z0-9_])/EVENT_MIN_/g;
s/(?<![A-Za-z0-9_])_ExcludeExitNodesUnion(?![A-Za-z0-9_])/ExcludeExitNodesUnion_/g;
s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MAX(?![A-Za-z0-9_])/EXIT_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MIN(?![A-Za-z0-9_])/EXIT_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MAX(?![A-Za-z0-9_])/EXIT_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MIN(?![A-Za-z0-9_])/EXIT_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_extrainfo_free(?![A-Za-z0-9_])/extrainfo_free_/g;
s/(?<![A-Za-z0-9_])_find_by_keyword(?![A-Za-z0-9_])/find_by_keyword_/g;
s/(?<![A-Za-z0-9_])_free_cached_dir(?![A-Za-z0-9_])/free_cached_dir_/g;
s/(?<![A-Za-z0-9_])_free_cached_resolve(?![A-Za-z0-9_])/free_cached_resolve_/g;
s/(?<![A-Za-z0-9_])_free_duplicate_routerstatus_entry(?![A-Za-z0-9_])/free_duplicate_routerstatus_entry_/g;
s/(?<![A-Za-z0-9_])_free_link_history(?![A-Za-z0-9_])/free_link_history_/g;
s/(?<![A-Za-z0-9_])_geoip_compare_entries(?![A-Za-z0-9_])/geoip_compare_entries_/g;
s/(?<![A-Za-z0-9_])_geoip_compare_key_to_entry(?![A-Za-z0-9_])/geoip_compare_key_to_entry_/g;
s/(?<![A-Za-z0-9_])_hex_decode_digit(?![A-Za-z0-9_])/hex_decode_digit_/g;
s/(?<![A-Za-z0-9_])_idxplus1(?![A-Za-z0-9_])/idxplus1_/g;
s/(?<![A-Za-z0-9_])__libc_enable_secure(?![A-Za-z0-9_])/_libc_enable_secure_/g;
s/(?<![A-Za-z0-9_])_log_debug(?![A-Za-z0-9_])/log_debug_/g;
s/(?<![A-Za-z0-9_])_log_err(?![A-Za-z0-9_])/log_err_/g;
s/(?<![A-Za-z0-9_])_log_fn(?![A-Za-z0-9_])/log_fn_/g;
s/(?<![A-Za-z0-9_])_log_fn_function_name(?![A-Za-z0-9_])/log_fn_function_name_/g;
s/(?<![A-Za-z0-9_])_log_global_min_severity(?![A-Za-z0-9_])/log_global_min_severity_/g;
s/(?<![A-Za-z0-9_])_log_info(?![A-Za-z0-9_])/log_info_/g;
s/(?<![A-Za-z0-9_])_log_notice(?![A-Za-z0-9_])/log_notice_/g;
s/(?<![A-Za-z0-9_])_log_prefix(?![A-Za-z0-9_])/log_prefix_/g;
s/(?<![A-Za-z0-9_])_log_warn(?![A-Za-z0-9_])/log_warn_/g;
s/(?<![A-Za-z0-9_])_magic(?![A-Za-z0-9_])/magic_/g;
s/(?<![A-Za-z0-9_])_MALLOC_LOCK(?![A-Za-z0-9_])/MALLOC_LOCK_/g;
s/(?<![A-Za-z0-9_])_MALLOC_LOCK_INIT(?![A-Za-z0-9_])/MALLOC_LOCK_INIT_/g;
s/(?<![A-Za-z0-9_])_MALLOC_UNLOCK(?![A-Za-z0-9_])/MALLOC_UNLOCK_/g;
s/(?<![A-Za-z0-9_])_microdesc_eq(?![A-Za-z0-9_])/microdesc_eq_/g;
s/(?<![A-Za-z0-9_])_microdesc_hash(?![A-Za-z0-9_])/microdesc_hash_/g;
s/(?<![A-Za-z0-9_])_MIN_TOR_TLS_ERROR_VAL(?![A-Za-z0-9_])/MIN_TOR_TLS_ERROR_VAL_/g;
s/(?<![A-Za-z0-9_])_mm_free(?![A-Za-z0-9_])/mm_free_/g;
s/(?<![A-Za-z0-9_])_NIL(?![A-Za-z0-9_])/NIL_/g;
s/(?<![A-Za-z0-9_])_n_openssl_mutexes(?![A-Za-z0-9_])/n_openssl_mutexes_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_create_cb(?![A-Za-z0-9_])/openssl_dynlock_create_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_destroy_cb(?![A-Za-z0-9_])/openssl_dynlock_destroy_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_lock_cb(?![A-Za-z0-9_])/openssl_dynlock_lock_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_locking_cb(?![A-Za-z0-9_])/openssl_locking_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_mutexes(?![A-Za-z0-9_])/openssl_mutexes_/g;
s/(?<![A-Za-z0-9_])_option_abbrevs(?![A-Za-z0-9_])/option_abbrevs_/g;
s/(?<![A-Za-z0-9_])_option_vars(?![A-Za-z0-9_])/option_vars_/g;
s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MAX(?![A-Za-z0-9_])/OR_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MIN(?![A-Za-z0-9_])/OR_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv4(?![A-Za-z0-9_])/OutboundBindAddressIPv4_/g;
s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv6(?![A-Za-z0-9_])/OutboundBindAddressIPv6_/g;
s/(?<![A-Za-z0-9_])_PDS_PREFER_TUNNELED_DIR_CONNS(?![A-Za-z0-9_])/PDS_PREFER_TUNNELED_DIR_CONNS_/g;
s/(?<![A-Za-z0-9_])_port(?![A-Za-z0-9_])/port_/g;
s/(?<![A-Za-z0-9_])__progname(?![A-Za-z0-9_])/_progname_/g;
s/(?<![A-Za-z0-9_])_PublishServerDescriptor(?![A-Za-z0-9_])/PublishServerDescriptor_/g;
s/(?<![A-Za-z0-9_])_remove_old_client_helper(?![A-Za-z0-9_])/remove_old_client_helper_/g;
s/(?<![A-Za-z0-9_])_rend_cache_entry_free(?![A-Za-z0-9_])/rend_cache_entry_free_/g;
s/(?<![A-Za-z0-9_])_routerlist_find_elt(?![A-Za-z0-9_])/routerlist_find_elt_/g;
s/(?<![A-Za-z0-9_])_SafeLogging(?![A-Za-z0-9_])/SafeLogging_/g;
s/(?<![A-Za-z0-9_])_SHORT_FILE_(?![A-Za-z0-9_])/SHORT_FILE__/g;
s/(?<![A-Za-z0-9_])_state_abbrevs(?![A-Za-z0-9_])/state_abbrevs_/g;
s/(?<![A-Za-z0-9_])_state_vars(?![A-Za-z0-9_])/state_vars_/g;
s/(?<![A-Za-z0-9_])_t(?![A-Za-z0-9_])/t_/g;
s/(?<![A-Za-z0-9_])_t32(?![A-Za-z0-9_])/t32_/g;
s/(?<![A-Za-z0-9_])_test_op_ip6(?![A-Za-z0-9_])/test_op_ip6_/g;
s/(?<![A-Za-z0-9_])_thread1_name(?![A-Za-z0-9_])/thread1_name_/g;
s/(?<![A-Za-z0-9_])_thread2_name(?![A-Za-z0-9_])/thread2_name_/g;
s/(?<![A-Za-z0-9_])_thread_test_func(?![A-Za-z0-9_])/thread_test_func_/g;
s/(?<![A-Za-z0-9_])_thread_test_mutex(?![A-Za-z0-9_])/thread_test_mutex_/g;
s/(?<![A-Za-z0-9_])_thread_test_start1(?![A-Za-z0-9_])/thread_test_start1_/g;
s/(?<![A-Za-z0-9_])_thread_test_start2(?![A-Za-z0-9_])/thread_test_start2_/g;
s/(?<![A-Za-z0-9_])_thread_test_strmap(?![A-Za-z0-9_])/thread_test_strmap_/g;
s/(?<![A-Za-z0-9_])_tor_calloc(?![A-Za-z0-9_])/tor_calloc_/g;
s/(?<![A-Za-z0-9_])_TOR_CHANNEL_INTERNAL(?![A-Za-z0-9_])/TOR_CHANNEL_INTERNAL_/g;
s/(?<![A-Za-z0-9_])_TOR_CIRCUITMUX_EWMA_C(?![A-Za-z0-9_])/TOR_CIRCUITMUX_EWMA_C_/g;
s/(?<![A-Za-z0-9_])_tor_free(?![A-Za-z0-9_])/tor_free_/g;
s/(?<![A-Za-z0-9_])_tor_malloc(?![A-Za-z0-9_])/tor_malloc_/g;
s/(?<![A-Za-z0-9_])_tor_malloc_zero(?![A-Za-z0-9_])/tor_malloc_zero_/g;
s/(?<![A-Za-z0-9_])_tor_memdup(?![A-Za-z0-9_])/tor_memdup_/g;
s/(?<![A-Za-z0-9_])_tor_realloc(?![A-Za-z0-9_])/tor_realloc_/g;
s/(?<![A-Za-z0-9_])_tor_strdup(?![A-Za-z0-9_])/tor_strdup_/g;
s/(?<![A-Za-z0-9_])_tor_strndup(?![A-Za-z0-9_])/tor_strndup_/g;
s/(?<![A-Za-z0-9_])_TOR_TLS_SYSCALL(?![A-Za-z0-9_])/TOR_TLS_SYSCALL_/g;
s/(?<![A-Za-z0-9_])_TOR_TLS_ZERORETURN(?![A-Za-z0-9_])/TOR_TLS_ZERORETURN_/g;
s/(?<![A-Za-z0-9_])__USE_ISOC99(?![A-Za-z0-9_])/_USE_ISOC99_/g;
s/(?<![A-Za-z0-9_])_UsingTestNetworkDefaults(?![A-Za-z0-9_])/UsingTestNetworkDefaults_/g;
s/(?<![A-Za-z0-9_])_val(?![A-Za-z0-9_])/val_/g;
s/(?<![A-Za-z0-9_])_void_for_alignment(?![A-Za-z0-9_])/void_for_alignment_/g;
==============================
329 lines
9.8 KiB
C
329 lines
9.8 KiB
C
/* Copyright (c) 2008-2012, The Tor Project, Inc. */
|
|
/* See LICENSE for licensing information */
|
|
|
|
/** \file memarea.c
|
|
* \brief Implementation for memarea_t, an allocator for allocating lots of
|
|
* small objects that will be freed all at once.
|
|
*/
|
|
|
|
#include "orconfig.h"
|
|
#include <stdlib.h>
|
|
#include "memarea.h"
|
|
#include "util.h"
|
|
#include "compat.h"
|
|
#include "torlog.h"
|
|
|
|
/** If true, we try to detect any attempts to write beyond the length of a
|
|
* memarea. */
|
|
#define USE_SENTINELS
|
|
|
|
/** All returned pointers should be aligned to the nearest multiple of this
|
|
* value. */
|
|
#define MEMAREA_ALIGN SIZEOF_VOID_P
|
|
|
|
#if MEMAREA_ALIGN == 4
|
|
#define MEMAREA_ALIGN_MASK 3lu
|
|
#elif MEMAREA_ALIGN == 8
|
|
#define MEMAREA_ALIGN_MASK 7lu
|
|
#else
|
|
#error "void* is neither 4 nor 8 bytes long. I don't know how to align stuff."
|
|
#endif
|
|
|
|
#ifdef USE_SENTINELS
|
|
/** Magic value that we stick at the end of a memarea so we can make sure
|
|
* there are no run-off-the-end bugs. */
|
|
#define SENTINEL_VAL 0x90806622u
|
|
/** How many bytes per area do we devote to the sentinel? */
|
|
#define SENTINEL_LEN sizeof(uint32_t)
|
|
/** Given a mem_area_chunk_t with SENTINEL_LEN extra bytes allocated at the
|
|
* end, set those bytes. */
|
|
#define SET_SENTINEL(chunk) \
|
|
STMT_BEGIN \
|
|
set_uint32( &(chunk)->u.mem[chunk->mem_size], SENTINEL_VAL ); \
|
|
STMT_END
|
|
/** Assert that the sentinel on a memarea is set correctly. */
|
|
#define CHECK_SENTINEL(chunk) \
|
|
STMT_BEGIN \
|
|
uint32_t sent_val = get_uint32(&(chunk)->u.mem[chunk->mem_size]); \
|
|
tor_assert(sent_val == SENTINEL_VAL); \
|
|
STMT_END
|
|
#else
|
|
#define SENTINEL_LEN 0
|
|
#define SET_SENTINEL(chunk) STMT_NIL
|
|
#define CHECK_SENTINEL(chunk) STMT_NIL
|
|
#endif
|
|
|
|
/** Increment <b>ptr</b> until it is aligned to MEMAREA_ALIGN. */
|
|
static INLINE void *
|
|
realign_pointer(void *ptr)
|
|
{
|
|
uintptr_t x = (uintptr_t)ptr;
|
|
x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
|
|
/* Reinstate this if bug 930 ever reappears
|
|
tor_assert(((void*)x) >= ptr);
|
|
*/
|
|
return (void*)x;
|
|
}
|
|
|
|
/** Implements part of a memarea. New memory is carved off from chunk->mem in
|
|
* increasing order until a request is too big, at which point a new chunk is
|
|
* allocated. */
|
|
typedef struct memarea_chunk_t {
|
|
/** Next chunk in this area. Only kept around so we can free it. */
|
|
struct memarea_chunk_t *next_chunk;
|
|
size_t mem_size; /**< How much RAM is available in u.mem, total? */
|
|
char *next_mem; /**< Next position in u.mem to allocate data at. If it's
|
|
* greater than or equal to mem+mem_size, this chunk is
|
|
* full. */
|
|
union {
|
|
char mem[1]; /**< Memory space in this chunk. */
|
|
void *void_for_alignment_; /**< Dummy; used to make sure mem is aligned. */
|
|
} u;
|
|
} memarea_chunk_t;
|
|
|
|
/** How many bytes are needed for overhead before we get to the memory part
|
|
* of a chunk? */
|
|
#define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, u)
|
|
|
|
/** What's the smallest that we'll allocate a chunk? */
|
|
#define CHUNK_SIZE 4096
|
|
|
|
/** A memarea_t is an allocation region for a set of small memory requests
|
|
* that will all be freed at once. */
|
|
struct memarea_t {
|
|
memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
|
|
};
|
|
|
|
/** How many chunks will we put into the freelist before freeing them? */
|
|
#define MAX_FREELIST_LEN 4
|
|
/** The number of memarea chunks currently in our freelist. */
|
|
static int freelist_len=0;
|
|
/** A linked list of unused memory area chunks. Used to prevent us from
|
|
* spinning in malloc/free loops. */
|
|
static memarea_chunk_t *freelist = NULL;
|
|
|
|
/** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
|
|
static memarea_chunk_t *
|
|
alloc_chunk(size_t sz, int freelist_ok)
|
|
{
|
|
tor_assert(sz < SIZE_T_CEILING);
|
|
if (freelist && freelist_ok) {
|
|
memarea_chunk_t *res = freelist;
|
|
freelist = res->next_chunk;
|
|
res->next_chunk = NULL;
|
|
--freelist_len;
|
|
CHECK_SENTINEL(res);
|
|
return res;
|
|
} else {
|
|
size_t chunk_size = freelist_ok ? CHUNK_SIZE : sz;
|
|
memarea_chunk_t *res;
|
|
chunk_size += SENTINEL_LEN;
|
|
res = tor_malloc(chunk_size);
|
|
res->next_chunk = NULL;
|
|
res->mem_size = chunk_size - CHUNK_HEADER_SIZE - SENTINEL_LEN;
|
|
res->next_mem = res->u.mem;
|
|
tor_assert(res->next_mem+res->mem_size+SENTINEL_LEN ==
|
|
((char*)res)+chunk_size);
|
|
tor_assert(realign_pointer(res->next_mem) == res->next_mem);
|
|
SET_SENTINEL(res);
|
|
return res;
|
|
}
|
|
}
|
|
|
|
/** Release <b>chunk</b> from a memarea, either by adding it to the freelist
|
|
* or by freeing it if the freelist is already too big. */
|
|
static void
|
|
chunk_free_unchecked(memarea_chunk_t *chunk)
|
|
{
|
|
CHECK_SENTINEL(chunk);
|
|
if (freelist_len < MAX_FREELIST_LEN) {
|
|
++freelist_len;
|
|
chunk->next_chunk = freelist;
|
|
freelist = chunk;
|
|
chunk->next_mem = chunk->u.mem;
|
|
} else {
|
|
tor_free(chunk);
|
|
}
|
|
}
|
|
|
|
/** Allocate and return new memarea. */
|
|
memarea_t *
|
|
memarea_new(void)
|
|
{
|
|
memarea_t *head = tor_malloc(sizeof(memarea_t));
|
|
head->first = alloc_chunk(CHUNK_SIZE, 1);
|
|
return head;
|
|
}
|
|
|
|
/** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
|
|
* and friends for this area */
|
|
void
|
|
memarea_drop_all(memarea_t *area)
|
|
{
|
|
memarea_chunk_t *chunk, *next;
|
|
for (chunk = area->first; chunk; chunk = next) {
|
|
next = chunk->next_chunk;
|
|
chunk_free_unchecked(chunk);
|
|
}
|
|
area->first = NULL; /*fail fast on */
|
|
tor_free(area);
|
|
}
|
|
|
|
/** Forget about having allocated anything in <b>area</b>, and free some of
|
|
* the backing storage associated with it, as appropriate. Invalidates all
|
|
* pointers returned from memarea_alloc() for this area. */
|
|
void
|
|
memarea_clear(memarea_t *area)
|
|
{
|
|
memarea_chunk_t *chunk, *next;
|
|
if (area->first->next_chunk) {
|
|
for (chunk = area->first->next_chunk; chunk; chunk = next) {
|
|
next = chunk->next_chunk;
|
|
chunk_free_unchecked(chunk);
|
|
}
|
|
area->first->next_chunk = NULL;
|
|
}
|
|
area->first->next_mem = area->first->u.mem;
|
|
}
|
|
|
|
/** Remove all unused memarea chunks from the internal freelist. */
|
|
void
|
|
memarea_clear_freelist(void)
|
|
{
|
|
memarea_chunk_t *chunk, *next;
|
|
freelist_len = 0;
|
|
for (chunk = freelist; chunk; chunk = next) {
|
|
next = chunk->next_chunk;
|
|
tor_free(chunk);
|
|
}
|
|
freelist = NULL;
|
|
}
|
|
|
|
/** Return true iff <b>p</b> is in a range that has been returned by an
|
|
* allocation from <b>area</b>. */
|
|
int
|
|
memarea_owns_ptr(const memarea_t *area, const void *p)
|
|
{
|
|
memarea_chunk_t *chunk;
|
|
const char *ptr = p;
|
|
for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
|
|
if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
|
|
* bytes. <b>sz</b> should be significantly smaller than the area's chunk
|
|
* size, though we can deal if it isn't. */
|
|
void *
|
|
memarea_alloc(memarea_t *area, size_t sz)
|
|
{
|
|
memarea_chunk_t *chunk = area->first;
|
|
char *result;
|
|
tor_assert(chunk);
|
|
CHECK_SENTINEL(chunk);
|
|
tor_assert(sz < SIZE_T_CEILING);
|
|
if (sz == 0)
|
|
sz = 1;
|
|
if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
|
|
if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
|
|
/* This allocation is too big. Stick it in a special chunk, and put
|
|
* that chunk second in the list. */
|
|
memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE, 0);
|
|
new_chunk->next_chunk = chunk->next_chunk;
|
|
chunk->next_chunk = new_chunk;
|
|
chunk = new_chunk;
|
|
} else {
|
|
memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE, 1);
|
|
new_chunk->next_chunk = chunk;
|
|
area->first = chunk = new_chunk;
|
|
}
|
|
tor_assert(chunk->mem_size >= sz);
|
|
}
|
|
result = chunk->next_mem;
|
|
chunk->next_mem = chunk->next_mem + sz;
|
|
/* Reinstate these if bug 930 ever comes back
|
|
tor_assert(chunk->next_mem >= chunk->u.mem);
|
|
tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size);
|
|
*/
|
|
chunk->next_mem = realign_pointer(chunk->next_mem);
|
|
return result;
|
|
}
|
|
|
|
/** As memarea_alloc(), but clears the memory it returns. */
|
|
void *
|
|
memarea_alloc_zero(memarea_t *area, size_t sz)
|
|
{
|
|
void *result = memarea_alloc(area, sz);
|
|
memset(result, 0, sz);
|
|
return result;
|
|
}
|
|
|
|
/** As memdup, but returns the memory from <b>area</b>. */
|
|
void *
|
|
memarea_memdup(memarea_t *area, const void *s, size_t n)
|
|
{
|
|
char *result = memarea_alloc(area, n);
|
|
memcpy(result, s, n);
|
|
return result;
|
|
}
|
|
|
|
/** As strdup, but returns the memory from <b>area</b>. */
|
|
char *
|
|
memarea_strdup(memarea_t *area, const char *s)
|
|
{
|
|
return memarea_memdup(area, s, strlen(s)+1);
|
|
}
|
|
|
|
/** As strndup, but returns the memory from <b>area</b>. */
|
|
char *
|
|
memarea_strndup(memarea_t *area, const char *s, size_t n)
|
|
{
|
|
size_t ln;
|
|
char *result;
|
|
const char *cp, *end = s+n;
|
|
tor_assert(n < SIZE_T_CEILING);
|
|
for (cp = s; cp < end && *cp; ++cp)
|
|
;
|
|
/* cp now points to s+n, or to the 0 in the string. */
|
|
ln = cp-s;
|
|
result = memarea_alloc(area, ln+1);
|
|
memcpy(result, s, ln);
|
|
result[ln]='\0';
|
|
return result;
|
|
}
|
|
|
|
/** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
|
|
* and <b>used_out</b> to the number of bytes currently used. */
|
|
void
|
|
memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
|
|
{
|
|
size_t a = 0, u = 0;
|
|
memarea_chunk_t *chunk;
|
|
for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
|
|
CHECK_SENTINEL(chunk);
|
|
a += CHUNK_HEADER_SIZE + chunk->mem_size;
|
|
tor_assert(chunk->next_mem >= chunk->u.mem);
|
|
u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem);
|
|
}
|
|
*allocated_out = a;
|
|
*used_out = u;
|
|
}
|
|
|
|
/** Assert that <b>area</b> is okay. */
|
|
void
|
|
memarea_assert_ok(memarea_t *area)
|
|
{
|
|
memarea_chunk_t *chunk;
|
|
tor_assert(area->first);
|
|
|
|
for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
|
|
CHECK_SENTINEL(chunk);
|
|
tor_assert(chunk->next_mem >= chunk->u.mem);
|
|
tor_assert(chunk->next_mem <=
|
|
(char*) realign_pointer(chunk->u.mem+chunk->mem_size));
|
|
}
|
|
}
|
|
|