Skip to content

Commit e68610f

Browse files
committed
Merge trackC/REL1_6_STABLE/PointerGetJLong into REL1_6_STABLE
Merges PR #533.
2 parents d7f4cae + 1d84996 commit e68610f

File tree

17 files changed

+180
-404
lines changed

17 files changed

+180
-404
lines changed

pljava-so/src/main/c/DualState.c

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2023 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2018-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -88,20 +88,16 @@ void pljava_DualState_cleanEnqueuedInstances(void)
8888
*/
8989
void pljava_DualState_nativeRelease(void *ro)
9090
{
91-
Ptr2Long p2l;
92-
9391
/*
94-
* This static assertion does not need to be in every file
95-
* that uses Ptr2Long, but it should be somewhere once, so here it is.
92+
* This static assertion does not need to be in every file that uses
93+
* PointerGetJLong, but it should be somewhere once, so here it is.
9694
*/
97-
StaticAssertStmt(sizeof p2l.ptrVal <= sizeof p2l.longVal,
98-
"Pointer will not fit in long on this platform");
95+
StaticAssertStmt(sizeof (uintptr_t) <= sizeof (jlong),
96+
"uintptr_t will not fit in jlong on this platform");
9997

100-
p2l.longVal = 0L;
101-
p2l.ptrVal = ro;
10298
JNI_callStaticVoidMethodLocked(s_DualState_class,
10399
s_DualState_resourceOwnerRelease,
104-
p2l.longVal);
100+
PointerGetJLong(ro));
105101
}
106102

107103
void pljava_DualState_initialize(void)
@@ -284,9 +280,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SinglePfree__1pfree(
284280
JNIEnv* env, jobject _this, jlong pointer)
285281
{
286282
BEGIN_NATIVE_NO_ERRCHECK
287-
Ptr2Long p2l;
288-
p2l.longVal = pointer;
289-
pfree(p2l.ptrVal);
283+
pfree(JLongGet(void *, pointer));
290284
END_NATIVE
291285
}
292286

@@ -302,9 +296,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleMemContextDelete__1memC
302296
JNIEnv* env, jobject _this, jlong pointer)
303297
{
304298
BEGIN_NATIVE_NO_ERRCHECK
305-
Ptr2Long p2l;
306-
p2l.longVal = pointer;
307-
MemoryContextDelete(p2l.ptrVal);
299+
MemoryContextDelete(JLongGet(MemoryContext, pointer));
308300
END_NATIVE
309301
}
310302

@@ -320,9 +312,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleFreeTupleDesc__1freeTup
320312
JNIEnv* env, jobject _this, jlong pointer)
321313
{
322314
BEGIN_NATIVE_NO_ERRCHECK
323-
Ptr2Long p2l;
324-
p2l.longVal = pointer;
325-
FreeTupleDesc(p2l.ptrVal);
315+
FreeTupleDesc(JLongGet(TupleDesc, pointer));
326316
END_NATIVE
327317
}
328318

@@ -338,9 +328,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleHeapFreeTuple__1heapFre
338328
JNIEnv* env, jobject _this, jlong pointer)
339329
{
340330
BEGIN_NATIVE_NO_ERRCHECK
341-
Ptr2Long p2l;
342-
p2l.longVal = pointer;
343-
heap_freetuple(p2l.ptrVal);
331+
heap_freetuple(JLongGet(HeapTuple, pointer));
344332
END_NATIVE
345333
}
346334

@@ -356,9 +344,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleFreeErrorData__1freeErr
356344
JNIEnv* env, jobject _this, jlong pointer)
357345
{
358346
BEGIN_NATIVE_NO_ERRCHECK
359-
Ptr2Long p2l;
360-
p2l.longVal = pointer;
361-
FreeErrorData(p2l.ptrVal);
347+
FreeErrorData(JLongGet(ErrorData *, pointer));
362348
END_NATIVE
363349
}
364350

@@ -374,11 +360,9 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIfreeplan__1spiFreePl
374360
JNIEnv* env, jobject _this, jlong pointer)
375361
{
376362
BEGIN_NATIVE_NO_ERRCHECK
377-
Ptr2Long p2l;
378-
p2l.longVal = pointer;
379363
PG_TRY();
380364
{
381-
SPI_freeplan(p2l.ptrVal);
365+
SPI_freeplan(JLongGet(SPIPlanPtr, pointer));
382366
}
383367
PG_CATCH();
384368
{
@@ -400,8 +384,6 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIcursorClose__1spiCur
400384
JNIEnv* env, jobject _this, jlong pointer)
401385
{
402386
BEGIN_NATIVE_NO_ERRCHECK
403-
Ptr2Long p2l;
404-
p2l.longVal = pointer;
405387
PG_TRY();
406388
{
407389
/*
@@ -413,7 +395,7 @@ Java_org_postgresql_pljava_internal_DualState_00024SingleSPIcursorClose__1spiCur
413395
*/
414396
if ( NULL != currentInvocation && ! currentInvocation->errorOccurred
415397
&& ! currentInvocation->inExprContextCB )
416-
SPI_cursor_close(p2l.ptrVal);
398+
SPI_cursor_close(JLongGet(Portal, pointer));
417399
}
418400
PG_CATCH();
419401
{

pljava-so/src/main/c/ExecutionPlan.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2023 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -82,7 +82,8 @@ void pljava_ExecutionPlan_initialize(void)
8282
"Ljava/lang/Object;J)V");
8383
}
8484

85-
static bool coerceObjects(void* ePlan, jobjectArray jvalues, Datum** valuesPtr, char** nullsPtr)
85+
static bool coerceObjects(
86+
SPIPlanPtr ePlan, jobjectArray jvalues, Datum** valuesPtr, char** nullsPtr)
8687
{
8788
char* nulls = 0;
8889
Datum* values = 0;
@@ -149,11 +150,10 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen(JNIEnv* env, jobj
149150
STACK_BASE_PUSH(env)
150151
PG_TRY();
151152
{
152-
Ptr2Long p2l;
153+
SPIPlanPtr plan = JLongGet(SPIPlanPtr, _this);
153154
Datum* values = 0;
154155
char* nulls = 0;
155-
p2l.longVal = _this;
156-
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
156+
if(coerceObjects(plan, jvalues, &values, &nulls))
157157
{
158158
Portal portal;
159159
char* name = 0;
@@ -167,7 +167,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen(JNIEnv* env, jobj
167167
else
168168
read_only = (SPI_READONLY_FORCED == readonly_spec);
169169
portal = SPI_cursor_open(
170-
name, p2l.ptrVal, values, nulls, read_only);
170+
name, plan, values, nulls, read_only);
171171
if(name != 0)
172172
pfree(name);
173173
if(values != 0)
@@ -204,10 +204,8 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1isCursorPlan(JNIEnv* env, jc
204204
BEGIN_NATIVE
205205
PG_TRY();
206206
{
207-
Ptr2Long p2l;
208-
p2l.longVal = _this;
209207
Invocation_assertConnect();
210-
result = (jboolean)SPI_is_cursor_plan(p2l.ptrVal);
208+
result = (jboolean)SPI_is_cursor_plan(JLongGet(SPIPlanPtr, _this));
211209
}
212210
PG_CATCH();
213211
{
@@ -235,11 +233,10 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1execute(JNIEnv* env, jclass
235233
STACK_BASE_PUSH(env)
236234
PG_TRY();
237235
{
238-
Ptr2Long p2l;
236+
SPIPlanPtr plan = JLongGet(SPIPlanPtr, _this);
239237
Datum* values = 0;
240238
char* nulls = 0;
241-
p2l.longVal = _this;
242-
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
239+
if(coerceObjects(plan, jvalues, &values, &nulls))
243240
{
244241
bool read_only;
245242
Invocation_assertConnect();
@@ -248,7 +245,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1execute(JNIEnv* env, jclass
248245
else
249246
read_only = (SPI_READONLY_FORCED == readonly_spec);
250247
result = (jint)SPI_execute_plan(
251-
p2l.ptrVal, values, nulls, read_only, (int)count);
248+
plan, values, nulls, read_only, (int)count);
252249
if(result < 0)
253250
Exception_throwSPI("execute_plan", result);
254251

@@ -285,7 +282,7 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare(JNIEnv* env, jclass
285282
PG_TRY();
286283
{
287284
char* cmd;
288-
void* ePlan;
285+
SPIPlanPtr ePlan;
289286
int paramCount = 0;
290287
Oid* paramOids = 0;
291288

@@ -314,21 +311,16 @@ Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare(JNIEnv* env, jclass
314311
Exception_throwSPI("prepare", SPI_result);
315312
else
316313
{
317-
Ptr2Long p2l;
318-
319314
/* Make the plan durable
320315
*/
321-
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
322316
spi_ret = SPI_keepplan(ePlan);
323-
if ( 0 == spi_ret )
324-
p2l.ptrVal = ePlan;
325-
else
317+
if ( 0 != spi_ret )
326318
Exception_throwSPI("keepplan", spi_ret);
327319

328320
result = JNI_newObjectLocked(
329321
s_ExecutionPlan_class, s_ExecutionPlan_init,
330322
/* (jlong)0 as resource owner: the saved plan isn't transient */
331-
pljava_DualState_key(), (jlong)0, key, p2l.longVal);
323+
pljava_DualState_key(), (jlong)0, key, PointerGetJLong(ePlan));
332324
}
333325
}
334326
PG_CATCH();

pljava-so/src/main/c/Function.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2023 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -709,7 +709,6 @@ static Function Function_create(
709709
jstring lname = String_createJavaStringFromNTS(NameStr(lngStruct->lanname));
710710
bool ltrust = lngStruct->lanpltrusted;
711711
jstring schemaName;
712-
Ptr2Long p2l;
713712
Datum d;
714713
jobject invocable;
715714

@@ -725,14 +724,12 @@ static Function Function_create(
725724

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

731728
PG_TRY();
732729
{
733730
invocable =
734731
JNI_callStaticObjectMethod(s_Function_class, s_Function_create,
735-
p2l.longVal, Type_coerceDatum(s_pgproc_Type, d), lname,
732+
PointerGetJLong(self), Type_coerceDatum(s_pgproc_Type, d), lname,
736733
schemaName,
737734
trusted ? JNI_TRUE : JNI_FALSE,
738735
forTrigger ? JNI_TRUE : JNI_FALSE,
@@ -1150,7 +1147,6 @@ JNIEXPORT jboolean JNICALL
11501147
jint numParams, jint returnType, jstring returnJType,
11511148
jintArray paramTypes, jobjectArray paramJTypes, jobjectArray outJTypes)
11521149
{
1153-
Ptr2Long p2l;
11541150
Function self;
11551151
MemoryContext ctx;
11561152
jstring jtn;
@@ -1159,8 +1155,7 @@ JNIEXPORT jboolean JNICALL
11591155
uint16 primParams = 0;
11601156
bool returnTypeIsOutParameter = false;
11611157

1162-
p2l.longVal = wrappedPtr;
1163-
self = (Function)p2l.ptrVal;
1158+
self = JLongGet(Function, wrappedPtr);
11641159
ctx = GetMemoryChunkContext(self);
11651160

11661161
BEGIN_NATIVE_NO_ERRCHECK
@@ -1259,13 +1254,11 @@ JNIEXPORT void JNICALL
12591254
JNIEnv *env, jclass jFunctionClass, jlong wrappedPtr, jobject schemaLoader,
12601255
jclass clazz, jboolean readOnly, jint funcInitial, jint udtId)
12611256
{
1262-
Ptr2Long p2l;
12631257
Function self;
12641258
HeapTuple typeTup;
12651259
Form_pg_type pgType;
12661260

1267-
p2l.longVal = wrappedPtr;
1268-
self = (Function)p2l.ptrVal;
1261+
self = JLongGet(Function, wrappedPtr);
12691262

12701263
BEGIN_NATIVE_NO_ERRCHECK
12711264
PG_TRY();
@@ -1330,7 +1323,6 @@ JNIEXPORT void JNICALL
13301323
JNIEnv *env, jclass jFunctionClass, jlong wrappedPtr,
13311324
jobjectArray resolvedTypes, jobjectArray explicitTypes, jint index)
13321325
{
1333-
Ptr2Long p2l;
13341326
Function self;
13351327
Type origType;
13361328
Type replType;
@@ -1354,8 +1346,7 @@ JNIEXPORT void JNICALL
13541346
bool actOnReturnType = ( -1 == index || -2 == index );
13551347
bool coerceOutAndSingleton = ( -2 == index );
13561348

1357-
p2l.longVal = wrappedPtr;
1358-
self = (Function)p2l.ptrVal;
1349+
self = JLongGet(Function, wrappedPtr);
13591350

13601351
BEGIN_NATIVE_NO_ERRCHECK
13611352
PG_TRY();

pljava-so/src/main/c/HashMap.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2020 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -134,10 +134,8 @@ static void StringKey_init(StringKey self, const char* keyVal)
134134
*/
135135
static uint32 _OpaqueKey_hashCode(HashKey self)
136136
{
137-
Ptr2Long p2l;
138-
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
139-
p2l.ptrVal = ((OpaqueKey)self)->key;
140-
return (uint32)(p2l.longVal >> 3);
137+
uintptr_t p = (uintptr_t) ((OpaqueKey)self)->key;
138+
return (uint32)(p >> 3);
141139
}
142140

143141
/*

pljava-so/src/main/c/SQLInputFromTuple.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2019 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -24,20 +24,14 @@ static jmethodID s_SQLInputFromTuple_init;
2424

2525
jobject pljava_SQLInputFromTuple_create(HeapTupleHeader hth)
2626
{
27-
Ptr2Long p2lht;
28-
Ptr2Long p2lro;
27+
jlong heapTup = PointerGetJLong(hth);
28+
jlong lifespan = PointerGetJLong(currentInvocation);
2929
jobject result;
3030
jobject jtd = pljava_SingleRowReader_getTupleDesc(hth);
3131

32-
p2lht.longVal = 0L;
33-
p2lro.longVal = 0L;
34-
35-
p2lht.ptrVal = hth;
36-
p2lro.ptrVal = currentInvocation;
37-
3832
result =
3933
JNI_newObjectLocked(s_SQLInputFromTuple_class, s_SQLInputFromTuple_init,
40-
pljava_DualState_key(), p2lro.longVal, p2lht.longVal, jtd);
34+
pljava_DualState_key(), lifespan, heapTup, jtd);
4135

4236
JNI_deleteLocalRef(jtd);
4337
return result;

0 commit comments

Comments
 (0)