Sys.Signals module for a Variant type of signals (and a set_signal function that...
[ocaml.git] / byterun / custom.c
blob24281db84bbbabf4fb77778d4b1b4665e1073aeb
1 /***********************************************************************/
2 /* */
3 /* Objective Caml */
4 /* */
5 /* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
6 /* */
7 /* Copyright 2000 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../LICENSE. */
11 /* */
12 /***********************************************************************/
14 /* $Id$ */
16 #include <string.h>
18 #include "alloc.h"
19 #include "custom.h"
20 #include "fail.h"
21 #include "memory.h"
22 #include "mlvalues.h"
24 CAMLexport value caml_alloc_custom(struct custom_operations * ops,
25 uintnat size,
26 mlsize_t mem,
27 mlsize_t max)
29 mlsize_t wosize;
30 value result;
32 wosize = 1 + (size + sizeof(value) - 1) / sizeof(value);
33 if (ops->finalize == NULL && wosize <= Max_young_wosize) {
34 result = caml_alloc_small(wosize, Custom_tag);
35 Custom_ops_val(result) = ops;
36 } else {
37 result = caml_alloc_shr(wosize, Custom_tag);
38 Custom_ops_val(result) = ops;
39 caml_adjust_gc_speed(mem, max);
40 result = caml_check_urgent_gc(result);
42 return result;
45 struct custom_operations_list {
46 struct custom_operations * ops;
47 struct custom_operations_list * next;
50 static struct custom_operations_list * custom_ops_table = NULL;
52 CAMLexport void caml_register_custom_operations(struct custom_operations * ops)
54 struct custom_operations_list * l =
55 caml_stat_alloc(sizeof(struct custom_operations_list));
56 Assert(ops->identifier != NULL);
57 Assert(ops->deserialize != NULL);
58 l->ops = ops;
59 l->next = custom_ops_table;
60 custom_ops_table = l;
63 struct custom_operations * caml_find_custom_operations(char * ident)
65 struct custom_operations_list * l;
66 for (l = custom_ops_table; l != NULL; l = l->next)
67 if (strcmp(l->ops->identifier, ident) == 0) return l->ops;
68 return NULL;
71 static struct custom_operations_list * custom_ops_final_table = NULL;
73 struct custom_operations * caml_final_custom_operations(final_fun fn)
75 struct custom_operations_list * l;
76 struct custom_operations * ops;
77 for (l = custom_ops_final_table; l != NULL; l = l->next)
78 if (l->ops->finalize == fn) return l->ops;
79 ops = caml_stat_alloc(sizeof(struct custom_operations));
80 ops->identifier = "_final";
81 ops->finalize = fn;
82 ops->compare = custom_compare_default;
83 ops->hash = custom_hash_default;
84 ops->serialize = custom_serialize_default;
85 ops->deserialize = custom_deserialize_default;
86 l = caml_stat_alloc(sizeof(struct custom_operations_list));
87 l->ops = ops;
88 l->next = custom_ops_final_table;
89 custom_ops_final_table = l;
90 return ops;
93 extern struct custom_operations caml_int32_ops,
94 caml_nativeint_ops,
95 caml_int64_ops;
97 void caml_init_custom_operations(void)
99 caml_register_custom_operations(&caml_int32_ops);
100 caml_register_custom_operations(&caml_nativeint_ops);
101 caml_register_custom_operations(&caml_int64_ops);