1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* 1. Allocation functions doing the same work as the macros in the
|
|
|
|
case where [Setup_for_gc] and [Restore_after_gc] are no-ops.
|
|
|
|
2. Convenience functions related to allocation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "alloc.h"
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "custom.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "major_gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
|
|
|
|
#define Setup_for_gc
|
|
|
|
#define Restore_after_gc
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc (mlsize_t wosize, tag_t tag)
|
1998-10-26 11:19:32 -08:00
|
|
|
{
|
|
|
|
value result;
|
|
|
|
mlsize_t i;
|
|
|
|
|
2000-03-30 09:29:29 -08:00
|
|
|
Assert (tag < 256);
|
2002-01-18 07:13:26 -08:00
|
|
|
Assert (tag != Infix_tag);
|
2001-10-09 07:34:14 -07:00
|
|
|
if (wosize == 0){
|
|
|
|
result = Atom (tag);
|
|
|
|
}else if (wosize <= Max_young_wosize){
|
1998-10-26 11:19:32 -08:00
|
|
|
Alloc_small (result, wosize, tag);
|
|
|
|
if (tag < No_scan_tag){
|
2014-05-01 06:07:48 -07:00
|
|
|
for (i = 0; i < wosize; i++) Field (result, i) = Val_unit;
|
1998-10-26 11:19:32 -08:00
|
|
|
}
|
|
|
|
}else{
|
2003-12-31 06:20:40 -08:00
|
|
|
result = caml_alloc_shr (wosize, tag);
|
2014-05-01 06:07:48 -07:00
|
|
|
if (tag < No_scan_tag){
|
|
|
|
for (i = 0; i < wosize; i++) Field (result, i) = Val_unit;
|
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
result = caml_check_urgent_gc (result);
|
1998-10-26 11:19:32 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc_small (mlsize_t wosize, tag_t tag)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value result;
|
1999-11-29 11:03:05 -08:00
|
|
|
|
2000-09-08 04:48:06 -07:00
|
|
|
Assert (wosize > 0);
|
2000-08-23 10:10:03 -07:00
|
|
|
Assert (wosize <= Max_young_wosize);
|
2000-03-30 09:29:29 -08:00
|
|
|
Assert (tag < 256);
|
1995-05-04 03:15:53 -07:00
|
|
|
Alloc_small (result, wosize, tag);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc_tuple(mlsize_t n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_alloc(n, 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc_string (mlsize_t len)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value result;
|
|
|
|
mlsize_t offset_index;
|
|
|
|
mlsize_t wosize = (len + sizeof (value)) / sizeof (value);
|
|
|
|
|
|
|
|
if (wosize <= Max_young_wosize) {
|
|
|
|
Alloc_small (result, wosize, String_tag);
|
|
|
|
}else{
|
2003-12-31 06:20:40 -08:00
|
|
|
result = caml_alloc_shr (wosize, String_tag);
|
|
|
|
result = caml_check_urgent_gc (result);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
Field (result, wosize - 1) = 0;
|
|
|
|
offset_index = Bsize_wsize (wosize) - 1;
|
|
|
|
Byte (result, offset_index) = offset_index - len;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc_final (mlsize_t len, final_fun fun,
|
|
|
|
mlsize_t mem, mlsize_t max)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
return caml_alloc_custom(caml_final_custom_operations(fun),
|
|
|
|
len * sizeof(value), mem, max);
|
2000-02-10 06:04:59 -08:00
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_copy_string(char const *s)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
value res;
|
|
|
|
|
|
|
|
len = strlen(s);
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_string(len);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(String_val(res), s, len);
|
1995-05-04 03:15:53 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_alloc_array(value (*funct)(char const *),
|
|
|
|
char const ** arr)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam0 ();
|
1995-05-04 03:15:53 -07:00
|
|
|
mlsize_t nbr, n;
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLlocal2 (v, result);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
nbr = 0;
|
|
|
|
while (arr[nbr] != 0) nbr++;
|
|
|
|
if (nbr == 0) {
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (Atom(0));
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
2003-12-29 14:15:02 -08:00
|
|
|
result = caml_alloc (nbr, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
for (n = 0; n < nbr; n++) {
|
|
|
|
/* The two statements below must be separate because of evaluation
|
|
|
|
order (don't take the address &Field(result, n) before
|
|
|
|
calling funct, which may cause a GC and move result). */
|
|
|
|
v = funct(arr[n]);
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_modify(&Field(result, n), v);
|
1999-11-29 11:03:05 -08:00
|
|
|
}
|
|
|
|
CAMLreturn (result);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport value caml_copy_string_array(char const ** arr)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_alloc_array(caml_copy_string, arr);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLexport int caml_convert_flag_list(value list, int *flags)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
res = 0;
|
1995-06-18 07:44:56 -07:00
|
|
|
while (list != Val_int(0)) {
|
|
|
|
res |= flags[Int_val(Field(list, 0))];
|
1995-05-04 03:15:53 -07:00
|
|
|
list = Field(list, 1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1998-04-06 02:15:55 -07:00
|
|
|
|
|
|
|
/* For compiling let rec over values */
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLprim value caml_alloc_dummy(value size)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
mlsize_t wosize = Int_val(size);
|
|
|
|
|
|
|
|
if (wosize == 0) return Atom(0);
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_alloc (wosize, 0);
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
|
|
|
|
2007-02-09 05:31:15 -08:00
|
|
|
CAMLprim value caml_alloc_dummy_float (value size)
|
|
|
|
{
|
|
|
|
mlsize_t wosize = Int_val(size) * Double_wosize;
|
|
|
|
|
|
|
|
if (wosize == 0) return Atom(0);
|
|
|
|
return caml_alloc (wosize, 0);
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
CAMLprim value caml_update_dummy(value dummy, value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
mlsize_t size, i;
|
2007-02-09 05:31:15 -08:00
|
|
|
tag_t tag;
|
|
|
|
|
1998-04-06 02:15:55 -07:00
|
|
|
size = Wosize_val(newval);
|
2007-02-09 05:31:15 -08:00
|
|
|
tag = Tag_val (newval);
|
1998-04-06 02:15:55 -07:00
|
|
|
Assert (size == Wosize_val(dummy));
|
2007-02-09 05:31:15 -08:00
|
|
|
Assert (tag < No_scan_tag || tag == Double_array_tag);
|
|
|
|
|
|
|
|
Tag_val(dummy) = tag;
|
|
|
|
if (tag == Double_array_tag){
|
|
|
|
size = Wosize_val (newval) / Double_wosize;
|
|
|
|
for (i = 0; i < size; i++){
|
|
|
|
Store_double_field (dummy, i, Double_field (newval, i));
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for (i = 0; i < size; i++){
|
|
|
|
caml_modify (&Field(dummy, i), Field(newval, i));
|
|
|
|
}
|
|
|
|
}
|
1998-04-06 02:15:55 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|