libtrbase  1.0.2
Web server and task management solution.
memory.h
Go to the documentation of this file.
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 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  if (sizeof(value) == 8 && (t3 = value >> 32)) {
66  if ((t2 = t3 >> 16)) {
67  r = (t1 = t2 >> 8) ? 56 + LogTable256[t1] : 48 + LogTable256[t2];
68  } else {
69  r = (t1 = t3 >> 8) ? 40 + LogTable256[t1] : 32 + LogTable256[t3];
70  }
71  } else {
72  if ((t2 = value >> 16)) {
73  r = (t1 = t2 >> 8) ? 24 + LogTable256[t1] : 16 + LogTable256[t2];
74  } else {
75  r = (t1 = value >> 8) ? 8 + LogTable256[t1] : LogTable256[value];
76  }
77  }
78 
79  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 TR_getSize(void * mem)
96 {
97  struct memSegment * segment =
98  (struct memSegment *)(mem - sizeof(struct memSegment));
99 
100  return segment ? segment->size : 0;
101 }
102 
103 inline
104 size_t
105 TR_getUsableSize(void * mem)
106 {
107  size_t size = TR_getSize(mem);
108 
109  return size ? size - sizeof(struct memSegment) : 0;
110 }
111 
112 inline
113 int
114 TR_getIdx(void * mem)
115 {
116  struct memSegment * segment =
117  (struct memSegment *)(mem - sizeof(struct memSegment));
118 
119  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:
void * TR_reference(void *)
Definition: memory.c:141
void * ptr
Definition: memory.h:36
void * TR_malloc(size_t)
Definition: memory.c:178
size_t TR_getSize(void *mem)
Definition: memory.h:95
size_t size
Definition: memory.h:34
void TR_cleanup()
Definition: memory.c:276
int idx
Definition: memory.h:35
struct memSegment * next
Definition: memory.h:38
char * TR_strdup(const char *)
Definition: memory.c:292
size_t ref_count
Definition: memory.h:33
void * TR_calloc(size_t, size_t)
Definition: memory.c:242
void TR_free(void **)
Definition: memory.c:253
int TR_bitwidth(size_t value)
Definition: memory.h:52
int TR_getIdx(void *mem)
Definition: memory.h:114
struct memSegment * _getMemInfo(void *mem)
Definition: memory.h:84
size_t TR_getUsableSize(void *mem)
Definition: memory.h:105
#define LT(n)