@@ -19,6 +19,10 @@ local pairs = pairs
1919local tostring = tostring
2020local tonumber = tonumber
2121local bxor = bit .bxor
22+ local error = error
23+
24+
25+ local CHASH_OK = 0
2226
2327
2428ffi .cdef [[
@@ -31,12 +35,12 @@ typedef struct {
3135
3236void chash_point_init (chash_point_t * points , uint32_t base_hash , uint32_t start ,
3337 uint32_t num , uint32_t id );
34- void chash_point_sort (chash_point_t * points , uint32_t size );
38+ int chash_point_sort (chash_point_t * points , uint32_t size );
3539
36- void chash_point_add (chash_point_t * old_points , uint32_t old_length ,
40+ int chash_point_add (chash_point_t * old_points , uint32_t old_length ,
3741 uint32_t base_hash , uint32_t from , uint32_t num , uint32_t id ,
3842 chash_point_t * new_points );
39- void chash_point_reduce (chash_point_t * old_points , uint32_t old_length ,
43+ int chash_point_reduce (chash_point_t * old_points , uint32_t old_length ,
4044 uint32_t base_hash , uint32_t from , uint32_t num , uint32_t id );
4145void chash_point_delete (chash_point_t * old_points , uint32_t old_length ,
4246 uint32_t id );
@@ -117,7 +121,9 @@ local function _precompute(nodes)
117121 start = start + num
118122 end
119123
120- clib .chash_point_sort (points , npoints )
124+ if clib .chash_point_sort (points , npoints ) ~= CHASH_OK then
125+ error (" no memory" )
126+ end
121127
122128 return ids , points , npoints , newnodes
123129end
@@ -194,14 +200,21 @@ local function _incr(self, id, weight)
194200 local new_npoints = self .npoints + weight * CONSISTENT_POINTS
195201 if self .size < new_npoints then
196202 new_points = ffi_new (chash_point_t , new_npoints )
197- self .size = new_npoints
198203 end
199204
200205 local base_hash = bxor (crc32 (tostring (id )), 0xffffffff )
201- clib .chash_point_add (self .points , self .npoints , base_hash ,
202- old_weight * CONSISTENT_POINTS ,
203- weight * CONSISTENT_POINTS ,
204- index , new_points )
206+ local rc = clib .chash_point_add (self .points , self .npoints , base_hash ,
207+ old_weight * CONSISTENT_POINTS ,
208+ weight * CONSISTENT_POINTS ,
209+ index , new_points )
210+
211+ if rc ~= CHASH_OK then
212+ error (" no memory" )
213+ end
214+
215+ if self .size < new_npoints then
216+ self .size = new_npoints
217+ end
205218
206219 self .points = new_points
207220 self .npoints = new_npoints
@@ -230,10 +243,15 @@ local function _decr(self, id, weight)
230243 end
231244
232245 local base_hash = bxor (crc32 (tostring (id )), 0xffffffff )
233- clib .chash_point_reduce (self .points , self .npoints , base_hash ,
234- (old_weight - weight ) * CONSISTENT_POINTS ,
235- CONSISTENT_POINTS * weight ,
236- index )
246+ local from = (old_weight - weight ) * CONSISTENT_POINTS
247+ local num = CONSISTENT_POINTS * weight
248+
249+ local rc = clib .chash_point_reduce (self .points , self .npoints , base_hash ,
250+ from , num , index )
251+
252+ if rc ~= CHASH_OK then
253+ error (" no memory" )
254+ end
237255
238256 nodes [id ] = old_weight - weight
239257 self .npoints = self .npoints - CONSISTENT_POINTS * weight
0 commit comments