Const correctness for toxcore/assoc.c

This commit is contained in:
Marc Schütz 2014-06-13 22:08:03 +02:00
parent 0b4640a508
commit 6d7a4d3746
2 changed files with 31 additions and 30 deletions

View File

@ -103,7 +103,7 @@ struct Assoc {
/* the complete distance would be CLIENT_ID_SIZE long...
* returns DISTANCE_INDEX_DISTANCE_BITS valid bits */
static uint64_t id_distance(Assoc *assoc, void *callback_data, uint8_t *id_ref, uint8_t *id_test)
static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test)
{
/* with BIG_ENDIAN, this would be a one-liner... */
uint64_t retval = 0;
@ -196,7 +196,7 @@ static void dist_index_bubble(Assoc *assoc, uint64_t *dist_list, size_t first, s
*
* Result is NOT MAPPED to CANDIDATES_TO_KEEP range, i.e. map before using
* it for list access. */
static hash_t id_hash(Assoc *assoc, uint8_t *id)
static hash_t id_hash(const Assoc *assoc, const uint8_t *id)
{
uint32_t i, res = 0x19a64e82;
@ -213,7 +213,7 @@ static hash_t id_hash(Assoc *assoc, uint8_t *id)
/* up to HASH_COLLIDE_COUNT calls to different spots,
* result IS mapped to CANDIDATES_TO_KEEP range */
static hash_t hash_collide(Assoc *assoc, hash_t hash)
static hash_t hash_collide(const Assoc *assoc, hash_t hash)
{
uint64_t hash64 = hash % assoc->candidates_bucket_size;
hash64 = (hash64 * HASH_COLLIDE_PRIME) % assoc->candidates_bucket_size;
@ -241,7 +241,7 @@ static hash_t hash_collide(Assoc *assoc, hash_t hash)
}
/* returns the "seen" assoc related to the ipp */
static IPPTsPng *entry_assoc(Client_entry *cl_entry, IP_Port *ipp)
static IPPTsPng *entry_assoc(Client_entry *cl_entry, const IP_Port *ipp)
{
if (!cl_entry)
return NULL;
@ -256,7 +256,7 @@ static IPPTsPng *entry_assoc(Client_entry *cl_entry, IP_Port *ipp)
}
/* returns the "heard" assoc related to the ipp */
static IP_Port *entry_heard_get(Client_entry *entry, IP_Port *ipp)
static IP_Port *entry_heard_get(Client_entry *entry, const IP_Port *ipp)
{
if (ipp->ip.family == AF_INET)
return &entry->assoc_heard4;
@ -271,7 +271,7 @@ static IP_Port *entry_heard_get(Client_entry *entry, IP_Port *ipp)
* LAN ip
*
* returns 1 if the entry did change */
static int entry_heard_store(Client_entry *entry, IPPTs *ippts)
static int entry_heard_store(Client_entry *entry, const IPPTs *ippts)
{
if (!entry || !ippts)
return 0;
@ -279,7 +279,8 @@ static int entry_heard_store(Client_entry *entry, IPPTs *ippts)
if (!ipport_isset(&ippts->ip_port))
return 0;
IP_Port *heard, *ipp = &ippts->ip_port;
IP_Port *heard;
const IP_Port *ipp = &ippts->ip_port;
if (ipp->ip.family == AF_INET)
heard = &entry->assoc_heard4;
@ -314,13 +315,13 @@ static int entry_heard_store(Client_entry *entry, IPPTs *ippts)
}
/* maps Assoc callback signature to id_closest() */
static int assoc_id_closest(Assoc *assoc, void *callback_data, uint8_t *client_id, uint8_t *client_id1,
uint8_t *client_id2)
static int assoc_id_closest(const Assoc *assoc, void *callback_data, const uint8_t *client_id, const uint8_t *client_id1,
const uint8_t *client_id2)
{
return id_closest(client_id, client_id1, client_id2);
}
static bucket_t id_bucket(uint8_t *id, uint8_t bits)
static bucket_t id_bucket(const uint8_t *id, uint8_t bits)
{
/* return the first "bits" bits of id */
bucket_t retval = 0;
@ -340,12 +341,12 @@ static bucket_t id_bucket(uint8_t *id, uint8_t bits)
/*****************************************************************************/
static bucket_t candidates_id_bucket(Assoc *assoc, uint8_t *id)
static bucket_t candidates_id_bucket(const Assoc *assoc, const uint8_t *id)
{
return id_bucket(id, assoc->candidates_bucket_bits);
}
static uint8_t candidates_search(Assoc *assoc, uint8_t *id, hash_t hash, Client_entry **entryptr)
static uint8_t candidates_search(const Assoc *assoc, const uint8_t *id, hash_t hash, Client_entry **entryptr)
{
bucket_t bucket = candidates_id_bucket(assoc, id);
candidates_bucket *cnd_bckt = &assoc->candidates[bucket];
@ -365,8 +366,8 @@ static uint8_t candidates_search(Assoc *assoc, uint8_t *id, hash_t hash, Client_
return 0;
}
static void candidates_update_assoc(Assoc *assoc, Client_entry *entry, uint8_t used, IPPTs *ippts_send,
IP_Port *ipp_recv)
static void candidates_update_assoc(const Assoc *assoc, Client_entry *entry, uint8_t used, const IPPTs *ippts_send,
const IP_Port *ipp_recv)
{
if (!assoc || !entry || !ippts_send)
return;
@ -397,7 +398,7 @@ static void candidates_update_assoc(Assoc *assoc, Client_entry *entry, uint8_t u
entry_heard_store(entry, ippts_send);
}
static uint8_t candidates_create_internal(Assoc *assoc, hash_t hash, uint8_t *id, uint8_t seen,
static uint8_t candidates_create_internal(const Assoc *assoc, hash_t const hash, const uint8_t *id, uint8_t seen,
uint8_t used, bucket_t *bucketptr, size_t *posptr)
{
if (!assoc || !id || !bucketptr || !posptr)
@ -454,8 +455,8 @@ static uint8_t candidates_create_internal(Assoc *assoc, hash_t hash, uint8_t *id
return 0;
}
static uint8_t candidates_create_new(Assoc *assoc, hash_t hash, uint8_t *id, uint8_t used,
IPPTs *ippts_send, IP_Port *ipp_recv)
static uint8_t candidates_create_new(const Assoc *assoc, hash_t hash, const uint8_t *id, uint8_t used,
const IPPTs *ippts_send, const IP_Port *ipp_recv)
{
if (!assoc || !id || !ippts_send)
return 0;
@ -550,7 +551,7 @@ static void client_id_self_update(Assoc *assoc)
* seen should be 0 (zero), if the candidate was announced by someone else,
* seen should be 1 (one), if there is confirmed connectivity (a definite response)
*/
uint8_t Assoc_add_entry(Assoc *assoc, uint8_t *id, IPPTs *ippts_send, IP_Port *ipp_recv, uint8_t used)
uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send, const IP_Port *ipp_recv, uint8_t used)
{
if (!assoc || !id || !ippts_send)
return 0;
@ -777,7 +778,7 @@ static size_t prime_upto_min9(size_t limit)
}
/* create */
Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id)
Assoc *new_Assoc(size_t bits, size_t entries, const uint8_t *public_id)
{
if (!public_id)
return NULL;
@ -850,7 +851,7 @@ Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id)
return assoc;
}
Assoc *new_Assoc_default(uint8_t *public_id)
Assoc *new_Assoc_default(const uint8_t *public_id)
{
/* original 8, 251 averages to ~32k entries... probably the whole DHT :D
* 320 entries is fine, hopefully */
@ -858,7 +859,7 @@ Assoc *new_Assoc_default(uint8_t *public_id)
}
/* own client_id, assocs for this have to be ignored */
void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *id)
void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *id)
{
if (assoc && id) {
assoc->self_hash = 0;

View File

@ -21,21 +21,21 @@ typedef struct Assoc Assoc;
/* custom distance handler, if it's not ID-distance based
* return values exactly like id_closest() */
typedef int (*Assoc_distance_relative_callback)(Assoc *assoc, void *callback_data, uint8_t *client_id,
uint8_t *client_id1, uint8_t *client_id2);
typedef int (*Assoc_distance_relative_callback)(const Assoc *assoc, void *callback_data, const uint8_t *client_id,
const uint8_t *client_id1, const uint8_t *client_id2);
#define DISTANCE_INDEX_DISTANCE_BITS 44
/* absolute distance: can be same for different client_id_check values
* return value should have DISTANCE_INDEX_DISTANCE_BITS valid bits */
typedef uint64_t (*Assoc_distance_absolute_callback)(Assoc *assoc, void *callback_data,
uint8_t *client_id_ref, uint8_t *client_id_check);
typedef uint64_t (*Assoc_distance_absolute_callback)(const Assoc *assoc, void *callback_data,
const uint8_t *client_id_ref, const uint8_t *client_id_check);
/*****************************************************************************/
/* Central entry point for new associations: add a new candidate to the cache
* returns 1 if entry is stored, 2 if existing entry was updated, 0 else */
uint8_t Assoc_add_entry(Assoc *assoc, uint8_t *id, IPPTs *ippts_send, IP_Port *ipp_recv, uint8_t used);
uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send, const IP_Port *ipp_recv, uint8_t used);
/*****************************************************************************/
@ -72,7 +72,7 @@ uint8_t Assoc_get_close_entries(Assoc *assoc, Assoc_close_entries *close_entries
/*****************************************************************************/
/* create: default sizes (6, 5 => 320 entries) */
Assoc *new_Assoc_default(uint8_t *public_id);
Assoc *new_Assoc_default(const uint8_t *public_id);
/* create: customized sizes
* total is (2^bits) * entries
@ -81,10 +81,10 @@ Assoc *new_Assoc_default(uint8_t *public_id);
*
* preferably bits should be large and entries small to ensure spread
* in the search space (e. g. 5, 5 is preferable to 2, 41) */
Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id);
Assoc *new_Assoc(size_t bits, size_t entries, const uint8_t *public_id);
/* public_id changed (loaded), update which entry isn't stored */
void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *public_id);
void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *public_id);
/* every 45s send out a getnodes() for a "random" bucket */
#define ASSOC_BUCKET_REFRESH 45
@ -97,7 +97,7 @@ void do_Assoc(Assoc *assoc, DHT *dht);
void kill_Assoc(Assoc *assoc);
#ifdef LOGGING
void Assoc_status(Assoc *assoc);
void Assoc_status(const Assoc *assoc);
#endif /* LOGGING */
#endif /* !__ASSOC_H__ */