HAPMEM
HAP Memory Allocator
HAP_mem.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2020 Qualcomm Technologies, Inc.
3  * All Rights Reserved.
4  * Confidential and Proprietary - Qualcomm Technologies, Inc
5  */
6 
7 #ifndef HAP_MEM_H
8 #define HAP_MEM_H
9 #include <stdlib.h>
10 #include "AEEStdDef.h"
11 #include "AEEStdErr.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
23 /*
24  * Protections are chosen from these bits, or-ed together
25  */
26 
27 
36 
41 #define HAP_PROT_NONE 0x00 /* no permissions */
42 
46 #define HAP_PROT_READ 0x01 /* pages can be read */
47 
51 #define HAP_PROT_WRITE 0x02 /* pages can be written */
52 
57 #define HAP_PROT_EXEC 0x04 /* pages can be executed */
58 
59 
61 
62 /*
63  * Cache policy or-ed with protections parameter
64  */
65 
73 
78 #define HAP_MEM_CACHE_WRITEBACK (0x10) /* cache write back */
79 
83 #define HAP_MEM_CACHE_NON_SHARED (0x20) /* normal uncached memory */
84 
89 #define HAP_MEM_CACHE_WRITETHROUGH (0x40) /* write through memory */
90 
92 
100 
106 #define HAP_MEM_FLAGS_SKIP_DSP_MAP 0
107 
112 #define HAP_MEM_FLAGS_DSP_MAP 1
113 
120 #define HAP_MEM_FLAGS_EXTENDED_MAP 2
121 
126 #define HAP_MEM_FLAGS_MAX (HAP_MEM_FLAGS_EXTENDED_MAP + 1)
127 
129 
137 static inline int HAP_malloc(uint32 bytes, void** pptr)
138 {
139  *pptr = malloc(bytes);
140  if (*pptr) {
141  return AEE_SUCCESS;
142  }
143  return AEE_ENOMEMORY;
144 }
145 
154 static inline int HAP_free(void* ptr)
155 {
156  if(ptr == NULL)
157  return AEE_EBADCLASS;
158  free(ptr);
159  return AEE_SUCCESS;
160 }
161 
164  uint64 bytes_free;
167  uint64 bytes_used;
168  uint64 seg_free;
169  uint64 seg_used;
170  uint64 min_grow_bytes;
171 };
172 
177  USAGE_STATS = 1,
178  MAX_USED
179 };
180 
184 typedef struct {
185  enum HAP_mem_stats_request req_id;
186  union {
187  struct HAP_mem_stats usage_stats;
188  unsigned long max_used; /* Peak heap usage */
189  };
191 
198 int HAP_mem_get_stats(struct HAP_mem_stats *stats);
199 
207 
212 typedef enum
213 {
214  START = 0,
215  END
216 } marker_t;
217 
234 typedef enum
235 {
236  HAP_MEM_LOG_BLOCKS = 1,
237  HAP_MEM_SET_MARKER = 2,
238  HAP_MEM_MAP = 3,
239  HAP_MEM_UNMAP = 4,
240  HAP_RESERVE_VA = 5,
241  HAP_UNRESERVE_VA = 6
242 } HAP_mem_req_t;
243 
251 typedef struct
252 {
253  marker_t marker_type;
254  uint16_t instance;
256 
257 /* Payload structure for HAP_MEM_MAP request */
258 typedef struct {
259  uint64_t addr; // [in] reserved va (optional). If 0, buffer mapped at random VA
260  uint64_t len; // [in] length of buffer to be mapped
261  int prot; // [in] permissions and cache-mode of mapping
262  int flags; // [in] buffer flags
263  int fd; // [in] file descriptor of buffer
264  uint64_t dsp_pa; // [in] Offset
265  uint64_t dsp_va; // [out] Mapped DSP virtual address
266 } HAP_mem_map_t;
267 
268 /* Payload structure for HAP_MEM_UNMAP request */
269 typedef struct {
270  uint64_t dsp_va; // [in] DSP VA to be unmapped
271  uint64_t len; // [in] length of mapping
273 
274 /* Payload structure for HAP_RESERVE_VA request */
275 typedef struct {
276  uint64_t len; // [in] Length of VA space to be reserved
277  int prot; // [in] Permissions of the VA space
278  int flags; // [in] flags (unused for now)
279  uint64_t dsp_va; // [out] Reserved DSP virtual address
281 
282 /* Payload structure for HAP_UNRESERVE_VA request */
283 typedef struct {
284  uint64_t dsp_va; // [in] DSP VA to be unreserved
285  uint64_t len; // [in] Length of buffer to be unreserved
287 
293 typedef struct
294 {
295  HAP_mem_req_t request_id;
296  union {
297  HAP_mem_marker_payload_t mem_marker_payload;
298  HAP_mem_map_t mmap;
299  HAP_mem_unmap_t munmap;
300  HAP_mem_reserve_t reserve;
301  HAP_mem_unreserve_t unreserve;
302  };
304 
312 int HAP_mem_request(HAP_mem_req_payload_t *mem_payload);
313 
329 int HAP_mem_set_grow_size(uint64 min, uint64 max);
330 
345 int HAP_mem_set_heap_thresholds(unsigned int low_largest_block_size, unsigned int high_largest_block_size);
346 
347 
364 void* HAP_mmap(void *addr, int len, int prot, int flags, int fd, long offset);
365 
380 void* HAP_mmap2(void *addr, size_t len, int prot, int flags, int fd, long offset);
381 
392 int HAP_munmap(void *addr, int len);
393 
402 int HAP_munmap2(void *addr, size_t len);
403 
414 int HAP_mmap_get(int fd, void **vaddr, uint64 *paddr);
415 
424 int HAP_mmap_put(int fd);
425 
433 uint64 HAP_mem_available_stack(void);
434 
448 int HAP_apps_mem_request(size_t len, uint32_t flags, int *fd, uint64_t *dsp_va);
449 
459 int HAP_apps_mem_release(int fd);
460 
461 #ifdef __cplusplus
462 }
463 #endif
464 
465 #endif // HAP_MEM_H
466 
static int HAP_free(void *ptr)
Definition: HAP_mem.h:154
uint64 seg_free
Definition: HAP_mem.h:168
RequestID/Response for HAP_mem_get_heap_info.
Definition: HAP_mem.h:184
Definition: HAP_mem.h:258
static int HAP_malloc(uint32 bytes, void **pptr)
Definition: HAP_mem.h:137
HAP_mem_req_t
Definition: HAP_mem.h:234
Definition: HAP_mem.h:251
HAP_mem_stats_request
Enum for reqID for HAP_mem_get_heap_info()
Definition: HAP_mem.h:176
Definition: HAP_mem.h:269
int HAP_mmap_put(int fd)
marker_t
Definition: HAP_mem.h:212
Definition: HAP_mem.h:275
int HAP_munmap2(void *addr, size_t len)
uint64 seg_used
Definition: HAP_mem.h:169
Definition: HAP_mem.h:163
int HAP_munmap(void *addr, int len)
uint64 min_grow_bytes
Definition: HAP_mem.h:170
int HAP_mmap_get(int fd, void **vaddr, uint64 *paddr)
int HAP_mem_set_grow_size(uint64 min, uint64 max)
int HAP_mem_get_stats(struct HAP_mem_stats *stats)
int HAP_apps_mem_release(int fd)
int HAP_mem_request(HAP_mem_req_payload_t *mem_payload)
int HAP_mem_set_heap_thresholds(unsigned int low_largest_block_size, unsigned int high_largest_block_size)
Definition: HAP_mem.h:283
uint64 bytes_free
Definition: HAP_mem.h:164
int HAP_apps_mem_request(size_t len, uint32_t flags, int *fd, uint64_t *dsp_va)
uint64 bytes_used
Definition: HAP_mem.h:167
void * HAP_mmap2(void *addr, size_t len, int prot, int flags, int fd, long offset)
uint64 HAP_mem_available_stack(void)
int HAP_mem_get_heap_info(HAP_mem_heap_info_t *payload)
void * HAP_mmap(void *addr, int len, int prot, int flags, int fd, long offset)
Definition: HAP_mem.h:293