Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Peach_AudioChannelDividerAndCompensator
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, 2009, 2010 Max-Planck-Society 00029 * Author: Martin Reinecke 00030 */ 00031 00032 #include <stdio.h> 00033 #include <stdlib.h> 00034 #include "mytype.h" 00035 #include "c_utils.h " 00036 00037 void util_fail_ (const char *file, int line, const char *func, const char *msg) 00038 { 00039 fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg); 00040 exit(1); 00041 } 00042 void util_warn_ (const char *file, int line, const char *func, const char *msg) 00043 { 00044 fprintf(stderr,"%s, %i (%s):\n%s\n",file,line,func,msg); 00045 exit(1); 00046 } 00047 00048 /* This function tries to avoid allocations with a total size close to a high 00049 power of two (called the "critical stride" here), by adding a few more bytes 00050 if necssary. This lowers the probability that two arrays differ by a multiple 00051 of the critical stride in their starting address, which in turn lowers the 00052 risk of cache line contention. */ 00053 static size_t manipsize(size_t sz) 00054 { 00055 const size_t critical_stride=4096, cacheline=64, overhead=32; 00056 if (sz < (critical_stride/2)) return sz; 00057 if (((sz+overhead)%critical_stride)>(2*cacheline)) return sz; 00058 return sz+2*cacheline; 00059 } 00060 00061 #ifdef __SSE__ 00062 #include <xmmintrin.h> 00063 void *util_malloc_ (size_t sz) 00064 { 00065 void *res; 00066 if (sz==0) return NULL; 00067 res = _mm_malloc(manipsize(sz),16); 00068 UTIL_ASSERT(res,"_mm_malloc() failed"); 00069 return res; 00070 } 00071 void util_free_ (void *ptr) 00072 { if ((ptr)!=NULL) _mm_free(ptr); } 00073 #else 00074 void *util_malloc_ (size_t sz) 00075 { 00076 void *res; 00077 if (sz==0) return NULL; 00078 res = malloc(manipsize(sz)); 00079 UTIL_ASSERT(res,"malloc() failed"); 00080 return res; 00081 } 00082 void util_free_ (void *ptr) 00083 { if ((ptr)!=NULL) free(ptr); } 00084 #endif
Generated on Mon Jul 18 2022 23:30:05 by
1.7.2