From f22f5635ab8edcdc224ca42381cd32ce4e09ddf4 Mon Sep 17 00:00:00 2001 From: ChenHengming Date: Wed, 19 Aug 2015 10:37:38 +0000 Subject: [PATCH] [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 --- inc/mpp_buffer.h | 1 + mpp/mpp_buffer_impl.cpp | 4 +- mpp/mpp_packet.cpp | 2 +- osal/CMakeLists.txt | 2 + osal/android/os_allocator.c | 32 +++++++++ osal/inc/mpp_allocator.h | 45 ++++++++++++ osal/linux/os_allocator.c | 32 +++++++++ osal/mpp_allocator.cpp | 139 ++++++++++++++++++++++++++++++++++++ osal/mpp_allocator_impl.h | 30 ++++++++ osal/os_allocator.h | 37 ++++++++++ osal/window/os_allocator.c | 31 ++++++++ 11 files changed, 352 insertions(+), 3 deletions(-) create mode 100644 osal/android/os_allocator.c create mode 100644 osal/inc/mpp_allocator.h create mode 100644 osal/linux/os_allocator.c create mode 100644 osal/mpp_allocator.cpp create mode 100644 osal/mpp_allocator_impl.h create mode 100644 osal/os_allocator.h create mode 100644 osal/window/os_allocator.c diff --git a/inc/mpp_buffer.h b/inc/mpp_buffer.h index 765b842b..01c9adf3 100644 --- a/inc/mpp_buffer.h +++ b/inc/mpp_buffer.h @@ -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) diff --git a/mpp/mpp_buffer_impl.cpp b/mpp/mpp_buffer_impl.cpp index 74698371..927320df 100644 --- a/mpp/mpp_buffer_impl.cpp +++ b/mpp/mpp_buffer_impl.cpp @@ -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, diff --git a/mpp/mpp_packet.cpp b/mpp/mpp_packet.cpp index b69c79f5..a698061c 100644 --- a/mpp/mpp_packet.cpp +++ b/mpp/mpp_packet.cpp @@ -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; } diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 627c259d..ad57c17b 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -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 diff --git a/osal/android/os_allocator.c b/osal/android/os_allocator.c new file mode 100644 index 00000000..e1c61f9a --- /dev/null +++ b/osal/android/os_allocator.c @@ -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 +#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); +} + diff --git a/osal/inc/mpp_allocator.h b/osal/inc/mpp_allocator.h new file mode 100644 index 00000000..6303ff1b --- /dev/null +++ b/osal/inc/mpp_allocator.h @@ -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__*/ + diff --git a/osal/linux/os_allocator.c b/osal/linux/os_allocator.c new file mode 100644 index 00000000..e1c61f9a --- /dev/null +++ b/osal/linux/os_allocator.c @@ -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 +#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); +} + diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp new file mode 100644 index 00000000..22794adb --- /dev/null +++ b/osal/mpp_allocator.cpp @@ -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; +} + diff --git a/osal/mpp_allocator_impl.h b/osal/mpp_allocator_impl.h new file mode 100644 index 00000000..891d639e --- /dev/null +++ b/osal/mpp_allocator_impl.h @@ -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__*/ + diff --git a/osal/os_allocator.h b/osal/os_allocator.h new file mode 100644 index 00000000..58f957af --- /dev/null +++ b/osal/os_allocator.h @@ -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__*/ + + diff --git a/osal/window/os_allocator.c b/osal/window/os_allocator.c new file mode 100644 index 00000000..6a1480f3 --- /dev/null +++ b/osal/window/os_allocator.c @@ -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); +} +