Browse code

Does not use errno anymore, instead directly returne error code through rval.

Nat! authored on 29-04-2016 14:56:05
Showing 6 changed files
... ...
@@ -1,3 +1,8 @@
1
+# v0.4
2
+
3
+* does not use `errno` directly anymore, but instead returns the errno codes
4
+  as the return value (sometimes as negative numbers)
5
+
1 6
 # v0.3
2 7
 
3 8
 * change init error code to EINVAL, because that's what the other code uses.
... ...
@@ -69,8 +69,10 @@ static struct _mulle_concurrent_hashmapstorage *
69 69
 {
70 70
    struct _mulle_concurrent_hashmapstorage  *p;
71 71
    
72
-   if( n < 8)
73
-      n = 8;
72
+   assert( (~(n - 1) & n) == n);
73
+   
74
+   if( n < 4)
75
+      n = 4;
74 76
    
75 77
    p = _mulle_allocator_calloc( allocator, 1, sizeof( struct _mulle_concurrent_hashvaluepair) * (n - 1) +
76 78
                              sizeof( struct _mulle_concurrent_hashmapstorage));
... ...
@@ -301,7 +303,7 @@ static int   _mulle_concurrent_hashmapstorage_remove( struct _mulle_concurrent_h
301 303
       }
302 304
       
303 305
       if( entry->hash == MULLE_CONCURRENT_NO_HASH)
304
-         return( 0);
306
+         return( ENOENT);
305 307
       
306 308
       ++index;
307 309
       assert( index != sentinel);  // can't happen we always leave space
... ...
@@ -361,19 +363,17 @@ int  _mulle_concurrent_hashmap_init( struct mulle_concurrent_hashmap *map,
361 363
    
362 364
    assert( allocator->abafree && allocator->abafree != (void *) abort);
363 365
    if( ! allocator->abafree || allocator->abafree == (void *) abort)
364
-   {
365
-      errno = EINVAL;
366
-      return( -1);
367
-   }
366
+      return( EINVAL);
368 367
 
369 368
    map->allocator = allocator;
370 369
    storage        = _mulle_concurrent_alloc_hashmapstorage( size, allocator);
371 370
 
371
+   if( ! storage)
372
+      return( ENOMEM);
373
+
372 374
    _mulle_atomic_pointer_nonatomic_write( &map->storage.pointer, storage);
373 375
    _mulle_atomic_pointer_nonatomic_write( &map->next_storage.pointer, storage);
374 376
    
375
-   if( ! storage)
376
-      return( -1);
377 377
    return( 0);
378 378
 }
379 379
 
... ...
@@ -477,10 +477,7 @@ static int   _mulle_concurrent_hashmap_search_next( struct mulle_concurrent_hash
477 477
 retry:
478 478
    p = _mulle_atomic_pointer_read( &map->storage.pointer);
479 479
    if( *expect_mask && p->mask != *expect_mask)
480
-   {
481
-      errno = ECANCELED;
482
-      return( -1);
483
-   }
480
+      return( -ECANCELED);
484 481
    
485 482
    for(;;)
486 483
    {
... ...
@@ -495,7 +492,7 @@ retry:
495 492
       if( value == REDIRECT_VALUE)
496 493
       {
497 494
          if( _mulle_concurrent_hashmap_migrate_storage( map, p))
498
-            return( -1);
495
+            return( -ENOMEM);
499 496
          goto retry;
500 497
       }
501 498
    }
... ...
@@ -534,19 +531,18 @@ retry:
534 531
    if( n >= max)
535 532
    {
536 533
       if( _mulle_concurrent_hashmap_migrate_storage( map, p))
537
-         return( -1);
534
+         return( ENOMEM);
538 535
       goto retry;
539 536
    }
540 537
    
541 538
    switch( _mulle_concurrent_hashmapstorage_insert( p, hash, value))
542 539
    {
543 540
    case EEXIST :
544
-      errno = EEXIST;
545
-      return( -1);
541
+      return( EEXIST);
546 542
 
547 543
    case EBUSY  :
548 544
       if( _mulle_concurrent_hashmap_migrate_storage( map, p))
549
-         return( -1);
545
+         return( ENOMEM);
550 546
       goto retry;
551 547
    }
552 548
 
... ...
@@ -564,9 +560,12 @@ retry:
564 560
    p = _mulle_atomic_pointer_read( &map->storage.pointer);
565 561
    switch( _mulle_concurrent_hashmapstorage_remove( p, hash, value))
566 562
    {
563
+   case ENOENT :
564
+     return( ENOENT);
565
+         
567 566
    case EBUSY  :
568 567
       if( _mulle_concurrent_hashmap_migrate_storage( map, p))
569
-         return( -1);
568
+         return( ENOMEM);
570 569
       goto retry;
571 570
    }
572 571
    return( 0);
... ...
@@ -600,7 +599,7 @@ retry:
600 599
       rval = _mulle_concurrent_hashmapenumerator_next( &rover, NULL, NULL);
601 600
       if( ! rval)
602 601
          break;
603
-      if( rval == -1)
602
+      if( rval < 0)
604 603
       {
605 604
          _mulle_concurrent_hashmapenumerator_done( &rover);
606 605
          goto retry;
... ...
@@ -625,12 +624,9 @@ int  _mulle_concurrent_hashmapenumerator_next( struct mulle_concurrent_hashmapen
625 624
    intptr_t   hash;
626 625
    
627 626
    rval = _mulle_concurrent_hashmap_search_next( rover->map, &rover->mask, &rover->index, &hash, &value);
628
-   switch( rval)
629
-   {
630
-   case 0 :
631
-   case -1:
627
+   
628
+   if( rval <= 0)
632 629
       return( rval);
633
-   }
634 630
    
635 631
    if( p_hash)
636 632
       *p_hash = hash;
... ...
@@ -64,10 +64,17 @@ int  _mulle_concurrent_hashmap_init( struct mulle_concurrent_hashmap *map,
64 64
 void  _mulle_concurrent_hashmap_done( struct mulle_concurrent_hashmap *map);
65 65
 
66 66
 
67
+// if rval == 0, inserted
68
+// rval == EEXIST, detected duplicate
69
+// rval == ENOMEM, must be out of memory
67 70
 int  _mulle_concurrent_hashmap_insert( struct mulle_concurrent_hashmap *map,
68 71
                                        intptr_t hash,
69 72
                                        void *value);
70 73
 
74
+// if rval == 0, removed
75
+// rval == ENOENT, not found
76
+// rval == ENOMEM, must be out of memory
77
+//
71 78
 int  _mulle_concurrent_hashmap_remove( struct mulle_concurrent_hashmap *map,
72 79
                                        intptr_t hash,
73 80
                                        void *value);
... ...
@@ -109,8 +116,8 @@ static inline struct mulle_concurrent_hashmapenumerator  mulle_concurrent_hashma
109 116
 
110 117
 //  1 : OK
111 118
 //  0 : nothing left
112
-// -1 : failed to enumerate further
113
-//
119
+// -ECANCELLED: mutation alert
120
+// -ENOMEM    : out of memory
114 121
 int  _mulle_concurrent_hashmapenumerator_next( struct mulle_concurrent_hashmapenumerator *rover,
115 122
                                                intptr_t *hash,
116 123
                                                void **value);
... ...
@@ -180,19 +180,17 @@ int  _mulle_concurrent_pointerarray_init( struct mulle_concurrent_pointerarray *
180 180
    assert( allocator->abafree && allocator->abafree != (void *) abort);
181 181
    
182 182
    if( ! allocator->abafree || allocator->abafree == (void *) abort)
183
-   {
184
-      errno = EINVAL;
185
-      return( -1);
186
-   }
183
+      return( EINVAL);
187 184
    
188 185
    array->allocator = allocator;
189 186
    storage          = _mulle_concurrent_alloc_pointerarraystorage( size, allocator);
190
-   
187
+
188
+   if( ! storage)
189
+      return( ENOMEM);
190
+
191 191
    _mulle_atomic_pointer_nonatomic_write( &array->storage.pointer, storage);
192 192
    _mulle_atomic_pointer_nonatomic_write( &array->next_storage.pointer, storage);
193 193
    
194
-   if( ! storage)
195
-      return( -1);
196 194
    return( 0);
197 195
 }
198 196
 
... ...
@@ -51,15 +51,14 @@ static void  insert_something( struct mulle_concurrent_hashmap *map)
51 51
    value    = (void *) (hash * 10);
52 52
 
53 53
    rval = _mulle_concurrent_hashmap_insert( map, hash, value);
54
-   if( rval == 0)
55
-      return;
56 54
 
57
-   switch( errno)
55
+   switch( rval)
58 56
    {
59 57
    default :
60 58
       perror( "mulle_concurrent_hashmap_insert");
61 59
       abort();
62 60
 
61
+   case 0 :
63 62
    case EEXIST :
64 63
       return;
65 64
    }
... ...
@@ -76,7 +75,7 @@ static void  delete_something( struct mulle_concurrent_hashmap *map)
76 75
    value    = (void *) (hash * 10);
77 76
 
78 77
    rval = _mulle_concurrent_hashmap_remove( map, hash, value);
79
-   if( rval)
78
+   if( rval == ENOMEM)
80 79
    {
81 80
       perror( "mulle_concurrent_hashmap_remove");
82 81
       abort();
... ...
@@ -112,7 +111,7 @@ retry:
112 111
       rval = _mulle_concurrent_hashmapenumerator_next( &rover, &hash, &value);
113 112
       if( ! rval)
114 113
          break;
115
-      if( rval == -1)
114
+      if( rval < 0)
116 115
       {
117 116
          _mulle_concurrent_hashmapenumerator_done( &rover);
118 117
          goto retry;
... ...
@@ -51,15 +51,14 @@ static void  insert_something( struct mulle_concurrent_pointerarray *map)
51 51
    while( ! value);
52 52
 
53 53
    rval = _mulle_concurrent_pointerarray_add( map, value);
54
-   if( rval == 0)
55
-      return;
56 54
 
57
-   switch( errno)
55
+   switch( rval)
58 56
    {
59 57
    default :
60 58
       perror( "_mulle_concurrentpointerarray_add");
61 59
       abort();
62 60
 
61
+   case 0 :
63 62
    case EEXIST :
64 63
       return;
65 64
    }