Shaping C-like structs into memory segments – Foreign (Function) Memory API

146. Shaping C-like structs into memory segments Let’s consider the C-like struct from the following figure: Figure 7.12 – A C-like structure So, in Figure 7.12, we have a C-like ... Read MoreRead More

0 Comments

Introducing sequence layout – Foreign (Function) Memory API

145. Introducing sequence layout In Problem X, we already covered the ValueLayout for basic data types. Next, let’s talk about the sequence layout (java.lang.foreign.SequenceLayout).But, before introducing the sequence layout, let’s ... Read MoreRead More

0 Comments

Introducing slice handle – Foreign (Function) Memory API

151. Introducing slice handle Let’s suppose that we have the following nested model (10 sequences of 5 double values each): SequenceLayout innerSeq  = MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_DOUBLE);SequenceLayout outerSeq  = MemoryLayout.sequenceLayout(10, innerSeq); Next, ... Read MoreRead More

0 Comments

Introducing PaddingLayout 4 – Foreign (Function) Memory API

Adding explicit extra space (explicit padding) to validate alignment Let’s consider the following C-like struct (let’s denote this as Case 1): StructLayout product = MemoryLayout.structLayout(  ValueLayout.JAVA_INT.withName(“sku”),  ValueLayout.JAVA_CHAR.withName(“energy”),  ValueLayout.JAVA_BYTE.withName(“weight”)); The product ... Read MoreRead More

0 Comments

Introducing PaddingLayout 3 – Foreign (Function) Memory API

Hooking stride The minimum byte-distance between two member layouts is called stride. The stride can be greater than or equal to the size. When we don’t face any alignment issues ... Read MoreRead More

0 Comments

Copying and slicing memory segments – Foreign (Function) Memory API

149. Copying and slicing memory segments Let’s consider the following memory segment (arena is an instance of Arena): MemorySegment srcSegment = arena.allocateArray(  ValueLayout.JAVA_INT, 1, 2, 3, 4, -1, -1, -1,                        ... Read MoreRead More

0 Comments

Calling the sumTwoInt() foreign function – Foreign (Function) Memory API

159. Calling the sumTwoInt() foreign function Do you remember the sumTwoInt() function? We have defined this C function in a native shared library named math.dll (check Problems 137, 138, and ... Read MoreRead More

0 Comments

Calling the strcat() foreign function – Foreign (Function) Memory API

161. Calling the strcat() foreign function The strcat() foreign function is part of the C standard library and it has the following signature (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-wcscat-mbscat): char *strcat(char *strDestination, const char *strSource); ... Read MoreRead More

0 Comments

Creating an electrical panel (hierarchy of classes) – Sealed and Hidden Classes

165. Creating an electrical panel (hierarchy of classes) Let’s assume that we want to model in code lines an electrical panel. Of course, we are not electricians, so in a ... Read MoreRead More

0 Comments

Closing the electrical panel before JDK 17 2 – Sealed and Hidden Classes

Declaring classes/interfaces as non-public Going further, we can declare interfaces/classes as non-public (by skipping the public keyword from the class/interface definition it becomes non-public and is set by default in ... Read MoreRead More

0 Comments