c_utils.c

00001 /*
00002  *  This file is part of libc_utils.
00003  *
00004  *  libc_utils is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libc_utils is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libc_utils; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*
00026  *  Convenience functions
00027  *
00028  *  Copyright (C) 2008-2013 Max-Planck-Society
00029  *  Author: Martin Reinecke
00030  */
00031 
00032 #include <stdio.h>
00033 #include "c_utils.h"
00034 
00035 void util_fail_ (const char *file, int line, const char *func, const char *msg)
00036   {
00037   fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
00038   exit(1);
00039   }
00040 void util_warn_ (const char *file, int line, const char *func, const char *msg)
00041   {
00042   fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg);
00043   }
00044 
00045 /* This function tries to avoid allocations with a total size close to a high
00046    power of two (called the "critical stride" here), by adding a few more bytes
00047    if necssary. This lowers the probability that two arrays differ by a multiple
00048    of the critical stride in their starting address, which in turn lowers the
00049    risk of cache line contention. */
00050 static size_t manipsize(size_t sz)
00051   {
00052   const size_t critical_stride=4096, cacheline=64, overhead=32;
00053   if (sz < (critical_stride/2)) return sz;
00054   if (((sz+overhead)%critical_stride)>(2*cacheline)) return sz;
00055   return sz+2*cacheline;
00056   }
00057 
00058 #ifdef __SSE__
00059 #include <xmmintrin.h>
00060 void *util_malloc_ (size_t sz)
00061   {
00062   void *res;
00063   if (sz==0) return NULL;
00064   res = _mm_malloc(manipsize(sz),16);
00065   UTIL_ASSERT(res,"_mm_malloc() failed");
00066   return res;
00067   }
00068 void util_free_ (void *ptr)
00069   { if ((ptr)!=NULL) _mm_free(ptr); }
00070 #else
00071 void *util_malloc_ (size_t sz)
00072   {
00073   void *res;
00074   if (sz==0) return NULL;
00075   res = malloc(manipsize(sz));
00076   UTIL_ASSERT(res,"malloc() failed");
00077   return res;
00078   }
00079 void util_free_ (void *ptr)
00080   { if ((ptr)!=NULL) free(ptr); }
00081 #endif

Generated on Thu Oct 8 14:48:49 2015 for LevelS C support library