2000-02-10 06:04:59 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 2000 Institut National de Recherche en Informatique et */
|
|
|
|
/* 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. */
|
2000-02-10 06:04:59 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2000-03-10 12:30:38 -08:00
|
|
|
#include <string.h>
|
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "custom.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLextern value caml_alloc_custom(struct custom_operations * ops,
|
|
|
|
unsigned long size,
|
|
|
|
mlsize_t mem,
|
|
|
|
mlsize_t max)
|
2000-02-11 04:03:31 -08:00
|
|
|
{
|
|
|
|
mlsize_t wosize;
|
|
|
|
value result;
|
|
|
|
|
|
|
|
wosize = 1 + (size + sizeof(value) - 1) / sizeof(value);
|
|
|
|
if (ops->finalize == NULL && wosize <= Max_young_wosize) {
|
2003-12-29 14:15:02 -08:00
|
|
|
result = caml_alloc_small(wosize, Custom_tag);
|
2000-02-11 04:03:31 -08:00
|
|
|
Custom_ops_val(result) = ops;
|
|
|
|
} else {
|
2003-12-31 06:20:40 -08:00
|
|
|
result = caml_alloc_shr(wosize, Custom_tag);
|
2000-02-11 04:03:31 -08:00
|
|
|
Custom_ops_val(result) = ops;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_adjust_gc_speed(mem, max);
|
|
|
|
result = caml_check_urgent_gc(result);
|
2000-02-11 04:03:31 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
struct custom_operations_list {
|
|
|
|
struct custom_operations * ops;
|
|
|
|
struct custom_operations_list * next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct custom_operations_list * custom_ops_table = NULL;
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLextern void caml_register_custom_operations(struct custom_operations * ops)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
struct custom_operations_list * l =
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_alloc(sizeof(struct custom_operations_list));
|
2000-02-10 06:04:59 -08:00
|
|
|
Assert(ops->identifier != NULL);
|
|
|
|
Assert(ops->deserialize != NULL);
|
|
|
|
l->ops = ops;
|
|
|
|
l->next = custom_ops_table;
|
|
|
|
custom_ops_table = l;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
struct custom_operations * caml_find_custom_operations(char * ident)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
struct custom_operations_list * l;
|
|
|
|
for (l = custom_ops_table; l != NULL; l = l->next)
|
|
|
|
if (strcmp(l->ops->identifier, ident) == 0) return l->ops;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct custom_operations_list * custom_ops_final_table = NULL;
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
struct custom_operations * caml_final_custom_operations(final_fun fn)
|
2000-02-10 06:04:59 -08:00
|
|
|
{
|
|
|
|
struct custom_operations_list * l;
|
|
|
|
struct custom_operations * ops;
|
|
|
|
for (l = custom_ops_final_table; l != NULL; l = l->next)
|
|
|
|
if (l->ops->finalize == fn) return l->ops;
|
2003-12-31 06:20:40 -08:00
|
|
|
ops = caml_stat_alloc(sizeof(struct custom_operations));
|
2000-02-10 06:04:59 -08:00
|
|
|
ops->identifier = "_final";
|
|
|
|
ops->finalize = fn;
|
|
|
|
ops->compare = custom_compare_default;
|
|
|
|
ops->hash = custom_hash_default;
|
|
|
|
ops->serialize = custom_serialize_default;
|
|
|
|
ops->deserialize = custom_deserialize_default;
|
2003-12-31 06:20:40 -08:00
|
|
|
l = caml_stat_alloc(sizeof(struct custom_operations_list));
|
2000-02-10 06:04:59 -08:00
|
|
|
l->ops = ops;
|
|
|
|
l->next = custom_ops_final_table;
|
2000-07-27 01:37:50 -07:00
|
|
|
custom_ops_final_table = l;
|
2000-02-10 06:04:59 -08:00
|
|
|
return ops;
|
|
|
|
}
|
2000-02-11 07:09:27 -08:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
extern struct custom_operations caml_int32_ops,
|
|
|
|
caml_nativeint_ops,
|
|
|
|
caml_int64_ops;
|
2000-02-11 07:09:27 -08:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_init_custom_operations(void)
|
2000-02-11 07:09:27 -08:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_register_custom_operations(&caml_int32_ops);
|
|
|
|
caml_register_custom_operations(&caml_nativeint_ops);
|
|
|
|
caml_register_custom_operations(&caml_int64_ops);
|
2000-02-11 07:09:27 -08:00
|
|
|
}
|