xref: /OK3568_Linux_fs/external/mpp/osal/allocator/allocator_std.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #include "os_mem.h"
20 #include "mpp_mem.h"
21 #include "mpp_debug.h"
22 
23 #include "allocator_std.h"
24 
25 typedef struct {
26     size_t alignment;
27     RK_S32 fd_count;
28 } allocator_ctx;
29 
allocator_std_open(void ** ctx,MppAllocatorCfg * cfg)30 static MPP_RET allocator_std_open(void **ctx, MppAllocatorCfg *cfg)
31 {
32     if (NULL == ctx) {
33         mpp_err_f("do not accept NULL input\n");
34         return MPP_ERR_NULL_PTR;
35     }
36 
37     mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
38     (void)cfg;
39 
40     *ctx = NULL;
41     return MPP_NOK;
42 }
43 
allocator_std_alloc(void * ctx,MppBufferInfo * info)44 static MPP_RET allocator_std_alloc(void *ctx, MppBufferInfo *info)
45 {
46     if (NULL == ctx) {
47         mpp_err_f("found NULL context input\n");
48         return MPP_ERR_NULL_PTR;
49     }
50 
51     mpp_err_f("Warning: std allocator should be used on simulation mode only\n");
52     (void)info;
53 
54     return MPP_NOK;
55 }
56 
allocator_std_free(void * ctx,MppBufferInfo * info)57 static MPP_RET allocator_std_free(void *ctx, MppBufferInfo *info)
58 {
59     (void) ctx;
60     if (info->ptr)
61         os_free(info->ptr);
62     return MPP_OK;
63 }
64 
allocator_std_import(void * ctx,MppBufferInfo * info)65 static MPP_RET allocator_std_import(void *ctx, MppBufferInfo *info)
66 {
67     allocator_ctx *p = (allocator_ctx *)ctx;
68     mpp_assert(ctx);
69     mpp_assert(info->ptr);
70     mpp_assert(info->size);
71     info->hnd   = NULL;
72     info->fd    = p->fd_count++;
73     return MPP_OK;
74 }
75 
allocator_std_release(void * ctx,MppBufferInfo * info)76 static MPP_RET allocator_std_release(void *ctx, MppBufferInfo *info)
77 {
78     (void) ctx;
79     mpp_assert(info->ptr);
80     mpp_assert(info->size);
81     info->ptr   = NULL;
82     info->size  = 0;
83     info->hnd   = NULL;
84     info->fd    = -1;
85     return MPP_OK;
86 }
87 
allocator_std_mmap(void * ctx,MppBufferInfo * info)88 static MPP_RET allocator_std_mmap(void *ctx, MppBufferInfo *info)
89 {
90     mpp_assert(ctx);
91     mpp_assert(info->ptr);
92     mpp_assert(info->size);
93     return MPP_OK;
94 }
95 
allocator_std_close(void * ctx)96 static MPP_RET allocator_std_close(void *ctx)
97 {
98     if (ctx) {
99         mpp_free(ctx);
100         return MPP_OK;
101     }
102     mpp_err_f("found NULL context input\n");
103     return MPP_NOK;
104 }
105 
106 os_allocator allocator_std = {
107     .type = MPP_BUFFER_TYPE_NORMAL,
108     .open = allocator_std_open,
109     .close = allocator_std_close,
110     .alloc = allocator_std_alloc,
111     .free = allocator_std_free,
112     .import = allocator_std_import,
113     .release = allocator_std_release,
114     .mmap = allocator_std_mmap,
115 };
116 
117