@@ -136,16 +136,18 @@ Return 1 on success, 0 on failure
136136static
137137int DCV_grow (DeltaChunkVector * vec , uint num_dc )
138138{
139- const ull grow_by_chunks = (vec -> size + num_dc ) - vec -> reserved_size ;
139+ const uint grow_by_chunks = (vec -> size + num_dc ) - vec -> reserved_size ;
140140 if (grow_by_chunks <= 0 ){
141141 return 1 ;
142142 }
143143
144- if (vec -> mem ){
145- vec -> mem = PyMem_Malloc (grow_by_chunks * sizeof (DeltaChunk ));
144+ if (vec -> mem == NULL ){
145+ vec -> mem = PyMem_Malloc (grow_by_chunks * sizeof (vec -> mem ));
146146 } else {
147- vec -> mem = PyMem_Realloc (vec -> mem , (vec -> size + grow_by_chunks )* sizeof (DeltaChunk ));
147+ vec -> mem = PyMem_Realloc (vec -> mem , (vec -> reserved_size + grow_by_chunks ) * sizeof (vec -> mem ));
148148 }
149+ assert (vec -> mem != NULL );
150+ vec -> reserved_size = vec -> reserved_size + grow_by_chunks ;
149151
150152 return vec -> mem != NULL ;
151153}
@@ -159,17 +161,6 @@ int DCV_init(DeltaChunkVector* vec, ull initial_size)
159161 return DCV_grow (vec , initial_size );
160162}
161163
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-
173164static inline
174165ull DCV_len (DeltaChunkVector * vec )
175166{
@@ -181,7 +172,7 @@ static inline
181172DeltaChunk * DCV_get (DeltaChunkVector * vec , Py_ssize_t i )
182173{
183174 assert (i < vec -> size && vec -> mem );
184- return & ( vec -> mem [i ]) ;
175+ return & vec -> mem [i ];
185176}
186177
187178static inline
@@ -190,6 +181,48 @@ int DCV_empty(DeltaChunkVector* vec)
190181 return vec -> size == 0 ;
191182}
192183
184+ // Return end pointer of the vector
185+ static inline
186+ DeltaChunk * DCV_end (DeltaChunkVector * vec )
187+ {
188+ assert (!DCV_empty (vec ));
189+ return & vec -> mem [vec -> size ];
190+ }
191+
192+ void DCV_dealloc (DeltaChunkVector * vec )
193+ {
194+ if (vec -> mem ){
195+ if (vec -> size ){
196+ const DeltaChunk * end = DCV_end (vec );
197+ DeltaChunk * i ;
198+ for (i = & vec -> mem [0 ]; i < end ; i ++ ){
199+ DC_destroy (i );
200+ }
201+ }
202+ PyMem_Free (vec -> mem );
203+ vec -> size = 0 ;
204+ vec -> reserved_size = 0 ;
205+ vec -> mem = 0 ;
206+ }
207+ }
208+
209+ // Append num-chunks to the end of the list, possibly reallocating existing ones
210+ // Return a pointer to the first of the added items. They are not yet initialized
211+ // If num-chunks == 0, it returns the end pointer of the allocated memory
212+ static inline
213+ DeltaChunk * DCV_append_multiple (DeltaChunkVector * vec , uint num_chunks )
214+ {
215+ if (vec -> size + num_chunks > vec -> reserved_size ){
216+ if (!DCV_grow (vec , (vec -> size + num_chunks ) - vec -> reserved_size )){
217+ Py_FatalError ("Could not allocate memory for append operation" );
218+ }
219+ }
220+ Py_FatalError ("Could not allocate memory for append operation" );
221+ Py_ssize_t old_size = vec -> size ;
222+ vec -> size += num_chunks ;
223+ return & vec -> mem [old_size ];
224+ }
225+
193226// DELTA CHUNK LIST (PYTHON)
194227/////////////////////////////
195228
@@ -200,12 +233,6 @@ typedef struct {
200233
201234} DeltaChunkList ;
202235
203- static
204- int DCL_new (DeltaChunkList * self , PyObject * args , PyObject * kwds )
205- {
206- return DCV_init (& self -> vec , 0 );
207- }
208-
209236
210237static
211238int DCL_init (DeltaChunkList * self , PyObject * args , PyObject * kwds )
@@ -215,19 +242,20 @@ int DCL_init(DeltaChunkList *self, PyObject *args, PyObject *kwds)
215242 return 0 ;
216243 }
217244
245+ assert (self -> vec .mem == NULL );
246+
218247 ull init_size = 0 ;
219248 PyArg_ParseTuple (args , "K" , & init_size );
220249 if (init_size == 0 ){
221- init_size = 125000 ;
250+ init_size = 12500 ;
222251 }
223-
224- return DCV_grow (& self -> vec , init_size );
252+ return DCV_init (& self -> vec , init_size );
225253}
226254
227255static
228256void DCL_dealloc (DeltaChunkList * self )
229257{
230- DCV_dealloc (& self -> vec );
258+ DCV_dealloc (& ( self -> vec ) );
231259}
232260
233261static
@@ -305,7 +333,7 @@ static PyTypeObject DeltaChunkListType = {
305333 0 , /* tp_dictoffset */
306334 (initproc )DCL_init , /* tp_init */
307335 0 , /* tp_alloc */
308- ( newfunc ) DCL_new , /* tp_new */
336+ 0 , /* tp_new */
309337};
310338
311339
0 commit comments