|
4 | 4 | #include "rust_task.h"
|
5 | 5 | #include <limits.h>
|
6 | 6 |
|
7 |
| -// Utility type: pointer-vector. |
8 |
| - |
9 |
| -template <typename T> |
10 |
| -ptr_vec<T>::ptr_vec(rust_task *task) : |
11 |
| - task(task), |
12 |
| - alloc(INIT_SIZE), |
13 |
| - fill(0), |
14 |
| - data(new (task, "ptr_vec<T>") T*[alloc]) |
15 |
| -{ |
16 |
| - I(task->thread, data); |
17 |
| - DLOG(task->thread, mem, "new ptr_vec(data=0x%" PRIxPTR ") -> 0x%" PRIxPTR, |
18 |
| - (uintptr_t)data, (uintptr_t)this); |
19 |
| -} |
20 |
| - |
21 |
| -template <typename T> |
22 |
| -ptr_vec<T>::~ptr_vec() |
23 |
| -{ |
24 |
| - I(task->thread, data); |
25 |
| - DLOG(task->thread, mem, "~ptr_vec 0x%" PRIxPTR ", data=0x%" PRIxPTR, |
26 |
| - (uintptr_t)this, (uintptr_t)data); |
27 |
| - I(task->thread, fill == 0); |
28 |
| - task->free(data); |
29 |
| -} |
30 |
| - |
31 |
| -template <typename T> T *& |
32 |
| -ptr_vec<T>::operator[](size_t offset) { |
33 |
| - I(task->thread, data[offset]->idx == offset); |
34 |
| - return data[offset]; |
35 |
| -} |
36 |
| - |
37 |
| -template <typename T> |
38 |
| -void |
39 |
| -ptr_vec<T>::push(T *p) |
40 |
| -{ |
41 |
| - I(task->thread, data); |
42 |
| - I(task->thread, fill <= alloc); |
43 |
| - if (fill == alloc) { |
44 |
| - alloc *= 2; |
45 |
| - data = (T **)task->realloc(data, alloc * sizeof(T*)); |
46 |
| - I(task->thread, data); |
47 |
| - } |
48 |
| - I(task->thread, fill < alloc); |
49 |
| - p->idx = fill; |
50 |
| - data[fill++] = p; |
51 |
| -} |
52 |
| - |
53 |
| -template <typename T> |
54 |
| -T * |
55 |
| -ptr_vec<T>::pop() |
56 |
| -{ |
57 |
| - return data[--fill]; |
58 |
| -} |
59 |
| - |
60 |
| -template <typename T> |
61 |
| -T * |
62 |
| -ptr_vec<T>::peek() |
63 |
| -{ |
64 |
| - return data[fill - 1]; |
65 |
| -} |
66 |
| - |
67 |
| -template <typename T> |
68 |
| -void |
69 |
| -ptr_vec<T>::trim(size_t sz) |
70 |
| -{ |
71 |
| - I(task->thread, data); |
72 |
| - if (sz <= (alloc / 4) && |
73 |
| - (alloc / 2) >= INIT_SIZE) { |
74 |
| - alloc /= 2; |
75 |
| - I(task->thread, alloc >= fill); |
76 |
| - data = (T **)task->realloc(data, alloc * sizeof(T*)); |
77 |
| - I(task->thread, data); |
78 |
| - } |
79 |
| -} |
80 |
| - |
81 |
| -template <typename T> |
82 |
| -void |
83 |
| -ptr_vec<T>::swap_delete(T *item) |
84 |
| -{ |
85 |
| - /* Swap the endpoint into i and decr fill. */ |
86 |
| - I(task->thread, data); |
87 |
| - I(task->thread, fill > 0); |
88 |
| - I(task->thread, item->idx < fill); |
89 |
| - fill--; |
90 |
| - if (fill > 0) { |
91 |
| - T *subst = data[fill]; |
92 |
| - size_t idx = item->idx; |
93 |
| - data[idx] = subst; |
94 |
| - subst->idx = idx; |
95 |
| - } |
96 |
| -} |
97 |
| - |
98 | 7 | // Inline fn used regularly elsewhere.
|
99 | 8 |
|
100 | 9 | static inline size_t
|
|
0 commit comments