forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx_alloc.h
More file actions
77 lines (66 loc) · 2.11 KB
/
ctx_alloc.h
File metadata and controls
77 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2026 Intel Corporation. All rights reserved.
*/
#ifndef __SOF_CTX_ALLOC_H__
#define __SOF_CTX_ALLOC_H__
#include <sof/audio/component.h>
#include <sof/lib/vregion.h>
#include <rtos/alloc.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/**
* Allocate memory from a mod_alloc_ctx context.
*
* When the context has a vregion, allocates from the vregion interim
* partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set
* in flags. Falls back to sof_heap_alloc() otherwise.
*
* @param ctx Allocation context (heap + optional vregion).
* @param flags Allocation flags (SOF_MEM_FLAG_*).
* @param size Size in bytes.
* @param alignment Required alignment in bytes.
* @return Pointer to allocated memory or NULL on failure.
*/
static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags,
size_t size, size_t alignment)
{
if (!ctx || !ctx->vreg)
return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment);
if (flags & SOF_MEM_FLAG_COHERENT)
return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);
return vregion_alloc_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment);
}
/**
* Allocate zero-initialized memory from a mod_alloc_ctx context.
* @param ctx Allocation context.
* @param flags Allocation flags (SOF_MEM_FLAG_*).
* @param size Size in bytes.
* @param alignment Required alignment in bytes.
* @return Pointer to allocated memory or NULL on failure.
*/
static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags,
size_t size, size_t alignment)
{
void *ptr = sof_ctx_alloc(ctx, flags, size, alignment);
if (ptr)
memset(ptr, 0, size);
return ptr;
}
/**
* Free memory allocated from a mod_alloc_ctx context.
* @param ctx Allocation context.
* @param ptr Pointer to free.
*/
static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr)
{
if (!ptr)
return;
if (ctx && ctx->vreg)
vregion_free(ctx->vreg, ptr);
else
sof_heap_free(ctx ? ctx->heap : NULL, ptr);
}
#endif /* __SOF_CTX_ALLOC_H__ */