NPU manager buffer
Summary
Enumerations |
|
|---|---|
ANpuBuffer_Priority{
|
enum The range of relative priority of the buffer within the app. |
ANpuBuffer_Type{
|
enum The purpose of the buffer. |
Typedefs |
|
|---|---|
ANpuBuffer
|
typedefstruct ANpuBuffer
|
ANpuBuffer_Priority
|
typedefenum ANpuBuffer_Priority
The range of relative priority of the buffer within the app. |
ANpuBuffer_Type
|
typedefenum ANpuBuffer_Type
The purpose of the buffer. |
ANpuManager_AllocCallback)(void *_Nullable cookie, int errorNum, ANpuBuffer *_Nullable buf)
|
typedefvoid(*
Callback function type when allocation and load is done. |
ANpuManager_AllocRequest
|
typedefstruct ANpuManager_AllocRequest
|
ANpuManager_CookieDeleter)(void *_Nullable cookie)
|
typedefvoid(*
Callback function type to delete the user cookie. |
ANpuManager_LoadCallback)(void *_Nullable cookie, int errorNum, ANpuBuffer *_Nonnull buf)
|
typedefvoid(*
Callback function type for buffer load. |
ANpuManager_PreemptCallback)(void *_Nullable cookie)
|
typedefvoid(*
Callback function type for buffer preemption. |
Functions |
|
|---|---|
ANpuBuffer_free(ANpuBuffer *_Nonnull const *_Nonnull buffers, size_t buffersLen)
|
int
Indicates that the buffers are not needed by the user and can be freed.
|
ANpuBuffer_loadAsync(ANpuBuffer *_Nonnull buf, int fdToOwn, int64_t fileOffset, int64_t segmentLength, int64_t bufferOffset, ANpuManager_LoadCallback _Nonnull onLoad)
|
void
Loads a file into the buffer asynchronously.
|
ANpuBuffer_map(ANpuBuffer *_Nonnull buf, void *_Nullable addr, size_t length, int prot, int flags, off_t offset)
|
void *_Nonnull
Maps a buffer into the application's address space.
|
ANpuBuffer_setPriority(ANpuBuffer *_Nonnull buf, int32_t newBufferPriority)
|
int
Sets the priority of the buffer.
|
ANpuBuffer_unmap(ANpuBuffer *_Nonnull buf, void *_Nonnull addr, size_t length)
|
int
Unmaps a previously mapped buffer.
|
ANpuManager_AllocRequest_create()
|
ANpuManager_AllocRequest *_Nonnull
Creates a new allocation request.
|
ANpuManager_AllocRequest_free(ANpuManager_AllocRequest *_Nullable request)
|
void
Destroys an allocation request.
|
ANpuManager_AllocRequest_setBufferPriority(ANpuManager_AllocRequest *_Nonnull request, int32_t bufferPriority)
|
void
Sets the buffer priority for an allocation request.
|
ANpuManager_AllocRequest_setBufferType(ANpuManager_AllocRequest *_Nonnull request, ANpuBuffer_Type bufferType)
|
void
Sets the purpose of the buffer for an allocation request.
|
ANpuManager_AllocRequest_setCookie(ANpuManager_AllocRequest *_Nonnull request, void *_Nullable cookie, ANpuManager_CookieDeleter_Nullable cookieDeleter)
|
void
Sets the user cookie for an allocation request.
|
ANpuManager_AllocRequest_setDeviceNumber(ANpuManager_AllocRequest *_Nonnull request, int32_t deviceNumber)
|
void
Sets the device number for an allocation request.
|
ANpuManager_AllocRequest_setFileSegmentToLoad(ANpuManager_AllocRequest *_Nonnull request, int fdToOwn, int64_t fileOffset, int64_t segmentLength, int64_t bufferOffset)
|
void
Sets the file segment to load for an allocation request.
|
ANpuManager_AllocRequest_setOnAlloc(ANpuManager_AllocRequest *_Nonnull request, ANpuManager_AllocCallback _Nonnull onAlloc)
|
void
Sets the allocation callback for an allocation request.
|
ANpuManager_AllocRequest_setOnPreempt(ANpuManager_AllocRequest *_Nonnull request, ANpuManager_PreemptCallback_Nullable onPreempt)
|
void
Sets the preemption callback for an allocation request.
|
ANpuManager_AllocRequest_setProtectionFlags(ANpuManager_AllocRequest *_Nonnull request, int32_t prot)
|
void
Sets the protection flags for the buffer to be allocated.
|
ANpuManager_AllocRequest_setSize(ANpuManager_AllocRequest *_Nonnull request, int64_t size)
|
void
Sets the size of the buffer in bytes for an allocation request.
|
ANpuManager_allocAsync(ANpuManager_AllocRequest *_Nonnull const *_Nonnull requests, size_t requestsLen)
|
void
Asynchronously allocates multiple buffers.
|
ANpuManager_isSupported(ANpuManager_AllocRequest *_Nonnull const *_Nonnull requests, size_t requestsLen, bool *_Nonnull outIsSupported)
|
int
Tests if the provided requests are supported or not.
|
Enumerations
ANpuBuffer_Priority
Declared inandroid/npumanager/buffer.h
ANpuBuffer_Priority
The range of relative priority of the buffer within the app.
These values defines the valid range for the buffer_priority field. The buffer_priority field must be within the range [ANPUBUFFER_PRIORITY_MIN, ANPUBUFFER_PRIORITY_MAX], inclusive.
| Properties | |
|---|---|
ANPUBUFFER_PRIORITY_DEFAULT
|
Introduced in API 37. |
ANPUBUFFER_PRIORITY_MAX
|
Introduced in API 37. |
ANPUBUFFER_PRIORITY_MIN
|
Introduced in API 37. |
ANpuBuffer_Type
Declared inandroid/npumanager/buffer.h
ANpuBuffer_Type
The purpose of the buffer.
For input/output buffers, use AHardwareBuffer instead.
Typedefs
ANpuBuffer
Declared inandroid/npumanager/buffer.h
struct ANpuBuffer ANpuBuffer
ANpuBuffer_Priority
Declared inandroid/npumanager/buffer.h
enum ANpuBuffer_Priority ANpuBuffer_Priority
The range of relative priority of the buffer within the app.
These values defines the valid range for the buffer_priority field. The buffer_priority field must be within the range [ANPUBUFFER_PRIORITY_MIN, ANPUBUFFER_PRIORITY_MAX], inclusive.
ANpuBuffer_Type
Declared inandroid/npumanager/buffer.h
enum ANpuBuffer_Type ANpuBuffer_Type
The purpose of the buffer.
For input/output buffers, use AHardwareBuffer instead.
ANpuManager_AllocCallback
Declared inandroid/npumanager/buffer.h
void(* ANpuManager_AllocCallback)(void *_Nullable cookie, int errorNum, ANpuBuffer *_Nullable buf)
Callback function type when allocation and load is done.
The callback is invoked in response to ANpuManager_allocAsync(). Even when the request has ANpuManager_AllocRequest_setFileSegmentToLoad(), indicating that a file is to be loaded into the buffer, this callback is still used to indicate that the load is complete (or failed), not ANpuManager_LoadCallback.
The callback may be invoked on any thread, including the current thread immediately inside ANpuManager_allocAsync() if an error is immediately detected.
The cookie is borrowed, i.e. the user does not own the cookie and must not free it. See documentation of ANpuManager_AllocRequest_setCookie() for details.
| Details | |||
|---|---|---|---|
| Parameters |
|
The value of cookie is the one stored in the ANpuManager_AllocRequest at the time when ANpuManager_allocAsync() is called. Subsequent changes to the cookie in the ANpuManager_AllocRequest object after a given ANpuManager_allocAsync() is called has no effect on the callbacks generated from the given ANpuManager_allocAsync() call.
The callback may be called from any thread. The user is responsible for accessing the cookie in a thread-safe manner.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest
Declared inandroid/npumanager/buffer.h
struct ANpuManager_AllocRequest ANpuManager_AllocRequest
ANpuManager_CookieDeleter
Declared inandroid/npumanager/buffer.h
void(* ANpuManager_CookieDeleter)(void *_Nullable cookie)
Callback function type to delete the user cookie.
The deleter may be called from any thread. The user is responsible for deleting the cookie or decrementing the associated reference counts in a thread-safe manner.
| Details | |||
|---|---|---|---|
| Parameters |
|
ANpuManager_LoadCallback
Declared inandroid/npumanager/buffer.h
void(* ANpuManager_LoadCallback)(void *_Nullable cookie, int errorNum, ANpuBuffer *_Nonnull buf)
Callback function type for buffer load.
NpuManager calls this function when the buffer load is done or has encountered an error.
This callback is invoked in response to ANpuBuffer_loadAsync(), NOT to ANpuManager_allocAsync().
| Details | |||||||
|---|---|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_PreemptCallback
Declared inandroid/npumanager/buffer.h
void(* ANpuManager_PreemptCallback)(void *_Nullable cookie)
Callback function type for buffer preemption.
NpuManager calls this function when it needs to forcefully deallocate the buffer. After this callback is invoked, any subsequent attempts to map the buffer are expected to fail.
Even if preempted, the buffer must still be explicitly freed by calling ANpuBuffer_free() to clean up remaining resources.
Note: On race conditions, the preemption callback may be invoked after the caller has already freed the buffer.
This callback will not be invoked while the application has the buffer mapped.
The cookie is borrowed, i.e. the user does not own the cookie and must not free it. See documentation of ANpuManager_AllocRequest_setCookie() for details.
The callback may be called from any thread. The user is responsible for accessing the cookie in a thread-safe manner.
| Details | |||
|---|---|---|---|
| Parameters |
|
Functions
ANpuBuffer_free
Declared inandroid/npumanager/buffer.h
int ANpuBuffer_free( ANpuBuffer *_Nonnull const *_Nonnull buffers, size_t buffersLen )
Indicates that the buffers are not needed by the user and can be freed.
If there are any existing mappings to the buffer via ANpuBuffer_map(), the mapping will continue to be valid until unmapped even after the buffer is freed. In this case, the associated memory will be freed after all mappings are unmapped.
To prevent use-after-free bugs, the caller must not use pointers in buffers during or after this call in any of the ANpuBuffer_* functions, except:
- ANpuBuffer_unmap() to unmap any existing mappings to the buffer.
Even if a buffer is preempted, it must still be explicitly freed by calling ANpuBuffer_free() to clean up remaining resources.
Available since API level 37.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
||||
| Returns |
0 on success, or -1 on error with errno set.
|
ANpuBuffer_loadAsync
Declared inandroid/npumanager/buffer.h
void ANpuBuffer_loadAsync( ANpuBuffer *_Nonnull buf, int fdToOwn, int64_t fileOffset, int64_t segmentLength, int64_t bufferOffset, ANpuManager_LoadCallback _Nonnull onLoad )
Loads a file into the buffer asynchronously.
There should not be any existing mapping (via ANpuBuffer_map()) to the buffer. Otherwise, the callback is replied with an error.
Available since API level 37.
The position of the associated file description is not changed by this function. This must be non-NULL. Otherwise, the process will crash.
| Details | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Parameters |
|
||||||||
| Parameters |
|
ANpuBuffer_map
Declared inandroid/npumanager/buffer.h
void *_Nonnull ANpuBuffer_map( ANpuBuffer *_Nonnull buf, void *_Nullable addr, size_t length, int prot, int flags, off_t offset )
Maps a buffer into the application's address space.
The arguments are similar to mmap().
The returned address must be unmapped by calling ANpuBuffer_unmap().
Available since API level 37.
There is a small time window between when the buffer is preempted and when ANpuManager_OnPreempt is called. If the app calls ANpuBuffer_map() during this time window, it will still get -1 with errno set to ENOENT, even though when ANpuManager_OnPreempt has not been called yet.
| Details | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Parameters |
|
||||||||||||
| Returns |
The mapped address on success, or MAP_FAILED on error and errno is set. If the buffer was preempted, errno is set to ENOENT.
|
ANpuBuffer_setPriority
Declared inandroid/npumanager/buffer.h
int ANpuBuffer_setPriority( ANpuBuffer *_Nonnull buf, int32_t newBufferPriority )
Sets the priority of the buffer.
Available since API level 37.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
||||
| Returns |
0 on success, or -1 on error with errno set.
|
ANpuBuffer_unmap
Declared inandroid/npumanager/buffer.h
int ANpuBuffer_unmap( ANpuBuffer *_Nonnull buf, void *_Nonnull addr, size_t length )
Unmaps a previously mapped buffer.
The arguments are similar to munmap().
Available since API level 37.
| Details | |||||||
|---|---|---|---|---|---|---|---|
| Parameters |
|
||||||
| Returns |
0 on success, or -1 on error with errno set.
|
ANpuManager_AllocRequest_create
Declared inandroid/npumanager/buffer.h
ANpuManager_AllocRequest *_Nonnull ANpuManager_AllocRequest_create()
Creates a new allocation request.
Fields are initialized to a default state.
| Details | |
|---|---|
| Returns |
A new allocation request.
|
ANpuManager_AllocRequest_free
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_free( ANpuManager_AllocRequest *_Nullable request )
Destroys an allocation request.
| Details | |||
|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setBufferPriority
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setBufferPriority( ANpuManager_AllocRequest *_Nonnull request, int32_t bufferPriority )
Sets the buffer priority for an allocation request.
This is the relative priority of the buffer within the app.
The value must be within the range [ANPUBUFFER_PRIORITY_MIN, ANPUBUFFER_PRIORITY_MAX], inclusive.
If never called, the default value is ANPUBUFFER_PRIORITY_DEFAULT.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setBufferType
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setBufferType( ANpuManager_AllocRequest *_Nonnull request, ANpuBuffer_Type bufferType )
Sets the purpose of the buffer for an allocation request.
The default value is ANPUBUFFER_TYPE_UNKNOWN, which is invalid. Hence, this must be set with a valid buffer type before being sent to NpuManagerService. Otherwise, the request is responded with errorNum set to EINVAL.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setCookie
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setCookie( ANpuManager_AllocRequest *_Nonnull request, void *_Nullable cookie, ANpuManager_CookieDeleter_Nullable cookieDeleter )
Sets the user cookie for an allocation request.
The user may use this cookie to identify the request. It is not dereferenced directly by the implementation of the NDK library.
Ownership of cookie
This donates the ownership of the cookie to ANpuManager. In other words, ANpuManager will be responsible for freeing the cookie with the provided deleter function.
This assumes the caller owns the cookie before calling this function, and does not own the cookie after calling this function.
In particular, to use the same cookie in multiple ANpuManager_AllocRequest objects, or in OnPreempt() to reuse the cookie in a new ANpuManager_AllocRequest object, reference count the cookie, and:
- Before calling setCookie(), increment the reference count by 1.
- In the deleter function, decrement the reference count by 1.
Calling the deleter function
When ANpuManager decides to free the cookie:
- If cookieDeleter is not NULL, it will be called with the cookie value. This will happen even if the cookie is NULL. In this case, the deleter must be able to handle NULL gracefully.
- If the cookieDeleter is NULL, nothing will happen, even when the cookie is not NULL. This is useful if the user stores an arbitrary 64-bit integer value in the cookie (so it is not a real pointer.) However, if the cookie points to a heap object, it is strongly recommended to set the deleter so ANpuManager handles its lifetime properly and reduce UAF bugs.
Other semantics of setCookie()
When setCookie() is called multiple times, the previous cookie will be deleted with the previous deleter function (if set) before the new cookie and the new deleter function are set.
To clear the cookie, set both cookie and cookieDeleter to NULL.
If never called, the default cookie and cookieDeleter are NULL.
The deleter may be called from any thread. The user is responsible for deleting the cookie or decrementing the associated reference counts in a thread-safe manner.
Callbacks
The existing onAlloc and onPreempt callbacks must be able to handle the new cookie value after setCookie() is called. If not, modify the callbacks accordingly before sending them using ANpuManager_allocAsync().
| Details | |||||||
|---|---|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setDeviceNumber
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setDeviceNumber( ANpuManager_AllocRequest *_Nonnull request, int32_t deviceNumber )
Sets the device number for an allocation request.
An identifier for the NPU that is involved with the work. The identifiers are vendor-specific and opaque to the ANpuManager.
The default value is a negative value, which is invalid. Hence, this must be with a valid non-negative device number before being sent to NpuManagerService. Otherwise, the request is responded with errorNum set to EINVAL.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setFileSegmentToLoad
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setFileSegmentToLoad( ANpuManager_AllocRequest *_Nonnull request, int fdToOwn, int64_t fileOffset, int64_t segmentLength, int64_t bufferOffset )
Sets the file segment to load for an allocation request.
Ownership: The ANpuManager_AllocRequest object takes ownership of the provided file descriptor. The caller must not close it manually.
The ownership of the fd is transferred to ANpuManager.
| Details | |||||||
|---|---|---|---|---|---|---|---|
| Parameters |
|
||||||
| Parameters |
|
ANpuManager_AllocRequest_setOnAlloc
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setOnAlloc( ANpuManager_AllocRequest *_Nonnull request, ANpuManager_AllocCallback _Nonnull onAlloc )
Sets the allocation callback for an allocation request.
Callback function to be invoked when allocation and load is finished or has encountered an error.
setOnAlloc() must be called with a non-NULL value. Otherwise, the process will crash.
setOnAlloc() must be called before calling ANpuManager_allocAsync(). Otherwise, the process will crash.
See documentation of ANpuManager_AllocCallback for details about the arguments when the callback is invoked.
The new callback must be able to handle any existing cookie value set by setCookie(), or NULL if setCookie() is never called. If not, modify the cookie or the callback accordingly before sending them using ANpuManager_allocAsync().
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setOnPreempt
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setOnPreempt( ANpuManager_AllocRequest *_Nonnull request, ANpuManager_PreemptCallback_Nullable onPreempt )
Sets the preemption callback for an allocation request.
Callback function to be invoked when NpuManager needs to forcefully deallocate the buffer.
This may be set to NULL, in which case NpuManager will not invoke any callback when preempting the buffer. Default value is NULL.
When the buffer is preempted, any subsequent attempt to map the buffer will fail with errno set to ENOENT.
See documentation of ANpuManager_PreemptCallback for details about the arguments when the callback is invoked.
The new callback must be able to handle any existing cookie value set by setCookie(), or NULL if setCookie() is never called. If not, modify the cookie or the callback accordingly before sending them using ANpuManager_allocAsync().
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setProtectionFlags
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setProtectionFlags( ANpuManager_AllocRequest *_Nonnull request, int32_t prot )
Sets the protection flags for the buffer to be allocated.
Note: This specifies the protection flags for the buffer, not the flags for the mmap call. The flags for the mmap call is passed via ANpuBuffer_map().
If never called, the default value is PROT_READ.
After the buffer is allocated, ANpuBuffer_map() may only be called with |prot| to be a subset of the value set by this function. If not, ANpuBuffer_map() fails and returns MAP_FAILED.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_AllocRequest_setSize
Declared inandroid/npumanager/buffer.h
void ANpuManager_AllocRequest_setSize( ANpuManager_AllocRequest *_Nonnull request, int64_t size )
Sets the size of the buffer in bytes for an allocation request.
If never called, the default value is 0.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_allocAsync
Declared inandroid/npumanager/buffer.h
void ANpuManager_allocAsync( ANpuManager_AllocRequest *_Nonnull const *_Nonnull requests, size_t requestsLen )
Asynchronously allocates multiple buffers.
Each ANpuBuffer*, after being received from the onAlloc callback, must be explicitly freed by calling ANpuBuffer_free(). This is the case even if the buffer is preempted by NpuManagerService.
Available since API level 37.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
ANpuManager_isSupported
Declared inandroid/npumanager/buffer.h
int ANpuManager_isSupported( ANpuManager_AllocRequest *_Nonnull const *_Nonnull requests, size_t requestsLen, bool *_Nonnull outIsSupported )
Tests if the provided requests are supported or not.
If a request is supported, ANpuManager_allocAsync() may still respond onAlloc with a failure due to other reasons, e.g., no memory. If a request is not supported, ANpuManager_allocAsync() guarantees to respond onAlloc with a failure.
The following fields in the requests are ignored:
- cookie
- onAlloc
- onPreempt
The list must not be modified during the execution of
ANpuManager_isSupported(). Otherwise, the behavior is undefined.
It is okay to modify the list after ANpuManager_isSupported()
returns, even before the onAlloc callback is invoked.
The elements of the list must not be modified or freed during the
execution of ANpuManager_isSupported(), e.g. via the
ANpuManager_AllocRequest_setXXX() functions and
ANpuManager_AllocRequest_free(). Otherwise, the behavior
is undefined. It is okay to modify or free the elements
after ANpuManager_isSupported() returns, even before the
onAlloc callback is invoked.| Details | |||
|---|---|---|---|
| Parameters |
|
The caller must not simultaneously write to this array during the execution of ANpuManager_isSupported(). Otherwise, the behavior is undefined.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
||||
| Returns |
0 on successfully testing whether the requests are supported or not (even if some or all of them are not supported); results are stored in outIsSupported. Otherwise, if there is an error, returns -1 with with errno set, and results in outIsSupported should be ignored by the caller.
|