]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | /* |
2 | * Copyright(c) 2012-2018 Intel Corporation | |
3 | * SPDX-License-Identifier: BSD-3-Clause-Clear | |
4 | */ | |
5 | ||
6 | ||
7 | #include "ocf_env.h" | |
8 | #include <sys/types.h> | |
9 | ||
10 | #include <stdarg.h> | |
11 | #include <stddef.h> | |
12 | #include <setjmp.h> | |
13 | #include <cmocka.h> | |
14 | ||
15 | void bug_on(int cond) | |
16 | { | |
17 | /* Wrap this to use your implementation */ | |
18 | assert_false(cond); | |
19 | } | |
20 | ||
21 | void *env_malloc(size_t size, int flags) | |
22 | { | |
23 | return malloc(size); | |
24 | } | |
25 | ||
26 | void *env_zalloc(size_t size, int flags) | |
27 | { | |
28 | return calloc(1, size); | |
29 | } | |
30 | ||
31 | void env_free(const void *ptr) | |
32 | { | |
33 | return free((void *) ptr); | |
34 | } | |
35 | ||
36 | void *env_vmalloc(size_t size) | |
37 | { | |
38 | return malloc(size); | |
39 | } | |
40 | ||
41 | void *env_vzalloc(size_t size) | |
42 | { | |
43 | return calloc(1, size); | |
44 | } | |
45 | ||
46 | void env_vfree(const void *ptr) | |
47 | { | |
48 | return free((void *) ptr); | |
49 | } | |
50 | ||
51 | uint64_t env_get_free_memory(void) | |
52 | { | |
53 | return sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES); | |
54 | } | |
55 | ||
56 | /* *** ALLOCATOR *** */ | |
57 | ||
58 | struct _env_allocator { | |
59 | /*!< Memory pool ID unique name */ | |
60 | char *name; | |
61 | ||
62 | /*!< Size of specific item of memory pool */ | |
63 | uint32_t item_size; | |
64 | ||
65 | /*!< Number of currently allocated items in pool */ | |
66 | env_atomic count; | |
67 | }; | |
68 | ||
69 | size_t env_allocator_align(size_t size) | |
70 | { | |
71 | if (size <= 2) | |
72 | return size; | |
73 | return (1ULL << 32) >> __builtin_clz(size - 1); | |
74 | } | |
75 | ||
76 | struct _env_allocator_item { | |
77 | uint32_t flags; | |
78 | uint32_t cpu; | |
79 | char data[]; | |
80 | }; | |
81 | ||
82 | void *env_allocator_new(env_allocator *allocator) | |
83 | { | |
84 | struct _env_allocator_item *item = NULL; | |
85 | ||
86 | item = calloc(1, allocator->item_size); | |
87 | if (item) { | |
88 | item->cpu = 0; | |
89 | env_atomic_inc(&allocator->count); | |
90 | } | |
91 | ||
92 | return &item->data; | |
93 | } | |
94 | ||
95 | env_allocator *env_allocator_create(uint32_t size, const char *name) | |
96 | { | |
97 | env_allocator *allocator = calloc(1, sizeof(*allocator)); | |
98 | ||
99 | allocator->item_size = size + sizeof(struct _env_allocator_item); | |
100 | ||
101 | allocator->name = strdup(name); | |
102 | ||
103 | return allocator; | |
104 | } | |
105 | ||
106 | void env_allocator_del(env_allocator *allocator, void *obj) | |
107 | { | |
108 | struct _env_allocator_item *item; | |
109 | ||
110 | item = container_of(obj, struct _env_allocator_item, data); | |
111 | ||
112 | env_atomic_dec(&allocator->count); | |
113 | ||
114 | free(item); | |
115 | } | |
116 | ||
117 | void env_allocator_destroy(env_allocator *allocator) | |
118 | { | |
119 | if (allocator) { | |
120 | if (env_atomic_read(&allocator->count)) { | |
121 | fprintf(stderr, "Not all object deallocated\n"); | |
122 | ENV_WARN(true, "Cleanup problem\n"); | |
123 | } | |
124 | ||
125 | free(allocator->name); | |
126 | free(allocator); | |
127 | } | |
128 | } | |
129 | ||
130 | uint32_t env_allocator_item_count(env_allocator *allocator) | |
131 | { | |
132 | return env_atomic_read(&allocator->count); | |
133 | } | |
134 | ||
135 | /* *** COMPLETION *** */ | |
136 | ||
137 | void env_completion_init(env_completion *completion) | |
138 | { | |
139 | function_called(); | |
140 | check_expected_ptr(completion); | |
141 | } | |
142 | ||
143 | void env_completion_wait(env_completion *completion) | |
144 | { | |
145 | function_called(); | |
146 | check_expected_ptr(completion); | |
147 | } | |
148 | ||
149 | void env_completion_complete(env_completion *completion) | |
150 | { | |
151 | function_called(); | |
152 | check_expected_ptr(completion); | |
153 | } | |
154 | ||
155 | ||
156 | int env_mutex_init(env_mutex *mutex) | |
157 | { | |
158 | function_called(); | |
159 | check_expected_ptr(mutex); | |
160 | return mock(); | |
161 | } | |
162 | ||
163 | void env_mutex_lock(env_mutex *mutex) | |
164 | { | |
165 | function_called(); | |
166 | check_expected_ptr(mutex); | |
167 | } | |
168 | ||
169 | int env_mutex_lock_interruptible(env_mutex *mutex) | |
170 | { | |
171 | function_called(); | |
172 | check_expected_ptr(mutex); | |
173 | return mock(); | |
174 | } | |
175 | ||
176 | int env_mutex_trylock(env_mutex *mutex) | |
177 | { | |
178 | function_called(); | |
179 | check_expected_ptr(mutex); | |
180 | return mock(); | |
181 | } | |
182 | ||
183 | void env_mutex_unlock(env_mutex *mutex) | |
184 | { | |
185 | function_called(); | |
186 | check_expected_ptr(mutex); | |
187 | } | |
188 | ||
189 | int env_mutex_is_locked(env_mutex *mutex) | |
190 | { | |
191 | function_called(); | |
192 | check_expected_ptr(mutex); | |
193 | return mock(); | |
194 | } | |
195 | ||
196 | int env_rmutex_init(env_rmutex *rmutex) | |
197 | { | |
198 | function_called(); | |
199 | check_expected_ptr(rmutex); | |
200 | return mock(); | |
201 | } | |
202 | ||
203 | void env_rmutex_lock(env_rmutex *rmutex) | |
204 | { | |
205 | function_called(); | |
206 | check_expected_ptr(rmutex); | |
207 | } | |
208 | ||
209 | int env_rmutex_lock_interruptible(env_rmutex *rmutex) | |
210 | { | |
211 | function_called(); | |
212 | check_expected_ptr(rmutex); | |
213 | return mock(); | |
214 | } | |
215 | ||
216 | int env_rmutex_trylock(env_rmutex *rmutex) | |
217 | { | |
218 | function_called(); | |
219 | check_expected_ptr(rmutex); | |
220 | return mock(); | |
221 | } | |
222 | ||
223 | void env_rmutex_unlock(env_rmutex *rmutex) | |
224 | { | |
225 | function_called(); | |
226 | check_expected_ptr(rmutex); | |
227 | } | |
228 | ||
229 | int env_rmutex_is_locked(env_rmutex *rmutex) | |
230 | { | |
231 | function_called(); | |
232 | check_expected_ptr(rmutex); | |
233 | return mock(); | |
234 | } | |
235 | ||
236 | int env_rwsem_init(env_rwsem *s) | |
237 | { | |
238 | function_called(); | |
239 | check_expected_ptr(s); | |
240 | return mock(); | |
241 | } | |
242 | ||
243 | void env_rwsem_up_read(env_rwsem *s) | |
244 | { | |
245 | function_called(); | |
246 | check_expected_ptr(s); | |
247 | } | |
248 | ||
249 | void env_rwsem_down_read(env_rwsem *s) | |
250 | { | |
251 | function_called(); | |
252 | check_expected_ptr(s); | |
253 | } | |
254 | ||
255 | int env_rwsem_down_read_trylock(env_rwsem *s) | |
256 | { | |
257 | function_called(); | |
258 | check_expected_ptr(s); | |
259 | return mock(); | |
260 | } | |
261 | ||
262 | void env_rwsem_up_write(env_rwsem *s) | |
263 | { | |
264 | function_called(); | |
265 | check_expected_ptr(s); | |
266 | } | |
267 | ||
268 | void env_rwsem_down_write(env_rwsem *s) | |
269 | { | |
270 | function_called(); | |
271 | check_expected_ptr(s); | |
272 | } | |
273 | ||
274 | int env_rwsem_down_write_trylock(env_rwsem *s) | |
275 | { | |
276 | function_called(); | |
277 | check_expected_ptr(s); | |
278 | return mock(); | |
279 | } | |
280 | ||
281 | int env_atomic_read(const env_atomic *a) | |
282 | { | |
283 | return *a; | |
284 | } | |
285 | ||
286 | void env_atomic_set(env_atomic *a, int i) | |
287 | { | |
288 | *a = i; | |
289 | } | |
290 | ||
291 | void env_atomic_add(int i, env_atomic *a) | |
292 | { | |
293 | *a += i; | |
294 | } | |
295 | ||
296 | void env_atomic_sub(int i, env_atomic *a) | |
297 | { | |
298 | *a -= i; | |
299 | } | |
300 | ||
301 | bool env_atomic_sub_and_test(int i, env_atomic *a) | |
302 | { | |
303 | return *a-=i == 0; | |
304 | } | |
305 | ||
306 | void env_atomic_inc(env_atomic *a) | |
307 | { | |
308 | ++*a; | |
309 | } | |
310 | ||
311 | void env_atomic_dec(env_atomic *a) | |
312 | { | |
313 | --*a; | |
314 | } | |
315 | ||
316 | bool env_atomic_dec_and_test(env_atomic *a) | |
317 | { | |
318 | return --*a == 0; | |
319 | } | |
320 | ||
321 | bool env_atomic_inc_and_test(env_atomic *a) | |
322 | { | |
323 | return ++*a == 0; | |
324 | } | |
325 | ||
326 | int env_atomic_add_return(int i, env_atomic *a) | |
327 | { | |
328 | return *a+=i; | |
329 | } | |
330 | ||
331 | int env_atomic_sub_return(int i, env_atomic *a) | |
332 | { | |
333 | return *a-=i; | |
334 | } | |
335 | ||
336 | int env_atomic_inc_return(env_atomic *a) | |
337 | { | |
338 | return ++*a; | |
339 | } | |
340 | ||
341 | int env_atomic_dec_return(env_atomic *a) | |
342 | { | |
343 | return --*a; | |
344 | } | |
345 | ||
346 | int env_atomic_cmpxchg(env_atomic *a, int old, int new_value) | |
347 | { | |
348 | int oldval = *a; | |
349 | if (oldval == old) | |
350 | *a = new_value; | |
351 | return oldval; | |
352 | } | |
353 | ||
354 | int env_atomic_add_unless(env_atomic *a, int i, int u) | |
355 | { | |
356 | int c, old; | |
357 | c = *a; | |
358 | for (;;) { | |
359 | if (c == (u)) | |
360 | break; | |
361 | old = env_atomic_cmpxchg((a), c, c + (i)); | |
362 | if (old == c) | |
363 | break; | |
364 | c = old; | |
365 | } | |
366 | return c != (u); | |
367 | } | |
368 | ||
369 | long env_atomic64_read(const env_atomic64 *a) | |
370 | { | |
371 | return *a; | |
372 | } | |
373 | ||
374 | void env_atomic64_set(env_atomic64 *a, long i) | |
375 | { | |
376 | *a=i; | |
377 | } | |
378 | ||
379 | void env_atomic64_add(long i, env_atomic64 *a) | |
380 | { | |
381 | *a += i; | |
382 | } | |
383 | ||
384 | void env_atomic64_sub(long i, env_atomic64 *a) | |
385 | { | |
386 | *a -= i; | |
387 | } | |
388 | ||
389 | void env_atomic64_inc(env_atomic64 *a) | |
390 | { | |
391 | ++*a; | |
392 | } | |
393 | ||
394 | void env_atomic64_dec(env_atomic64 *a) | |
395 | { | |
396 | --*a; | |
397 | } | |
398 | ||
399 | long env_atomic64_cmpxchg(env_atomic64 *a, long old, long new) | |
400 | { | |
401 | long oldval = *a; | |
402 | if (oldval == old) | |
403 | *a = new; | |
404 | return oldval; | |
405 | } | |
406 | ||
407 | void env_spinlock_init(env_spinlock *l) | |
408 | { | |
409 | function_called(); | |
410 | check_expected_ptr(l); | |
411 | } | |
412 | ||
413 | void env_spinlock_lock(env_spinlock *l) | |
414 | { | |
415 | function_called(); | |
416 | check_expected_ptr(l); | |
417 | } | |
418 | ||
419 | void env_spinlock_unlock(env_spinlock *l) | |
420 | { | |
421 | function_called(); | |
422 | check_expected_ptr(l); | |
423 | } | |
424 | ||
425 | void env_spinlock_lock_irq(env_spinlock *l) | |
426 | { | |
427 | function_called(); | |
428 | check_expected_ptr(l); | |
429 | } | |
430 | ||
431 | void env_spinlock_unlock_irq(env_spinlock *l) | |
432 | { | |
433 | function_called(); | |
434 | check_expected_ptr(l); | |
435 | } | |
436 | ||
437 | void env_rwlock_init(env_rwlock *l) | |
438 | { | |
439 | function_called(); | |
440 | check_expected_ptr(l); | |
441 | } | |
442 | ||
443 | void env_rwlock_read_lock(env_rwlock *l) | |
444 | { | |
445 | function_called(); | |
446 | check_expected_ptr(l); | |
447 | } | |
448 | ||
449 | void env_rwlock_read_unlock(env_rwlock *l) | |
450 | { | |
451 | function_called(); | |
452 | check_expected_ptr(l); | |
453 | } | |
454 | ||
455 | void env_rwlock_write_lock(env_rwlock *l) | |
456 | { | |
457 | function_called(); | |
458 | check_expected_ptr(l); | |
459 | } | |
460 | ||
461 | void env_rwlock_write_unlock(env_rwlock *l) | |
462 | { | |
463 | function_called(); | |
464 | check_expected_ptr(l); | |
465 | } | |
466 | ||
467 | void env_waitqueue_init(env_waitqueue *w) | |
468 | { | |
469 | w->completed = false; | |
470 | w->waiting = false; | |
471 | w->co = NULL; | |
472 | } | |
473 | ||
474 | void env_waitqueue_wake_up(env_waitqueue *w) | |
475 | { | |
476 | w->completed = true; | |
477 | if (!w->waiting || !w->co) | |
478 | return; | |
479 | } | |
480 | ||
481 | void env_bit_set(int nr, volatile void *addr) | |
482 | { | |
483 | char *byte = (char *) addr + (nr >> 3); | |
484 | char mask = 1 << (nr & 7); | |
485 | ||
486 | __sync_or_and_fetch(byte, mask); | |
487 | } | |
488 | ||
489 | void env_bit_clear(int nr, volatile void *addr) | |
490 | { | |
491 | char *byte = (char *) addr + (nr >> 3); | |
492 | char mask = 1 << (nr & 7); | |
493 | ||
494 | mask = ~mask; | |
495 | __sync_and_and_fetch(byte, mask); | |
496 | } | |
497 | ||
498 | bool env_bit_test(int nr, const volatile unsigned long *addr) | |
499 | { | |
500 | const char *byte = (char *) addr + (nr >> 3); | |
501 | char mask = 1 << (nr & 7); | |
502 | ||
503 | return !!(*byte & mask); | |
504 | } | |
505 | ||
506 | /* *** SCHEDULING *** */ | |
507 | ||
508 | void env_touch_softlockup_wd(void) | |
509 | { | |
510 | function_called(); | |
511 | } | |
512 | ||
513 | void env_schedule(void) | |
514 | { | |
515 | function_called(); | |
516 | } | |
517 | ||
518 | int env_in_interrupt(void) | |
519 | { | |
520 | function_called(); | |
521 | return mock(); | |
522 | } | |
523 | ||
524 | uint64_t env_get_tick_count(void) | |
525 | { | |
526 | struct timeval tv; | |
527 | gettimeofday(&tv, NULL); | |
528 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; | |
529 | } | |
530 | ||
531 | uint64_t env_ticks_to_msecs(uint64_t j) | |
532 | { | |
533 | return j; | |
534 | } | |
535 | ||
536 | uint64_t env_ticks_to_secs(uint64_t j) | |
537 | { | |
538 | return j / 1000; | |
539 | } | |
540 | ||
541 | uint64_t env_secs_to_ticks(uint64_t j) | |
542 | { | |
543 | return j * 1000; | |
544 | } | |
545 | ||
546 | int env_memset(void *dest, size_t count, int ch) | |
547 | { | |
548 | memset(dest, ch, count); | |
549 | return 0; | |
550 | } | |
551 | ||
552 | int env_memcpy(void *dest, size_t destsz, const void * src, size_t count) | |
553 | { | |
554 | if (destsz < count) | |
555 | memcpy(dest, src, destsz); | |
556 | else | |
557 | memcpy(dest, src, count); | |
558 | return 0; | |
559 | ||
560 | } | |
561 | ||
562 | int env_memcmp(const void *str1, size_t n1, const void *str2, size_t n2, | |
563 | int *diff) | |
564 | { | |
565 | size_t n = n1 > n2 ? n2 : n1; | |
566 | ||
567 | *diff = memcmp(str1, str2, n); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | int env_strncpy(char * dest, size_t destsz, const char *src, size_t count) | |
572 | { | |
573 | if (destsz < count) | |
574 | strncpy(dest, src, destsz); | |
575 | else | |
576 | strncpy(dest, src, count); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | size_t env_strnlen(const char *str, size_t strsz) | |
581 | { | |
582 | return strlen(str); | |
583 | } | |
584 | ||
585 | void env_sort(void *base, size_t num, size_t size, | |
586 | int (*cmp_fn)(const void *, const void *), | |
587 | void (*swap_fn)(void *, void *, int size)) | |
588 | { | |
589 | qsort(base, num, size, cmp_fn); | |
590 | } | |
591 | ||
592 | int env_strncmp(const char * str1, const char * str2, size_t num) | |
593 | { | |
594 | return strncmp(str1, str2, num); | |
595 | } | |
596 | ||
597 | void env_msleep(uint64_t n) | |
598 | { | |
599 | ||
600 | } | |
601 | ||
602 | /* *** CRC *** */ | |
603 | ||
604 | uint32_t env_crc32(uint32_t crc, uint8_t const *data, size_t len) | |
605 | { | |
606 | function_called(); | |
607 | check_expected(crc); | |
608 | check_expected(len); | |
609 | check_expected_ptr(data); | |
610 | return mock(); | |
611 | } |