Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Memory and Pointer in JNA

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Memory and Pointer in JNA". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Memory and Pointer in JNA.

Brief introduction

We know that there are many pointers in the code of native, which are mapped to Pointer in JNA.

Pointer

Pointer is a class introduced in JNA to represent pointers in native methods. Let's recall what the pointer is in the native method.

The pointer in the native method is actually an address, which is the memory address of the real object. So a peer property is defined in Pointer to store the memory address of the real object:

Protected long peer

In real time, the constructor of Pointer needs to pass this peer parameter:

Public Pointer (long peer) {this.peer = peer;}

Let's take a look at how to fetch a real object from Pointer, taking the byte array as an example:

Public void read (long offset, byte [] buf, int index, int length) {Native.read (this, this.peer, offset, buf, index, length);}

This method actually calls the Native.read method, so let's take a look at the read method:

Static native void read (Pointer pointer, long baseaddr, long offset, byte [] buf, int index, int length)

You can see that it is a real native method that reads a pointer object.

In addition to the Byte array, Pointer provides many other types of reading methods.

Read and write again. Let's take a look at how Pointer writes data:

Public void write (long offset, byte [] buf, int index, int length) {Native.write (this, this.peer, offset, buf, index, length);}

Again, the Native.write method is called to write the data.

Here the Native.write method is also a native method:

Static native void write (Pointer pointer, long baseaddr, long offset, byte [] buf, int index, int length)

Pointer also provides many other types of data writing methods.

Of course, there is a more direct method of get*:

Public byte getByte (long offset) {return Native.getByte (this, this.peer, offset);} Special Pointer:Opaque

In Pointer, there are also two createConstant methods to create a Pointer that is neither readable nor writable:

Public static final Pointer createConstant (long peer) {return new Opaque (peer);} public static final Pointer createConstant (int peer) {return new Opaque ((long) peer & 0xFFFFFFFF);}

What is actually returned is the Opaque class, which inherits from Pointer, but all read or write methods in it throw UnsupportedOperationException:

Private static class Opaque extends Pointer {private Opaque (long peer) {super (peer);} @ Override public Pointer share (long offset, long size) {throw new UnsupportedOperationException (MSG);} Memory

Pointer is a basic pointer mapping, and for memory space allocated by using native's malloc method, we need to know how much space is allocated in addition to the starting position of the Pointer pointer. So a simple Pointer is not enough.

In this case, we need to use Memory.

Memory is a special Pointer that preserves the amount of space allocated.

Let's take a look at the definition of Memory and the properties it contains:

Public class Memory extends Pointer {... Private static ReferenceQueue QUEUE = new ReferenceQueue (); private static LinkedReference HEAD; / / the head of the doubly linked list used for instance tracking private static final WeakMemoryHolder buffers = new WeakMemoryHolder (); private final LinkedReference reference; / / used to track the instance protected long size; / / Size of the malloc'ed space...}

There are five pieces of data defined in Memory, which we will introduce one by one.

First, the most important size,size represents the amount of memory space in Memory. Let's take a look at the constructor of Memory:

Public Memory (long size) {this.size = size; if (size)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report