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 */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Structured output */
|
|
|
|
|
1996-05-28 05:41:37 -07:00
|
|
|
#include <string.h>
|
1996-04-01 07:24:38 -08:00
|
|
|
#include "alloc.h"
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "custom.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "fail.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "intext.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "reverse.h"
|
|
|
|
|
|
|
|
/* To keep track of sharing in externed objects */
|
|
|
|
|
|
|
|
typedef unsigned long byteoffset_t;
|
|
|
|
|
|
|
|
struct extern_obj {
|
|
|
|
byteoffset_t ofs;
|
1997-04-11 06:57:04 -07:00
|
|
|
value obj;
|
1995-05-04 03:15:53 -07:00
|
|
|
};
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
static byteoffset_t initial_ofs = 1; /* Initial value of object offsets */
|
1997-04-11 06:57:04 -07:00
|
|
|
static struct extern_obj * extern_table = NULL;
|
1995-11-10 04:39:48 -08:00
|
|
|
static unsigned long extern_table_size;
|
1997-07-02 11:16:15 -07:00
|
|
|
static byteoffset_t obj_counter; /* Number of objects emitted so far */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
1995-11-10 04:39:48 -08:00
|
|
|
#define Hash(v) (((unsigned long) ((v) >> 3)) % extern_table_size)
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
1995-11-10 04:39:48 -08:00
|
|
|
#define Hash(v) (((unsigned long) ((v) >> 2)) % extern_table_size)
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
|
1997-04-11 06:57:04 -07:00
|
|
|
/* Allocate a new extern table */
|
1997-09-02 05:55:01 -07:00
|
|
|
static void alloc_extern_table(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
asize_t i;
|
|
|
|
extern_table = (struct extern_obj *)
|
1997-04-11 06:57:04 -07:00
|
|
|
stat_alloc(extern_table_size * sizeof(struct extern_obj));
|
|
|
|
for (i = 0; i < extern_table_size; i++) extern_table[i].ofs = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-04-11 06:57:04 -07:00
|
|
|
/* Grow the extern table */
|
1997-09-02 05:55:01 -07:00
|
|
|
static void resize_extern_table(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
asize_t oldsize;
|
|
|
|
struct extern_obj * oldtable;
|
1996-04-01 07:24:38 -08:00
|
|
|
value obj;
|
1997-04-11 06:57:04 -07:00
|
|
|
byteoffset_t ofs;
|
1995-05-04 03:15:53 -07:00
|
|
|
asize_t i, h;
|
|
|
|
|
|
|
|
oldsize = extern_table_size;
|
|
|
|
oldtable = extern_table;
|
|
|
|
extern_table_size = 2 * extern_table_size;
|
|
|
|
alloc_extern_table();
|
|
|
|
for (i = 0; i < oldsize; i++) {
|
1997-04-11 06:57:04 -07:00
|
|
|
ofs = oldtable[i].ofs;
|
|
|
|
if (ofs >= initial_ofs) {
|
|
|
|
obj = oldtable[i].obj;
|
1996-04-01 07:24:38 -08:00
|
|
|
h = Hash(obj);
|
1997-04-11 06:57:04 -07:00
|
|
|
while (extern_table[h].ofs >= initial_ofs) {
|
1996-04-01 07:24:38 -08:00
|
|
|
h++;
|
|
|
|
if (h >= extern_table_size) h = 0;
|
|
|
|
}
|
1997-04-11 06:57:04 -07:00
|
|
|
extern_table[h].ofs = ofs;
|
1996-04-01 07:24:38 -08:00
|
|
|
extern_table[h].obj = obj;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free(oldtable);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-04-11 06:57:04 -07:00
|
|
|
/* Free the extern table. We keep it around for next call if
|
|
|
|
it's still small (we did not grow it) and the initial offset
|
|
|
|
does not risk running over next time. */
|
1997-09-02 05:55:01 -07:00
|
|
|
static void free_extern_table(void)
|
1997-04-11 06:57:04 -07:00
|
|
|
{
|
|
|
|
if (extern_table_size > INITIAL_EXTERN_TABLE_SIZE ||
|
|
|
|
initial_ofs >= INITIAL_OFFSET_MAX) {
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free(extern_table);
|
1997-04-11 06:57:04 -07:00
|
|
|
extern_table = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
/* To buffer the output */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
static char * extern_block, * extern_ptr, * extern_limit;
|
1997-07-02 11:16:15 -07:00
|
|
|
static int extern_block_malloced;
|
1996-04-01 07:24:38 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void alloc_extern_block(void)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
extern_block = stat_alloc(INITIAL_EXTERN_BLOCK_SIZE);
|
|
|
|
extern_limit = extern_block + INITIAL_EXTERN_BLOCK_SIZE;
|
|
|
|
extern_ptr = extern_block;
|
1997-07-02 11:16:15 -07:00
|
|
|
extern_block_malloced = 1;
|
1996-04-01 07:24:38 -08:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void resize_extern_block(int required)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
long curr_pos, size, reqd_size;
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
if (! extern_block_malloced) {
|
|
|
|
initial_ofs += obj_counter;
|
|
|
|
free_extern_table();
|
|
|
|
failwith("Marshal.to_buffer: buffer overflow");
|
|
|
|
}
|
1996-04-01 07:24:38 -08:00
|
|
|
curr_pos = extern_ptr - extern_block;
|
|
|
|
size = extern_limit - extern_block;
|
|
|
|
reqd_size = curr_pos + required;
|
|
|
|
while (size <= reqd_size) size *= 2;
|
|
|
|
extern_block = stat_resize(extern_block, size);
|
|
|
|
extern_limit = extern_block + size;
|
|
|
|
extern_ptr = extern_block + curr_pos;
|
|
|
|
}
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
/* Write characters, integers, and blocks in the output buffer */
|
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
#define Write(c) \
|
|
|
|
if (extern_ptr >= extern_limit) resize_extern_block(1); \
|
|
|
|
*extern_ptr++ = (c)
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void writeblock(char *data, long int len)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + len > extern_limit) resize_extern_block(len);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(extern_ptr, data, len);
|
1996-04-01 07:24:38 -08:00
|
|
|
extern_ptr += len;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void writecode8(int code, long int val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-01 07:24:38 -08:00
|
|
|
if (extern_ptr + 2 > extern_limit) resize_extern_block(2);
|
|
|
|
extern_ptr[0] = code;
|
|
|
|
extern_ptr[1] = val;
|
|
|
|
extern_ptr += 2;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void writecode16(int code, long int val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-01 07:24:38 -08:00
|
|
|
if (extern_ptr + 3 > extern_limit) resize_extern_block(3);
|
|
|
|
extern_ptr[0] = code;
|
|
|
|
extern_ptr[1] = val >> 8;
|
|
|
|
extern_ptr[2] = val;
|
|
|
|
extern_ptr += 3;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void write32(long int val)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + 4 > extern_limit) resize_extern_block(4);
|
|
|
|
extern_ptr[0] = val >> 24;
|
|
|
|
extern_ptr[1] = val >> 16;
|
|
|
|
extern_ptr[2] = val >> 8;
|
|
|
|
extern_ptr[3] = val;
|
|
|
|
extern_ptr += 4;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void writecode32(int code, long int val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-01 07:24:38 -08:00
|
|
|
if (extern_ptr + 5 > extern_limit) resize_extern_block(5);
|
|
|
|
extern_ptr[0] = code;
|
|
|
|
extern_ptr[1] = val >> 24;
|
|
|
|
extern_ptr[2] = val >> 16;
|
|
|
|
extern_ptr[3] = val >> 8;
|
|
|
|
extern_ptr[4] = val;
|
|
|
|
extern_ptr += 5;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
1997-09-02 05:55:01 -07:00
|
|
|
static void writecode64(int code, long val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int i;
|
1996-04-01 07:24:38 -08:00
|
|
|
if (extern_ptr + 9 > extern_limit) resize_extern_block(9);
|
|
|
|
*extern_ptr ++ = code;
|
|
|
|
for (i = 64 - 8; i >= 0; i -= 8) *extern_ptr++ = val >> i;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1996-04-01 07:24:38 -08:00
|
|
|
/* Marshal the given value in the output buffer */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
static unsigned long size_32; /* Size in words of 32-bit block for struct. */
|
|
|
|
static unsigned long size_64; /* Size in words of 64-bit block for struct. */
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
static int extern_ignore_sharing; /* Flag to ignore sharing */
|
|
|
|
static int extern_closures; /* Flag to allow externing code pointers */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void extern_invalid_argument(char *msg)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
1997-07-02 11:16:15 -07:00
|
|
|
if (extern_block_malloced) stat_free(extern_block);
|
1997-04-11 06:57:04 -07:00
|
|
|
initial_ofs += obj_counter;
|
|
|
|
free_extern_table();
|
|
|
|
invalid_argument(msg);
|
1996-04-01 07:24:38 -08:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void extern_rec(value v)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
tailcall:
|
|
|
|
if (Is_long(v)) {
|
|
|
|
long n = Long_val(v);
|
|
|
|
if (n >= 0 && n < 0x40) {
|
1996-04-01 07:24:38 -08:00
|
|
|
Write(PREFIX_SMALL_INT + n);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else if (n >= -(1 << 7) && n < (1 << 7)) {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode8(CODE_INT8, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else if (n >= -(1 << 15) && n < (1 << 15)) {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode16(CODE_INT16, n);
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
1995-05-04 03:15:53 -07:00
|
|
|
} else if (n < -(1L << 31) || n >= (1L << 31)) {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode64(CODE_INT64, n);
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
} else
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode32(CODE_INT32, n);
|
1997-07-02 11:16:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Is_young(v) || Is_in_heap(v) || Is_atom(v)) {
|
1995-05-04 03:15:53 -07:00
|
|
|
header_t hd = Hd_val(v);
|
|
|
|
tag_t tag = Tag_hd(hd);
|
|
|
|
mlsize_t sz = Wosize_hd(hd);
|
1996-04-01 07:24:38 -08:00
|
|
|
asize_t h;
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Atoms are treated specially for two reasons: they are not allocated
|
|
|
|
in the externed block, and they are automatically shared. */
|
|
|
|
if (sz == 0) {
|
|
|
|
if (tag < 16) {
|
1996-04-01 07:24:38 -08:00
|
|
|
Write(PREFIX_SMALL_BLOCK + tag);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode32(CODE_BLOCK32, hd);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Check if already seen */
|
1999-01-12 09:13:29 -08:00
|
|
|
if (! extern_ignore_sharing && tag != Infix_tag) {
|
1995-05-04 03:15:53 -07:00
|
|
|
if (2 * obj_counter >= extern_table_size) resize_extern_table();
|
|
|
|
h = Hash(v);
|
1997-04-11 06:57:04 -07:00
|
|
|
while (extern_table[h].ofs >= initial_ofs) {
|
1995-05-04 03:15:53 -07:00
|
|
|
if (extern_table[h].obj == v) {
|
1997-04-11 06:57:04 -07:00
|
|
|
byteoffset_t d = obj_counter - (extern_table[h].ofs - initial_ofs);
|
1995-05-04 03:15:53 -07:00
|
|
|
if (d < 0x100) {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode8(CODE_SHARED8, d);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else if (d < 0x10000) {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode16(CODE_SHARED16, d);
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
1996-04-01 07:24:38 -08:00
|
|
|
writecode32(CODE_SHARED32, d);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
h++;
|
|
|
|
if (h >= extern_table_size) h = 0;
|
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
/* Not seen yet. Record the object */
|
1997-04-11 06:57:04 -07:00
|
|
|
extern_table[h].ofs = initial_ofs + obj_counter;
|
1995-05-04 03:15:53 -07:00
|
|
|
extern_table[h].obj = v;
|
|
|
|
obj_counter++;
|
1997-07-02 11:16:15 -07:00
|
|
|
}
|
|
|
|
/* Output the contents of the object */
|
|
|
|
switch(tag) {
|
|
|
|
case String_tag: {
|
|
|
|
mlsize_t len = string_length(v);
|
|
|
|
if (len < 0x20) {
|
|
|
|
Write(PREFIX_SMALL_STRING + len);
|
|
|
|
} else if (len < 0x100) {
|
|
|
|
writecode8(CODE_STRING8, len);
|
|
|
|
} else {
|
|
|
|
writecode32(CODE_STRING32, len);
|
1995-07-27 10:41:09 -07:00
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
writeblock(String_val(v), len);
|
|
|
|
size_32 += 1 + (len + 4) / 4;
|
|
|
|
size_64 += 1 + (len + 8) / 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Double_tag: {
|
|
|
|
if (sizeof(double) != 8)
|
|
|
|
extern_invalid_argument("output_value: non-standard floats");
|
|
|
|
Write(CODE_DOUBLE_NATIVE);
|
|
|
|
writeblock((char *) v, 8);
|
|
|
|
size_32 += 1 + 2;
|
|
|
|
size_64 += 1 + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Double_array_tag: {
|
|
|
|
mlsize_t nfloats;
|
|
|
|
if (sizeof(double) != 8)
|
|
|
|
extern_invalid_argument("output_value: non-standard floats");
|
|
|
|
nfloats = Wosize_val(v) / Double_wosize;
|
|
|
|
if (nfloats < 0x100) {
|
|
|
|
writecode8(CODE_DOUBLE_ARRAY8_NATIVE, nfloats);
|
|
|
|
} else {
|
|
|
|
writecode32(CODE_DOUBLE_ARRAY32_NATIVE, nfloats);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
writeblock((char *) v, Bosize_val(v));
|
|
|
|
size_32 += 1 + nfloats * 2;
|
|
|
|
size_64 += 1 + nfloats;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Abstract_tag:
|
|
|
|
extern_invalid_argument("output_value: abstract value");
|
|
|
|
break;
|
|
|
|
case Infix_tag:
|
|
|
|
writecode32(CODE_INFIXPOINTER, Infix_offset_hd(hd));
|
|
|
|
extern_rec(v - Infix_offset_hd(hd));
|
|
|
|
break;
|
|
|
|
case Object_tag:
|
|
|
|
extern_invalid_argument("output_value: object value");
|
|
|
|
break;
|
2000-02-10 06:04:59 -08:00
|
|
|
case Custom_tag: {
|
|
|
|
unsigned long sz_32, sz_64;
|
|
|
|
char * ident = Custom_ops_val(v)->identifier;
|
|
|
|
Write(CODE_CUSTOM);
|
|
|
|
writeblock(ident, strlen(ident) + 1);
|
|
|
|
Custom_ops_val(v)->serialize(v, &sz_32, &sz_64);
|
|
|
|
size_32 += 2 + ((sz_32 + 3) >> 2); /* header + ops + data */
|
|
|
|
size_64 += 2 + ((sz_64 + 7) >> 3);
|
|
|
|
break;
|
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
default: {
|
|
|
|
mlsize_t i;
|
|
|
|
if (tag < 16 && sz < 8) {
|
|
|
|
Write(PREFIX_SMALL_BLOCK + tag + (sz << 4));
|
|
|
|
} else {
|
2000-01-02 08:10:00 -08:00
|
|
|
writecode32(CODE_BLOCK32, Whitehd_hd (hd));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
size_32 += 1 + sz;
|
|
|
|
size_64 += 1 + sz;
|
|
|
|
for (i = 0; i < sz - 1; i++) extern_rec(Field(v, i));
|
|
|
|
v = Field(v, i);
|
|
|
|
goto tailcall;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
return;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
if ((char *) v >= code_area_start && (char *) v < code_area_end) {
|
|
|
|
if (!extern_closures)
|
|
|
|
extern_invalid_argument("output_value: functional value");
|
|
|
|
writecode32(CODE_CODEPOINTER, (char *) v - code_area_start);
|
1997-07-30 05:28:31 -07:00
|
|
|
writeblock((char *) code_checksum(), 16);
|
1997-07-02 11:16:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
extern_invalid_argument("output_value: abstract value");
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
enum { NO_SHARING = 1, CLOSURES = 2 };
|
|
|
|
static int extern_flags[] = { NO_SHARING, CLOSURES };
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static long extern_value(value v, value flags)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-04-01 07:24:38 -08:00
|
|
|
long res_len;
|
1997-07-02 11:16:15 -07:00
|
|
|
int fl;
|
|
|
|
/* Parse flag list */
|
|
|
|
fl = convert_flag_list(flags, extern_flags);
|
|
|
|
extern_ignore_sharing = fl & NO_SHARING;
|
|
|
|
extern_closures = fl & CLOSURES;
|
1997-04-11 06:57:04 -07:00
|
|
|
/* Allocate hashtable of objects already seen, if needed */
|
1995-05-04 03:15:53 -07:00
|
|
|
extern_table_size = INITIAL_EXTERN_TABLE_SIZE;
|
1997-04-11 06:57:04 -07:00
|
|
|
if (extern_table == NULL) {
|
|
|
|
alloc_extern_table();
|
|
|
|
initial_ofs = 1;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
obj_counter = 0;
|
|
|
|
size_32 = 0;
|
|
|
|
size_64 = 0;
|
1996-04-01 07:24:38 -08:00
|
|
|
/* Write magic number */
|
|
|
|
write32(Intext_magic_number);
|
|
|
|
/* Set aside space for the sizes */
|
|
|
|
extern_ptr += 4*4;
|
|
|
|
/* Marshal the object */
|
|
|
|
extern_rec(v);
|
1997-04-11 06:57:04 -07:00
|
|
|
/* Update initial offset for next call to extern_value(),
|
|
|
|
if we decide to keep the table of shared objects. */
|
|
|
|
initial_ofs += obj_counter;
|
|
|
|
/* Free the table of shared objects (if needed) */
|
|
|
|
free_extern_table();
|
1996-04-01 07:24:38 -08:00
|
|
|
/* Write the sizes */
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
1995-05-04 03:15:53 -07:00
|
|
|
if (size_32 >= (1L << 32) || size_64 >= (1L << 32)) {
|
|
|
|
/* The object is so big its size cannot be written in the header.
|
|
|
|
Besides, some of the block sizes or string lengths or shared offsets
|
|
|
|
it contains may have overflowed the 32 bits used to write them. */
|
|
|
|
failwith("output_value: object too big");
|
|
|
|
}
|
|
|
|
#endif
|
1996-04-01 07:24:38 -08:00
|
|
|
res_len = extern_ptr - extern_block;
|
|
|
|
extern_ptr = extern_block + 4;
|
|
|
|
write32(res_len - 5*4);
|
|
|
|
write32(obj_counter);
|
|
|
|
write32(size_32);
|
|
|
|
write32(size_64);
|
|
|
|
/* Result is res_len bytes starting at extern_block */
|
|
|
|
return res_len;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void output_val(struct channel *chan, value v, value flags)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
long len;
|
2000-02-07 06:07:31 -08:00
|
|
|
char * block;
|
|
|
|
|
1998-02-26 04:51:39 -08:00
|
|
|
if (! channel_binary_mode(chan))
|
|
|
|
failwith("output_value: not a binary channel");
|
1997-07-02 11:16:15 -07:00
|
|
|
alloc_extern_block();
|
|
|
|
len = extern_value(v, flags);
|
2000-02-07 06:07:31 -08:00
|
|
|
/* During really_putblock, concurrent output_val operations can take
|
|
|
|
place (via signal handlers or context switching in systhreads),
|
|
|
|
and extern_block may change. So, save the pointer in a local variable. */
|
|
|
|
block = extern_block;
|
1996-04-18 09:29:57 -07:00
|
|
|
really_putblock(chan, extern_block, len);
|
2000-02-07 06:07:31 -08:00
|
|
|
stat_free(block);
|
1997-08-29 08:37:22 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value output_value(value vchan, value v, value flags)
|
1997-08-29 08:37:22 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam3 (vchan, v, flags);
|
1997-08-29 08:37:22 -07:00
|
|
|
struct channel * channel = Channel(vchan);
|
1999-11-29 11:03:05 -08:00
|
|
|
|
|
|
|
Lock(channel);
|
|
|
|
output_val(channel, v, flags);
|
|
|
|
Unlock(channel);
|
|
|
|
CAMLreturn (Val_unit);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1996-04-01 07:24:38 -08:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value output_value_to_string(value v, value flags)
|
1996-04-01 07:24:38 -08:00
|
|
|
{
|
|
|
|
long len;
|
|
|
|
value res;
|
1997-07-02 11:16:15 -07:00
|
|
|
alloc_extern_block();
|
|
|
|
len = extern_value(v, flags);
|
1996-04-01 07:24:38 -08:00
|
|
|
res = alloc_string(len);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(String_val(res), extern_block, len);
|
1996-04-01 07:24:38 -08:00
|
|
|
stat_free(extern_block);
|
|
|
|
return res;
|
|
|
|
}
|
1997-07-02 11:16:15 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value output_value_to_buffer(value buf, value ofs, value len,
|
|
|
|
value v, value flags)
|
1997-07-02 11:16:15 -07:00
|
|
|
{
|
|
|
|
long len_res;
|
|
|
|
extern_block = &Byte(buf, Long_val(ofs));
|
|
|
|
extern_limit = extern_block + Long_val(len);
|
|
|
|
extern_ptr = extern_block;
|
|
|
|
extern_block_malloced = 0;
|
|
|
|
len_res = extern_value(v, flags);
|
|
|
|
return Val_long(len_res);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void output_value_to_malloc(value v, value flags,
|
|
|
|
/*out*/ char ** buf, /*out*/ long * len)
|
1998-06-11 05:53:45 -07:00
|
|
|
{
|
2000-07-28 05:23:25 -07:00
|
|
|
long len_res;
|
1998-06-11 05:53:45 -07:00
|
|
|
alloc_extern_block();
|
2000-07-28 05:23:25 -07:00
|
|
|
len_res = extern_value(v, flags);
|
1998-06-11 05:53:45 -07:00
|
|
|
*buf = extern_block;
|
2000-07-28 05:23:25 -07:00
|
|
|
*len = len_res;
|
1998-06-11 05:53:45 -07:00
|
|
|
}
|
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
/* Functions for writing user-defined marshallers */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_int_1(int i)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + 1 > extern_limit) resize_extern_block(1);
|
|
|
|
extern_ptr[0] = i;
|
|
|
|
extern_ptr += 1;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_int_2(int i)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + 2 > extern_limit) resize_extern_block(2);
|
|
|
|
extern_ptr[0] = i >> 8;
|
|
|
|
extern_ptr[1] = i;
|
|
|
|
extern_ptr += 2;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_int_4(int32 i)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + 4 > extern_limit) resize_extern_block(4);
|
|
|
|
extern_ptr[0] = i >> 24;
|
|
|
|
extern_ptr[1] = i >> 16;
|
|
|
|
extern_ptr[2] = i >> 8;
|
|
|
|
extern_ptr[3] = i;
|
|
|
|
extern_ptr += 4;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_int_8(int64 i)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
serialize_block_8(&i, 1);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_float_4(float f)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
serialize_block_4(&f, 1);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_float_8(double f)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
serialize_block_8(&f, 1);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_block_1(void * data, long len)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
if (extern_ptr + len > extern_limit) resize_extern_block(len);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(extern_ptr, data, len);
|
2000-02-10 06:04:59 -08:00
|
|
|
extern_ptr += len;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_block_2(void * data, long len)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
2000-02-14 06:30:40 -08:00
|
|
|
unsigned char * p;
|
|
|
|
char * q;
|
2000-02-10 06:04:59 -08:00
|
|
|
if (extern_ptr + 2 * len > extern_limit) resize_extern_block(2 * len);
|
|
|
|
#ifndef ARCH_BIG_ENDIAN
|
|
|
|
for (p = data, q = extern_ptr; len > 0; len--, p += 2, q += 2)
|
|
|
|
Reverse_16(q, p);
|
|
|
|
extern_ptr = q;
|
|
|
|
#else
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(extern_ptr, data, len * 2);
|
2000-02-10 06:04:59 -08:00
|
|
|
extern_ptr += len * 2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_block_4(void * data, long len)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
2000-02-14 06:30:40 -08:00
|
|
|
unsigned char * p;
|
|
|
|
char * q;
|
2000-02-10 06:04:59 -08:00
|
|
|
if (extern_ptr + 4 * len > extern_limit) resize_extern_block(4 * len);
|
|
|
|
#ifndef ARCH_BIG_ENDIAN
|
|
|
|
for (p = data, q = extern_ptr; len > 0; len--, p += 4, q += 4)
|
|
|
|
Reverse_32(q, p);
|
|
|
|
extern_ptr = q;
|
|
|
|
#else
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(extern_ptr, data, len * 4);
|
2000-02-10 06:04:59 -08:00
|
|
|
extern_ptr += len * 4;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void serialize_block_8(void * data, long len)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
2000-02-14 06:30:40 -08:00
|
|
|
unsigned char * p;
|
|
|
|
char * q;
|
2000-02-10 06:04:59 -08:00
|
|
|
if (extern_ptr + 8 * len > extern_limit) resize_extern_block(8 * len);
|
|
|
|
#ifndef ARCH_BIG_ENDIAN
|
|
|
|
for (p = data, q = extern_ptr; len > 0; len--, p += 8, q += 8)
|
|
|
|
Reverse_64(q, p);
|
|
|
|
extern_ptr = q;
|
|
|
|
#else
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(extern_ptr, data, len * 8);
|
2000-02-10 06:04:59 -08:00
|
|
|
extern_ptr += len * 8;
|
|
|
|
#endif
|
|
|
|
}
|