xref: /rockchip-linux_mpp/osal/allocator/allocator_std.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2015 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include <stdio.h>
7 
8 #include "os_mem.h"
9 #include "mpp_mem.h"
10 #include "mpp_debug.h"
11 
12 #include "allocator_std.h"
13 
14 typedef struct {
15     size_t              alignment;
16     MppAllocFlagType    flags;
17     RK_S32              fd_count;
18 } allocator_ctx;
19 
allocator_std_open(void ** ctx,size_t alignment,MppAllocFlagType flags)20 static MPP_RET allocator_std_open(void **ctx, size_t alignment, MppAllocFlagType flags)
21 {
22     allocator_ctx *p = NULL;
23 
24     if (NULL == ctx) {
25         mpp_err_f("do not accept NULL input\n");
26         return MPP_ERR_NULL_PTR;
27     }
28 
29     mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
30 
31     p = mpp_malloc(allocator_ctx, 1);
32     if (p) {
33         p->alignment = alignment;
34         p->flags = flags;
35         p->fd_count = 0;
36     }
37 
38     *ctx = p;
39     return p ? MPP_OK : MPP_NOK;
40 }
41 
allocator_std_alloc(void * ctx,MppBufferInfo * info)42 static MPP_RET allocator_std_alloc(void *ctx, MppBufferInfo *info)
43 {
44     if (NULL == ctx) {
45         mpp_err_f("found NULL context input\n");
46         return MPP_ERR_NULL_PTR;
47     }
48 
49     mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
50     (void)info;
51 
52     return MPP_NOK;
53 }
54 
allocator_std_free(void * ctx,MppBufferInfo * info)55 static MPP_RET allocator_std_free(void *ctx, MppBufferInfo *info)
56 {
57     (void) ctx;
58     if (info->ptr)
59         os_free(info->ptr);
60     return MPP_OK;
61 }
62 
allocator_std_import(void * ctx,MppBufferInfo * info)63 static MPP_RET allocator_std_import(void *ctx, MppBufferInfo *info)
64 {
65     allocator_ctx *p = (allocator_ctx *)ctx;
66     mpp_assert(ctx);
67     mpp_assert(info->ptr);
68     mpp_assert(info->size);
69     info->hnd   = NULL;
70     info->fd    = p->fd_count++;
71     return MPP_OK;
72 }
73 
allocator_std_release(void * ctx,MppBufferInfo * info)74 static MPP_RET allocator_std_release(void *ctx, MppBufferInfo *info)
75 {
76     (void) ctx;
77     mpp_assert(info->ptr);
78     mpp_assert(info->size);
79     info->ptr   = NULL;
80     info->size  = 0;
81     info->hnd   = NULL;
82     info->fd    = -1;
83     return MPP_OK;
84 }
85 
allocator_std_mmap(void * ctx,MppBufferInfo * info)86 static MPP_RET allocator_std_mmap(void *ctx, MppBufferInfo *info)
87 {
88     mpp_assert(ctx);
89     mpp_assert(info->ptr);
90     mpp_assert(info->size);
91     return MPP_OK;
92 }
93 
allocator_std_close(void * ctx)94 static MPP_RET allocator_std_close(void *ctx)
95 {
96     MPP_FREE(ctx);
97     return MPP_OK;
98 }
99 
os_allocator_std_flags(void * ctx)100 static MppAllocFlagType os_allocator_std_flags(void *ctx)
101 {
102     (void) ctx;
103     return MPP_ALLOC_FLAG_NONE;
104 }
105 
106 os_allocator allocator_std = {
107     .type = MPP_BUFFER_TYPE_NORMAL,
108     .name = "std",
109     .open = allocator_std_open,
110     .close = allocator_std_close,
111     .alloc = allocator_std_alloc,
112     .free = allocator_std_free,
113     .import = allocator_std_import,
114     .release = allocator_std_release,
115     .mmap = allocator_std_mmap,
116     .flags = os_allocator_std_flags,
117 };
118