[osal]: add allocator type to MppBufferAllocator, passed on window and linux

git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@139 6e48237b-75ef-9749-8fc9-41990f28c85a
This commit is contained in:
ChenHengming
2015-08-20 10:03:38 +00:00
parent e679ce5d19
commit 505e4dd8ae
8 changed files with 92 additions and 44 deletions

View File

@@ -284,7 +284,7 @@ MPP_RET mpp_buffer_group_init(MppBufferGroupImpl **group, const char *tag, MppBu
break; break;
} while (p->group_id != service.group_count); } while (p->group_id != service.group_count);
mpp_alloctor_get(&p->allocator, &p->alloc_api); mpp_alloctor_get(&p->allocator, &p->alloc_api, type);
MPP_BUFFER_SERVICE_UNLOCK(); MPP_BUFFER_SERVICE_UNLOCK();
*group = p; *group = p;

View File

@@ -154,7 +154,7 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size)
p = (allocator_ion *)ctx; p = (allocator_ion *)ctx;
return os_malloc(&data->ptr, p->alignment, size); return (MPP_RET)os_malloc(&data->ptr, p->alignment, size);
} }
void os_allocator_free(void *ctx, MppBufferData *data) void os_allocator_free(void *ctx, MppBufferData *data)

View File

@@ -34,7 +34,7 @@ typedef struct {
extern "C" { extern "C" {
#endif #endif
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api); MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type);
MPP_RET mpp_alloctor_put(MppAllocator *allocator); MPP_RET mpp_alloctor_put(MppAllocator *allocator);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -22,11 +22,16 @@
#include "mpp_mem.h" #include "mpp_mem.h"
#include "mpp_log.h" #include "mpp_log.h"
/*
* Linux only support MPP_BUFFER_TYPE_NORMAL so far
* we can support MPP_BUFFER_TYPE_V4L2 later
*/
typedef struct { typedef struct {
size_t alignment; size_t alignment;
MppBufferType type;
} allocator_impl; } allocator_impl;
int os_allocator_open(void **ctx, size_t alignment) MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type)
{ {
allocator_impl *p = NULL; allocator_impl *p = NULL;
@@ -35,6 +40,8 @@ int os_allocator_open(void **ctx, size_t alignment)
return MPP_ERR_NULL_PTR; return MPP_ERR_NULL_PTR;
} }
switch (type) {
case MPP_BUFFER_TYPE_NORMAL : {
p = mpp_malloc(allocator_impl, 1); p = mpp_malloc(allocator_impl, 1);
if (NULL == p) { if (NULL == p) {
*ctx = NULL; *ctx = NULL;
@@ -43,12 +50,19 @@ int os_allocator_open(void **ctx, size_t alignment)
} }
p->alignment = alignment; p->alignment = alignment;
} break;
default : {
mpp_err("os_allocator_open Window do not accept type %d\n");
} break;
}
*ctx = p; *ctx = p;
return MPP_OK; return MPP_OK;
} }
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size)
{ {
MPP_RET ret = MPP_OK;
allocator_impl *p = NULL; allocator_impl *p = NULL;
if (NULL == ctx) { if (NULL == ctx) {
@@ -57,20 +71,37 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size)
} }
p = (allocator_impl *)ctx; p = (allocator_impl *)ctx;
return os_malloc(&data->ptr, p->alignment, size); switch (p->type) {
case MPP_BUFFER_TYPE_NORMAL : {
ret = os_malloc(&data->ptr, p->alignment, size);
} break;
case MPP_BUFFER_TYPE_V4L2 : {
// TODO: support v4l2 vb2 buffer queue
mpp_err("os_allocator_alloc Linux MPP_BUFFER_TYPE_V4L2 will support later\n");
ret = MPP_ERR_UNKNOW;
} break;
default : {
mpp_err("os_allocator_alloc Linux do not accept type %d\n", p->type);
ret = MPP_ERR_UNKNOW;
} break;
}
return ret;
} }
void os_allocator_free(void *ctx, MppBufferData *data) MPP_RET os_allocator_free(void *ctx, MppBufferData *data)
{ {
(void) ctx; (void) ctx;
os_free(data->ptr); os_free(data->ptr);
return MPP_OK;
} }
void os_allocator_close(void *ctx) MPP_RET os_allocator_close(void *ctx)
{ {
if (ctx) if (ctx) {
mpp_free(ctx); mpp_free(ctx);
else return MPP_OK;
}
mpp_err("os_allocator_close Linux found NULL context input\n"); mpp_err("os_allocator_close Linux found NULL context input\n");
return MPP_NOK;
} }

View File

@@ -36,10 +36,10 @@ MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferData *data, size_t
MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator; MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator;
MPP_ALLOCATOR_LOCK(palloc); MPP_ALLOCATOR_LOCK(palloc);
int ret = os_allocator_alloc(palloc->allocator, data, size); MPP_RET ret = os_allocator_alloc(palloc->allocator, data, size);
MPP_ALLOCATOR_UNLOCK(palloc); MPP_ALLOCATOR_UNLOCK(palloc);
return (0 == ret) ? (MPP_OK) : (MPP_NOK); return ret;
} }
MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data) MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data)
@@ -58,11 +58,11 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data)
return MPP_OK; return MPP_OK;
} }
MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api) MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type)
{ {
if (NULL == allocator || NULL == api) { if (NULL == allocator || NULL == api || type >= MPP_BUFFER_TYPE_BUTT) {
mpp_err("mpp_alloctor_get invalid input: buffer %p api %p\n", mpp_err("mpp_alloctor_get invalid input: buffer %p api %p type %d\n",
allocator, api); allocator, api, type);
return MPP_ERR_UNKNOW; return MPP_ERR_UNKNOW;
} }
@@ -80,7 +80,7 @@ MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api)
} }
palloc->alignment = SZ_4K; palloc->alignment = SZ_4K;
os_allocator_open(&palloc->allocator, palloc->alignment); os_allocator_open(&palloc->allocator, palloc->alignment, type);
papi->size = sizeof(papi->size); papi->size = sizeof(papi->size);
papi->version = 1; papi->version = 1;

View File

@@ -18,9 +18,11 @@
#define __MPP_ALLOCATOR_IMPL_H__ #define __MPP_ALLOCATOR_IMPL_H__
#include "mpp_thread.h" #include "mpp_thread.h"
#include "mpp_buffer.h"
typedef struct { typedef struct {
pthread_mutex_t lock; pthread_mutex_t lock;
MppBufferType type;
size_t alignment; size_t alignment;
void *allocator; void *allocator;
void *api; void *api;

View File

@@ -23,10 +23,10 @@
extern "C" { extern "C" {
#endif #endif
int os_allocator_open(void **ctx, size_t alignment); MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type);
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size); MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size);
void os_allocator_free(void *ctx, MppBufferData *data); MPP_RET os_allocator_free(void *ctx, MppBufferData *data);
void os_allocator_close(void *ctx); MPP_RET os_allocator_close(void *ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -20,11 +20,15 @@
#include "mpp_mem.h" #include "mpp_mem.h"
#include "mpp_log.h" #include "mpp_log.h"
/*
* window only support MPP_BUFFER_TYPE_NORMAL
*/
typedef struct { typedef struct {
size_t alignment; size_t alignment;
} allocator_impl; } allocator_impl;
int os_allocator_open(void **ctx, size_t alignment) MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type)
{ {
allocator_impl *p = NULL; allocator_impl *p = NULL;
@@ -33,6 +37,8 @@ int os_allocator_open(void **ctx, size_t alignment)
return MPP_ERR_NULL_PTR; return MPP_ERR_NULL_PTR;
} }
switch (type) {
case MPP_BUFFER_TYPE_NORMAL : {
p = mpp_malloc(allocator_impl, 1); p = mpp_malloc(allocator_impl, 1);
if (NULL == p) { if (NULL == p) {
*ctx = NULL; *ctx = NULL;
@@ -41,11 +47,17 @@ int os_allocator_open(void **ctx, size_t alignment)
} }
p->alignment = alignment; p->alignment = alignment;
} break;
default : {
mpp_err("os_allocator_open Window do not accept type %d\n");
} break;
}
*ctx = p; *ctx = p;
return MPP_OK; return MPP_OK;
} }
int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size)
{ {
allocator_impl *p = NULL; allocator_impl *p = NULL;
@@ -55,20 +67,23 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size)
} }
p = (allocator_impl *)ctx; p = (allocator_impl *)ctx;
return os_malloc(&data->ptr, p->alignment, size); return (MPP_RET)os_malloc(&data->ptr, p->alignment, size);
} }
void os_allocator_free(void *ctx, MppBufferData *data) MPP_RET os_allocator_free(void *ctx, MppBufferData *data)
{ {
(void) ctx; (void) ctx;
os_free(data->ptr); os_free(data->ptr);
return MPP_OK;
} }
void os_allocator_close(void *ctx) MPP_RET os_allocator_close(void *ctx)
{ {
if (ctx) if (ctx) {
mpp_free(ctx); mpp_free(ctx);
else return MPP_OK;
}
mpp_err("os_allocator_close Window found NULL context input\n"); mpp_err("os_allocator_close Window found NULL context input\n");
return MPP_NOK;
} }