> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# RPCMEM API functions

## Functions

### `rpcmem_init`

Initialize the RPCMEM Library.

Only call this function once before using the RPCMEM Library.

This API is mandatory on pre-Lahaina targets IF the client has linked to the rpcmem.a static library. If the client has only linked libadsprpc.so, libcdsprpc.so, or libsdsprpc.so, then the rpcmem\_init call is not required on any target and other rpcmem APIs such as rpcmem\_alloc can be called directly.

NOTE: This function is not thread safe.

```cpp theme={null}
void rpcmem_init(void)
```

### `rpcmem_deinit`

Deinitialize the RPCMEM Library.

Only call this function once when the RPCMEM Library is no longer required.

This API is mandatory on pre-Lahaina targets IF the client has linked to the rpcmem.a static library. If the client has only linked libadsprpc.so, libcdsprpc.so, or libsdsprpc.so, then the rpcmem\_deinit call is not required on any target.

NOTE: This function is not thread safe.

```cpp theme={null}
void rpcmem_deinit(void)
```

### `rpcmem_alloc`

Allocate a zero-copy buffer for size upto 2 GB with the FastRPC framework.

Default memory attributes, 2 KB rpcmem\_alloc(RPCMEM\_HEAP\_ID\_SYSTEM,RPCMEM\_DEFAULT\_FLAGS,2048);
Or rpcmem\_alloc\_def(2048);

Heap 22, uncached, 1 KB rpcmem\_alloc(22,0,1024);
Or rpcmem\_alloc(22,RPCMEM\_FLAG\_UNCACHED,1024);

Heap 21, cached, 2 KB rpcmem\_alloc(21,RPCMEM\_FLAG\_CACHED,2048);
Or #include\<ion.h>
rpcmem\_alloc(21,ION\_FLAG\_CACHED,2048);

Default memory attributes but from heap 18, 4 KB rpcmem\_alloc(18,RPCMEM\_DEFAULT\_FLAGS,4096);

```cpp theme={null}
void * rpcmem_alloc(int heapid, uint32_t flags, int size)
```

**Parameters**

<ParamField path="heapid" type="int">
  Heap ID to use for memory allocation.
</ParamField>

<ParamField path="flags" type="uint32_t">
  ION flags to use for memory allocation.
</ParamField>

<ParamField path="size" type="int">
  Buffer size to allocate.
</ParamField>

**Returns**

Pointer to the buffer on success; NULL on failure.

### `rpcmem_alloc2`

Allocate a zero-copy buffer with the FastRPC framework.

The usage examples are same as rpcmem\_alloc.

```cpp theme={null}
void * rpcmem_alloc2(int heapid, uint32_t flags, size_t size)
```

**Parameters**

<ParamField path="heapid" type="int">
  Heap ID to use for memory allocation.
</ParamField>

<ParamField path="flags" type="uint32_t">
  ION flags to use for memory allocation.
</ParamField>

<ParamField path="size" type="size_t">
  Buffer size to allocate.
</ParamField>

**Returns**

Pointer to the buffer on success; NULL on failure.

### `__attribute__`

Allocate a buffer with default settings.

```cpp theme={null}
 __attribute__((unused)) static __inline void *rpcmem_alloc_def(int size)
```

**Parameters**

<ParamField path="size" type="">
  Size of the buffer to be allocated.
</ParamField>

**Returns**

Pointer to the allocated memory buffer.

### `rpcmem_free`

Free a buffer and ignore invalid buffers.

```cpp theme={null}
void rpcmem_free(void *po)
```

### `rpcmem_to_fd`

Return an associated file descriptor.

```cpp theme={null}
int rpcmem_to_fd(void *po)
```

**Parameters**

<ParamField path="po" type="void *">
  Data pointer for an RPCMEM-allocated buffer.
</ParamField>

**Returns**

Buffer file descriptor.
