util.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #define _GNU_SOURCE
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <unistd.h>
00013 #include <string.h>
00014 #include <sys/time.h>
00015
00016 #include "util.h"
00017
00018 void
00019 sat_oom(size_t num, size_t len)
00020 {
00021 if (num)
00022 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
00023 else
00024 fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
00025 abort();
00026 exit(1);
00027 }
00028
00029 void *
00030 sat_malloc(size_t len)
00031 {
00032 void *r = malloc(len ? len : 1);
00033 if (!r)
00034 sat_oom(0, len);
00035 return r;
00036 }
00037
00038 void *
00039 sat_malloc2(size_t num, size_t len)
00040 {
00041 if (len && (num * len) / len != num)
00042 sat_oom(num, len);
00043 return sat_malloc(num * len);
00044 }
00045
00046 void *
00047 sat_realloc(void *old, size_t len)
00048 {
00049 if (old == 0)
00050 old = malloc(len ? len : 1);
00051 else
00052 old = realloc(old, len ? len : 1);
00053 if (!old)
00054 sat_oom(0, len);
00055 return old;
00056 }
00057
00058 void *
00059 sat_realloc2(void *old, size_t num, size_t len)
00060 {
00061 if (len && (num * len) / len != num)
00062 sat_oom(num, len);
00063 return sat_realloc(old, num * len);
00064 }
00065
00066 void *
00067 sat_calloc(size_t num, size_t len)
00068 {
00069 void *r;
00070 if (num == 0 || len == 0)
00071 r = malloc(1);
00072 else
00073 r = calloc(num, len);
00074 if (!r)
00075 sat_oom(num, len);
00076 return r;
00077 }
00078
00079 void *
00080 sat_free(void *mem)
00081 {
00082 if (mem)
00083 free(mem);
00084 return 0;
00085 }
00086
00087 unsigned int
00088 sat_timems(unsigned int subtract)
00089 {
00090 struct timeval tv;
00091 unsigned int r;
00092
00093 if (gettimeofday(&tv, 0))
00094 return 0;
00095 r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
00096 r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
00097 r += (unsigned int)tv.tv_usec / 1000;
00098 return r - subtract;
00099 }
00100
00101
00102
00103
00104
00105
00106 void
00107 sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
00108 {
00109 #if defined(__GLIBC__)
00110 # if __GLIBC_PREREQ(2, 8)
00111 qsort_r(base, nmemb, size, compar, compard);
00112 # else
00113
00114 __qsort_r(base, nmemb, size, compar, compard);
00115 # endif
00116 #else
00117 # error please add qsort_r call here
00118 #endif
00119 }
00120
00121 char *
00122 sat_dupjoin(const char *str1, const char *str2, const char *str3)
00123 {
00124 int l1, l2, l3;
00125 char *s, *str;
00126 l1 = str1 ? strlen(str1) : 0;
00127 l2 = str2 ? strlen(str2) : 0;
00128 l3 = str3 ? strlen(str3) : 0;
00129 s = str = sat_malloc(l1 + l2 + l3 + 1);
00130 if (l1)
00131 {
00132 strcpy(s, str1);
00133 s += l1;
00134 }
00135 if (l2)
00136 {
00137 strcpy(s, str2);
00138 s += l2;
00139 }
00140 if (l3)
00141 {
00142 strcpy(s, str3);
00143 s += l3;
00144 }
00145 *s = 0;
00146 return str;
00147 }