ucx/utils.c

changeset 124
80609f9675f1
parent 0
1f419bd32da1
child 152
62921b370c60
equal deleted inserted replaced
123:55adc92e7c09 124:80609f9675f1
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved. 4 * Copyright 2015 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
52 write_func writefnc, char* buf, size_t bufsize, size_t n) { 52 write_func writefnc, char* buf, size_t bufsize, size_t n) {
53 if(n == 0 || bufsize == 0) { 53 if(n == 0 || bufsize == 0) {
54 return 0; 54 return 0;
55 } 55 }
56 56
57 char *lbuf;
57 size_t ncp = 0; 58 size_t ncp = 0;
58 if (!buf) { 59
59 buf = (char*)malloc(bufsize); 60 if(buf) {
60 if(buf == NULL) { 61 lbuf = buf;
62 } else {
63 lbuf = (char*)malloc(bufsize);
64 if(lbuf == NULL) {
61 return 0; 65 return 0;
62 } 66 }
63 } 67 }
64 68
65 size_t r; 69 size_t r;
66 size_t rn = bufsize > n ? n : bufsize; 70 size_t rn = bufsize > n ? n : bufsize;
67 while((r = readfnc(buf, 1, rn, src)) != 0) { 71 while((r = readfnc(lbuf, 1, rn, src)) != 0) {
68 r = writefnc(buf, 1, r, dest); 72 r = writefnc(lbuf, 1, r, dest);
69 ncp += r; 73 ncp += r;
70 n -= r; 74 n -= r;
71 rn = bufsize > n ? n : bufsize; 75 rn = bufsize > n ? n : bufsize;
72 if(r == 0 || n == 0) { 76 if(r == 0 || n == 0) {
73 break; 77 break;
74 } 78 }
75 } 79 }
76 80
77 free(buf); 81 if (lbuf != buf) {
82 free(lbuf);
83 }
84
78 return ncp; 85 return ncp;
79 } 86 }
80 87
81 /* COMPARE FUNCTIONS */ 88 /* COMPARE FUNCTIONS */
82 89
119 return a < b ? -1 : 1; 126 return a < b ? -1 : 1;
120 } 127 }
121 } 128 }
122 129
123 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 130 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
124 if (ptr1 == ptr2) { 131 intptr_t p1 = (intptr_t) ptr1;
132 intptr_t p2 = (intptr_t) ptr2;
133 if (p1 == p2) {
125 return 0; 134 return 0;
126 } else { 135 } else {
127 return ptr1 < ptr2 ? -1 : 1; 136 return p1 < p2 ? -1 : 1;
128 } 137 }
129 } 138 }
130 139
131 int ucx_memcmp(void *ptr1, void *ptr2, void *n) { 140 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
132 return memcmp(ptr1, ptr2, *((size_t*)n)); 141 return memcmp(ptr1, ptr2, *((size_t*)n));
210 #ifdef va_copy 219 #ifdef va_copy
211 va_list ap2; 220 va_list ap2;
212 va_copy(ap2, ap); 221 va_copy(ap2, ap);
213 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 222 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
214 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { 223 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
215 s.ptr = (char*)a->malloc(a->pool, ret + 1); 224 s.ptr = (char*)almalloc(a, ret + 1);
216 s.length = (size_t)ret; 225 if (s.ptr) {
217 memcpy(s.ptr, buf, ret); 226 s.length = (size_t)ret;
218 s.ptr[s.length] = '\0'; 227 memcpy(s.ptr, buf, ret);
228 s.ptr[s.length] = '\0';
229 }
219 } else if (ret == INT_MAX) { 230 } else if (ret == INT_MAX) {
220 errno = ENOMEM; 231 errno = ENOMEM;
221 } else { 232 } else {
222 int len = ret + 1; 233 int len = ret + 1;
223 s.ptr = (char*)a->malloc(a->pool, len); 234 s.ptr = (char*)almalloc(a, len);
224 ret = vsnprintf(s.ptr, len, fmt, ap2); 235 if (s.ptr) {
225 if (ret < 0) { 236 ret = vsnprintf(s.ptr, len, fmt, ap2);
226 free(s.ptr); 237 if (ret < 0) {
227 s.ptr = NULL; 238 free(s.ptr);
228 } else { 239 s.ptr = NULL;
240 } else {
241 s.length = (size_t)ret;
242 }
243 }
244 }
245 #else
246 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
247 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
248 s.ptr = (char*)almalloc(a, ret + 1);
249 if (s.ptr) {
229 s.length = (size_t)ret; 250 s.length = (size_t)ret;
230 } 251 memcpy(s.ptr, buf, ret);
231 } 252 s.ptr[s.length] = '\0';
232 #else 253 }
233 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
234 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
235 s.ptr = (char*)a->malloc(a->pool, ret + 1);
236 s.length = (size_t)ret;
237 memcpy(s.ptr, buf, ret);
238 s.ptr[s.length] = '\0';
239 } else { 254 } else {
240 errno = ENOMEM; 255 errno = ENOMEM;
241 } 256 }
242 #endif 257 #endif
243 return s; 258 return s;

mercurial