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