Advertisement
vitor_expertise

Untitled

Nov 19th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.37 KB | None | 0 0
  1. diff --git a/src/lib/eina/eina_array.c b/src/lib/eina/eina_array.c
  2. index cda5181213..7548388cbe 100644
  3. --- a/src/lib/eina/eina_array.c
  4. +++ b/src/lib/eina/eina_array.c
  5. @@ -200,7 +200,9 @@ eina_array_grow(Eina_Array *array)
  6.     EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
  7.     EINA_MAGIC_CHECK_ARRAY(array, EINA_FALSE);
  8.  
  9. -   total = array->total + array->step;
  10. +   if (array->step) total = array->total + array->step;
  11. +   else total = (array->total ? array->total + (array->total+1u)/2u : 1u);
  12. +
  13.     tmp = realloc(array->data, sizeof (void *) * total);
  14.     if (EINA_UNLIKELY(!tmp)) return 0;
  15.  
  16. @@ -210,6 +212,53 @@ eina_array_grow(Eina_Array *array)
  17.     return 1;
  18.  }
  19.  
  20. +EAPI Eina_Bool
  21. +eina_array_reserve(Eina_Array *array, unsigned int total)
  22. +{
  23. +   void **tmp;
  24. +
  25. +   EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
  26. +   EINA_MAGIC_CHECK_ARRAY(array, EINA_FALSE);
  27. +
  28. +   if (total <= array->total)
  29. +     return EINA_TRUE;
  30. +
  31. +   if (array->step)
  32. +     total = ((total / array->step) + (total % array->step ? 1u : 0u)) * array->step;
  33. +
  34. +   tmp = realloc(array->data, sizeof (void *) * total);
  35. +   if (EINA_UNLIKELY(!tmp)) return EINA_FALSE;
  36. +
  37. +   array->total = total;
  38. +   array->data = tmp;
  39. +
  40. +   return EINA_TRUE;
  41. +}
  42. +
  43. +EAPI Eina_Bool
  44. +eina_array_shrink_to_fit(Eina_Array *array)
  45. +{
  46. +   if (array->count == 0)
  47. +     {
  48. +        free(array->data);
  49. +        array->total = 0;
  50. +        array->data = NULL;
  51. +     }
  52. +   else if ((array->total - array->count) >= array->step)
  53. +     {  // If we reduced size by more than N (block size) then realloc back down
  54. +        unsigned int size;
  55. +        void **tmp;
  56. +
  57. +        // realloc back down - rounding up to the nearest step size
  58. +        size = (array->step ? ((array->count / array->step) + (array->count % array->step ? 1u : 0u)) * array->step : array->count);
  59. +        tmp = realloc(array->data, sizeof(void *) * size);
  60. +        if (!tmp) return EINA_FALSE;
  61. +        array->total = size;
  62. +        array->data = tmp;
  63. +     }
  64. +   return EINA_TRUE;
  65. +}
  66. +
  67.  /**
  68.   * @endcond
  69.   */
  70. @@ -347,7 +396,7 @@ eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data,
  71.                                                         void *gdata),
  72.                    void *gdata)
  73.  {
  74. -   unsigned int i, j, size, count;
  75. +   unsigned int i, j, count;
  76.     void *data, **tmp;
  77.  
  78.     EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
  79. @@ -367,27 +416,9 @@ eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data,
  80.               j++;
  81.            }
  82.       }
  83. +   // 2. resize and realloc back down
  84.     array->count = j;
  85. -   // 2. if we reduced size by more than N (block size) then realloc back down
  86. -   if ((array->total - array->count) >= array->step)
  87. -     {
  88. -        if (array->count == 0)
  89. -          {
  90. -             free(array->data);
  91. -             array->total = 0;
  92. -             array->data = NULL;
  93. -          }
  94. -        else
  95. -          {
  96. -             // realloc back down - rounding up to the nearest step size
  97. -             size = ((array->count / array->step) + (array->count % array->step ? 1 : 0)) * array->step;
  98. -             tmp = realloc(array->data, sizeof(void *) * size);
  99. -             if (!tmp) return EINA_FALSE;
  100. -             array->total = size;
  101. -             array->data = tmp;
  102. -          }
  103. -     }
  104. -   return EINA_TRUE;
  105. +   return eina_array_shrink_to_fit(array);
  106.  }
  107.  
  108.  EAPI Eina_Iterator *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement