Skip to content

Commit 90f16ac

Browse files
committed
Stub implementation of new custom memory API
1 parent d40254f commit 90f16ac

File tree

10 files changed

+52
-25
lines changed

10 files changed

+52
-25
lines changed

runtime/bigarray.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
251251
uses_resources =
252252
((flags & CAML_BA_MANAGED_MASK) == CAML_BA_MANAGED)
253253
&& !(flags & CAML_BA_SUBARRAY);
254-
res = caml_alloc_custom_mem(&caml_ba_ops, asize, uses_resources ? size : 0);
254+
res = caml_alloc_custom_dep(&caml_ba_ops, asize, uses_resources ? size : 0);
255255
b = Caml_ba_array_val(res);
256256
b->data = data;
257257
b->num_dims = num_dims;
@@ -291,9 +291,11 @@ CAMLexport void caml_ba_finalize(value v)
291291
case CAML_BA_MANAGED:
292292
if (b->proxy == NULL) {
293293
free(b->data);
294+
caml_free_dependent_memory(v, caml_ba_byte_size(b));
294295
} else {
295296
if (caml_atomic_refcount_decr(&b->proxy->refcount) == 1) {
296297
free(b->proxy->data);
298+
caml_free_dependent_memory(v, b->proxy->size);
297299
free(b->proxy);
298300
}
299301
}
@@ -620,6 +622,7 @@ CAMLexport uintnat caml_ba_deserialize(void * dst)
620622
caml_deserialize_error("input_value: size overflow for bigarray");
621623
/* Allocate room for data */
622624
b->data = malloc(size);
625+
caml_alloc_dependent_memory(Custom_val_data (dst), size);
623626
if (b->data == NULL)
624627
caml_deserialize_error("input_value: out of memory for bigarray");
625628
/* Read data */
@@ -1138,8 +1141,7 @@ static void caml_ba_update_proxy(struct caml_ba_array * b1,
11381141
caml_atomic_refcount_init(&proxy->refcount, 2);
11391142
/* initial refcount: 2 = original array + sub array */
11401143
proxy->data = b1->data;
1141-
proxy->size =
1142-
b1->flags & CAML_BA_MAPPED_FILE ? caml_ba_byte_size(b1) : 0;
1144+
proxy->size = caml_ba_byte_size(b1);
11431145
b1->proxy = proxy;
11441146
b2->proxy = proxy;
11451147
}

runtime/caml/custom.h

+11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ CAMLextern value caml_alloc_custom_mem(const struct custom_operations * ops,
6767
uintnat size, /*size in bytes*/
6868
mlsize_t mem /*memory consumed*/);
6969

70+
/* [caml_alloc_custom_dep] allocates a custom block with dependent memory
71+
(memory outside the heap that will be reclaimed when the block is
72+
finalized). If [mem] is greater than [custom_minor_max_size] (see gc.mli)
73+
the block is allocated directly in the major heap.
74+
The program must call [caml_free_dependent_memory] when the memory is
75+
reclaimed.
76+
*/
77+
CAMLextern value caml_alloc_custom_dep(const struct custom_operations * ops,
78+
uintnat size, /*size in bytes*/
79+
mlsize_t mem /*dep memory in bytes*/);
80+
7081
CAMLextern void
7182
caml_register_custom_operations(const struct custom_operations * ops);
7283

runtime/caml/memory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ CAMLextern value caml_alloc_local(mlsize_t, tag_t);
4040

4141
CAMLextern void caml_adjust_gc_speed (mlsize_t, mlsize_t);
4242
CAMLextern void caml_adjust_minor_gc_speed (mlsize_t, mlsize_t);
43-
CAMLextern void caml_alloc_dependent_memory (mlsize_t bsz);
44-
CAMLextern void caml_free_dependent_memory (mlsize_t bsz);
43+
CAMLextern void caml_alloc_dependent_memory (value v, mlsize_t bsz);
44+
CAMLextern void caml_free_dependent_memory (value v, mlsize_t bsz);
4545
CAMLextern void caml_modify (volatile value *, value);
4646
CAMLextern void caml_modify_local (value obj, intnat i, value val);
4747
CAMLextern void caml_initialize (volatile value *, value);

runtime/caml/mlvalues.h

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ CAMLextern int caml_is_double_array (value); /* 0 is false, 1 is true */
527527
See [custom.h] for operations on method suites. */
528528
#define Custom_tag 255
529529
#define Data_custom_val(v) ((void *) (Op_val(v) + 1))
530+
#define Custom_val_data(d) (Val_op((value *)d - 1))
530531
struct custom_operations; /* defined in [custom.h] */
531532

532533
/* Int32.t, Int64.t and Nativeint.t are represented as custom blocks. */

runtime/custom.c

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ CAMLexport value caml_alloc_custom_mem(const struct custom_operations * ops,
130130
return v;
131131
}
132132

133+
CAMLexport value caml_alloc_custom_dep(const struct custom_operations * ops,
134+
uintnat size, mlsize_t mem)
135+
{
136+
return caml_alloc_custom(ops, size, mem);
137+
}
138+
133139
struct custom_operations_list {
134140
const struct custom_operations * ops;
135141
struct custom_operations_list * next;

runtime/memory.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,14 @@ CAMLexport CAMLweakdef void caml_modify (volatile value *fp, value val)
236236
free it. In both cases, you pass as argument the size (in bytes)
237237
of the block being allocated or freed.
238238
*/
239-
CAMLexport void caml_alloc_dependent_memory (mlsize_t nbytes)
239+
CAMLexport void caml_alloc_dependent_memory (value v, mlsize_t nbytes)
240240
{
241-
Caml_state->dependent_size += nbytes / sizeof (value);
242-
Caml_state->dependent_allocated += nbytes / sizeof (value);
241+
/* No-op for now */
243242
}
244243

245-
CAMLexport void caml_free_dependent_memory (mlsize_t nbytes)
244+
CAMLexport void caml_free_dependent_memory (value v, mlsize_t nbytes)
246245
{
247-
if (Caml_state->dependent_size < nbytes / sizeof (value)){
248-
Caml_state->dependent_size = 0;
249-
}else{
250-
Caml_state->dependent_size -= nbytes / sizeof (value);
251-
}
246+
/* No-op for now */
252247
}
253248

254249
/* Use this function to tell the major GC to speed up when you use

runtime4/caml/custom.h

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ CAMLextern value caml_alloc_custom_mem(struct custom_operations * ops,
6565
uintnat size, /*size in bytes*/
6666
mlsize_t mem /*memory consumed*/);
6767

68+
/* [caml_alloc_custom_dep] allocates a custom block with dependent memory
69+
(memory outside the heap that will be reclaimed when the block is
70+
finalized). If [mem] is greater than [custom_minor_max_size] (see gc.mli)
71+
the block is allocated directly in the major heap.
72+
The program must call [caml_free_dependent_memory] when the memory is
73+
reclaimed.
74+
*/
75+
CAMLextern value caml_alloc_custom_dep(struct custom_operations * ops,
76+
uintnat size, /*size in bytes*/
77+
mlsize_t mem /*dep memory in bytes*/);
78+
6879
CAMLextern void caml_register_custom_operations(struct custom_operations * ops);
6980

7081
/* Global variable moved to Caml_state in 4.10 */

runtime4/caml/memory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ CAMLextern value caml_alloc_shr_for_minor_gc (mlsize_t, tag_t, header_t);
5757
CAMLextern value caml_alloc_local(mlsize_t, tag_t);
5858

5959
CAMLextern void caml_adjust_gc_speed (mlsize_t, mlsize_t);
60-
CAMLextern void caml_alloc_dependent_memory (mlsize_t bsz);
61-
CAMLextern void caml_free_dependent_memory (mlsize_t bsz);
60+
CAMLextern void caml_alloc_dependent_memory (value v, mlsize_t bsz);
61+
CAMLextern void caml_free_dependent_memory (value v, mlsize_t bsz);
6262
CAMLextern void caml_modify (value *, value);
6363
CAMLextern void caml_modify_local (value obj, intnat i, value val);
6464
CAMLextern void caml_initialize (value *, value);

runtime4/custom.c

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ CAMLexport value caml_alloc_custom_mem(struct custom_operations * ops,
108108
return v;
109109
}
110110

111+
CAMLexport value caml_alloc_custom_dep(struct custom_operations * ops,
112+
uintnat size, mlsize_t mem)
113+
{
114+
return caml_alloc_custom_mem(ops, size, mem);
115+
}
116+
111117
struct custom_operations_list {
112118
struct custom_operations * ops;
113119
struct custom_operations_list * next;

runtime4/memory.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -587,19 +587,14 @@ CAMLexport value caml_alloc_shr_no_track_noexc (mlsize_t wosize, tag_t tag)
587587
free it. In both cases, you pass as argument the size (in bytes)
588588
of the block being allocated or freed.
589589
*/
590-
CAMLexport void caml_alloc_dependent_memory (mlsize_t nbytes)
590+
CAMLexport void caml_alloc_dependent_memory (value v, mlsize_t nbytes)
591591
{
592-
caml_dependent_size += nbytes / sizeof (value);
593-
caml_dependent_allocated += nbytes / sizeof (value);
592+
/* No-op for now */
594593
}
595594

596-
CAMLexport void caml_free_dependent_memory (mlsize_t nbytes)
595+
CAMLexport void caml_free_dependent_memory (value v, mlsize_t nbytes)
597596
{
598-
if (caml_dependent_size < nbytes / sizeof (value)){
599-
caml_dependent_size = 0;
600-
}else{
601-
caml_dependent_size -= nbytes / sizeof (value);
602-
}
597+
/* No-op for now */
603598
}
604599

605600
/* Use this function to tell the major GC to speed up when you use

0 commit comments

Comments
 (0)