00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libts.h"
00024
00025 #include <assert.h>
00026 #if defined(linux)
00027
00028 #ifndef _XOPEN_SOURCE
00029 #define _XOPEN_SOURCE 600
00030 #endif
00031 #endif
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 void *
00036 ats_malloc(size_t size)
00037 {
00038 void *ptr = NULL;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 if (likely(size > 0)) {
00050 if (unlikely((ptr = malloc(size)) == NULL)) {
00051 ink_stack_trace_dump();
00052 ink_fatal(1, "ats_malloc: couldn't allocate %zu bytes", size);
00053 }
00054 }
00055 return ptr;
00056 }
00057
00058 void *
00059 ats_calloc(size_t nelem, size_t elsize)
00060 {
00061 void *ptr = calloc(nelem, elsize);
00062 if (unlikely(ptr == NULL)) {
00063 ink_stack_trace_dump();
00064 ink_fatal(1, "ats_calloc: couldn't allocate %zu %zu byte elements", nelem, elsize);
00065 }
00066 return ptr;
00067 }
00068
00069 void *
00070 ats_realloc(void *ptr, size_t size)
00071 {
00072 void *newptr = realloc(ptr, size);
00073 if (unlikely(newptr == NULL)) {
00074 ink_stack_trace_dump();
00075 ink_fatal(1, "ats_realloc: couldn't reallocate %zu bytes", size);
00076 }
00077 return newptr;
00078 }
00079
00080
00081
00082 void *
00083 ats_memalign(size_t alignment, size_t size)
00084 {
00085 void *ptr;
00086
00087 #if HAVE_POSIX_MEMALIGN || TS_HAS_JEMALLOC
00088 if (alignment <= 8)
00089 return ats_malloc(size);
00090
00091 #if defined(openbsd)
00092 if (alignment > PAGE_SIZE)
00093 alignment = PAGE_SIZE;
00094 #endif
00095
00096 int retcode = posix_memalign(&ptr, alignment, size);
00097
00098 if (unlikely(retcode)) {
00099 if (retcode == EINVAL) {
00100 ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - invalid alignment parameter",
00101 size, alignment);
00102 } else if (retcode == ENOMEM) {
00103 ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - insufficient memory",
00104 size, alignment);
00105 } else {
00106 ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - unknown error %d",
00107 size, alignment, retcode);
00108 }
00109 }
00110 #else
00111 ptr = memalign(alignment, size);
00112 if (unlikely(ptr == NULL)) {
00113 ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu", size, alignment);
00114 }
00115 #endif
00116 return ptr;
00117 }
00118
00119 void
00120 ats_free(void *ptr)
00121 {
00122 if (likely(ptr != NULL))
00123 free(ptr);
00124 }
00125
00126 void*
00127 ats_free_null(void *ptr)
00128 {
00129 if (likely(ptr != NULL))
00130 free(ptr);
00131 return NULL;
00132 }
00133
00134 void
00135 ats_memalign_free(void *ptr)
00136 {
00137 if (likely(ptr))
00138 free(ptr);
00139 }
00140
00141
00142
00143
00144
00145
00146 int
00147 ats_mallopt(int param ATS_UNUSED, int value ATS_UNUSED)
00148 {
00149 #if TS_HAS_JEMALLOC
00150
00151 #else
00152 #if TS_HAS_TCMALLOC
00153
00154 #else
00155 #if defined(linux)
00156 return mallopt(param, value);
00157 #endif // ! defined(linux)
00158 #endif // ! TS_HAS_TCMALLOC
00159 #endif // ! TS_HAS_JEMALLOC
00160 return 0;
00161 }
00162
00163 int
00164 ats_msync(caddr_t addr, size_t len, caddr_t end, int flags)
00165 {
00166 size_t pagesize = ats_pagesize();
00167
00168
00169 caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1));
00170
00171 size_t l = (len + (addr - a) + (pagesize - 1)) & ~(pagesize - 1);
00172 if ((a + l) > end)
00173 l = end - a;
00174 #if defined(linux)
00175
00176
00177
00178
00179
00180 #if 0
00181
00182 if (flags & MS_SYNC)
00183 flags = (flags & ~MS_SYNC) | MS_ASYNC;
00184 #endif
00185 #endif
00186 int res = msync(a, l, flags);
00187 return res;
00188 }
00189
00190 int
00191 ats_madvise(caddr_t addr, size_t len, int flags)
00192 {
00193 #if defined(linux)
00194 (void) addr;
00195 (void) len;
00196 (void) flags;
00197 return 0;
00198 #else
00199 size_t pagesize = ats_pagesize();
00200 caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1));
00201 size_t l = (len + (addr - a) + pagesize - 1) & ~(pagesize - 1);
00202 int res = 0;
00203 #if HAVE_POSIX_MADVISE
00204 res = posix_madvise(a, l, flags);
00205 #else
00206 res = madvise(a, l, flags);
00207 #endif
00208 return res;
00209 #endif
00210 }
00211
00212 int
00213 ats_mlock(caddr_t addr, size_t len)
00214 {
00215 size_t pagesize = ats_pagesize();
00216
00217 caddr_t a = (caddr_t) (((uintptr_t) addr) & ~(pagesize - 1));
00218 size_t l = (len + (addr - a) + pagesize - 1) & ~(pagesize - 1);
00219 int res = mlock(a, l);
00220 return res;
00221 }
00222
00223
00224
00225
00226
00227 char *
00228 _xstrdup(const char *str, int length, const char* )
00229 {
00230 char *newstr;
00231
00232 if (likely(str)) {
00233 if (length < 0)
00234 length = strlen(str);
00235
00236 newstr = (char *)ats_malloc(length + 1);
00237 ink_strlcpy(newstr, str, length + 1);
00238 return newstr;
00239 }
00240 return NULL;
00241 }