-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
75 lines (59 loc) · 1.67 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
int get_time_diff_ms(struct timeval start, struct timeval end) {
int ms = 0;
ms += (end.tv_sec - start.tv_sec) * 1000.0;
ms += (end.tv_usec - start.tv_usec) / 1000.0;
return ms;
}
void buf_init(struct buf_t *buf, int value_size, int init_size) {
buf->cap = init_size;
buf->value_size = value_size;
buf->data = (void *) -1;
if (init_size > 0)
buf->data = malloc(value_size * init_size);
buf->size = 0;
}
void buf_resize(struct buf_t *buf, int nsize) {
buf->cap = nsize;
if (buf->data != (void *) -1)
buf->data = realloc(buf->data, buf->cap * buf->value_size);
else
buf->data = malloc(buf->cap * buf->value_size);
}
void buf_compact(struct buf_t *buf) {
buf_resize(buf, buf->size);
}
void buf_reset(struct buf_t *buf) {
buf->size = 0;
}
void buf_push(struct buf_t *buf, const void *item) {
if (buf->size >= buf->cap) {
buf_resize(buf, 2 * buf->size);
}
buf_set(buf, buf->size, item);
buf->size += 1;
}
void *buf_get(struct buf_t *buf, int index) {
return buf->data + (index * buf->value_size);
}
void *buf_set(struct buf_t *buf, int index, const void *val) {
memcpy(buf->data + (index * buf->value_size), val, buf->value_size);
return buf->data + (index * buf->value_size);
}
void buf_free(struct buf_t *buf) {
free(buf->data);
buf->data = (void *) -1;
buf->cap = 0;
buf->size = 0;
}
int mk_list_len(struct mk_list *list) {
int len = 0;
struct mk_list *curr;
mk_list_foreach(curr, list) {
len++;
}
return len;
}