LCOV - code coverage report
Current view: top level - include/tr - memory.h (source / functions) Hit Total Coverage
Test: libtrbase 1.0.2 Lines: 11 18 61.1 %
Date: 2016-04-12 12:13:13 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /**
       2                 :            :  * \file
       3                 :            :  *
       4                 :            :  * \author      Georg Hopp
       5                 :            :  *
       6                 :            :  * \copyright
       7                 :            :  * Copyright © 2014 Georg Hopp
       8                 :            :  *
       9                 :            :  * This program is free software: you can redistribute it and/or modify
      10                 :            :  * it under the terms of the GNU General Public License as published by
      11                 :            :  * the Free Software Foundation, either version 3 of the License, or
      12                 :            :  * (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This program is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17                 :            :  * GNU General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU General Public License
      20                 :            :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21                 :            :  */
      22                 :            : 
      23                 :            : #ifndef __TR_MEMORY_H__
      24                 :            : #define __TR_MEMORY_H__
      25                 :            : 
      26                 :            : #define TR_MEM_FREE(seg)        (TR_free((void **)&(seg)))
      27                 :            : 
      28                 :            : #include <stdlib.h>      // for NULL definition
      29                 :            : #include <sys/types.h>
      30                 :            : 
      31                 :            : struct memSegment
      32                 :            : {
      33                 :            :         size_t   ref_count;
      34                 :            :         size_t   size;
      35                 :            :         int      idx;
      36                 :            :         void   * ptr;
      37                 :            : 
      38                 :            :         struct memSegment * next;
      39                 :            : };
      40                 :            : 
      41                 :            : /**
      42                 :            :  * I found this at stanford.edu:
      43                 :            :  * https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
      44                 :            :  *
      45                 :            :  * Really cool way of dealing with this. The oneat stanford.edu is slightly
      46                 :            :  * different as ist deals only with 32bit values. I need a 64bit version
      47                 :            :  * because on a 64bit system size_t is also 64bit and thus it is possible
      48                 :            :  * to allocate that much amount of memory theoretically.
      49                 :            :  */
      50                 :            : inline
      51                 :            : int
      52                 :          1 : TR_bitwidth(size_t value)
      53                 :            : {
      54                 :            :         static const char LogTable256[256] = {
      55                 :            :                 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
      56                 :            : #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
      57                 :            :                 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
      58                 :            :                 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
      59                 :            :         };
      60                 :            : #undef LT
      61                 :            : 
      62                 :            :         int             r;          // r will be lg(v)
      63                 :            :         register size_t t1, t2, t3; // temporaries
      64                 :            : 
      65                 :          1 :         if (sizeof(value) == 8 && (t3 = value >> 32)) {
      66                 :          0 :                 if ((t2 = t3 >> 16)) {
      67                 :          0 :                         r = (t1 = t2 >> 8) ? 56 + LogTable256[t1] : 48 + LogTable256[t2];
      68                 :            :                 } else {
      69                 :          0 :                         r = (t1 = t3 >> 8) ? 40 + LogTable256[t1] : 32 + LogTable256[t3];
      70                 :            :                 }
      71                 :            :         } else {
      72                 :          1 :                 if ((t2 = value >> 16)) {
      73                 :          0 :                         r = (t1 = t2 >> 8) ? 24 + LogTable256[t1] : 16 + LogTable256[t2];
      74                 :            :                 } else {
      75                 :          1 :                         r = (t1 = value >> 8) ? 8 + LogTable256[t1] : LogTable256[value];
      76                 :            :                 }
      77                 :            :         }
      78                 :            : 
      79                 :          1 :         return r;
      80                 :            : }
      81                 :            : 
      82                 :            : inline
      83                 :            : struct memSegment *
      84                 :            : _getMemInfo(void * mem)
      85                 :            : {
      86                 :            :         if (NULL == mem) {
      87                 :            :                 return 0;
      88                 :            :         }
      89                 :            : 
      90                 :            :         return (struct memSegment *)(mem - sizeof(struct memSegment));
      91                 :            : }
      92                 :            : 
      93                 :            : inline
      94                 :            : size_t
      95                 :          5 : TR_getSize(void * mem)
      96                 :            : {
      97                 :          5 :         struct memSegment * segment =
      98                 :            :                 (struct memSegment *)(mem - sizeof(struct memSegment));
      99                 :            : 
     100                 :          5 :         return segment ? segment->size : 0;
     101                 :            : }
     102                 :            : 
     103                 :            : inline
     104                 :            : size_t
     105                 :          5 : TR_getUsableSize(void * mem)
     106                 :            : {
     107                 :          5 :         size_t size = TR_getSize(mem);
     108                 :            : 
     109                 :          5 :         return size ? size - sizeof(struct memSegment) : 0;
     110                 :            : }
     111                 :            : 
     112                 :            : inline
     113                 :            : int
     114                 :          0 : TR_getIdx(void * mem)
     115                 :            : {
     116                 :          0 :         struct memSegment * segment =
     117                 :            :                 (struct memSegment *)(mem - sizeof(struct memSegment));
     118                 :            : 
     119                 :          0 :         return segment ? segment->idx : -1;
     120                 :            : }
     121                 :            : 
     122                 :            : void   * TR_malloc(size_t);
     123                 :            : void   * TR_calloc(size_t, size_t);
     124                 :            : void   * TR_reference(void *);
     125                 :            : void     TR_free(void **);
     126                 :            : void     TR_cleanup();
     127                 :            : 
     128                 :            : char *   TR_strdup(const char *);
     129                 :            : 
     130                 :            : #endif // __TR_MEMORY_H__
     131                 :            : 
     132                 :            : // vim: set ts=4 sw=4:

Generated by: LCOV version 1.11