- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_vector.c
More file actions
Latest commit
104 lines (78 loc) · 1.92 KB
/
Copy pathjs_vector.c
File metadata and controls
104 lines (78 loc) · 1.92 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<errno.h>
#ifndef__APPLE__
#include<malloc.h>
#endif
#include"js.h"
#include"js_malloc.h"
#include"database/db_malloc.h"
externboolmallocDebug;
#definefirstCapacity 10
// duplicate the vector
void*vec_dup(void*vector) {
rawobj_t*raw=vector, *mem;
uint32_tsize;
if (!vector)
returnNULL;
size=js_size(vec_raw(vector));
if ((mem=js_alloc(size, false)))
memcpy(mem, raw, size);
else {
fprintf (stderr, "vec_dup: out of memory!\n");
exit(1);
}
*mem[-1].refCnt=*raw[-1].refCnt;
*mem[-1].weakCnt=*raw[-1].weakCnt;
return (uint32_t*)mem+2;
}
// dynamically grow the vector
void*vec_grow(void*vector, uint32_tincrement, uint32_titemsize, boolmap) {
uint32_tdbl_cur=2*vec_max(vector);
uint32_tmin_needed=vec_cnt(vector) +increment;
uint32_tcap=dbl_cur>min_needed ? dbl_cur : min_needed;
uint32_toff, size, mapSize=0, *p;
//rawobj_t *raw, *nxt;
// raw = (rawobj_t *)v;
if (cap<firstCapacity)
cap=firstCapacity;
if (map) {
if (cap<255) {
mapSize=sizeof(uint8_t);
} elseif (cap<65535) {
mapSize=sizeof(uint16_t);
} else {
mapSize=sizeof(uint32_t);
}
}
size=itemsize*cap;
size+=sizeof(int) *2;
size+=mapSize*3*cap / 2;
p=js_alloc(size, false);
// nxt = (rawobj_t *)p;
if (vector) {
off=vec_cnt(vector) *itemsize+2*sizeof(int);
memcpy (p, vec_raw(vector), off);
memset ((uint8_t*)p+off, 0, size-off);
js_free(vec_raw(vector));
} else
memset (p, 0, size);
p[0] =cap;
returnp+2;
}
// slice slots from beginning of the vector
void*vec_sliceqty(void*vector, uint32_tqty, uint32_titemsize) {
uint8_t*dest;
uint32_tidx;
if (!vector)
returnNULL;
if (vec_size(vector) <qty) {
vec_size(vector) =0;
returnvector;
}
vec_size(vector) -=qty;
dest=vector;
for (idx=0; idx<vec_size(vector); idx++) {
memcpy (dest, dest+qty*itemsize, itemsize);
dest+=itemsize;
}
returnvector;
}