1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
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 */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
|
|
|
#include "major_gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
|
|
|
|
#define Setup_for_gc
|
|
|
|
#define Restore_after_gc
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc (mlsize_t wosize, tag_t tag)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value result;
|
|
|
|
|
|
|
|
Assert (wosize > 0 && wosize <= Max_young_wosize);
|
|
|
|
Alloc_small (result, wosize, tag);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_tuple(mlsize_t n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return alloc(n, 0);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value 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{
|
|
|
|
result = alloc_shr (wosize, String_tag);
|
1996-11-15 07:25:26 -08:00
|
|
|
result = 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;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_final (mlsize_t len, final_fun fun, mlsize_t mem, mlsize_t max)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value result = alloc_shr (len, Final_tag);
|
|
|
|
|
|
|
|
Field (result, 0) = (value) fun;
|
|
|
|
adjust_gc_speed (mem, max);
|
1996-11-15 07:25:26 -08:00
|
|
|
result = check_urgent_gc (result);
|
1995-05-04 03:15:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value copy_string(char *s)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
value res;
|
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
res = alloc_string(len);
|
|
|
|
bcopy(s, String_val(res), len);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_array(value (*funct)(char *), char ** arr)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
mlsize_t nbr, n;
|
1997-05-26 10:16:31 -07:00
|
|
|
value v, result;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
nbr = 0;
|
|
|
|
while (arr[nbr] != 0) nbr++;
|
|
|
|
if (nbr == 0) {
|
1995-07-10 02:48:27 -07:00
|
|
|
return Atom(0);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
1997-05-26 10:16:31 -07:00
|
|
|
result = nbr < Max_young_wosize ? alloc(nbr, 0) : alloc_shr(nbr, 0);
|
|
|
|
for (n = 0; n < nbr; n++) Field(result, n) = Val_int(0);
|
|
|
|
Begin_root(result);
|
|
|
|
for (n = 0; n < nbr; n++) {
|
|
|
|
/* The two statements below must be separate because of evaluation
|
|
|
|
order. */
|
|
|
|
v = funct(arr[n]);
|
|
|
|
modify(&Field(result, n), v);
|
|
|
|
}
|
|
|
|
End_roots();
|
|
|
|
return result;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value copy_string_array(char **arr)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return alloc_array(copy_string, arr);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
int 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;
|
|
|
|
}
|