1 /* pvm-val.c - Memory allocator for the PVM.  */
2 
3 /* Copyright (C) 2019, 2020, 2021 Jose E. Marchesi */
4 
5 /* This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <config.h>
20 #include <gc/gc.h>
21 
22 #include "pvm.h"
23 #include "pvm-val.h"
24 
25 void *
pvm_alloc(size_t size)26 pvm_alloc (size_t size)
27 {
28   return GC_MALLOC (size);
29 }
30 
31 void *
pvm_realloc(void * ptr,size_t size)32 pvm_realloc (void *ptr, size_t size)
33 {
34   return GC_REALLOC (ptr, size);
35 }
36 
37 char *
pvm_alloc_strdup(const char * string)38 pvm_alloc_strdup (const char *string)
39 {
40   return GC_strdup (string);
41 }
42 
43 static void
pvm_alloc_finalize_closure(void * object,void * client_data)44 pvm_alloc_finalize_closure (void *object, void *client_data)
45 {
46   /* XXX this causes a crash because of a cycle in the finalizers:
47      routines of recursive PVM programs contain a reference to
48      themselves, be it directly or indirectly.  */
49   /* pvm_cls cls = (pvm_cls) object; */
50   /*  pvm_destroy_program (PVM_VAL_CLS_PROGRAM (cls)); */
51 }
52 
53 void *
pvm_alloc_cls(void)54 pvm_alloc_cls (void)
55 {
56   pvm_cls cls = pvm_alloc (sizeof (struct pvm_cls));
57 
58   GC_register_finalizer_no_order (cls, pvm_alloc_finalize_closure, NULL,
59                                   NULL, NULL);
60   return cls;
61 }
62 
63 void
pvm_alloc_initialize()64 pvm_alloc_initialize ()
65 {
66   /* Initialize the Boehm Garbage Collector.  */
67   GC_INIT ();
68 }
69 
70 void
pvm_alloc_finalize()71 pvm_alloc_finalize ()
72 {
73   GC_gcollect ();
74 }
75 
76 void
pvm_alloc_add_gc_roots(void * pointer,size_t nelems)77 pvm_alloc_add_gc_roots (void *pointer, size_t nelems)
78 {
79   GC_add_roots (pointer,
80                 ((char*) pointer) + sizeof (void*) * nelems);
81 }
82 
83 void
pvm_alloc_remove_gc_roots(void * pointer,size_t nelems)84 pvm_alloc_remove_gc_roots (void *pointer, size_t nelems)
85 {
86   GC_remove_roots (pointer,
87                    ((char*) pointer) + sizeof (void*) * nelems);
88 }
89