ucx/utils.c

Mon, 14 Jul 2014 13:20:03 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 Jul 2014 13:20:03 +0200
changeset 181
1e9012ad8215
parent 177
11ad03783baf
child 192
1e51558b9d09
permissions
-rw-r--r--

fixed stream copy bug + fixed doc issues in mempool

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@177 4 * Copyright 2014 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
universe@103 27 */
universe@103 28
universe@94 29 #include "utils.h"
universe@140 30 #include <math.h>
olaf@142 31 #include <stdio.h>
olaf@142 32 #include <limits.h>
olaf@142 33 #include <errno.h>
universe@89 34
universe@94 35 /* COPY FUCNTIONS */
universe@94 36 void* ucx_strcpy(void* s, void* data) {
universe@94 37 char *str = (char*) s;
universe@94 38 size_t n = 1+strlen(str);
universe@94 39 char *cpy = (char*) malloc(n);
universe@94 40 memcpy(cpy, str, n);
universe@94 41 return cpy;
universe@94 42 }
universe@94 43
universe@94 44 void* ucx_memcpy(void* m, void* n) {
universe@94 45 size_t k = *((size_t*)n);
universe@94 46 void *cpy = malloc(k);
universe@94 47 memcpy(cpy, m, k);
universe@94 48 return cpy;
universe@94 49 }
universe@94 50
universe@140 51 size_t ucx_stream_copy(void *src, void *dest, read_func readfnc,
universe@140 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
universe@140 53 if(n == 0 || bufsize == 0) {
universe@140 54 return 0;
universe@140 55 }
universe@140 56
universe@181 57 char *lbuf;
universe@140 58 size_t ncp = 0;
universe@181 59
universe@181 60 if(buf) {
universe@181 61 lbuf = buf;
universe@181 62 } else {
universe@181 63 lbuf = (char*)malloc(bufsize);
universe@181 64 if(lbuf == NULL) {
universe@140 65 return 0;
universe@140 66 }
universe@140 67 }
universe@140 68
universe@140 69 size_t r;
universe@140 70 size_t rn = bufsize > n ? n : bufsize;
universe@181 71 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
universe@181 72 r = writefnc(lbuf, 1, r, dest);
universe@140 73 ncp += r;
universe@140 74 n -= r;
universe@140 75 rn = bufsize > n ? n : bufsize;
universe@140 76 if(r == 0 || n == 0) {
universe@140 77 break;
universe@140 78 }
universe@140 79 }
universe@140 80
universe@181 81 if (lbuf != buf) {
universe@181 82 free(lbuf);
universe@181 83 }
universe@181 84
universe@140 85 return ncp;
universe@140 86 }
universe@140 87
olaf@142 88 /* COMPARE FUNCTIONS */
universe@94 89
universe@89 90 int ucx_strcmp(void *s1, void *s2, void *data) {
universe@89 91 return strcmp((char*)s1, (char*)s2);
universe@89 92 }
universe@89 93
universe@89 94 int ucx_strncmp(void *s1, void *s2, void *n) {
universe@89 95 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
universe@89 96 }
universe@89 97
universe@89 98 int ucx_intcmp(void *i1, void *i2, void *data) {
universe@89 99 int a = *((int*) i1);
universe@89 100 int b = *((int*) i2);
universe@89 101 if (a == b) {
universe@89 102 return 0;
universe@89 103 } else {
universe@89 104 return a < b ? -1 : 1;
universe@89 105 }
universe@89 106 }
universe@89 107
universe@92 108 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
universe@92 109 float a = *((float*) f1);
universe@92 110 float b = *((float*) f2);
universe@92 111 float e = !epsilon ? 1e-6f : *((float*)epsilon);
universe@92 112 if (fabsf(a - b) < e) {
universe@92 113 return 0;
universe@92 114 } else {
universe@92 115 return a < b ? -1 : 1;
universe@92 116 }
universe@92 117 }
universe@92 118
universe@92 119 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
universe@92 120 double a = *((float*) d1);
universe@92 121 double b = *((float*) d2);
universe@92 122 double e = !epsilon ? 1e-14 : *((double*)epsilon);
universe@92 123 if (fabs(a - b) < e) {
universe@92 124 return 0;
universe@92 125 } else {
universe@92 126 return a < b ? -1 : 1;
universe@92 127 }
universe@92 128 }
universe@92 129
universe@89 130 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
universe@89 131 if (ptr1 == ptr2) {
universe@89 132 return 0;
universe@89 133 } else {
universe@89 134 return ptr1 < ptr2 ? -1 : 1;
universe@89 135 }
universe@89 136 }
universe@91 137
universe@91 138 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
universe@91 139 return memcmp(ptr1, ptr2, *((size_t*)n));
universe@91 140 }
olaf@142 141
olaf@142 142 /* PRINTF FUNCTIONS */
olaf@142 143
universe@150 144 #ifdef va_copy
olaf@142 145 #define UCX_PRINTF_BUFSIZE 256
universe@150 146 #else
universe@150 147 #pragma message("WARNING: C99 va_copy macro not supported by this platform" \
universe@150 148 " - limiting ucx_*printf to 2 KiB")
universe@150 149 #define UCX_PRINTF_BUFSIZE 0x800
universe@150 150 #endif
olaf@142 151
olaf@142 152 int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
universe@150 153 int ret;
olaf@142 154 va_list ap;
olaf@142 155 va_start(ap, fmt);
olaf@142 156 ret = ucx_vfprintf(stream, wfc, fmt, ap);
olaf@142 157 va_end(ap);
olaf@142 158 return ret;
olaf@142 159 }
olaf@142 160
olaf@142 161 int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
olaf@142 162 char buf[UCX_PRINTF_BUFSIZE];
universe@150 163 #ifdef va_copy
olaf@144 164 va_list ap2;
olaf@144 165 va_copy(ap2, ap);
olaf@142 166 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 167 if (ret < 0) {
olaf@142 168 return ret;
olaf@142 169 } else if (ret < UCX_PRINTF_BUFSIZE) {
olaf@142 170 return (int)wfc(buf, 1, ret, stream);
olaf@142 171 } else {
olaf@142 172 if (ret == INT_MAX) {
olaf@142 173 errno = ENOMEM;
olaf@142 174 return -1;
olaf@142 175 }
olaf@142 176
olaf@142 177 int len = ret + 1;
olaf@142 178 char *newbuf = (char*)malloc(len);
olaf@142 179 if (!newbuf) {
olaf@142 180 return -1;
olaf@142 181 }
olaf@142 182
olaf@144 183 ret = vsnprintf(newbuf, len, fmt, ap2);
olaf@142 184 if (ret > 0) {
olaf@142 185 ret = (int)wfc(newbuf, 1, ret, stream);
olaf@142 186 }
olaf@142 187 free(newbuf);
olaf@142 188 }
olaf@142 189 return ret;
universe@150 190 #else
universe@150 191 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 192 if (ret < 0) {
universe@150 193 return ret;
universe@150 194 } else if (ret < UCX_PRINTF_BUFSIZE) {
universe@150 195 return (int)wfc(buf, 1, ret, stream);
universe@150 196 } else {
universe@150 197 errno = ENOMEM;
universe@150 198 return -1;
universe@150 199 }
universe@150 200 #endif
olaf@142 201 }
olaf@142 202
olaf@142 203 sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
olaf@142 204 va_list ap;
olaf@142 205 sstr_t ret;
olaf@142 206 va_start(ap, fmt);
olaf@142 207 ret = ucx_vasprintf(allocator, fmt, ap);
olaf@142 208 va_end(ap);
olaf@142 209 return ret;
olaf@142 210 }
olaf@142 211
olaf@142 212 sstr_t ucx_vasprintf(UcxAllocator *a, const char *fmt, va_list ap) {
olaf@142 213 sstr_t s;
olaf@142 214 s.ptr = NULL;
olaf@142 215 s.length = 0;
universe@150 216 char buf[UCX_PRINTF_BUFSIZE];
universe@150 217 #ifdef va_copy
olaf@144 218 va_list ap2;
olaf@144 219 va_copy(ap2, ap);
olaf@142 220 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
olaf@142 221 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 222 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 223 if (s.ptr) {
universe@174 224 s.length = (size_t)ret;
universe@174 225 memcpy(s.ptr, buf, ret);
universe@174 226 s.ptr[s.length] = '\0';
universe@174 227 }
olaf@142 228 } else if (ret == INT_MAX) {
olaf@142 229 errno = ENOMEM;
olaf@142 230 } else {
olaf@142 231 int len = ret + 1;
universe@173 232 s.ptr = (char*)almalloc(a, len);
universe@174 233 if (s.ptr) {
universe@174 234 ret = vsnprintf(s.ptr, len, fmt, ap2);
universe@174 235 if (ret < 0) {
universe@174 236 free(s.ptr);
universe@174 237 s.ptr = NULL;
universe@174 238 } else {
universe@174 239 s.length = (size_t)ret;
universe@174 240 }
olaf@142 241 }
olaf@142 242 }
universe@150 243 #else
universe@150 244 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
universe@150 245 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
universe@173 246 s.ptr = (char*)almalloc(a, ret + 1);
universe@174 247 if (s.ptr) {
universe@174 248 s.length = (size_t)ret;
universe@174 249 memcpy(s.ptr, buf, ret);
universe@174 250 s.ptr[s.length] = '\0';
universe@174 251 }
universe@150 252 } else {
universe@150 253 errno = ENOMEM;
universe@150 254 }
universe@150 255 #endif
olaf@142 256 return s;
olaf@142 257 }

mercurial