LevelS C support library  3.50
c_utils.c
1 /*
2  * This file is part of libc_utils.
3  *
4  * libc_utils is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libc_utils is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libc_utils; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*
26  * Convenience functions
27  *
28  * Copyright (C) 2008-2017 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include <stdio.h>
33 #include "c_utils.h"
34 
35 void util_fail_ (const char *file, int line, const char *func, const char *msg)
36  {
37  fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
38  exit(1);
39  }
40 void util_warn_ (const char *file, int line, const char *func, const char *msg)
41  {
42  fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
43  }
44 
45 /* This function tries to avoid allocations with a total size close to a high
46  power of two (called the "critical stride" here), by adding a few more bytes
47  if necessary. This lowers the probability that two arrays differ by a multiple
48  of the critical stride in their starting address, which in turn lowers the
49  risk of cache line contention. */
50 static size_t manipsize(size_t sz)
51  {
52  const size_t critical_stride=4096, cacheline=64, overhead=32;
53  if (sz < (critical_stride/2)) return sz;
54  if (((sz+overhead)%critical_stride)>(2*cacheline)) return sz;
55  return sz+2*cacheline;
56  }
57 
58 #ifdef __SSE__
59 #include <xmmintrin.h>
60 void *util_malloc_ (size_t sz)
61  {
62  void *res;
63  if (sz==0) return NULL;
64  res = _mm_malloc(manipsize(sz),32);
65  UTIL_ASSERT(res,"_mm_malloc() failed");
66  return res;
67  }
68 void util_free_ (void *ptr)
69  { if ((ptr)!=NULL) _mm_free(ptr); }
70 #else
71 void *util_malloc_ (size_t sz)
72  {
73  void *res;
74  if (sz==0) return NULL;
75  res = malloc(manipsize(sz));
76  UTIL_ASSERT(res,"malloc() failed");
77  return res;
78  }
79 void util_free_ (void *ptr)
80  { if ((ptr)!=NULL) free(ptr); }
81 #endif
#define UTIL_ASSERT(cond, msg)
Definition: c_utils.h:59

Generated on Mon Dec 10 2018 10:24:19 for LevelS C support library