Skip to content
Merged
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
44 changes: 13 additions & 31 deletions pljava-so/src/main/c/DualState.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2023 Tada AB and other contributors, as listed below.
* Copyright (c) 2018-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand Down Expand Up @@ -88,20 +88,16 @@ void pljava_DualState_cleanEnqueuedInstances(void)
*/
void pljava_DualState_nativeRelease(void *ro)
{
Ptr2Long p2l;

/*
* This static assertion does not need to be in every file
* that uses Ptr2Long, but it should be somewhere once, so here it is.
* This static assertion does not need to be in every file that uses
* PointerGetJLong, but it should be somewhere once, so here it is.
*/
StaticAssertStmt(sizeof p2l.ptrVal <= sizeof p2l.longVal,
"Pointer will not fit in long on this platform");
StaticAssertStmt(sizeof (uintptr_t) <= sizeof (jlong),
"uintptr_t will not fit in jlong on this platform");

p2l.longVal = 0L;
p2l.ptrVal = ro;
JNI_callStaticVoidMethodLocked(s_DualState_class,
s_DualState_resourceOwnerRelease,
p2l.longVal);
PointerGetJLong(ro));
}

void pljava_DualState_initialize(void)
Expand Down Expand Up @@ -284,9 +280,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SinglePfree__1pfree(
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
pfree(p2l.ptrVal);
pfree(JLongGet(void *, pointer));
END_NATIVE
}

Expand All @@ -302,9 +296,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleMemContextDelete__1memC
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
MemoryContextDelete(p2l.ptrVal);
MemoryContextDelete(JLongGet(MemoryContext, pointer));
END_NATIVE
}

Expand All @@ -320,9 +312,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleFreeTupleDesc__1freeTup
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
FreeTupleDesc(p2l.ptrVal);
FreeTupleDesc(JLongGet(TupleDesc, pointer));
END_NATIVE
}

Expand All @@ -338,9 +328,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleHeapFreeTuple__1heapFre
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
heap_freetuple(p2l.ptrVal);
heap_freetuple(JLongGet(HeapTuple, pointer));
END_NATIVE
}

Expand All @@ -356,9 +344,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleFreeErrorData__1freeErr
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
FreeErrorData(p2l.ptrVal);
FreeErrorData(JLongGet(ErrorData *, pointer));
END_NATIVE
}

Expand All @@ -374,11 +360,9 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIfreeplan__1spiFreePl
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
PG_TRY();
{
SPI_freeplan(p2l.ptrVal);
SPI_freeplan(JLongGet(SPIPlanPtr, pointer));
}
PG_CATCH();
{
Expand All @@ -400,8 +384,6 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIcursorClose__1spiCur
JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
PG_TRY();
{
/*
Expand All @@ -413,7 +395,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIcursorClose__1spiCur
*/
if ( NULL != currentInvocation && ! currentInvocation->errorOccurred
&& ! currentInvocation->inExprContextCB )
SPI_cursor_close(p2l.ptrVal);
SPI_cursor_close(JLongGet(Portal, pointer));
}
PG_CATCH();
{
Expand Down
34 changes: 13 additions & 21 deletions pljava-so/src/main/c/ExecutionPlan.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2023 Tada AB and other contributors, as listed below.
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand Down Expand Up @@ -82,7 +82,8 @@ void pljava_ExecutionPlan_initialize(void)
"Ljava/lang/Object;J)V");
}

static bool coerceObjects(void* ePlan, jobjectArray jvalues, Datum** valuesPtr, char** nullsPtr)
static bool coerceObjects(
SPIPlanPtr ePlan, jobjectArray jvalues, Datum** valuesPtr, char** nullsPtr)
{
char* nulls = 0;
Datum* values = 0;
Expand Down Expand Up @@ -149,11 +150,10 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen(JNIEnv* env, jobj
STACK_BASE_PUSH(env)
PG_TRY();
{
Ptr2Long p2l;
SPIPlanPtr plan = JLongGet(SPIPlanPtr, _this);
Datum* values = 0;
char* nulls = 0;
p2l.longVal = _this;
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
if(coerceObjects(plan, jvalues, &values, &nulls))
{
Portal portal;
char* name = 0;
Expand All @@ -167,7 +167,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen(JNIEnv* env, jobj
else
read_only = (SPI_READONLY_FORCED == readonly_spec);
portal = SPI_cursor_open(
name, p2l.ptrVal, values, nulls, read_only);
name, plan, values, nulls, read_only);
if(name != 0)
pfree(name);
if(values != 0)
Expand Down Expand Up @@ -204,10 +204,8 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1isCursorPlan(JNIEnv* env, jc
BEGIN_NATIVE
PG_TRY();
{
Ptr2Long p2l;
p2l.longVal = _this;
Invocation_assertConnect();
result = (jboolean)SPI_is_cursor_plan(p2l.ptrVal);
result = (jboolean)SPI_is_cursor_plan(JLongGet(SPIPlanPtr, _this));
}
PG_CATCH();
{
Expand Down Expand Up @@ -235,11 +233,10 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1execute(JNIEnv* env, jclass
STACK_BASE_PUSH(env)
PG_TRY();
{
Ptr2Long p2l;
SPIPlanPtr plan = JLongGet(SPIPlanPtr, _this);
Datum* values = 0;
char* nulls = 0;
p2l.longVal = _this;
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
if(coerceObjects(plan, jvalues, &values, &nulls))
{
bool read_only;
Invocation_assertConnect();
Expand All @@ -248,7 +245,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1execute(JNIEnv* env, jclass
else
read_only = (SPI_READONLY_FORCED == readonly_spec);
result = (jint)SPI_execute_plan(
p2l.ptrVal, values, nulls, read_only, (int)count);
plan, values, nulls, read_only, (int)count);
if(result < 0)
Exception_throwSPI("execute_plan", result);

Expand Down Expand Up @@ -285,7 +282,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare(JNIEnv* env, jclass
PG_TRY();
{
char* cmd;
void* ePlan;
SPIPlanPtr ePlan;
int paramCount = 0;
Oid* paramOids = 0;

Expand Down Expand Up @@ -314,21 +311,16 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare(JNIEnv* env, jclass
Exception_throwSPI("prepare", SPI_result);
else
{
Ptr2Long p2l;

/* Make the plan durable
*/
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
spi_ret = SPI_keepplan(ePlan);
if ( 0 == spi_ret )
p2l.ptrVal = ePlan;
else
if ( 0 != spi_ret )
Exception_throwSPI("keepplan", spi_ret);

result = JNI_newObjectLocked(
s_ExecutionPlan_class, s_ExecutionPlan_init,
/* (jlong)0 as resource owner: the saved plan isn't transient */
pljava_DualState_key(), (jlong)0, key, p2l.longVal);
pljava_DualState_key(), (jlong)0, key, PointerGetJLong(ePlan));
}
}
PG_CATCH();
Expand Down
19 changes: 5 additions & 14 deletions pljava-so/src/main/c/Function.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2023 Tada AB and other contributors, as listed below.
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand Down Expand Up @@ -708,7 +708,6 @@ static Function Function_create(
jstring lname = String_createJavaStringFromNTS(NameStr(lngStruct->lanname));
bool ltrust = lngStruct->lanpltrusted;
jstring schemaName;
Ptr2Long p2l;
Datum d;
jobject invocable;

Expand All @@ -724,14 +723,12 @@ static Function Function_create(

self = /* will rely on the fact that allocInstance zeroes memory */
(Function)PgObjectClass_allocInstance(s_FunctionClass,TopMemoryContext);
p2l.longVal = 0;
p2l.ptrVal = (void *)self;

PG_TRY();
{
invocable =
JNI_callStaticObjectMethod(s_Function_class, s_Function_create,
p2l.longVal, Type_coerceDatum(s_pgproc_Type, d), lname,
PointerGetJLong(self), Type_coerceDatum(s_pgproc_Type, d), lname,
schemaName,
trusted ? JNI_TRUE : JNI_FALSE,
forTrigger ? JNI_TRUE : JNI_FALSE,
Expand Down Expand Up @@ -1149,7 +1146,6 @@ JNIEXPORT jboolean JNICALL
jint numParams, jint returnType, jstring returnJType,
jintArray paramTypes, jobjectArray paramJTypes, jobjectArray outJTypes)
{
Ptr2Long p2l;
Function self;
MemoryContext ctx;
jstring jtn;
Expand All @@ -1158,8 +1154,7 @@ JNIEXPORT jboolean JNICALL
uint16 primParams = 0;
bool returnTypeIsOutParameter = false;

p2l.longVal = wrappedPtr;
self = (Function)p2l.ptrVal;
self = JLongGet(Function, wrappedPtr);
ctx = GetMemoryChunkContext(self);

BEGIN_NATIVE_NO_ERRCHECK
Expand Down Expand Up @@ -1258,13 +1253,11 @@ JNIEXPORT void JNICALL
JNIEnv *env, jclass jFunctionClass, jlong wrappedPtr, jobject schemaLoader,
jclass clazz, jboolean readOnly, jint funcInitial, jint udtId)
{
Ptr2Long p2l;
Function self;
HeapTuple typeTup;
Form_pg_type pgType;

p2l.longVal = wrappedPtr;
self = (Function)p2l.ptrVal;
self = JLongGet(Function, wrappedPtr);

BEGIN_NATIVE_NO_ERRCHECK
PG_TRY();
Expand Down Expand Up @@ -1329,7 +1322,6 @@ JNIEXPORT void JNICALL
JNIEnv *env, jclass jFunctionClass, jlong wrappedPtr,
jobjectArray resolvedTypes, jobjectArray explicitTypes, jint index)
{
Ptr2Long p2l;
Function self;
Type origType;
Type replType;
Expand All @@ -1353,8 +1345,7 @@ JNIEXPORT void JNICALL
bool actOnReturnType = ( -1 == index || -2 == index );
bool coerceOutAndSingleton = ( -2 == index );

p2l.longVal = wrappedPtr;
self = (Function)p2l.ptrVal;
self = JLongGet(Function, wrappedPtr);

BEGIN_NATIVE_NO_ERRCHECK
PG_TRY();
Expand Down
8 changes: 3 additions & 5 deletions pljava-so/src/main/c/HashMap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2020 Tada AB and other contributors, as listed below.
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand Down Expand Up @@ -134,10 +134,8 @@ static void StringKey_init(StringKey self, const char* keyVal)
*/
static uint32 _OpaqueKey_hashCode(HashKey self)
{
Ptr2Long p2l;
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
p2l.ptrVal = ((OpaqueKey)self)->key;
return (uint32)(p2l.longVal >> 3);
uintptr_t p = (uintptr_t) ((OpaqueKey)self)->key;
return (uint32)(p >> 3);
}

/*
Expand Down
14 changes: 4 additions & 10 deletions pljava-so/src/main/c/SQLInputFromTuple.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2019 Tada AB and other contributors, as listed below.
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand All @@ -24,20 +24,14 @@ static jmethodID s_SQLInputFromTuple_init;

jobject pljava_SQLInputFromTuple_create(HeapTupleHeader hth)
{
Ptr2Long p2lht;
Ptr2Long p2lro;
jlong heapTup = PointerGetJLong(hth);
jlong lifespan = PointerGetJLong(currentInvocation);
jobject result;
jobject jtd = pljava_SingleRowReader_getTupleDesc(hth);

p2lht.longVal = 0L;
p2lro.longVal = 0L;

p2lht.ptrVal = hth;
p2lro.ptrVal = currentInvocation;

result =
JNI_newObjectLocked(s_SQLInputFromTuple_class, s_SQLInputFromTuple_init,
pljava_DualState_key(), p2lro.longVal, p2lht.longVal, jtd);
pljava_DualState_key(), lifespan, heapTup, jtd);

JNI_deleteLocalRef(jtd);
return result;
Expand Down
Loading
Loading