Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.graal.python.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks a Java primitive constant as the source of truth for a C {@code #define} generated into
* {@code capi.gen.h}. This is the opposite direction of {@link CApiConstants}, which marks C
* constants that should be mirrored to Java.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface CApiConstant {
}
16 changes: 8 additions & 8 deletions graalpython/com.oracle.graal.python.cext/src/capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ uint32_t Py_Truffle_Options;
#undef bool
static void initialize_builtin_types_and_structs() {
clock_t t = clock();
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE, "initialize_builtin_types_and_structs...");
GraalPyPrivate_Log(GRAALPY_LOG_FINE, "initialize_builtin_types_and_structs...");
static int64_t builtin_types[] = {
#define PY_TRUFFLE_TYPE_GENERIC(GLOBAL_NAME, __TYPE_NAME__, a, b, c, d, e, f, g) &GLOBAL_NAME, __TYPE_NAME__,
#define PY_TRUFFLE_TYPE_EXTERN(GLOBAL_NAME, __TYPE_NAME__) &GLOBAL_NAME, __TYPE_NAME__,
Expand All @@ -271,7 +271,7 @@ static void initialize_builtin_types_and_structs() {
// fix up for circular dependency:
PyType_Type.tp_base = &PyBaseObject_Type;

GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE, "initialize_builtin_types_and_structs: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
GraalPyPrivate_Log(GRAALPY_LOG_FINE, "initialize_builtin_types_and_structs: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
}

int mmap_getbuffer(PyObject *self, Py_buffer *view, int flags) {
Expand All @@ -284,7 +284,7 @@ int mmap_getbuffer(PyObject *self, Py_buffer *view, int flags) {
}

PyAPI_FUNC(void) GraalPyPrivate_MMap_InitBufferProtocol(PyObject* mmap_type) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE, "GraalPyPrivate_MMap_InitBufferProtocol");
GraalPyPrivate_Log(GRAALPY_LOG_FINE, "GraalPyPrivate_MMap_InitBufferProtocol");
assert(PyType_Check(mmap_type));

static PyBufferProcs mmap_as_buffer = {
Expand Down Expand Up @@ -433,7 +433,7 @@ GraalPyPrivate_SUBREF(intptr_t ptr, Py_ssize_t value)

Py_ssize_t new_value = ((obj->ob_refcnt) -= value);
if (new_value == 0) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINER, "%s: _Py_Dealloc(0x%zx)",
GraalPyPrivate_Log(GRAALPY_LOG_FINER, "%s: _Py_Dealloc(0x%zx)",
__func__, obj);
_Py_Dealloc(obj);
}
Expand All @@ -451,7 +451,7 @@ GraalPyPrivate_BulkDealloc(intptr_t ptrArray[], int64_t len)
{
for (int i = 0; i < len; i++) {
PyObject *obj = (PyObject *)ptrArray[i];
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINER,
GraalPyPrivate_Log(GRAALPY_LOG_FINER,
"%s: _Py_Dealloc(a %s at 0x%zx)",
__func__, Py_TYPE(obj)->tp_name, obj);
_Py_Dealloc(obj);
Expand All @@ -476,7 +476,7 @@ GraalPyPrivate_BulkDeallocOnShutdown(intptr_t ptrArray[], int64_t len)
/* we don't need to care about objects with default deallocation
process */
obj->ob_refcnt = 0;
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINER, "%s: _Py_Dealloc(0x%zx)",
GraalPyPrivate_Log(GRAALPY_LOG_FINER, "%s: _Py_Dealloc(0x%zx)",
__func__, obj);
_Py_Dealloc(obj);
}
Expand Down Expand Up @@ -623,7 +623,7 @@ PyAPI_FUNC(PyThreadState **) initialize_graal_capi(void **builtin_closures, GCSt
* context exits and its table is the "latest", we delay freeing it.
*/
initialize_builtins(builtin_closures);
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE, "initialize_builtins: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
GraalPyPrivate_Log(GRAALPY_LOG_FINE, "initialize_builtins: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
Py_Truffle_Options = GraalPyPrivate_Native_Options();

initialize_builtin_types_and_structs();
Expand All @@ -638,7 +638,7 @@ PyAPI_FUNC(PyThreadState **) initialize_graal_capi(void **builtin_closures, GCSt
// TODO: initialize during cext initialization doesn't work at the moment
Py_FileSystemDefaultEncoding = "utf-8"; // strdup(PyUnicode_AsUTF8(GraalPyPrivate_FileSystemDefaultEncoding()));

GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE, "initialize_graal_capi: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
GraalPyPrivate_Log(GRAALPY_LOG_FINE, "initialize_graal_capi: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
return &tstate_current;
}

Expand Down
46 changes: 20 additions & 26 deletions graalpython/com.oracle.graal.python.cext/src/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
typedef int (*graalpy_attach_native_thread_func)(void);
typedef void (*graalpy_detach_native_thread_func)(void);

#define GRAALPY_ATTACH_NATIVE_FAILED (-1)
#define GRAALPY_ATTACH_NATIVE_OWNED 1
#define GRAALPY_ATTACH_NATIVE_FOREIGN 2

extern graalpy_attach_native_thread_func graalpy_attach_native_thread;
extern graalpy_detach_native_thread_func graalpy_detach_native_thread;

Expand All @@ -97,18 +93,6 @@ extern graalpy_detach_native_thread_func graalpy_detach_native_thread;

#define SRC_CS "utf-8"

/* Flags definitions representing global (debug) options. */
#define PY_TRUFFLE_TRACE_MEM 0x1
#define PY_TRUFFLE_LOG_INFO 0x2
#define PY_TRUFFLE_LOG_CONFIG 0x4
#define PY_TRUFFLE_LOG_FINE 0x8
#define PY_TRUFFLE_LOG_FINER 0x10
#define PY_TRUFFLE_LOG_FINEST 0x20
#define PY_TRUFFLE_DEBUG_CAPI 0x40
#define PY_TRUFFLE_PYTHON_GC 0x80
#define PY_TRUFFLE_POISON_NATIVE_MEMORY_ON_FREE 0x100
#define PY_TRUFFLE_SAMPLE_NATIVE_MEMORY_ALLOC_SITES 0x200

typedef struct mmap_object mmap_object;
typedef struct _gc_runtime_state GCState; // originally in 'gcmodule.c'

Expand Down Expand Up @@ -157,6 +141,16 @@ typedef struct {
double ob_fval;
} GraalPyFloatObject;

typedef struct {
GraalPyObject ob_base;
Py_ssize_t length;
Py_ssize_t byte_length;
Py_hash_t hash;
/* Bits 0-2: kind; bit 3: is_ascii; bits 4-5: interned state. */
uint64_t state;
void *data;
} GraalPyUnicodeObject;

typedef struct gc_generation GCGeneration;

// {{start CAPI_BUILTINS}}
Expand Down Expand Up @@ -220,35 +214,35 @@ static void print_c_stacktrace() {

/* Flags definitions representing global (debug) options. */
static MUST_INLINE int GraalPyPrivate_Trace_Memory() {
return Py_Truffle_Options & PY_TRUFFLE_TRACE_MEM;
return Py_Truffle_Options & GRAALPY_TRACE_MEM;
}
static MUST_INLINE int GraalPyPrivate_Log_Info() {
return Py_Truffle_Options & PY_TRUFFLE_LOG_INFO;
return Py_Truffle_Options & GRAALPY_LOG_INFO;
}
static MUST_INLINE int GraalPyPrivate_Log_Config() {
return Py_Truffle_Options & PY_TRUFFLE_LOG_CONFIG;
return Py_Truffle_Options & GRAALPY_LOG_CONFIG;
}
static MUST_INLINE int GraalPyPrivate_Log_Fine() {
return Py_Truffle_Options & PY_TRUFFLE_LOG_FINE;
return Py_Truffle_Options & GRAALPY_LOG_FINE;
}
static MUST_INLINE int GraalPyPrivate_Log_Finer() {
return Py_Truffle_Options & PY_TRUFFLE_LOG_FINER;
return Py_Truffle_Options & GRAALPY_LOG_FINER;
}
static MUST_INLINE int GraalPyPrivate_Log_Finest() {
return Py_Truffle_Options & PY_TRUFFLE_LOG_FINEST;
return Py_Truffle_Options & GRAALPY_LOG_FINEST;
}
static MUST_INLINE int GraalPyPrivate_Debug_CAPI() {
return Py_Truffle_Options & PY_TRUFFLE_DEBUG_CAPI;
return Py_Truffle_Options & GRAALPY_DEBUG_CAPI;
}

static MUST_INLINE int GraalPyPrivate_PythonGC() {
return Py_Truffle_Options & PY_TRUFFLE_PYTHON_GC;
return Py_Truffle_Options & GRAALPY_PYTHON_GC;
}
static MUST_INLINE int GraalPyPrivate_PoisonNativeMemoryOnFree() {
return Py_Truffle_Options & PY_TRUFFLE_POISON_NATIVE_MEMORY_ON_FREE;
return Py_Truffle_Options & GRAALPY_POISON_NATIVE_MEMORY_ON_FREE;
}
static MUST_INLINE int GraalPyPrivate_SampleNativeMemoryAllocSites() {
return Py_Truffle_Options & PY_TRUFFLE_SAMPLE_NATIVE_MEMORY_ALLOC_SITES;
return Py_Truffle_Options & GRAALPY_SAMPLE_NATIVE_MEMORY_ALLOC_SITES;
}

static void
Expand Down
6 changes: 3 additions & 3 deletions graalpython/com.oracle.graal.python.cext/src/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ call_traverse(traverseproc traverse, PyObject *op, visitproc visit, void *arg)
return 0;
}
if (!traverse) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE,
GraalPyPrivate_Log(GRAALPY_LOG_FINE,
"type '%.100s' is a GC type but tp_traverse is NULL",
Py_TYPE((op))->tp_name);
return 0;
} else {
if (_PyObject_IsFreed(op)) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE,
GraalPyPrivate_Log(GRAALPY_LOG_FINE,
"we tried to call tp_traverse on a freed object at %p (ctx %p)!",
op, arg);
return 0;
}
if (_PyObject_IsFreed((PyObject *)Py_TYPE(op))) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_FINE,
GraalPyPrivate_Log(GRAALPY_LOG_FINE,
"we tried to call tp_traverse on an object at %p with a freed type at %p (ctx %p)!",
op, Py_TYPE(op), arg);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion graalpython/com.oracle.graal.python.cext/src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2966,7 +2966,7 @@ void
GraalPy_SET_TYPE(PyObject *a, PyTypeObject *b)
{
if (points_to_py_handle_space(a)) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_INFO,
GraalPyPrivate_Log(GRAALPY_LOG_INFO,
"changing the type of an object is not supported\n");
} else {
a->ob_type = b;
Expand Down
14 changes: 7 additions & 7 deletions graalpython/com.oracle.graal.python.cext/src/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ _GraalPyMem_LogRecentSamples(const char *func, void *ptr)
const GraalPyMemSample_t *sample = &_GraalPyMem_Samples[index];
if (sample->ptr == ptr && sample->serial != 0) {
char prefix[128];
GraalPyPrivate_Log(PY_TRUFFLE_LOG_INFO,
GraalPyPrivate_Log(GRAALPY_LOG_INFO,
"%s: recent raw memory sample #%llu op=%c ptr=%p size=%lu depth=%lu\n",
func, sample->serial, sample->operation, sample->ptr, (unsigned long) sample->size, (unsigned long) sample->depth);
snprintf(prefix, sizeof(prefix), "%s: sample #%llu ", func, sample->serial);
GraalPyPrivate_LogCapturedStacktrace(PY_TRUFFLE_LOG_INFO, prefix, sample->stack, sample->depth);
GraalPyPrivate_LogCapturedStacktrace(GRAALPY_LOG_INFO, prefix, sample->stack, sample->depth);
printed++;
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ _GraalPyMem_FatalInvalidHeader(const char *func, void *ptr, const mem_head_t *pt
const char *reason = (ptr_with_head->size == GRAALPY_MEM_HEAD_POISON && ptr_with_head->dummy == GRAALPY_MEM_HEAD_POISON)
? "poisoned raw allocation header"
: "invalid raw allocation header";
GraalPyPrivate_Log(PY_TRUFFLE_LOG_INFO,
GraalPyPrivate_Log(GRAALPY_LOG_INFO,
"%s: %s for ptr=%p head=%p size=%lu dummy=0x%lx\n",
func, reason, ptr, ptr_with_head, (unsigned long) ptr_with_head->size, (unsigned long) ptr_with_head->dummy);
_GraalPyMem_LogRecentSamples(func, ptr);
Expand Down Expand Up @@ -512,7 +512,7 @@ _GraalPyMem_PrepareAlloc(GraalPyMem_t *state, size_t size)
state->native_memory_gc_barrier = GraalPyPrivate_GetInitialNativeMemory();
continue;
}
GraalPyPrivate_Log(PY_TRUFFLE_LOG_CONFIG,
GraalPyPrivate_Log(GRAALPY_LOG_CONFIG,
"%s: exceeding native_memory_gc_barrier (%lu) with allocation of size %lu, current allocated_memory: %lu\n",
__func__, state->native_memory_gc_barrier, size, state->allocated_memory);

Expand All @@ -532,12 +532,12 @@ _GraalPyMem_PrepareAlloc(GraalPyMem_t *state, size_t size)
if (state->native_memory_gc_barrier > state->max_native_memory) {
state->native_memory_gc_barrier = state->max_native_memory;
}
GraalPyPrivate_Log(PY_TRUFFLE_LOG_CONFIG,
GraalPyPrivate_Log(GRAALPY_LOG_CONFIG,
"%s: enlarging native_memory_gc_barrier to %lu\n",
__func__, state->native_memory_gc_barrier);
}
else {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_INFO,
GraalPyPrivate_Log(GRAALPY_LOG_INFO,
"%s: native memory exhausted while allocating %lu bytes\n",
__func__, size);
return 1;
Expand Down Expand Up @@ -658,7 +658,7 @@ _GraalPyMem_RawFree(void *ctx, void *ptr)
mem_head_t *ptr_with_head = _GraalPyMem_GetValidatedHead(__func__, ptr);
const size_t size = ptr_with_head->size;
if (state->allocated_memory < size) {
GraalPyPrivate_Log(PY_TRUFFLE_LOG_INFO,
GraalPyPrivate_Log(GRAALPY_LOG_INFO,
"%s: freed memory size (%lu) is larger than allocated memory size (%lu)\n",
__func__, size, state->allocated_memory);
state->allocated_memory = size;
Expand Down
Loading
Loading