inmemory_kv 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a97aba7afbf83a32217a12502a710f7a5de71409
4
- data.tar.gz: c95c7ecdd68a37366c98ec405fc77453e2097e16
3
+ metadata.gz: ee6413bdf95e2e902b4abb882e72284a8742c7bc
4
+ data.tar.gz: cdc96cbf5654abbd99aa4a263271e8a29e12c374
5
5
  SHA512:
6
- metadata.gz: 66ae0a210562dbae12f555c15313981b1187a4bf68ec83b02d7f4b3eab2863c90af88716508ed5f768acd315407f20fe9101f43376b211da43b8fa6afaf5703a
7
- data.tar.gz: 431d21bef61a4a003ec7a73d97ae10c51ed51c70d2270358bdc79e4054f416976345e9e89c03c6a774855d5ad590deefb1a7c6b52a6bffd5ac0607930ad37eff
6
+ metadata.gz: b442b6dabd287018b2e519d98eb182340f9e5653f6bc31f973492a965af6be7d4c3dc9324e5353efef71d42f42602f4b383da804cdb19f0504664dedcfac5883
7
+ data.tar.gz: 1826699cf819121d272488f187dd05367ca85db0944ff9b2ab7eecb95a3771a37cace4e895c4b82afaf1186aec7bbd4ef3063c3a583be8655423ae826023e17f
data/README.md CHANGED
@@ -90,3 +90,6 @@ sts['3'] != cpy['3']
90
90
  3. Commit your changes (`git commit -am 'Add some feature'`)
91
91
  4. Push to the branch (`git push origin my-new-feature`)
92
92
  5. Create a new Pull Request
93
+
94
+ ## Changelog
95
+ 0.1.5 - raise error on memory allocation failure
@@ -262,9 +262,11 @@ hash_insert(hash_table* tab, u32 hash) {
262
262
  u32 i, pos, buc, npos;
263
263
  if (tab->size == tab->alloced) {
264
264
  u32 new_alloced = tab->alloced ? tab->alloced * 1.5 : 32;
265
- tab->entries = realloc(tab->entries,
265
+ hash_entry* new_entries = realloc(tab->entries,
266
266
  sizeof(hash_entry)*new_alloced);
267
- assert(tab->entries);
267
+ if (new_entries == NULL)
268
+ return end;
269
+ tab->entries = new_entries;
268
270
  memset(tab->entries + tab->alloced, 0,
269
271
  sizeof(hash_entry)*(new_alloced - tab->alloced));
270
272
  for (i=tab->alloced; i<new_alloced-1; i++) {
@@ -275,8 +277,11 @@ hash_insert(hash_table* tab, u32 hash) {
275
277
  }
276
278
  if (tab->size >= tab->nbuckets * 2) {
277
279
  u32 new_nbuckets = tab->nbuckets ? (tab->nbuckets+1)*2-1 : 15;
280
+ u32* new_buckets = calloc(new_nbuckets, sizeof(u32));
281
+ if (new_buckets == NULL)
282
+ return end;
278
283
  free(tab->buckets);
279
- tab->buckets = calloc(new_nbuckets, sizeof(u32));
284
+ tab->buckets = new_buckets;
280
285
  for (i=0; i<tab->alloced; i++) {
281
286
  if (tab->entries[i].item == NULL)
282
287
  continue;
@@ -373,7 +378,7 @@ static hash_item*
373
378
  kv_insert(inmemory_kv *kv, const char* key, u32 key_size, const char* val, u32 val_size) {
374
379
  u32 hash = kv_hash(key, key_size);
375
380
  u32 pos;
376
- hash_item* item;
381
+ hash_item *item, *old_item = NULL;
377
382
  pos = hash_hash_first(&kv->tab, hash);
378
383
  while (pos != end) {
379
384
  item = kv->tab.entries[pos].item;
@@ -385,15 +390,13 @@ kv_insert(inmemory_kv *kv, const char* key, u32 key_size, const char* val, u32 v
385
390
  }
386
391
  if (pos == end) {
387
392
  pos = hash_insert(&kv->tab, hash);
393
+ if (pos == end)
394
+ return NULL;
388
395
  item = NULL;
389
396
  } else {
390
397
  hash_up(&kv->tab, pos);
391
398
  if (!item_compatible(item, val_size) || item->rc > 0) {
392
- kv->total_size -= item_size(item);
393
- if (item->rc > 0)
394
- item->rc--;
395
- else
396
- free(item);
399
+ old_item = item;
397
400
  item = NULL;
398
401
  }
399
402
  }
@@ -401,16 +404,24 @@ kv_insert(inmemory_kv *kv, const char* key, u32 key_size, const char* val, u32 v
401
404
  u32 new_size = item_need_size(key_size, val_size);
402
405
  #ifdef HAVE_MALLOC_USABLE_SIZE
403
406
  item = malloc(new_size);
404
- assert(item);
407
+ if (item == NULL)
408
+ return NULL;
405
409
  new_size = malloc_usable_size(item);
406
- item->rc = 0;
407
410
  #else
408
411
  new_size = (new_size + 7) & 7;
409
412
  item = malloc(new_size);
410
- assert(item);
411
- item->rc = 0;
413
+ if (item == NULL)
414
+ return NULL;
412
415
  item->item_size = new_size;
413
416
  #endif
417
+ if (old_item != NULL) {
418
+ kv->total_size -= item_size(old_item);
419
+ if (old_item->rc > 0)
420
+ old_item->rc--;
421
+ else
422
+ free(old_item);
423
+ }
424
+ item->rc = 0;
414
425
  kv->total_size += new_size;
415
426
  item_set_sizes(item, key_size, val_size);
416
427
  item->pos = pos;
@@ -632,7 +643,9 @@ rb_kv_set(VALUE self, VALUE vkey, VALUE vval) {
632
643
  val = RSTRING_PTR(vval);
633
644
  vsize = RSTRING_LEN(vval);
634
645
 
635
- kv_insert(kv, key, ksize, val, vsize);
646
+ if (kv_insert(kv, key, ksize, val, vsize) == NULL) {
647
+ rb_raise(rb_eNoMemError, "could not malloc");
648
+ }
636
649
 
637
650
  return vval;
638
651
  }
@@ -701,6 +714,9 @@ rb_kv_unshift(VALUE self, VALUE vkey, VALUE vval) {
701
714
  vsize = RSTRING_LEN(vval);
702
715
 
703
716
  item = kv_insert(kv, key, ksize, val, vsize);
717
+ if (item == NULL) {
718
+ rb_raise(rb_eNoMemError, "could not malloc");
719
+ }
704
720
  kv_down(kv, item);
705
721
 
706
722
  return vval;
@@ -1,3 +1,3 @@
1
1
  module InMemoryKV
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inmemory_kv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sokolov Yura aka funny_falcon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-01 00:00:00.000000000 Z
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler