Skip to content

Commit 511a29d

Browse files
committed
DeltaChunkVector is now a separate structure. I wished I had c++, but ... its probably a good exercise
1 parent 2409faf commit 511a29d

File tree

1 file changed

+83
-43
lines changed

1 file changed

+83
-43
lines changed

_fun.c

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -85,56 +85,47 @@ static PyObject *PackIndexFile_sha_to_index(PyObject *self, PyObject *args)
8585

8686

8787
typedef unsigned long long ull;
88+
typedef unsigned int uint;
8889

90+
91+
// DELTA CHUNK
92+
////////////////
8993
// Internal Delta Chunk Objects
9094
typedef struct {
9195
ull to;
9296
ull ts;
9397
ull so;
9498
PyObject* data;
95-
96-
void* next;
9799
} DeltaChunk;
98100

99-
100-
void DC_init(DeltaChunk* dc, ull to, ull ts, ull so, PyObject* data, DeltaChunk* next)
101+
void DC_init(DeltaChunk* dc, ull to, ull ts, ull so, PyObject* data)
101102
{
102103
dc->to = to;
103104
dc->ts = ts;
104105
dc->so = so;
105106
Py_XINCREF(data);
106107
dc->data = data;
107-
108-
dc->next = next;
109108
}
110109

111110
void DC_destroy(DeltaChunk* dc)
112111
{
113112
Py_XDECREF(dc->data);
114113
}
115114

116-
typedef struct {
117-
PyObject_HEAD
118-
// -----------
119-
DeltaChunk* mem;
120-
ull size;
121-
ull reserved_size;
122-
123-
} DeltaChunkList;
124-
125115
ull DC_rbound(DeltaChunk* dc)
126116
{
127117
return dc->to + dc->ts;
128118
}
129119

130-
static
131-
int DCL_new(DeltaChunkList* self, PyObject* args, PyObject* kwds)
132-
{
133-
self->mem = NULL; // Memory
134-
self->size = 0; // Size in DeltaChunks
135-
self->reserved_size = 0; // Reserve in DeltaChunks
136-
return 1;
137-
}
120+
121+
// DELTA CHUNK VECTOR
122+
/////////////////////
123+
124+
typedef struct {
125+
DeltaChunk* mem; // Memory
126+
Py_ssize_t size; // Size in DeltaChunks
127+
Py_ssize_t reserved_size; // Reserve in DeltaChunks
128+
} DeltaChunkVector;
138129

139130
/*
140131
Grow the delta chunk list by the given amount of bytes.
@@ -143,20 +134,76 @@ large enough.
143134
Return 1 on success, 0 on failure
144135
*/
145136
static
146-
int DCL_grow(DeltaChunkList* self, ull num_dc)
137+
int DCV_grow(DeltaChunkVector* vec, uint num_dc)
147138
{
148-
const ull grow_by_chunks = (self->size + num_dc) - self->reserved_size;
139+
const ull grow_by_chunks = (vec->size + num_dc) - vec->reserved_size;
149140
if (grow_by_chunks <= 0){
150141
return 1;
151142
}
152143

153-
if (self->mem){
154-
self->mem = PyMem_Malloc(grow_by_chunks*sizeof(DeltaChunk));
144+
if (vec->mem){
145+
vec->mem = PyMem_Malloc(grow_by_chunks*sizeof(DeltaChunk));
155146
} else {
156-
self->mem = PyMem_Realloc(self->mem, (self->size + grow_by_chunks)*sizeof(DeltaChunk));
147+
vec->mem = PyMem_Realloc(vec->mem, (vec->size + grow_by_chunks)*sizeof(DeltaChunk));
157148
}
158149

159-
return self->mem != NULL;
150+
return vec->mem != NULL;
151+
}
152+
153+
int DCV_init(DeltaChunkVector* vec, ull initial_size)
154+
{
155+
vec->mem = NULL;
156+
vec->size = 0;
157+
vec->reserved_size = 0;
158+
159+
return DCV_grow(vec, initial_size);
160+
}
161+
162+
163+
void DCV_dealloc(DeltaChunkVector* vec)
164+
{
165+
if (vec->mem){
166+
PyMem_Free(vec->mem);
167+
vec->size = 0;
168+
vec->reserved_size = 0;
169+
vec->mem = 0;
170+
}
171+
}
172+
173+
static inline
174+
ull DCV_len(DeltaChunkVector* vec)
175+
{
176+
return vec->size;
177+
}
178+
179+
// Return item at index
180+
static inline
181+
DeltaChunk* DCV_get(DeltaChunkVector* vec, Py_ssize_t i)
182+
{
183+
assert(i < vec->size && vec->mem);
184+
return &(vec->mem[i]);
185+
}
186+
187+
static inline
188+
int DCV_empty(DeltaChunkVector* vec)
189+
{
190+
return vec->size == 0;
191+
}
192+
193+
// DELTA CHUNK LIST (PYTHON)
194+
/////////////////////////////
195+
196+
typedef struct {
197+
PyObject_HEAD
198+
// -----------
199+
DeltaChunkVector vec;
200+
201+
} DeltaChunkList;
202+
203+
static
204+
int DCL_new(DeltaChunkList* self, PyObject* args, PyObject* kwds)
205+
{
206+
return DCV_init(&self->vec, 0);
160207
}
161208

162209

@@ -174,34 +221,27 @@ int DCL_init(DeltaChunkList *self, PyObject *args, PyObject *kwds)
174221
init_size = 125000;
175222
}
176223

177-
return DCL_grow(self, init_size);
224+
return DCV_grow(&self->vec, init_size);
178225
}
179226

180227
static
181228
void DCL_dealloc(DeltaChunkList* self)
182229
{
183-
// TODO: deallocate linked list
184-
if (self->mem){
185-
PyMem_Free(self->mem);
186-
self->size = 0;
187-
self->reserved_size = 0;
188-
self->mem = 0;
189-
}
230+
DCV_dealloc(&self->vec);
190231
}
191232

192233
static
193-
PyObject* DCL_len(PyObject* self)
234+
PyObject* DCL_len(DeltaChunkList* self)
194235
{
195-
return PyLong_FromUnsignedLongLong(0);
236+
return PyLong_FromUnsignedLongLong(DCV_len(&self->vec));
196237
}
197238

198-
static
199-
inline
239+
static inline
200240
ull DCL_rbound(DeltaChunkList* self)
201241
{
202-
if (!self->mem | !self->size)
242+
if (DCV_empty(&self->vec))
203243
return 0;
204-
return DC_rbound(&(self->mem[self->size-1]));
244+
return DC_rbound(DCV_get(&self->vec, self->vec.size - 1));
205245
}
206246

207247
static

0 commit comments

Comments
 (0)