c_utils.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00046
00047
00048
00049
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