Debian Codegraph @

lib/common/utils.c

raw
1 #include "utils.h"
2
3 #include <sys/time.h>
4 #include <time.h>
5 #include <stdlib.h> // getenv, malloc,...
6 #include <string.h> // strcat,...
7
8 // mutex for locking parallelism in large memory transfers
9 #ifdef AVM_COMPATIBLE
10 uint_t (*localcount)(void);
11 int64_t (*longcount)(void);
12 #endif
13
14 WAVEFORMATEX* avm_get_leWAVEFORMATEX(void* waveformatex)
15 {
16 WAVEFORMATEX* wf = (WAVEFORMATEX*) waveformatex;
17
18 wf->wFormatTag = avm_get_le16(&wf->wFormatTag);
19 wf->nChannels = avm_get_le16(&wf->nChannels);
20 wf->nSamplesPerSec = avm_get_le32(&wf->nSamplesPerSec);
21 wf->nAvgBytesPerSec = avm_get_le32(&wf->nAvgBytesPerSec);
22 wf->nBlockAlign = avm_get_le16(&wf->nBlockAlign);
23 wf->wBitsPerSample = avm_get_le16(&wf->wBitsPerSample);
24 wf->cbSize = avm_get_le16(&wf->cbSize);
25
26 /* convert other types */
27
28 return wf;
29 }
30
31 BITMAPINFOHEADER* avm_get_leBITMAPINFOHEADER(void* bitmapinfoheader)
32 {
33 BITMAPINFOHEADER* bi = (BITMAPINFOHEADER*) bitmapinfoheader;
34
35 bi->biSize = avm_get_le32(&bi->biSize);
36 bi->biWidth = (int32_t)avm_get_le32(&bi->biWidth);
37 bi->biHeight = (int32_t)avm_get_le32(&bi->biHeight);
38 bi->biPlanes = avm_get_le16(&bi->biPlanes);
39 bi->biBitCount = avm_get_le16(&bi->biBitCount);
40 bi->biCompression = avm_get_le32(&bi->biCompression);
41 bi->biSizeImage = avm_get_le32(&bi->biCompression);
42 bi->biXPelsPerMeter = (int32_t)avm_get_le32(&bi->biXPelsPerMeter);
43 bi->biYPelsPerMeter = (int32_t)avm_get_le32(&bi->biYPelsPerMeter);
44 bi->biClrUsed = avm_get_le32(&bi->biClrUsed);
45 bi->biClrImportant = avm_get_le32(&bi->biClrImportant);
46
47 return bi;
48 }
49
50 GUID* avm_get_leGUID(void* guid)
51 {
52 GUID* g = (GUID*) guid;
53
54 g->f1 = avm_get_le32(&g->f1);
55 g->f2 = avm_get_le16(&g->f2);
56 g->f3 = avm_get_le16(&g->f3);
57
58 return g;
59 }
60
61
62 /*
63 * A thread-safe usec sleep
64 * (Note: on solaris, usleep is not thread-safe)
65 */
66 #ifndef WIN32
67 int avm_usleep(unsigned long delay)
68 {
69 #if HAVE_NANOSLEEP
70 struct timespec tsp;
71 tsp.tv_sec = (time_t)(delay / 1000000);
72 tsp.tv_nsec = (long)((delay % 1000000) * 1000);
73 return nanosleep(&tsp, NULL);
74 #else
75 return usleep(delay);
76 #endif
77 }
78 #endif
79
80 int64_t avm_get_time_us(void)
81 {
82 #ifndef WIN32
83 struct timeval tv;
84 gettimeofday(&tv, 0);
85 int64_t result = tv.tv_usec;
86 result += (int64_t)tv.tv_sec * 1000000;
87 return result;
88 #else
89 return time(NULL);
90 #endif
91 }
92
93 /*
94 * Solaris (maybe other operating systems, too) does not have avm_setenv(),
95 * and avm_unsetenv() in libc, provide our own implementation.
96 */
97 int avm_setenv(const char *name, const char *value, int overwrite)
98 {
99 #if HAVE_SETENV
100 return setenv(name, value, overwrite);
101 #else
102 char *env;
103
104 if (getenv(name) != NULL && !overwrite)
105 return 0;
106
107 env = malloc(strlen(name) + strlen(value) + 2);
108 strcpy(env, name);
109 strcat(env, "=");
110 strcat(env, value);
111 return putenv(env);
112 #endif
113 }
114
115 void avm_unsetenv(const char *name)
116 {
117 #if HAVE_UNSETENV
118 unsetenv(name);
119 #else
120 extern char **environ;
121 char **ep;
122 int len = strlen(name);
123 for (ep = environ; *ep; ep++)
124 {
125 if (strncmp(*ep, name, len) == 0 && (*ep)[len] == '=')
126 {
127 while ((ep[0] = ep[1]) != NULL)
128 ep++;
129 break;
130 }
131 }
132 #endif
133 }