Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a64b07c8884314f7b5abe955c241a345da99bb6f
[simgrid.git] / src / mc / mc_address_space.h
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef MC_ADDRESS_SPACE_H
8 #define MC_ADDRESS_SPACE_H
9
10 #include <xbt/misc.h>
11
12 #include <stdint.h>
13
14 #include "mc_forward.h"
15
16 SG_BEGIN_DECL()
17
18 // ***** Data types
19
20 /** Options for the read() operation
21  *
22  *  - MC_ADDRESS_SPACE_READ_FLAGS_LAZY, avoid a copy when the data is
23  *    available in the current process. In this case, the return value
24  *    of MC_address_space_read might be different from the provided one.
25  */
26 typedef int adress_space_read_flags_t;
27 #define MC_ADDRESS_SPACE_READ_FLAGS_NONE 0
28 #define MC_ADDRESS_SPACE_READ_FLAGS_LAZY 1
29
30 /** Process index used when no process is available
31  *
32  *  The expected behaviour is that if a process index is needed it will fail.
33  * */
34 #define MC_PROCESS_INDEX_MISSING -1
35
36 #define MC_PROCESS_INDEX_DISABLED -2
37
38 /** Process index when any process is suitable
39  *
40  * We could use a special negative value in the future.
41  */
42 #define MC_PROCESS_INDEX_ANY 0
43
44 // ***** Class definition
45
46 typedef struct s_mc_address_space s_mc_address_space_t, *mc_address_space_t;
47 typedef struct s_mc_address_space_class s_mc_address_space_class_t, *mc_address_space_class_t;
48
49 /** Abstract base class for an address space
50  *
51  *  This is the base class for all virtual address spaces (process, snapshot).
52  *  It uses dynamic dispatch based on a vtable (`address_space_class`).
53  */
54 struct s_mc_address_space {
55   const s_mc_address_space_class_t* address_space_class;
56 };
57
58 /** Class object (vtable) for the virtual address spaces
59  */
60 struct s_mc_address_space_class {
61   const void* (*read)(
62     mc_address_space_t address_space, adress_space_read_flags_t flags,
63     void* target, const void* addr, size_t size,
64     int process_index);
65   mc_process_t (*get_process)(mc_address_space_t address_space);
66 };
67
68 typedef const void* (*mc_address_space_class_read_callback_t)(
69   mc_address_space_t address_space, adress_space_read_flags_t flags,
70   void* target, const void* addr, size_t size,
71   int process_index);
72 typedef mc_process_t (*mc_address_space_class_get_process_callback_t)(mc_address_space_t address_space);
73
74 // ***** Virtual/non-final methods
75
76 /** Read data from the given address space
77  *
78  *  Dysnamic dispatch.
79  */
80 static inline __attribute__((always_inline))
81 const void* MC_address_space_read(
82   mc_address_space_t address_space, adress_space_read_flags_t flags,
83   void* target, const void* addr, size_t size,
84   int process_index)
85 {
86   return address_space->address_space_class->read(
87     address_space, flags, target, addr, size,
88     process_index);
89 }
90
91 static inline __attribute__((always_inline))
92 const void* MC_address_space_get_process(mc_address_space_t address_space)
93 {
94   if (address_space->address_space_class->get_process)
95     return address_space->address_space_class->get_process(address_space);
96   else
97     return NULL;
98 }
99
100 SG_END_DECL()
101
102 #endif