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, projet Cristal, 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-07-11 11:09:15 -07:00
|
|
|
/* Operations on objects */
|
|
|
|
|
2003-06-23 05:46:13 -07:00
|
|
|
#include <string.h>
|
1995-07-11 11:09:15 -07:00
|
|
|
#include "alloc.h"
|
1998-08-27 05:11:51 -07:00
|
|
|
#include "fail.h"
|
|
|
|
#include "gc.h"
|
1995-07-11 11:09:15 -07:00
|
|
|
#include "major_gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "minor_gc.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "prims.h"
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value static_alloc(value size)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
|
|
|
return (value) stat_alloc((asize_t) Long_val(size));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value static_free(value blk)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free((void *) blk);
|
1995-07-11 11:09:15 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value static_resize(value blk, value new_size)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
|
|
|
return (value) stat_resize((char *) blk, (asize_t) Long_val(new_size));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value obj_is_block(value arg)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
|
|
|
return Val_bool(Is_block(arg));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value obj_tag(value arg)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
|
|
|
return Val_int(Tag_val(arg));
|
|
|
|
}
|
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
CAMLprim value obj_set_tag (value arg, value new_tag)
|
|
|
|
{
|
|
|
|
Tag_val (arg) = Int_val (new_tag);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value obj_block(value tag, value size)
|
1995-07-11 11:09:15 -07:00
|
|
|
{
|
|
|
|
value res;
|
|
|
|
mlsize_t sz, i;
|
|
|
|
tag_t tg;
|
|
|
|
|
|
|
|
sz = Long_val(size);
|
|
|
|
tg = Long_val(tag);
|
|
|
|
if (sz == 0) return Atom(tg);
|
|
|
|
res = alloc(sz, tg);
|
|
|
|
for (i = 0; i < sz; i++)
|
|
|
|
Field(res, i) = Val_long(0);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value obj_dup(value arg)
|
1998-06-24 12:22:26 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (arg);
|
|
|
|
CAMLlocal1 (res);
|
1998-06-24 12:22:26 -07:00
|
|
|
mlsize_t sz, i;
|
|
|
|
tag_t tg;
|
|
|
|
|
|
|
|
sz = Wosize_val(arg);
|
|
|
|
if (sz == 0) return arg;
|
|
|
|
tg = Tag_val(arg);
|
2003-06-23 05:46:13 -07:00
|
|
|
if (tg >= No_scan_tag) {
|
|
|
|
res = alloc(sz, tg);
|
|
|
|
memcpy(Bp_val(res), Bp_val(arg), sz * sizeof(value));
|
|
|
|
} else if (sz <= Max_young_wosize) {
|
|
|
|
res = alloc_small(sz, tg);
|
|
|
|
for (i = 0; i < sz; i++) Field(res, i) = Field(arg, i);
|
|
|
|
} else {
|
|
|
|
res = alloc_shr(sz, tg);
|
|
|
|
for (i = 0; i < sz; i++) initialize(&Field(res, i), Field(arg, i));
|
|
|
|
}
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (res);
|
1998-06-24 12:22:26 -07:00
|
|
|
}
|
1998-08-27 05:11:51 -07:00
|
|
|
|
|
|
|
/* Shorten the given block to the given size and return void.
|
|
|
|
Raise Invalid_argument if the given size is less than or equal
|
|
|
|
to 0 or greater than the current size.
|
|
|
|
|
|
|
|
algorithm:
|
|
|
|
Change the length field of the header. Make up a white object
|
|
|
|
with the leftover part of the object: this is needed in the major
|
|
|
|
heap and harmless in the minor heap.
|
|
|
|
*/
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value obj_truncate (value v, value newsize)
|
1998-08-27 05:11:51 -07:00
|
|
|
{
|
|
|
|
mlsize_t new_wosize = Long_val (newsize);
|
|
|
|
header_t hd = Hd_val (v);
|
|
|
|
tag_t tag = Tag_hd (hd);
|
|
|
|
color_t color = Color_hd (hd);
|
|
|
|
mlsize_t wosize = Wosize_hd (hd);
|
2000-03-30 09:29:09 -08:00
|
|
|
mlsize_t i;
|
1998-08-27 05:11:51 -07:00
|
|
|
|
2000-07-10 04:28:38 -07:00
|
|
|
if (tag == Double_array_tag) new_wosize *= Double_wosize; /* PR#156 */
|
|
|
|
|
2002-06-05 05:10:40 -07:00
|
|
|
if (new_wosize <= 0 || new_wosize > wosize) invalid_argument ("Obj.truncate");
|
1998-08-27 05:11:51 -07:00
|
|
|
if (new_wosize == wosize) return Val_unit;
|
2000-03-30 09:29:09 -08:00
|
|
|
/* PR#61: since we're about to lose our references to the elements
|
|
|
|
beyond new_wosize in v, erase them explicitly so that the GC
|
|
|
|
can darken them as appropriate. */
|
2000-03-30 09:33:43 -08:00
|
|
|
if (tag < No_scan_tag) {
|
2000-04-03 01:34:22 -07:00
|
|
|
for (i = new_wosize; i < wosize; i++){
|
|
|
|
modify(&Field(v, i), Val_unit);
|
|
|
|
#ifdef DEBUG
|
|
|
|
Field (v, i) = Debug_free_truncate;
|
|
|
|
#endif
|
|
|
|
}
|
2000-03-30 09:33:43 -08:00
|
|
|
}
|
2002-06-05 05:10:40 -07:00
|
|
|
/* We must use an odd tag for the header of the leftovers so it does not
|
|
|
|
look like a pointer because there may be some references to it in
|
|
|
|
ref_table. */
|
1998-08-27 05:11:51 -07:00
|
|
|
Field (v, new_wosize) =
|
2002-06-05 05:10:40 -07:00
|
|
|
Make_header (Wosize_whsize (wosize-new_wosize), 1, Caml_white);
|
1998-08-27 05:11:51 -07:00
|
|
|
Hd_val (v) = Make_header (new_wosize, tag, color);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
2002-09-18 06:59:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* [lazy_is_forward] and [lazy_follow_forward] are used in stdlib/lazy.ml.
|
|
|
|
They are not written in O'Caml because they must be atomic with respect
|
|
|
|
to the GC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CAMLprim value lazy_is_forward (value v)
|
|
|
|
{
|
|
|
|
return Val_bool (Is_block (v) && Tag_val (v) == Forward_tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value lazy_follow_forward (value v)
|
|
|
|
{
|
|
|
|
if (Is_block (v) && Tag_val (v) == Forward_tag){
|
|
|
|
return Forward_val (v);
|
|
|
|
}else{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|