Skip to content

Commit 18bea5d

Browse files
committed
Better abstracting the choice of allocator.
1 parent 60e37b6 commit 18bea5d

File tree

8 files changed

+26
-22
lines changed

8 files changed

+26
-22
lines changed

DHT/dht.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ void set_dhtDebug(int const d) {dhtDebug = d;}
5050
#endif /*DEBUG_DHT*/
5151

5252
#if !defined(New) /* TODO: Is this the correct check for all of the below lines? */
53-
# define New(type) fxfAlloc(sizeof(type), type)
53+
# define New(type) DHTVALUE_ALLOC(sizeof(type), type)
5454
# define nNew(n,type) ((type *)nNewImpl(n,sizeof(type),ALIGNMENT_OF_TYPE(type)))
5555
# define Nil(type) ((type *)0)
5656
static inline void * nNewImpl(size_t const nmemb, size_t const size, size_t desired_alignment) {
57-
return ((size && (nmemb > (((size_t)-1)/size))) ? Nil(void) : fxfAllocRaw(nmemb*size, desired_alignment));
57+
return ((size && (nmemb > (((size_t)-1)/size))) ? Nil(void) : DHTVALUE_ALLOC_RAW(nmemb*size, desired_alignment));
5858
}
5959
#endif /*New*/
6060

@@ -109,11 +109,11 @@ typedef struct InternHsElement {
109109

110110
#define NilInternHsElement Nil(InternHsElement)
111111
#define NewInternHsElement New(InternHsElement)
112-
#define FreeInternHsElement(h) fxfFree(h, sizeof(InternHsElement))
112+
#define FreeInternHsElement(h) DHTVALUE_FREE(h, sizeof(InternHsElement))
113113

114114
static InternHsElement EndOfTable;
115115

116-
#define freeDir(t) fxfFree(t, sizeof(ht_dir))
116+
#define freeDir(t) DHTVALUE_FREE(t, sizeof(ht_dir))
117117

118118
typedef unsigned long uLong;
119119
typedef unsigned char uChar;
@@ -423,8 +423,8 @@ typedef struct dht {
423423
#if !defined(HashTable)
424424
#define HashTable struct dht
425425
#endif
426-
#define NewHashTable fxfAlloc(sizeof(dht), HashTable)
427-
#define FreeHashTable(h) fxfFree(h, sizeof(dht))
426+
#define NewHashTable DHTVALUE_ALLOC(sizeof(dht), HashTable)
427+
#define FreeHashTable(h) DHTVALUE_FREE(h, sizeof(dht))
428428
#define OVERFLOW_SAVE 1
429429
#if defined(OVERFLOW_SAVE)
430430
#define ActualLoadFactor(h) \

DHT/dhtbcmem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int DupBCMemValue(dhtValue kv, dhtValue *output)
101101
size += (length - num_bytes_in_Data);
102102
}
103103

104-
result = fxfAlloc(size, BCMemValue);
104+
result = DHTVALUE_ALLOC(size, BCMemValue);
105105
if (result)
106106
{
107107
result->Leng = length;
@@ -126,7 +126,7 @@ static void FreeBCMemValue(dhtValue kv)
126126
assert(length <= (((size_t)-1) - size + num_bytes_in_Data));
127127
size += (length - num_bytes_in_Data);
128128
}
129-
fxfFree(freed,size);
129+
DHTVALUE_FREE(freed,size);
130130
}
131131
}
132132

DHT/dhtcmem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int DupCompactMemoryValue(dhtValue kv, dhtValue *output)
9696
size += (length - num_bytes_in_Data);
9797
}
9898

99-
result = fxfAlloc(size, CompactMemVal);
99+
result = DHTVALUE_ALLOC(size, CompactMemVal);
100100
if (result)
101101
{
102102
result->Leng = length;
@@ -121,7 +121,7 @@ static void FreeCompactMemoryValue(dhtValue kv)
121121
assert(length <= (((size_t)-1) - size + num_bytes_in_Data));
122122
size += (length - num_bytes_in_Data);
123123
}
124-
fxfFree(v,size);
124+
DHTVALUE_FREE(v,size);
125125
}
126126
}
127127

DHT/dhtcmem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ typedef struct CompactMemVal {
1818
unsigned char Data[1];
1919
} CompactMemVal;
2020
#define NilCompactMemVal ((CompactMemVal *)0)
21-
#define NewCompactMemVal(n) fxfAlloc(sizeof(CompactMemVal)+(n)*sizeof(uChar), CompactMemVal)
22-
#define FreeCompactMemVal(v) fxfFree(v, sizeof(CompactMemVal)+((CompactMemVal const *)(v))->Leng*sizeof(uChar))
21+
#define NewCompactMemVal(n) DHTVALUE_ALLOC(sizeof(CompactMemVal)+(n)*sizeof(uChar), CompactMemVal)
22+
#define FreeCompactMemVal(v) DHTVALUE_FREE(v, sizeof(CompactMemVal)+((CompactMemVal const *)(v))->Leng*sizeof(uChar))
2323
#endif /*DHTCMEM_INCLUDED*/

DHT/dhtmem.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int DupMemoryValue(dhtValue kv, dhtValue *output)
9696
if (mv) {
9797
mv->Leng= length;
9898
if (length) {
99-
uChar *newBuffer= fxfAlloc(length, uChar);
99+
uChar *newBuffer= DHTVALUE_ALLOC(length, uChar);
100100
if (newBuffer) {
101101
memcpy(newBuffer, data, length);
102102
mv->Data = newBuffer;
@@ -118,8 +118,8 @@ static void FreeMemoryValue(dhtValue kv)
118118
{
119119
MemVal *v= (MemVal *)kv.object_pointer;
120120
if (v) {
121-
fxfFree(v->Data, v->Leng);
122-
fxfFree(v, sizeof *v);
121+
DHTVALUE_FREE(v->Data, v->Leng);
122+
DHTVALUE_FREE(v, sizeof *v);
123123
}
124124
}
125125
static void DumpMemoryValue(dhtValue kv, FILE *f) {

DHT/dhtmem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ typedef struct MemVal {
2020
unsigned char *Data;
2121
} MemVal;
2222
#define NilMemVal ((MemVal *)0)
23-
#define NewMemVal fxfAlloc(sizeof(MemVal), MemVal)
24-
#define FreeMemVal(v) fxfFree(v, sizeof(MemVal))
25-
#define DeleteMemVal(v) do {if (((MemVal const *)(v))!=NilMemVal) fxfFree(((MemVal const *)(v))->Data, ((MemVal const *)(v))->Leng), FreeMemVal(v);} while (0)
23+
#define NewMemVal DHTVALUE_ALLOC(sizeof(MemVal), MemVal)
24+
#define FreeMemVal(v) DHTVALUE_FREE(v, sizeof(MemVal))
25+
#define DeleteMemVal(v) do {if (((MemVal const *)(v))!=NilMemVal) DHTVALUE_FREE(((MemVal const *)(v))->Data, ((MemVal const *)(v))->Leng), FreeMemVal(v);} while (0)
2626

2727
#endif /*DHTMEM_INCLUDED*/

DHT/dhtstrng.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int DupString(dhtValue v, dhtValue *output)
6262
len= strlen(original);
6363
if ((len < ((size_t)-1)) && !original[len]) {
6464
++len;
65-
nv= fxfAlloc(len, char);
65+
nv= DHTVALUE_ALLOC(len, char);
6666
if (nv) {
6767
memcpy(nv, original, len);
6868
output->object_pointer= nv;
@@ -78,7 +78,7 @@ static void FreeString(dhtValue v)
7878
{
7979
size_t const len= strlen(s);
8080
assert((len < ((size_t)-1)) && !s[len]);
81-
fxfFree(s, len+1);
81+
DHTVALUE_FREE(s, len+1);
8282
}
8383
}
8484
static void DumpString(dhtValue v, FILE *f)

DHT/dhtvalue.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929

3030
#if defined(FXF)
3131
#include "fxf.h"
32+
#define DHTVALUE_ALLOC(size, type) fxfAlloc(size, type)
33+
#define DHTVALUE_ALLOC_RAW(size, alignment) fxfAllocRaw(size, alignment)
34+
#define DHTVALUE_FREE(ptr, size) fxfFree(ptr, size)
3235
#else
3336
#include <stdlib.h>
34-
#define fxfAlloc(x,type) ((type *) malloc(x)) /* TODO: Should we track allocations to ensure that we never allocate more than some chosen number (e.g., hashtable_kilos*1024) of total byte(s)? */
35-
#define fxfFree(x,n) free(x)
37+
#define DHTVALUE_ALLOC(size, type) ((type *) malloc(size))
38+
#define DHTVALUE_ALLOC_RAW(size, alignment) malloc(size)
39+
#define DHTVALUE_FREE(ptr, size) free(ptr)
3640
#endif /*FXF*/
3741

3842
#if !defined(LOCAL)

0 commit comments

Comments
 (0)