MySQL 9.4.0
Source Code Documentation
polyglot_common_context.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_COMMON_CONTEXT_H_
27#define MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_COMMON_CONTEXT_H_
28
30
31#include <memory>
32#include <optional>
33
38
39namespace shcore {
40namespace polyglot {
41
43 public:
44 std::optional<uint64_t> min_heap_size;
45 std::optional<uint64_t> max_heap_size;
46 std::optional<uint64_t> max_new_size;
47};
48
49/**
50 * Common context for GraalVM Languages
51 *
52 * Encloses the following global routines/components:
53 *
54 * - Initialization/TearDown of the GraalVM Isolate.
55 * - Registration of global logging callbacks.
56 * - Setup of the global engine.
57 * - Provides access to the garbage collector for event registration.
58 *
59 * This class can be specialized to:
60 *
61 * - Provide custom callbacks for the global logging functions.
62 * - Create a custom Engine to be used in the contexts created from this class.
63 * - Provide custom configuration for the garbage collector.
64 */
66 public:
68 virtual ~Polyglot_common_context() = default;
69
70 virtual void initialize(const IsolateArgs &isolate_args);
71 virtual void finalize();
72
73 poly_reference engine() const { return m_engine.get(); }
74 poly_isolate isolate() const { return m_isolate; }
75 poly_thread thread() const { return m_thread; }
76
77 void clean_collectables();
78
80
82 std::string get_gc_status();
83
84 protected:
85 poly_isolate m_isolate = nullptr;
86 poly_thread m_thread = nullptr;
87
88 private:
89 void init_engine();
90 static void fatal_error_callback(void *data);
91 static void flush_callback(void *data);
92 static void log_callback(const char *bytes, size_t length, void *data);
93 virtual void fatal_error() = 0;
94 virtual void flush() = 0;
95 virtual void log(const char *bytes, size_t length) = 0;
96 virtual poly_engine create_engine() { return nullptr; }
97 virtual Garbage_collector::Config gc_config() { return {}; }
98
100 std::unique_ptr<Polyglot_scope> m_scope;
102 int64_t m_max_heap_size = 0;
103 int64_t *m_heap_status = nullptr;
104
105 // Holds the used capacity for each generation
106 std::vector<int64_t *> m_generation_used;
107};
108
109} // namespace polyglot
110} // namespace shcore
111
112#endif // MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_COMMON_CONTEXT_H_
When a Polyglot wrapper for a C++ object is created, a collectable instance is created to be passed t...
Definition: polyglot_collectable.h:113
Common context for GraalVM Languages.
Definition: polyglot_common_context.h:65
poly_thread thread() const
Definition: polyglot_common_context.h:75
static void fatal_error_callback(void *data)
Definition: polyglot_common_context.cc:204
std::vector< int64_t * > m_generation_used
Definition: polyglot_common_context.h:106
int64_t * m_heap_status
Definition: polyglot_common_context.h:103
Collectable_registry m_registry
Definition: polyglot_common_context.h:101
std::string get_gc_status()
Definition: polyglot_common_context.cc:177
virtual void finalize()
Definition: polyglot_common_context.cc:188
poly_isolate m_isolate
Definition: polyglot_common_context.h:85
void init_engine()
Definition: polyglot_common_context.cc:220
virtual Garbage_collector::Config gc_config()
Definition: polyglot_common_context.h:97
std::unique_ptr< Polyglot_scope > m_scope
Definition: polyglot_common_context.h:100
virtual poly_engine create_engine()
Definition: polyglot_common_context.h:96
poly_reference engine() const
Definition: polyglot_common_context.h:73
Store m_engine
Definition: polyglot_common_context.h:99
int64_t m_max_heap_size
Definition: polyglot_common_context.h:102
double get_heap_usage_percent()
Definition: polyglot_common_context.cc:163
virtual void log(const char *bytes, size_t length)=0
poly_isolate isolate() const
Definition: polyglot_common_context.h:74
static void log_callback(const char *bytes, size_t length, void *data)
Definition: polyglot_common_context.cc:214
Collectable_registry * collectable_registry()
Definition: polyglot_common_context.h:79
poly_thread m_thread
Definition: polyglot_common_context.h:86
virtual void initialize(const IsolateArgs &isolate_args)
Definition: polyglot_common_context.cc:63
void clean_collectables()
Definition: polyglot_common_context.cc:227
static void flush_callback(void *data)
Definition: polyglot_common_context.cc:209
Helper class to handle polyglot values made permanent.
Definition: polyglot_store.h:47
poly_reference get() const noexcept
Definition: polyglot_store.h:70
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: file_system_exceptions.h:34
Definition: polyglot_garbage_collector.h:81
Definition: polyglot_common_context.h:42
std::optional< uint64_t > min_heap_size
Definition: polyglot_common_context.h:44
std::optional< uint64_t > max_new_size
Definition: polyglot_common_context.h:46
std::optional< uint64_t > max_heap_size
Definition: polyglot_common_context.h:45