X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Futil.h;h=5f7a93ab1e17adf473f520a95c0a76fa47593036;hb=refs%2Ftags%2Fupstream%2F0.6.15;hp=6191d439e4361f26058ec22107f9c5b0849343ab;hpb=a250cf21640477b117471a3a07d5e35c68abcdd5;p=platform%2Fupstream%2Flibsolv.git diff --git a/src/util.h b/src/util.h index 6191d43..5f7a93a 100644 --- a/src/util.h +++ b/src/util.h @@ -10,38 +10,51 @@ * */ -#ifndef SATSOLVER_UTIL_H -#define SATSOLVER_UTIL_H +#ifndef LIBSOLV_UTIL_H +#define LIBSOLV_UTIL_H #include #include +#ifdef __cplusplus +extern "C" { +#endif + /** * malloc * exits with error message on error */ -extern void *sat_malloc(size_t); -extern void *sat_malloc2(size_t, size_t); -extern void *sat_calloc(size_t, size_t); -extern void *sat_realloc(void *, size_t); -extern void *sat_realloc2(void *, size_t, size_t); -extern void *sat_free(void *); -extern void sat_oom(size_t, size_t); -extern unsigned int sat_timems(unsigned int subtract); -extern void sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard); -extern char *sat_dupjoin(const char *str1, const char *str2, const char *str3); +extern void *solv_malloc(size_t); +extern void *solv_malloc2(size_t, size_t); +extern void *solv_calloc(size_t, size_t); +extern void *solv_realloc(void *, size_t); +extern void *solv_realloc2(void *, size_t, size_t); +extern void *solv_extend_realloc(void *, size_t, size_t, size_t); +extern void *solv_free(void *); +extern char *solv_strdup(const char *); +extern void solv_oom(size_t, size_t); +extern unsigned int solv_timems(unsigned int subtract); +extern void solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard); +extern char *solv_dupjoin(const char *str1, const char *str2, const char *str3); +extern char *solv_dupappend(const char *str1, const char *str2, const char *str3); +extern int solv_hex2bin(const char **strp, unsigned char *buf, int bufl); +extern char *solv_bin2hex(const unsigned char *buf, int l, char *str); +extern size_t solv_validutf8(const char *buf); +extern char *solv_latin1toutf8(const char *buf); +extern char *solv_replacebadutf8(const char *buf, int replchar); + -static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block) +static inline void *solv_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block) { if (nmemb == 1) { if ((len & block) == 0) - buf = sat_realloc2(buf, len + (1 + block), size); + buf = solv_extend_realloc(buf, len + 1, size, block); } else { if (((len - 1) | block) != ((len + nmemb - 1) | block)) - buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size); + buf = solv_extend_realloc(buf, len + nmemb, size, block); } return buf; } @@ -54,27 +67,54 @@ static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, * size size of each element * block block size used to allocate the elements */ -static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block) +static inline void *solv_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block) { - buf = sat_extend(buf, len, nmemb, size, block); + buf = solv_extend(buf, len, nmemb, size, block); memset((char *)buf + len * size, 0, nmemb * size); return buf; } -static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block) +static inline void *solv_extend_resize(void *buf, size_t len, size_t size, size_t block) { if (len) - buf = sat_realloc2(buf, (len + block) & ~block, size); + buf = solv_extend_realloc(buf, len, size, block); return buf; } -static inline void *sat_calloc_block(size_t len, size_t size, size_t block) +static inline void *solv_calloc_block(size_t len, size_t size, size_t block) { void *buf; if (!len) return 0; - buf = sat_malloc2((len + block) & ~block, size); + buf = solv_extend_realloc((void *)0, len, size, block); memset(buf, 0, ((len + block) & ~block) * size); return buf; } -#endif /* SATSOLVER_UTIL_H */ + +static inline void *solv_memdup(void *buf, size_t len) +{ + void *newbuf; + if (!buf) + return 0; + newbuf = solv_malloc(len); + if (len) + memcpy(newbuf, buf, len); + return newbuf; +} + +static inline void *solv_memdup2(void *buf, size_t num, size_t len) +{ + void *newbuf; + if (!buf) + return 0; + newbuf = solv_malloc2(num, len); + if (num) + memcpy(newbuf, buf, num * len); + return newbuf; +} + +#ifdef __cplusplus +} +#endif + +#endif /* LIBSOLV_UTIL_H */