Skip to content

Commit 2c94209

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

File tree

2 files changed

+100
-45
lines changed

2 files changed

+100
-45
lines changed

ocaml/runtime/caml/mlvalues.h

Lines changed: 53 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,61 @@ 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+
/* flambda-backend: We rename the size macros to [Allocated_...] so that we're
271+
forced to think about whether C code needs to updated for mixed blocks, which
272+
have separate notions of scannable size and total size of an object, even for
273+
scannable tags. We call an object's size (including possibly non-scannable
274+
fields) its "allocated" size to document the fact that you shouldn't scan
275+
fields on the basis of this size alone.
276+
*/
277+
278+
#define Allocated_wosize_val(val) (Allocated_wosize_hd (Hd_val (val)))
279+
#define Allocated_wosize_op(op) (Allocated_wosize_val (op))
280+
#define Allocated_wosize_bp(bp) (Allocated_wosize_val (bp))
281+
#define Allocated_wosize_hp(hp) (Allocated_wosize_hd (Hd_hp (hp)))
282+
#define Allocated_bosize_val(val) (Bsize_wsize (Allocated_wosize_val (val)))
283+
#define Allocated_bosize_op(op) (Allocated_bosize_val (Val_op (op)))
284+
#define Allocated_bosize_bp(bp) (Allocated_bosize_val (Val_bp (bp)))
285+
#define Allocated_bosize_hd(hd) (Bsize_wsize (Allocated_wosize_hd (hd)))
286+
#define Allocated_whsize_hp(hp) (Whsize_wosize (Allocated_wosize_hp (hp)))
287+
#define Allocated_whsize_val(val) (Allocated_whsize_hp (Hp_val (val)))
288+
#define Allocated_whsize_bp(bp) (Allocated_whsize_val (Val_bp (bp)))
289+
#define Allocated_whsize_hd(hd) (Whsize_wosize (Allocated_wosize_hd (hd)))
290+
#define Allocated_bhsize_hp(hp) (Bsize_wsize (Allocated_whsize_hp (hp)))
291+
#define Allocated_bhsize_hd(hd) (Bsize_wsize (Allocated_whsize_hd (hd)))
292+
293+
#ifndef Hide_upstream_size_macros
294+
#define Wosize_hd(hd) Allocated_wosize_hd(hd)
295+
#define Wosize_val(val) Allocated_wosize_val(val)
296+
#define Wosize_op(op) Allocated_wosize_op(op)
297+
#define Wosize_bp(bp) Allocated_wosize_bp(bp)
298+
#define Wosize_hp(hp) Allocated_wosize_hp(hp)
299+
#define Bosize_val(val) Allocated_bosize_val(val)
300+
#define Bosize_op(op) Allocated_bosize_op(op)
301+
#define Bosize_bp(bp) Allocated_bosize_bp(bp)
302+
#define Bosize_hd(hd) Allocated_bosize_hd(hd)
303+
#define Whsize_hp(hp) Allocated_whsize_hp(hp)
304+
#define Whsize_val(val) Allocated_whsize_val(val)
305+
#define Whsize_bp(bp) Allocated_whsize_bp(bp)
306+
#define Whsize_hd(hd) Allocated_whsize_hd(hd)
307+
#define Bhsize_hp(hp) Allocated_bhsize_hp(hp)
308+
#define Bhsize_hd(hd) Allocated_bhsize_hd(hd)
309+
#endif // Hide_upstream_size_macros
282310

283311
#define Reserved_val(val) (Reserved_hd (Hd_val (val)))
284312

@@ -333,7 +361,7 @@ Caml_inline mlsize_t Scannable_wosize_reserved_byte(reserved_t res,
333361
with tag Closure_tag (see compact.c). */
334362

335363
#define Infix_tag 249
336-
#define Infix_offset_hd(hd) (Bosize_hd(hd))
364+
#define Infix_offset_hd(hd) (Allocated_bosize_hd(hd))
337365
#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
338366

339367
/* Another special case: objects */

ocaml/runtime4/caml/mlvalues.h

Lines changed: 47 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,54 @@ 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+
/* flambda-backend: We rename the size macros to [Allocated_...] so that we're
256+
forced to think about whether C code needs to updated for mixed blocks, which
257+
have separate notions of scannable size and total size of an object, even for
258+
scannable tags. We call an object's size (including possibly non-scannable
259+
fields) its "allocated" size to document the fact that you shouldn't scan
260+
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+
#ifndef Hide_upstream_size_macros
279+
#define Wosize_hd(hd) Allocated_wosize_hd(hd)
280+
#define Wosize_val(val) Allocated_wosize_val(val)
281+
#define Wosize_op(op) Allocated_wosize_op(op)
282+
#define Wosize_bp(bp) Allocated_wosize_bp(bp)
283+
#define Wosize_hp(hp) Allocated_wosize_hp(hp)
284+
#define Bosize_val(val) Allocated_bosize_val(val)
285+
#define Bosize_op(op) Allocated_bosize_op(op)
286+
#define Bosize_bp(bp) Allocated_bosize_bp(bp)
287+
#define Bosize_hd(hd) Allocated_bosize_hd(hd)
288+
#define Whsize_hp(hp) Allocated_whsize_hp(hp)
289+
#define Whsize_val(val) Allocated_whsize_val(val)
290+
#define Whsize_bp(bp) Allocated_whsize_bp(bp)
291+
#define Whsize_hd(hd) Allocated_whsize_hd(hd)
292+
#define Bhsize_hp(hp) Allocated_bhsize_hp(hp)
293+
#define Bhsize_hd(hd) Allocated_bhsize_hd(hd)
294+
#endif // Hide_upstream_size_macros
268295

269296
#define Scannable_wosize_hp(hp) (Scannable_wosize_hd (Hd_hp (hp)))
270297

@@ -322,7 +349,7 @@ typedef opcode_t * code_t;
322349
with tag Closure_tag (see compact.c). */
323350

324351
#define Infix_tag 249
325-
#define Infix_offset_hd(hd) (Bosize_hd(hd))
352+
#define Infix_offset_hd(hd) (Allocated_bosize_hd(hd))
326353
#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
327354

328355
/* Another special case: objects */

0 commit comments

Comments
 (0)