Skip to content

Commit a271636

Browse files
committed
chore: savepoint
1 parent d339167 commit a271636

File tree

5 files changed

+85
-411
lines changed

5 files changed

+85
-411
lines changed

utp_api.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
#include <assert.h>
2526
#include <stdio.h>
2627
#include "utp_internal.h"
2728
#include "utp_utils.h"
@@ -72,7 +73,6 @@ struct_utp_context::struct_utp_context()
7273
memset(&context_stats, 0, sizeof(context_stats));
7374
memset(callbacks, 0, sizeof(callbacks));
7475
target_delay = CCONTROL_TARGET;
75-
utp_sockets = new UTPSocketHT;
7676

7777
callbacks[UTP_GET_UDP_MTU] = &utp_default_get_udp_mtu;
7878
callbacks[UTP_GET_UDP_OVERHEAD] = &utp_default_get_udp_overhead;
@@ -92,10 +92,6 @@ struct_utp_context::struct_utp_context()
9292
last_check = 0;
9393
}
9494

95-
struct_utp_context::~struct_utp_context() {
96-
delete this->utp_sockets;
97-
}
98-
9995
utp_context* utp_init (int version)
10096
{
10197
assert(version == 2);

utp_hash.cpp

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -20,73 +20,20 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
#include <assert.h>
24-
2523
#include "utp_hash.h"
2624
#include "utp_types.h"
2725

28-
#define LIBUTP_HASH_UNUSED ((utp_link_t)-1)
29-
3026
#ifdef STRICT_ALIGN
3127
inline uint32 Read32(const void *p)
3228
{
3329
uint32 tmp;
3430
memcpy(&tmp, p, sizeof tmp);
3531
return tmp;
3632
}
37-
3833
#else
3934
inline uint32 Read32(const void *p) { return *(uint32*)p; }
4035
#endif
4136

42-
43-
// Get the amount of memory required for the hash parameters and the bucket set
44-
// Waste a space for an unused bucket in order to ensure the following managed memory have 32-bit aligned addresses
45-
// TODO: make this 64-bit clean
46-
#define BASE_SIZE(bc) (sizeof(utp_hash_t) + sizeof(utp_link_t) * ((bc) + 1 - 1))
47-
48-
// Get a pointer to the base of the structure array managed by the hash table
49-
#define get_bep(h) ((byte*)(h)) + BASE_SIZE((h)->N)
50-
51-
// Get the address of the information associated with a specific structure in the array,
52-
// given the address of the base of the structure.
53-
// This assumes a utp_link_t link member is at the end of the structure.
54-
// Given compilers filling out the memory to a 32-bit clean value, this may mean that
55-
// the location named in the structure may not be the location actually used by the hash table,
56-
// since the compiler may have padded the end of the structure with 2 bytes after the utp_link_t member.
57-
// TODO: this macro should not require that the variable pointing at the hash table be named 'hash'
58-
#define ptr_to_link(p) (utp_link_t *) (((byte *) (p)) + hash->E - sizeof(utp_link_t))
59-
60-
// Calculate how much to allocate for a hash table with bucket count, total size, and structure count
61-
// TODO: make this 64-bit clean
62-
#define ALLOCATION_SIZE(bc, ts, sc) (BASE_SIZE((bc)) + (ts) * (sc))
63-
64-
utp_hash_t *utp_hash_create(int N, int key_size, int total_size, int initial, utp_hash_compute_t hashfun, utp_hash_equal_t compfun)
65-
{
66-
// Must have odd number of hash buckets (prime number is best)
67-
assert(N % 2);
68-
// Ensure structures will be at aligned memory addresses
69-
// TODO: make this 64-bit clean
70-
assert(0 == (total_size % 4));
71-
72-
int size = ALLOCATION_SIZE(N, total_size, initial);
73-
utp_hash_t *hash = (utp_hash_t *) malloc( size );
74-
memset( hash, 0, size );
75-
76-
for (int i = 0; i < N + 1; ++i)
77-
hash->inits[i] = LIBUTP_HASH_UNUSED;
78-
hash->N = N;
79-
hash->K = key_size;
80-
hash->E = total_size;
81-
hash->hash_compute = hashfun;
82-
hash->hash_equal = compfun;
83-
hash->allocated = initial;
84-
hash->count = 0;
85-
hash->used = 0;
86-
hash->free = LIBUTP_HASH_UNUSED;
87-
return hash;
88-
}
89-
9037
uint utp_hash_mem(const void *keyp, size_t keysize)
9138
{
9239
uint hash = 0;
@@ -105,144 +52,3 @@ uint utp_hash_mem(const void *keyp, size_t keysize)
10552
}
10653
return hash;
10754
}
108-
109-
uint utp_hash_mkidx(utp_hash_t *hash, const void *keyp)
110-
{
111-
// Generate a key from the hash
112-
return hash->hash_compute(keyp, hash->K) % hash->N;
113-
}
114-
115-
static inline bool compare(byte *a, byte *b,int n)
116-
{
117-
assert(n >= 4);
118-
if (Read32(a) != Read32(b)) return false;
119-
return memcmp(a+4, b+4, n-4) == 0;
120-
}
121-
122-
#define COMPARE(h,k1,k2,ks) (((h)->hash_equal) ? (h)->hash_equal((void*)k1,(void*)k2,ks) : compare(k1,k2,ks))
123-
124-
// Look-up a key in the hash table.
125-
// Returns NULL if not found
126-
void *utp_hash_lookup(utp_hash_t *hash, const void *key)
127-
{
128-
utp_link_t idx = utp_hash_mkidx(hash, key);
129-
130-
// base pointer
131-
byte *bep = get_bep(hash);
132-
133-
utp_link_t cur = hash->inits[idx];
134-
while (cur != LIBUTP_HASH_UNUSED) {
135-
byte *key2 = bep + (cur * hash->E);
136-
if (COMPARE(hash, (byte*)key, key2, hash->K))
137-
return key2;
138-
cur = *ptr_to_link(key2);
139-
}
140-
141-
return NULL;
142-
}
143-
144-
// Add a new element to the hash table.
145-
// Returns a pointer to the new element.
146-
// This assumes the element is not already present!
147-
void *utp_hash_add(utp_hash_t **hashp, const void *key)
148-
{
149-
//Allocate a new entry
150-
byte *elemp;
151-
utp_link_t elem;
152-
utp_hash_t *hash = *hashp;
153-
utp_link_t idx = utp_hash_mkidx(hash, key);
154-
155-
if ((elem=hash->free) == LIBUTP_HASH_UNUSED) {
156-
utp_link_t all = hash->allocated;
157-
if (hash->used == all) {
158-
utp_hash_t *nhash;
159-
if (all <= (LIBUTP_HASH_UNUSED/2)) {
160-
all *= 2;
161-
} else if (all != LIBUTP_HASH_UNUSED) {
162-
all = LIBUTP_HASH_UNUSED;
163-
} else {
164-
// too many items! can't grow!
165-
assert(0);
166-
return NULL;
167-
}
168-
// otherwise need to allocate.
169-
nhash = (utp_hash_t*)realloc(hash, ALLOCATION_SIZE(hash->N, hash->E, all));
170-
if (!nhash) {
171-
// out of memory (or too big to allocate)
172-
assert(nhash);
173-
return NULL;
174-
}
175-
hash = *hashp = nhash;
176-
hash->allocated = all;
177-
}
178-
179-
elem = hash->used++;
180-
elemp = get_bep(hash) + elem * hash->E;
181-
} else {
182-
elemp = get_bep(hash) + elem * hash->E;
183-
hash->free = *ptr_to_link(elemp);
184-
}
185-
186-
*ptr_to_link(elemp) = hash->inits[idx];
187-
hash->inits[idx] = elem;
188-
hash->count++;
189-
190-
// copy key into it
191-
memcpy(elemp, key, hash->K);
192-
return elemp;
193-
}
194-
195-
// Delete an element from the utp_hash_t
196-
// Returns a pointer to the already deleted element.
197-
void *utp_hash_del(utp_hash_t *hash, const void *key)
198-
{
199-
utp_link_t idx = utp_hash_mkidx(hash, key);
200-
201-
// base pointer
202-
byte *bep = get_bep(hash);
203-
204-
utp_link_t *curp = &hash->inits[idx];
205-
utp_link_t cur;
206-
while ((cur=*curp) != LIBUTP_HASH_UNUSED) {
207-
byte *key2 = bep + (cur * hash->E);
208-
if (COMPARE(hash,(byte*)key,(byte*)key2, hash->K )) {
209-
// found an item that matched. unlink it
210-
*curp = *ptr_to_link(key2);
211-
// Insert into freelist
212-
*ptr_to_link(key2) = hash->free;
213-
hash->free = cur;
214-
hash->count--;
215-
return key2;
216-
}
217-
curp = ptr_to_link(key2);
218-
}
219-
220-
return NULL;
221-
}
222-
223-
void *utp_hash_iterate(utp_hash_t *hash, utp_hash_iterator_t *iter)
224-
{
225-
utp_link_t elem;
226-
227-
if ((elem=iter->elem) == LIBUTP_HASH_UNUSED) {
228-
// Find a bucket with an element
229-
utp_link_t buck = iter->bucket + 1;
230-
for(;;) {
231-
if (buck >= hash->N)
232-
return NULL;
233-
if ((elem = hash->inits[buck]) != LIBUTP_HASH_UNUSED)
234-
break;
235-
buck++;
236-
}
237-
iter->bucket = buck;
238-
}
239-
240-
byte *elemp = get_bep(hash) + (elem * hash->E);
241-
iter->elem = *ptr_to_link(elemp);
242-
return elemp;
243-
}
244-
245-
void utp_hash_free_mem(utp_hash_t* hash)
246-
{
247-
free(hash);
248-
}

utp_hash.h

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -23,124 +23,8 @@
2323
#ifndef __UTP_HASH_H__
2424
#define __UTP_HASH_H__
2525

26-
#include <string.h> // memset
27-
#include <stdlib.h> // malloc
28-
2926
#include "utp_types.h"
30-
#include "utp_templates.h"
31-
32-
// TODO: make utp_link_t a template parameter to HashTable
33-
typedef uint32 utp_link_t;
34-
35-
#ifdef _MSC_VER
36-
// Silence the warning about the C99-compliant zero-length array at the end of the structure
37-
#pragma warning (disable: 4200)
38-
#endif
39-
40-
typedef uint32 (*utp_hash_compute_t)(const void *keyp, size_t keysize);
41-
typedef uint (*utp_hash_equal_t)(const void *key_a, const void *key_b, size_t keysize);
42-
43-
// In memory the HashTable is laid out as follows:
44-
// ---------------------------- low
45-
// | hash table data members |
46-
// ---------------------------- _
47-
// | indices | ^
48-
// | . | | utp_link_t indices into the key-values.
49-
// | . | .
50-
// ---------------------------- - <----- bep
51-
// | keys and values | each key-value pair has size total_size
52-
// | . |
53-
// | . |
54-
// ---------------------------- high
55-
//
56-
// The code depends on the ability of the compiler to pad the length
57-
// of the hash table data members structure to
58-
// a length divisible by 32-bits with no remainder.
59-
//
60-
// Since the number of hash buckets (indices) should be odd, the code
61-
// asserts this and adds one to the hash bucket count to ensure that the
62-
// following key-value pairs array starts on a 32-bit boundary.
63-
//
64-
// The key-value pairs array should start on a 32-bit boundary, otherwise
65-
// processors like the ARM will silently mangle 32-bit data in these structures
66-
// (e.g., turning 0xABCD into 0XCDAB when moving a value from memory to register
67-
// when the memory address is 16 bits offset from a 32-bit boundary),
68-
// also, the value will be stored at an address two bytes lower than the address
69-
// value would ordinarily indicate.
70-
//
71-
// The key-value pair is of type T. The first field in T must
72-
// be the key, i.e., the first K bytes of T contains the key.
73-
// total_size = sizeof(T) and thus sizeof(T) >= sizeof(K)
74-
//
75-
// N is the number of buckets.
76-
//
77-
struct utp_hash_t {
78-
utp_link_t N;
79-
byte K;
80-
byte E;
81-
size_t count;
82-
utp_hash_compute_t hash_compute;
83-
utp_hash_equal_t hash_equal;
84-
utp_link_t allocated;
85-
utp_link_t used;
86-
utp_link_t free;
87-
utp_link_t inits[1]; // dynamic
88-
};
89-
90-
#ifdef _MSC_VER
91-
#pragma warning (default: 4200)
92-
#endif
93-
94-
struct utp_hash_iterator_t {
95-
utp_link_t bucket;
96-
utp_link_t elem;
97-
98-
utp_hash_iterator_t() : bucket(0xffffffff), elem(0xffffffff) {}
99-
};
10027

10128
uint utp_hash_mem(const void *keyp, size_t keysize);
102-
uint utp_hash_comp(const void *key_a, const void *key_b, size_t keysize);
103-
104-
utp_hash_t *utp_hash_create(int N, int key_size, int total_size, int initial, utp_hash_compute_t hashfun = utp_hash_mem, utp_hash_equal_t eqfun = NULL);
105-
void *utp_hash_lookup(utp_hash_t *hash, const void *key);
106-
void *utp_hash_add(utp_hash_t **hashp, const void *key);
107-
void *utp_hash_del(utp_hash_t *hash, const void *key);
108-
109-
void *utp_hash_iterate(utp_hash_t *hash, utp_hash_iterator_t *iter);
110-
void utp_hash_free_mem(utp_hash_t *hash);
111-
112-
/*
113-
This HashTable requires that T have at least sizeof(K)+sizeof(utp_link_t) bytes.
114-
Usually done like this:
115-
116-
struct K {
117-
int whatever;
118-
};
119-
120-
struct T {
121-
K wtf;
122-
utp_link_t link; // also wtf
123-
};
124-
*/
125-
126-
template<typename K, typename T> class utpHashTable {
127-
utp_hash_t *hash;
128-
public:
129-
static uint compare(const void *k1, const void *k2, size_t /*ks*/) {
130-
return *((K*)k1) == *((K*)k2);
131-
}
132-
static uint32 compute_hash(const void *k, size_t /*ks*/) {
133-
return ((K*)k)->compute_hash();
134-
}
135-
void Init() { hash = NULL; }
136-
bool Allocated() { return (hash != NULL); }
137-
void Free() { utp_hash_free_mem(hash); hash = NULL; }
138-
void Create(int N, int initial) { hash = utp_hash_create(N, sizeof(K), sizeof(T), initial, &compute_hash, &compare); }
139-
T *Lookup(const K &key) { return (T*)utp_hash_lookup(hash, &key); }
140-
T *Add(const K &key) { return (T*)utp_hash_add(&hash, &key); }
141-
T *Delete(const K &key) { return (T*)utp_hash_del(hash, &key); }
142-
T *Iterate(utp_hash_iterator_t &iterator) { return (T*)utp_hash_iterate(hash, &iterator); }
143-
size_t GetCount() { return hash->count; }
144-
};
14529

14630
#endif //__UTP_HASH_H__

0 commit comments

Comments
 (0)