-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem.cc
203 lines (182 loc) · 4.13 KB
/
mem.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#define ICI_CORE
#include "mem.h"
#include "archiver.h"
#include "fwd.h"
#include "int.h"
#include "null.h"
#include "primes.h"
namespace ici
{
/*
* Return a new ICI mem object refering to the memory at address 'base'
* with length 'length', which is measured in units of 'accessz' bytes.
* 'accessz' must be either 1, 2 or 4. If 'free_func' is provided it
* will be called when the mem object is about to be freed with 'base'
* as an argument.
*
* Returns nullptr on error, usual conventions.
*
* This --func-- forms part of the --ici-api--.
*/
mem *new_mem(void *base, size_t length, int accessz, void (*free_func)(void *))
{
mem *m;
if ((m = ici_talloc(mem)) == nullptr)
{
return nullptr;
}
set_tfnz(m, TC_MEM, 0, 1, sizeof(mem));
rego(m);
m->m_base = base;
m->m_length = length;
m->m_accessz = accessz;
m->m_free = free_func;
return memof(atom(m, 1));
}
/*
* Returns 0 if these objects are equal, else non-zero.
* See the comments on t_cmp() in object.h.
*/
int mem_type::cmp(object *o1, object *o2)
{
auto m1 = memof(o1);
auto m2 = memof(o2);
return m1->m_base != m2->m_base || m1->m_length != m2->m_length || m1->m_accessz != m2->m_accessz ||
m1->m_free != m2->m_free;
}
/*
* Return a hash sensitive to the value of the object.
* See the comment on t_hash() in object.h
*/
unsigned long mem_type::hash(object *o)
{
auto m = memof(o);
return (unsigned long)m->m_base * MEM_PRIME_0 + (unsigned long)m->m_length * MEM_PRIME_1 +
(unsigned long)m->m_accessz * MEM_PRIME_2;
}
void mem_type::free(object *o)
{
auto m = memof(o);
if (m->m_free != nullptr)
{
(*m->m_free)(m->m_base);
}
ici_tfree(o, mem);
}
int mem_type::assign(object *o, object *k, object *v)
{
auto m = memof(o);
int64_t i;
if (!isint(k) || !isint(v))
{
return assign_fail(o, k, v);
}
i = intof(k)->i_value;
if (i < 0 || i >= (int64_t)m->m_length)
{
return set_error("attempt to write at mem index %ld\n", i);
}
switch (m->m_accessz)
{
case 1:
((uint8_t *)m->m_base)[i] = intof(v)->i_value;
break;
case 2:
((uint16_t *)m->m_base)[i] = intof(v)->i_value;
break;
case 4:
((uint32_t *)m->m_base)[i] = intof(v)->i_value;
break;
case 8:
((int64_t *)memof(o)->m_base)[i] = intof(v)->i_value;
break;
}
return 0;
}
object *mem_type::fetch(object *o, object *k)
{
auto m = memof(o);
int64_t i;
if (!isint(k))
{
return fetch_fail(o, k);
}
i = intof(k)->i_value;
if (i < 0 || i >= (int64_t)m->m_length)
{
return null;
}
switch (m->m_accessz)
{
case 1:
i = ((uint8_t *)memof(o)->m_base)[i];
break;
case 2:
i = ((uint16_t *)memof(o)->m_base)[i];
break;
case 4:
i = ((uint32_t *)memof(o)->m_base)[i];
break;
case 8:
i = ((int64_t *)memof(o)->m_base)[i];
break;
}
o = new_int(i);
decref(o);
return o;
}
int mem_type::save(archiver *ar, object *o)
{
auto m = memof(o);
if (ar->save_name(o))
{
return 1;
}
const int64_t len = m->m_length;
if (ar->write(len))
{
return 1;
}
const int16_t accessz = m->m_accessz;
if (ar->write(accessz))
{
return 1;
}
if (ar->write(m->m_base, m->m_length * m->m_accessz))
{
return 1;
}
return 0;
}
object *mem_type::restore(archiver *ar)
{
int64_t len;
int16_t accessz;
size_t sz;
void *p;
mem *m = 0;
object *name;
if (ar->restore_name(&name) || ar->read(&len) || ar->read(&accessz))
{
return nullptr;
}
sz = size_t(len) * size_t(accessz);
if ((p = ici_alloc(sz)) != nullptr)
{
if ((m = new_mem(p, len, accessz, ici_free)) == nullptr)
{
ici_free(p);
}
else if (ar->read(p, sz) || ar->record(name, m))
{
decref(m);
m = nullptr;
}
}
return m;
}
int64_t mem_type::len(object *o)
{
return memof(o)->m_length;
}
} // namespace ici