[osal]: add mpp_allocator and os_allocator for mpp_buffer

git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@130 6e48237b-75ef-9749-8fc9-41990f28c85a
This commit is contained in:
ChenHengming
2015-08-19 10:37:38 +00:00
parent 0de1933334
commit f22f5635ab
11 changed files with 352 additions and 3 deletions

View File

@@ -25,6 +25,7 @@
* buffer group may need to set a default group size limit
*/
#define SZ_1K (1024)
#define SZ_4K (SZ_1K*4)
#define SZ_1M (SZ_1K*SZ_1K)
#define SZ_2M (SZ_1M*2)
#define SZ_4M (SZ_1M*4)

View File

@@ -20,6 +20,7 @@
#include "mpp_log.h"
#include "mpp_mem.h"
#include "mpp_allocator.h"
#include "mpp_buffer_impl.h"
#define MPP_BUFFER_SERVICE_LOCK() pthread_mutex_lock(&services.lock)
@@ -36,8 +37,7 @@ typedef struct {
struct list_head list_orphan;
} MppBufferService;
static MppBufferService services =
{
static MppBufferService services = {
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
0,
0,

View File

@@ -25,7 +25,7 @@ MPP_RET mpp_packet_init(MppPacket *packet, void *data, size_t size)
{
if (NULL == packet || NULL == data || 0 == size) {
mpp_err("mpp_packet_init invalid input packet %p data %p size %d\n",
packet, data, size);
packet, data, size);
return MPP_ERR_NULL_PTR;
}

View File

@@ -12,12 +12,14 @@ endif()
include_directories(.)
add_library(osal STATIC
mpp_allocator.cpp
mpp_common.cpp
mpp_time.cpp
mpp_list.cpp
mpp_mem.cpp
mpp_env.cpp
mpp_log.cpp
${OS_DIR}/os_allocator.c
${OS_DIR}/os_mem.c
${OS_DIR}/os_env.c
${OS_DIR}/os_log.c

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include "os_mem.h"
#include "os_allocator.h"
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size)
{
(void) ctx;
return os_malloc(&data->ptr, alignment, size);
}
void os_allocator_free(void *ctx, MppBufferData *data)
{
(void) ctx;
os_free(data->ptr);
}

45
osal/inc/mpp_allocator.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_ALLOCATOR_H__
#define __MPP_ALLOCATOR_H__
#include "rk_type.h"
#include "mpp_buffer.h"
typedef void *MppAllocator;
typedef struct {
RK_U32 size;
RK_U32 version;
MPP_RET (*alloc)(MppAllocator allocator, MppBufferData **data, size_t size);
MPP_RET (*free)(MppAllocator allocator, MppBufferData **data);
} MppAllocatorApi;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api);
MPP_RET mpp_alloctor_put(MppAllocator *allocator);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_ALLOCATOR_H__*/

32
osal/linux/os_allocator.c Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include "os_mem.h"
#include "os_allocator.h"
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size)
{
(void) ctx;
return os_malloc(&data->ptr, alignment, size);
}
void os_allocator_free(void *ctx, MppBufferData *data)
{
(void) ctx;
os_free(data->ptr);
}

139
osal/mpp_allocator.cpp Normal file
View File

@@ -0,0 +1,139 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define MODULE_TAG "mpp_allocator"
#include "mpp_log.h"
#include "mpp_mem.h"
#include "mpp_allocator.h"
#include "mpp_allocator_impl.h"
#include "os_allocator.h"
#define MPP_ALLOCATOR_LOCK(p) pthread_mutex_lock(&(p)->lock);
#define MPP_ALLOCATOR_UNLOCK(p) pthread_mutex_unlock(&(p)->lock);
MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferData **data, size_t size)
{
if (NULL == allocator || NULL == data) {
mpp_err("mpp_allocator_alloc invalid input: allocator %p data %p\n",
allocator, data);
return MPP_ERR_UNKNOW;
}
if (0 == size) {
*data = NULL;
return MPP_NOK;
}
MppBufferData *pdata = mpp_malloc(MppBufferData, 1);
if (NULL == pdata) {
mpp_err("mpp_allocator_alloc failed to alloc MppBufferData\n");
*data = NULL;
return MPP_ERR_MALLOC;
}
MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator;
MPP_ALLOCATOR_LOCK(palloc);
int ret = os_allocator_alloc(palloc->allocator, pdata, palloc->alignment, size);
*data = pdata;
MPP_ALLOCATOR_UNLOCK(palloc);
return (0 == ret) ? (MPP_OK) : (MPP_NOK);
}
MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData **data)
{
if (NULL == allocator || NULL == data) {
mpp_err("mpp_allocator_alloc invalid input: allocator %p data %p\n",
allocator, data);
return MPP_ERR_UNKNOW;
}
MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator;
MPP_ALLOCATOR_LOCK(palloc);
MppBufferData *pdata = *data;
*data = NULL;
os_allocator_free(palloc->allocator, pdata);
MPP_ALLOCATOR_UNLOCK(palloc);
mpp_free(pdata);
return MPP_OK;
}
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api)
{
if (NULL == allocator || NULL == api) {
mpp_err("mpp_alloctor_get invalid input: buffer %p api %p\n",
allocator, api);
return MPP_ERR_UNKNOW;
}
MppAllocatorImpl *palloc = mpp_malloc(MppAllocatorImpl, 1);
if (NULL == palloc) {
mpp_err("mpp_alloctor_get failed to malloc allocator context\n");
return MPP_ERR_NULL_PTR;
}
MppAllocatorApi *papi = mpp_malloc(MppAllocatorApi, 1);
if (NULL == papi) {
mpp_err("mpp_alloctor_get failed to malloc api context\n");
mpp_free(palloc);
return MPP_ERR_NULL_PTR;
}
os_allocator_open(&palloc->allocator);
papi->size = sizeof(papi->size);
papi->version = 1;
papi->alloc = mpp_allocator_alloc;
papi->free = mpp_allocator_free;
palloc->api = papi;
palloc->alignment = SZ_4K;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&palloc->lock, &attr);
pthread_mutexattr_destroy(&attr);
return MPP_OK;
}
MPP_RET mpp_alloctor_put(MppAllocator *allocator)
{
if (NULL == allocator) {
mpp_err("mpp_alloctor_put invalid input: buffer %p\n", allocator);
return MPP_ERR_NULL_PTR;
}
MppAllocatorImpl *p = (MppAllocatorImpl *)*allocator;
*allocator = NULL;
os_allocator_close(p->allocator);
mpp_assert(p->api);
mpp_free(p->api);
if (p)
mpp_free(p);
return MPP_OK;
}

30
osal/mpp_allocator_impl.h Normal file
View File

@@ -0,0 +1,30 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_ALLOCATOR_IMPL_H__
#define __MPP_ALLOCATOR_IMPL_H__
#include "mpp_thread.h"
typedef struct {
pthread_mutex_t lock;
size_t alignment;
void *allocator;
void *api;
} MppAllocatorImpl;
#endif /*__MPP_ALLOCATOR_IMPL_H__*/

37
osal/os_allocator.h Normal file
View File

@@ -0,0 +1,37 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __OS_ALLOCATOR_H__
#define __OS_ALLOCATOR_H__
#include "mpp_allocator.h"
#ifdef __cplusplus
extern "C" {
#endif
int os_allocator_open(void **ctx);
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size);
void os_allocator_free(void *ctx, MppBufferData *data);
void os_allocator_close(void *ctx);
#ifdef __cplusplus
}
#endif
#endif /*__OS_ALLOCATOR_H__*/

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2010 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "os_mem.h"
#include "os_allocator.h"
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t alignment, size_t size)
{
(void) ctx;
return os_malloc(&data->ptr, alignment, size);
}
void os_allocator_free(void *ctx, MppBufferData *data)
{
(void) ctx;
os_free(data->ptr);
}