libtrbase  1.0.2
Web server and task management solution.
memory.h File Reference
#include <stdlib.h>
#include <sys/types.h>
+ Include dependency graph for memory.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  memSegment
 

Macros

#define TR_MEM_FREE(seg)   (TR_free((void **)&(seg)))
 
#define LT(n)   n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
 

Functions

int TR_bitwidth (size_t value)
 
struct memSegment_getMemInfo (void *mem)
 
size_t TR_getSize (void *mem)
 
size_t TR_getUsableSize (void *mem)
 
int TR_getIdx (void *mem)
 
void * TR_malloc (size_t)
 
void * TR_calloc (size_t, size_t)
 
void * TR_reference (void *)
 
void TR_free (void **)
 
void TR_cleanup ()
 
char * TR_strdup (const char *)
 

Detailed Description

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file memory.h.


Data Structure Documentation

struct memSegment

Definition at line 31 of file memory.h.

+ Collaboration diagram for memSegment:
Data Fields
int idx
struct memSegment * next
void * ptr
size_t ref_count
size_t size

Macro Definition Documentation

#define LT (   n)    n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n

Referenced by TR_bitwidth().

#define TR_MEM_FREE (   seg)    (TR_free((void **)&(seg)))

Definition at line 26 of file memory.h.

Referenced by sizedDataDtor(), TR_loggerLog(), and TR_sizedDataSetData().

Function Documentation

struct memSegment* _getMemInfo ( void *  mem)

Definition at line 84 of file memory.h.

85 {
86  if (NULL == mem) {
87  return 0;
88  }
89 
90  return (struct memSegment *)(mem - sizeof(struct memSegment));
91 }
int TR_bitwidth ( size_t  value)
inline

I found this at stanford.edu: https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup

Really cool way of dealing with this. The oneat stanford.edu is slightly different as ist deals only with 32bit values. I need a 64bit version because on a 64bit system size_t is also 64bit and thus it is possible to allocate that much amount of memory theoretically.

Definition at line 52 of file memory.h.

References LT.

Referenced by TR_malloc().

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 }
#define LT(n)

+ Here is the caller graph for this function:

void* TR_calloc ( size_t  nmemb,
size_t  size 
)

this is a really memory wasting solution....just to be able to use calloc, which might be faster then malloc/memset solution.

Maybe this is a bad idea, as we need to memset the buffer anyway if it comes from our tree, which hopefully should be the majority of cases.

Definition at line 242 of file memory.c.

References memSegment::size, TR_getUsableSize(), and TR_malloc().

243 {
244  size_t _size = nmemb * size;
245  void * mem = TR_malloc(_size);
246 
247  memset(mem, 0, TR_getUsableSize(mem));
248 
249  return mem;
250 }
void * TR_malloc(size_t size)
Definition: memory.c:178
size_t TR_getUsableSize(void *)
Definition: memory.h:105
size_t size
Definition: memory.h:34

+ Here is the call graph for this function:

void TR_cleanup ( )

Definition at line 276 of file memory.c.

References memSegment::next.

277 {
278 #ifdef MEM_OPT
279  int i;
280 
281  for (i=0; i<TR_MAX_MEM_IDX; i++) {
282  while(segments[i]) {
283  struct memSegment * next = segments[i]->next;
284  free(segments[i]);
285  segments[i] = next;
286  }
287  }
288 #endif
289 }
struct memSegment * next
Definition: memory.h:38
void TR_free ( void **  )

Definition at line 253 of file memory.c.

References memSegment::idx, and memSegment::ref_count.

254 {
255  if (NULL != *mem) {
256  struct memSegment * seg = (*mem - sizeof(struct memSegment));
257 
258  if (1 < seg->ref_count) {
259  seg->ref_count--;
260  } else {
261 #ifdef MEM_OPT
262  if (-1 != seg->idx) {
263  insertElement(&(segments[seg->idx]), seg);
264  } else
265 #endif
266  {
267  free(seg);
268  }
269  }
270 
271  *mem = NULL;
272  }
273 }
int idx
Definition: memory.h:35
size_t ref_count
Definition: memory.h:33
int TR_getIdx ( void *  mem)
inline

Definition at line 114 of file memory.h.

References memSegment::idx.

115 {
116  struct memSegment * segment =
117  (struct memSegment *)(mem - sizeof(struct memSegment));
118 
119  return segment ? segment->idx : -1;
120 }
int idx
Definition: memory.h:35
size_t TR_getSize ( void *  mem)
inline

Definition at line 95 of file memory.h.

References memSegment::size.

Referenced by TR_getUsableSize(), and TR_sizedDataSetData().

96 {
97  struct memSegment * segment =
98  (struct memSegment *)(mem - sizeof(struct memSegment));
99 
100  return segment ? segment->size : 0;
101 }
size_t size
Definition: memory.h:34

+ Here is the caller graph for this function:

size_t TR_getUsableSize ( void *  mem)
inline

Definition at line 105 of file memory.h.

References memSegment::size, and TR_getSize().

Referenced by TR_calloc().

106 {
107  size_t size = TR_getSize(mem);
108 
109  return size ? size - sizeof(struct memSegment) : 0;
110 }
size_t TR_getSize(void *mem)
Definition: memory.h:95
size_t size
Definition: memory.h:34

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* TR_malloc ( size_t  )

Definition at line 178 of file memory.c.

References memSegment::idx, MIN_BITS, newElement(), memSegment::ptr, and TR_bitwidth().

Referenced by sizedDataCtor(), TR_calloc(), TR_loggerLog(), TR_sizedDataSetData(), and TR_strdup().

179 {
180  struct memSegment * seg = NULL;
181  long psize = sysconf(_SC_PAGESIZE);
182  static int psize_width = 0;
183  int idx;
184 
185  psize_width = psize_width ? psize_width : TR_bitwidth(psize);
186 
187  size += sizeof(struct memSegment);
188 
189 #define MIN_BITS 8
190 
191  if (size >= psize) {
192  // get a multiple of pagesize
193  idx = size / psize;
194 
195  if (0 != (size % psize)) {
196  // size if not a multiple of pagesize so bring it to one.
197  idx++;
198  size = idx * psize;
199  }
200 
201  idx += psize_width - MIN_BITS;
202  } else {
203  if (size <= 1 << (MIN_BITS - 1)) {
204  size = 1 << (MIN_BITS - 1);
205  idx = 0;
206  } else {
207  // size-1 to ensure that powers of two will not be
208  // changed to the next power of two
209  idx = TR_bitwidth(size-1) + 1;
210  size = 1 << idx;
211  idx -= (MIN_BITS - 1);
212  }
213  }
214 
215 #undef MIN_BITS
216 
217 #ifdef MEM_OPT
218  if (idx < TR_MAX_MEM_IDX) {
219  seg = deleteElement(&(segments[idx]));
220  } else
221 #endif
222  {
223  idx = -1;
224  }
225 
226  if (NULL == seg) {
227  seg = newElement(size, idx);
228  }
229 
230  return seg->ptr;
231 }
void * ptr
Definition: memory.h:36
static struct memSegment * newElement(size_t size, int idx)
Definition: memory.c:80
size_t size
Definition: memory.h:34
int idx
Definition: memory.h:35
int TR_bitwidth(size_t)
Definition: memory.h:52
#define MIN_BITS

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* TR_reference ( void *  )

Definition at line 141 of file memory.c.

References memSegment::ref_count.

142 {
143  struct memSegment * seg = (mem - sizeof(struct memSegment));
144 
145  seg->ref_count++;
146 
147  return mem;
148 }
size_t ref_count
Definition: memory.h:33
char* TR_strdup ( const char *  )

Definition at line 292 of file memory.c.

References TR_malloc().

293 {
294  char * dup;
295 
296  if (NULL == src) {
297  return NULL;
298  }
299 
300  dup = TR_malloc(strlen(src)+1);
301  strcpy(dup, src);
302 
303  return dup;
304 }
void * TR_malloc(size_t size)
Definition: memory.c:178

+ Here is the call graph for this function: