LCOV - code coverage report
Current view: top level - src/support - arraylist.c (source / functions) Hit Total Coverage
Test: [build process] commit ef510b1f346f4c9f9d86eaceace5ca54961a1dbc Lines: 68 90 75.6 %
Date: 2022-07-17 01:01:28 Functions: 7 10 70.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 23 40 57.5 %

           Branch data     Line data    Source code
       1                 :            : // This file is a part of Julia. License is MIT: https://julialang.org/license
       2                 :            : 
       3                 :            : #include <stdlib.h>
       4                 :            : #include <stdio.h>
       5                 :            : #include <string.h>
       6                 :            : #include <assert.h>
       7                 :            : #include <limits.h>
       8                 :            : 
       9                 :            : #include "dtypes.h"
      10                 :            : #include "arraylist.h"
      11                 :            : 
      12                 :            : #ifdef __cplusplus
      13                 :            : extern "C" {
      14                 :            : #endif
      15                 :            : 
      16                 :     326922 : arraylist_t *arraylist_new(arraylist_t *a, size_t size)
      17                 :            : {
      18                 :     326922 :     a->len = 0;
      19         [ +  + ]:     326922 :     if (size <= AL_N_INLINE) {
      20                 :     326912 :         a->items = &a->_space[0];
      21                 :     326912 :         a->max = AL_N_INLINE;
      22                 :            :     }
      23                 :            :     else {
      24                 :         10 :         a->items = (void**)LLT_ALLOC(size*sizeof(void*));
      25         [ -  + ]:         10 :         if (a->items == NULL) return NULL;
      26                 :         10 :         a->max = size;
      27                 :            :     }
      28                 :     326922 :     return a;
      29                 :            : }
      30                 :            : 
      31                 :     326088 : void arraylist_free(arraylist_t *a)
      32                 :            : {
      33         [ +  + ]:     326088 :     if (a->items != &a->_space[0])
      34                 :        164 :         LLT_FREE(a->items);
      35                 :     326088 :     a->len = 0;
      36                 :     326088 :     a->max = AL_N_INLINE;
      37                 :     326088 :     a->items = &a->_space[0];
      38                 :     326088 : }
      39                 :            : 
      40                 :  111010400 : void arraylist_grow(arraylist_t *a, size_t n)
      41                 :            : {
      42                 :  111010400 :     size_t len = a->len;
      43                 :  111010400 :     size_t newlen = len + n;
      44         [ +  + ]:  111010400 :     if (newlen > a->max) {
      45         [ +  + ]:       1966 :         if (a->items == &a->_space[0]) {
      46                 :        262 :             void **p = (void**)LLT_ALLOC((a->len+n)*sizeof(void*));
      47         [ -  + ]:        262 :             if (p == NULL) return;
      48                 :        262 :             memcpy(p, a->items, len * sizeof(void*));
      49                 :        262 :             a->items = p;
      50                 :        262 :             a->max = newlen;
      51                 :            :         }
      52                 :            :         else {
      53                 :       1704 :             size_t nm = a->max * 2;
      54         [ +  + ]:       1704 :             if (nm == 0)
      55                 :          8 :                 nm = 1;
      56         [ -  + ]:       1704 :             while (newlen > nm)
      57                 :          0 :                 nm *= 2;
      58                 :       1704 :             void **p = (void**)LLT_REALLOC(a->items, nm * sizeof(void*));
      59         [ -  + ]:       1704 :             if (p == NULL) return;
      60                 :       1704 :             a->items = p;
      61                 :       1704 :             a->max = nm;
      62                 :            :         }
      63                 :            :     }
      64                 :  111010400 :     a->len = newlen;
      65                 :            : }
      66                 :            : 
      67                 :  111010400 : void arraylist_push(arraylist_t *a, void *elt)
      68                 :            : {
      69                 :  111010400 :     arraylist_grow(a, 1);
      70                 :  111010400 :     a->items[a->len - 1] = elt;
      71                 :  111010400 : }
      72                 :            : 
      73                 :        280 : void *arraylist_pop(arraylist_t *a)
      74                 :            : {
      75         [ -  + ]:        280 :     if (a->len == 0) return NULL;
      76                 :        280 :     void *p = a->items[--a->len];
      77                 :        280 :     a->items[a->len] = NULL;
      78                 :        280 :     return p;
      79                 :            : }
      80                 :            : 
      81                 :            : // small arraylist
      82                 :            : 
      83                 :         30 : small_arraylist_t *small_arraylist_new(small_arraylist_t *a, uint32_t size)
      84                 :            : {
      85                 :         30 :     a->len = 0;
      86         [ +  - ]:         30 :     if (size <= SMALL_AL_N_INLINE) {
      87                 :         30 :         a->items = &a->_space[0];
      88                 :         30 :         a->max = SMALL_AL_N_INLINE;
      89                 :            :     }
      90                 :            :     else {
      91                 :          0 :         a->items = (void**)LLT_ALLOC(size*sizeof(void*));
      92         [ #  # ]:          0 :         if (a->items == NULL) return NULL;
      93                 :          0 :         a->max = size;
      94                 :            :     }
      95                 :         30 :     return a;
      96                 :            : }
      97                 :            : 
      98                 :          0 : void small_arraylist_free(small_arraylist_t *a)
      99                 :            : {
     100         [ #  # ]:          0 :     if (a->items != &a->_space[0])
     101                 :          0 :         LLT_FREE(a->items);
     102                 :          0 :     a->len = 0;
     103                 :          0 :     a->max = SMALL_AL_N_INLINE;
     104                 :          0 :     a->items = &a->_space[0];
     105                 :          0 : }
     106                 :            : 
     107                 :         38 : JL_DLLEXPORT void small_arraylist_grow(small_arraylist_t *a, uint32_t n)
     108                 :            : {
     109                 :         38 :     size_t len = a->len;
     110                 :         38 :     size_t newlen = len + n;
     111         [ +  - ]:         38 :     if (newlen > a->max) {
     112         [ +  + ]:         38 :         if (a->items == &a->_space[0]) {
     113                 :         26 :             void **p = (void**)LLT_ALLOC((a->len+n)*sizeof(void*));
     114         [ -  + ]:         26 :             if (p == NULL) return;
     115                 :         26 :             memcpy(p, a->items, len * sizeof(void*));
     116                 :         26 :             a->items = p;
     117                 :         26 :             a->max = newlen;
     118                 :            :         }
     119                 :            :         else {
     120                 :         12 :             size_t nm = a->max * 2;
     121         [ -  + ]:         12 :             if (nm == 0)
     122                 :          0 :                 nm = 1;
     123         [ -  + ]:         12 :             while (newlen > nm)
     124                 :          0 :                 nm *= 2;
     125                 :         12 :             void **p = (void**)LLT_REALLOC(a->items, nm * sizeof(void*));
     126         [ -  + ]:         12 :             if (p == NULL) return;
     127                 :         12 :             a->items = p;
     128                 :         12 :             a->max = nm;
     129                 :            :         }
     130                 :            :     }
     131                 :         38 :     a->len = newlen;
     132                 :            : }
     133                 :            : 
     134                 :          0 : void small_arraylist_push(small_arraylist_t *a, void *elt)
     135                 :            : {
     136                 :          0 :     small_arraylist_grow(a, 1);
     137                 :          0 :     a->items[a->len - 1] = elt;
     138                 :          0 : }
     139                 :            : 
     140                 :          0 : void *small_arraylist_pop(small_arraylist_t *a)
     141                 :            : {
     142         [ #  # ]:          0 :     if (a->len == 0) return NULL;
     143                 :          0 :     void *p = a->items[--a->len];
     144                 :          0 :     a->items[a->len] = NULL;
     145                 :          0 :     return p;
     146                 :            : }
     147                 :            : 
     148                 :            : #ifdef __cplusplus
     149                 :            : }
     150                 :            : #endif

Generated by: LCOV version 1.14