Skip to content

Commit 755462b

Browse files
committed
Add new Allocated_* macros for accessing the size of an object
1 parent 4049385 commit 755462b

File tree

2 files changed

+104
-45
lines changed

2 files changed

+104
-45
lines changed

ocaml/runtime/caml/mlvalues.h

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mixed blocks. In the upstream compiler, R is set with the
130130

131131
#define Tag_hd(hd) ((tag_t) ((hd) & HEADER_TAG_MASK))
132132
#define Hd_with_tag(hd, tag) (((hd) &~ HEADER_TAG_MASK) | (tag))
133-
#define Wosize_hd(hd) ((mlsize_t) (((hd) & HEADER_WOSIZE_MASK) \
133+
#define Allocated_wosize_hd(hd) ((mlsize_t) (((hd) & HEADER_WOSIZE_MASK) \
134134
>> HEADER_WOSIZE_SHIFT))
135135

136136
/* A "clean" header, without reserved or color bits. */
@@ -192,7 +192,7 @@ Caml_inline mlsize_t Scannable_wosize_hd_native(header_t hd) {
192192
return
193193
Is_mixed_block_reserved(res)
194194
? Mixed_block_scannable_wosize_reserved_native(res)
195-
: Wosize_hd(hd);
195+
: Allocated_wosize_hd(hd);
196196
}
197197

198198
/* Bytecode versions of mixed block macros.
@@ -203,8 +203,8 @@ Caml_inline mlsize_t Scannable_wosize_hd_native(header_t hd) {
203203
in the header bits.
204204
*/
205205

206-
#define Scannable_wosize_hd_byte(hd) (Wosize_hd (hd))
207-
#define Scannable_wosize_val_byte(val) (Wosize_hd (Hd_val (val)))
206+
#define Scannable_wosize_hd_byte(hd) (Allocated_wosize_hd (hd))
207+
#define Scannable_wosize_val_byte(val) (Allocated_wosize_hd (Hd_val (val)))
208208
Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
209209
mlsize_t size) {
210210
(void)res;
@@ -252,33 +252,64 @@ Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
252252
#define Num_tags (1ull << HEADER_TAG_BITS)
253253
#define Max_wosize ((1ull << HEADER_WOSIZE_BITS) - 1ull)
254254

255-
// Note that Wosize_val and the other macros that read headers will not
256-
// be optimized by common subexpression elimination, because of the
257-
// atomic header loads. It is best to bind the results of such macros
258-
// to variables if they will be tested repeatedly, e.g. as the end condition
259-
// in a for-loop.
260-
261-
#define Wosize_val(val) (Wosize_hd (Hd_val (val)))
262-
#define Wosize_op(op) (Wosize_val (op))
263-
#define Wosize_bp(bp) (Wosize_val (bp))
264-
#define Wosize_hp(hp) (Wosize_hd (Hd_hp (hp)))
265255
#define Whsize_wosize(sz) ((sz) + 1)
266256
#define Wosize_whsize(sz) ((sz) - 1)
267257
#define Wosize_bhsize(sz) ((sz) / sizeof (value) - 1)
268258
#define Bsize_wsize(sz) ((sz) * sizeof (value))
269259
#define Wsize_bsize(sz) ((sz) / sizeof (value))
270260
#define Bhsize_wosize(sz) (Bsize_wsize (Whsize_wosize (sz)))
271261
#define Bhsize_bosize(sz) ((sz) + sizeof (header_t))
272-
#define Bosize_val(val) (Bsize_wsize (Wosize_val (val)))
273-
#define Bosize_op(op) (Bosize_val (Val_op (op)))
274-
#define Bosize_bp(bp) (Bosize_val (Val_bp (bp)))
275-
#define Bosize_hd(hd) (Bsize_wsize (Wosize_hd (hd)))
276-
#define Whsize_hp(hp) (Whsize_wosize (Wosize_hp (hp)))
277-
#define Whsize_val(val) (Whsize_hp (Hp_val (val)))
278-
#define Whsize_bp(bp) (Whsize_val (Val_bp (bp)))
279-
#define Whsize_hd(hd) (Whsize_wosize (Wosize_hd (hd)))
280-
#define Bhsize_hp(hp) (Bsize_wsize (Whsize_hp (hp)))
281-
#define Bhsize_hd(hd) (Bsize_wsize (Whsize_hd (hd)))
262+
263+
/* Note that Allocated_wosize_val and the other macros that read headers will not
264+
be optimized by common subexpression elimination, because of the
265+
atomic header loads. It is best to bind the results of such macros
266+
to variables if they will be tested repeatedly, e.g. as the end condition
267+
in a for-loop.
268+
*/
269+
270+
/* We rename the size macros to [Allocated_...] internally so that we're forced
271+
to think about whether imported code needs to updated for mixed blocks,
272+
which have separate notions of scannable size and total size of
273+
an object, even for scannable tags. We call an object's size
274+
(including possibly non-scannable fields) its "allocated" size
275+
to document the fact that you shouldn't scan fields on the
276+
basis of this size alone.
277+
*/
278+
279+
#define Allocated_wosize_val(val) (Allocated_wosize_hd (Hd_val (val)))
280+
#define Allocated_wosize_op(op) (Allocated_wosize_val (op))
281+
#define Allocated_wosize_bp(bp) (Allocated_wosize_val (bp))
282+
#define Allocated_wosize_hp(hp) (Allocated_wosize_hd (Hd_hp (hp)))
283+
#define Allocated_bosize_val(val) (Bsize_wsize (Allocated_wosize_val (val)))
284+
#define Allocated_bosize_op(op) (Allocated_bosize_val (Val_op (op)))
285+
#define Allocated_bosize_bp(bp) (Allocated_bosize_val (Val_bp (bp)))
286+
#define Allocated_bosize_hd(hd) (Bsize_wsize (Allocated_wosize_hd (hd)))
287+
#define Allocated_whsize_hp(hp) (Whsize_wosize (Allocated_wosize_hp (hp)))
288+
#define Allocated_whsize_val(val) (Allocated_whsize_hp (Hp_val (val)))
289+
#define Allocated_whsize_bp(bp) (Allocated_whsize_val (Val_bp (bp)))
290+
#define Allocated_whsize_hd(hd) (Whsize_wosize (Allocated_wosize_hd (hd)))
291+
#define Allocated_bhsize_hp(hp) (Bsize_wsize (Allocated_whsize_hp (hp)))
292+
#define Allocated_bhsize_hd(hd) (Bsize_wsize (Allocated_whsize_hd (hd)))
293+
294+
/* CR nroberts: Soon, we'd like to only define these macros if CAML_INTERNALS
295+
is defined. This allows us to ban the old names in user code.
296+
*/
297+
#define Wosize_hd(hd) Allocated_wosize_hd(hd)
298+
#define Wosize_val(val) Allocated_wosize_val(val)
299+
#define Wosize_op(op) Allocated_wosize_op(op)
300+
#define Wosize_bp(bp) Allocated_wosize_bp(bp)
301+
#define Wosize_hp(hp) Allocated_wosize_hp(hp)
302+
#define Bosize_val(val) Allocated_bosize_val(val)
303+
#define Bosize_op(op) Allocated_bosize_op(op)
304+
#define Bosize_bp(bp) Allocated_bosize_bp(bp)
305+
#define Bosize_hd(hd) Allocated_bosize_hd(hd)
306+
#define Whsize_hp(hp) Allocated_whsize_hp(hp)
307+
#define Whsize_val(val) Allocated_whsize_val(val)
308+
#define Whsize_bp(bp) Allocated_whsize_bp(bp)
309+
#define Whsize_hd(hd) Allocated_whsize_hd(hd)
310+
#define Bhsize_hp(hp) Allocated_bhsize_hp(hp)
311+
#define Bhsize_hd(hd) Allocated_bhsize_hd(hd)
312+
#endif // CAML_INTERNALS
282313

283314
#define Reserved_val(val) (Reserved_hd (Hd_val (val)))
284315

@@ -333,7 +364,7 @@ Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
333364
with tag Closure_tag (see compact.c). */
334365

335366
#define Infix_tag 249
336-
#define Infix_offset_hd(hd) (Bosize_hd(hd))
367+
#define Infix_offset_hd(hd) (Allocated_bosize_hd(hd))
337368
#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
338369

339370
/* Another special case: objects */

ocaml/runtime4/caml/mlvalues.h

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ originally built for Spacetime profiling, hence the odd name.
132132
#define PROFINFO_MASK (Gen_profinfo_mask(PROFINFO_WIDTH))
133133
#define NO_PROFINFO 0
134134
#define Hd_no_profinfo(hd) ((hd) & ~(PROFINFO_MASK << PROFINFO_SHIFT))
135-
#define Wosize_hd(hd) ((mlsize_t) ((Hd_no_profinfo(hd)) >> 10))
135+
#define Allocated_wosize_hd(hd) ((mlsize_t) ((Hd_no_profinfo(hd)) >> 10))
136136
#define Profinfo_hd(hd) (Gen_profinfo_hd(PROFINFO_WIDTH, hd))
137137
#else
138138
#define NO_PROFINFO 0
139-
#define Wosize_hd(hd) ((mlsize_t) ((hd) >> 10))
139+
#define Allocated_wosize_hd(hd) ((mlsize_t) ((hd) >> 10))
140140
#define Profinfo_hd(hd) NO_PROFINFO
141141
#endif /* WITH_PROFINFO */
142142

@@ -184,7 +184,7 @@ Caml_inline mlsize_t Scannable_wosize_hd_native(header_t hd) {
184184
return
185185
Is_mixed_block_reserved(res)
186186
? Mixed_block_scannable_wosize_reserved_native(res)
187-
: Wosize_hd(hd);
187+
: Allocated_wosize_hd(hd);
188188
}
189189

190190
/* Bytecode versions of mixed block macros.
@@ -195,8 +195,8 @@ Caml_inline mlsize_t Scannable_wosize_hd_native(header_t hd) {
195195
in the header bits.
196196
*/
197197

198-
#define Scannable_wosize_hd_byte(hd) (Wosize_hd (hd))
199-
#define Scannable_wosize_val_byte(val) (Wosize_hd (Hd_val (val)))
198+
#define Scannable_wosize_hd_byte(hd) (Allocated_wosize_hd (hd))
199+
#define Scannable_wosize_val_byte(val) (Allocated_wosize_hd (Hd_val (val)))
200200
Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
201201
mlsize_t size) {
202202
(void)res;
@@ -244,27 +244,55 @@ Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
244244
#define Max_wosize ((1 << 22) - 1)
245245
#endif /* ARCH_SIXTYFOUR */
246246

247-
#define Wosize_val(val) (Wosize_hd (Hd_val (val)))
248-
#define Wosize_op(op) (Wosize_val (op))
249-
#define Wosize_bp(bp) (Wosize_val (bp))
250-
#define Wosize_hp(hp) (Wosize_hd (Hd_hp (hp)))
251247
#define Whsize_wosize(sz) ((sz) + 1)
252248
#define Wosize_whsize(sz) ((sz) - 1)
253249
#define Wosize_bhsize(sz) ((sz) / sizeof (value) - 1)
254250
#define Bsize_wsize(sz) ((sz) * sizeof (value))
255251
#define Wsize_bsize(sz) ((sz) / sizeof (value))
256252
#define Bhsize_wosize(sz) (Bsize_wsize (Whsize_wosize (sz)))
257253
#define Bhsize_bosize(sz) ((sz) + sizeof (header_t))
258-
#define Bosize_val(val) (Bsize_wsize (Wosize_val (val)))
259-
#define Bosize_op(op) (Bosize_val (Val_op (op)))
260-
#define Bosize_bp(bp) (Bosize_val (Val_bp (bp)))
261-
#define Bosize_hd(hd) (Bsize_wsize (Wosize_hd (hd)))
262-
#define Whsize_hp(hp) (Whsize_wosize (Wosize_hp (hp)))
263-
#define Whsize_val(val) (Whsize_hp (Hp_val (val)))
264-
#define Whsize_bp(bp) (Whsize_val (Val_bp (bp)))
265-
#define Whsize_hd(hd) (Whsize_wosize (Wosize_hd (hd)))
266-
#define Bhsize_hp(hp) (Bsize_wsize (Whsize_hp (hp)))
267-
#define Bhsize_hd(hd) (Bsize_wsize (Whsize_hd (hd)))
254+
255+
/* Jane Street: We rename the size macros to [Allocated_...] internally so that
256+
we're forced to think about whether imported code needs to updated for mixed
257+
blocks, which have separate notions of scannable size and total size of an
258+
object, even for scannable tags. We call an object's size (including
259+
possibly non-scannable fields) its "allocated" size to document the fact
260+
that you shouldn't scan fields on the basis of this size alone.
261+
*/
262+
263+
#define Allocated_wosize_val(val) (Allocated_wosize_hd (Hd_val (val)))
264+
#define Allocated_wosize_op(op) (Allocated_wosize_val (op))
265+
#define Allocated_wosize_bp(bp) (Allocated_wosize_val (bp))
266+
#define Allocated_wosize_hp(hp) (Allocated_wosize_hd (Hd_hp (hp)))
267+
#define Allocated_bosize_val(val) (Bsize_wsize (Allocated_wosize_val (val)))
268+
#define Allocated_bosize_op(op) (Allocated_bosize_val (Val_op (op)))
269+
#define Allocated_bosize_bp(bp) (Allocated_bosize_val (Val_bp (bp)))
270+
#define Allocated_bosize_hd(hd) (Bsize_wsize (Allocated_wosize_hd (hd)))
271+
#define Allocated_whsize_hp(hp) (Whsize_wosize (Allocated_wosize_hp (hp)))
272+
#define Allocated_whsize_val(val) (Allocated_whsize_hp (Hp_val (val)))
273+
#define Allocated_whsize_bp(bp) (Allocated_whsize_val (Val_bp (bp)))
274+
#define Allocated_whsize_hd(hd) (Whsize_wosize (Allocated_wosize_hd (hd)))
275+
#define Allocated_bhsize_hp(hp) (Bsize_wsize (Allocated_whsize_hp (hp)))
276+
#define Allocated_bhsize_hd(hd) (Bsize_wsize (Allocated_whsize_hd (hd)))
277+
278+
/* CR nroberts: Soon, we'd like to only define these macros if CAML_INTERNALS
279+
is defined. This allows us to ban the old names in user code.
280+
*/
281+
#define Wosize_hd(hd) Allocated_wosize_hd(hd)
282+
#define Wosize_val(val) Allocated_wosize_val(val)
283+
#define Wosize_op(op) Allocated_wosize_op(op)
284+
#define Wosize_bp(bp) Allocated_wosize_bp(bp)
285+
#define Wosize_hp(hp) Allocated_wosize_hp(hp)
286+
#define Bosize_val(val) Allocated_bosize_val(val)
287+
#define Bosize_op(op) Allocated_bosize_op(op)
288+
#define Bosize_bp(bp) Allocated_bosize_bp(bp)
289+
#define Bosize_hd(hd) Allocated_bosize_hd(hd)
290+
#define Whsize_hp(hp) Allocated_whsize_hp(hp)
291+
#define Whsize_val(val) Allocated_whsize_val(val)
292+
#define Whsize_bp(bp) Allocated_whsize_bp(bp)
293+
#define Whsize_hd(hd) Allocated_whsize_hd(hd)
294+
#define Bhsize_hp(hp) Allocated_bhsize_hp(hp)
295+
#define Bhsize_hd(hd) Allocated_bhsize_hd(hd)
268296

269297
#define Scannable_wosize_hp(hp) (Scannable_wosize_hd (Hd_hp (hp)))
270298

@@ -322,7 +350,7 @@ typedef opcode_t * code_t;
322350
with tag Closure_tag (see compact.c). */
323351

324352
#define Infix_tag 249
325-
#define Infix_offset_hd(hd) (Bosize_hd(hd))
353+
#define Infix_offset_hd(hd) (Allocated_bosize_hd(hd))
326354
#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
327355

328356
/* Another special case: objects */

0 commit comments

Comments
 (0)