mirror of
https://github.com/nyanmisaka/mpp.git
synced 2025-10-06 01:26:49 +08:00
[rk_malloc]: add malloc function and test case on window
unit test passed git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@27 6e48237b-75ef-9749-8fc9-41990f28c85a
This commit is contained in:
@@ -51,4 +51,12 @@ typedef signed long long int RK_S64;
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#ifdef __cplusplus
|
||||
#define NULL 0
|
||||
#else
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /*__RK_TYPE_H__*/
|
||||
|
@@ -9,21 +9,14 @@ else()
|
||||
message(SEND_ERROR "Can not found platform definistion ${CMAKE_SYSTEM}")
|
||||
endif()
|
||||
|
||||
set(OSAL_HDR
|
||||
inc/rk_list.h
|
||||
inc/rk_thread.h
|
||||
inc/rk_log.h
|
||||
os_log.h
|
||||
)
|
||||
|
||||
set(OSAL_SRC
|
||||
rk_list.cpp
|
||||
rk_log.cpp
|
||||
${OS_DIR}/os_log.c
|
||||
)
|
||||
include_directories(.)
|
||||
|
||||
add_library(osal STATIC
|
||||
${OSAL_SRC} ${OSAL_HDR}
|
||||
rk_malloc.cpp
|
||||
rk_list.cpp
|
||||
rk_log.cpp
|
||||
${OS_DIR}/os_malloc.c
|
||||
${OS_DIR}/os_log.c
|
||||
)
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
36
osal/inc/rk_malloc.h
Normal file
36
osal/inc/rk_malloc.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 __RK_MALLOC_H__
|
||||
#define __RK_MALLOC_H__
|
||||
|
||||
#define rk_malloc(type, count) (type*)rk_mpp_malloc(sizeof(type) * (count))
|
||||
#define rk_free(ptr) rk_mpp_free(ptr)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void rk_mpp_set_memalign(size_t size);
|
||||
void *rk_mpp_malloc(size_t size);
|
||||
void rk_mpp_free(void *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__RK_MPP_MALLOC_H__*/
|
||||
|
33
osal/os_malloc.h
Normal file
33
osal/os_malloc.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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_MALLOC_H__
|
||||
#define __OS_MALLOC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int os_malloc(void **memptr, size_t alignment, size_t size);
|
||||
void os_free(void *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__OS_MALLOC_H__*/
|
||||
|
||||
|
48
osal/rk_malloc.cpp
Normal file
48
osal/rk_malloc.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 "rk_type.h"
|
||||
#include "rk_log.h"
|
||||
#include "rk_malloc.h"
|
||||
|
||||
#include "os_malloc.h"
|
||||
|
||||
// default memory align size is set to 64
|
||||
static size_t rk_mpp_memalign = 64;
|
||||
|
||||
void rk_mpp_set_memalign(size_t size)
|
||||
{
|
||||
/* determine align size is power of 2 */
|
||||
if (size && !(size & (size - 1)))
|
||||
rk_mpp_memalign = size;
|
||||
else
|
||||
rk_log("set memalign to %d failed\n", size);
|
||||
}
|
||||
|
||||
void *rk_mpp_malloc(size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
if (0 == os_malloc(&ptr, rk_mpp_memalign, size))
|
||||
return ptr;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rk_mpp_free(void *ptr)
|
||||
{
|
||||
os_free(ptr);
|
||||
}
|
||||
|
@@ -9,6 +9,13 @@ if(OSAL_LOG_TEST)
|
||||
target_link_libraries(rk_log_test osal)
|
||||
endif()
|
||||
|
||||
# malloc system unit test
|
||||
option(OSAL_MALLOC_TEST "Build osal rk_malloc unit test" ON)
|
||||
if(OSAL_MALLOC_TEST)
|
||||
add_executable(rk_malloc_test rk_malloc_test.c)
|
||||
target_link_libraries(rk_malloc_test osal)
|
||||
endif()
|
||||
|
||||
# thread implement unit test
|
||||
option(OSAL_THREAD_TEST "Build osal rk_thread unit test" ON)
|
||||
if(OSAL_THREAD_TEST)
|
||||
|
37
osal/test/rk_malloc_test.c
Normal file
37
osal/test/rk_malloc_test.c
Normal 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.
|
||||
*/
|
||||
|
||||
#define LOG_TAG "rk_malloc_test"
|
||||
#include "rk_log.h"
|
||||
#include "rk_malloc.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
void *tmp = NULL;
|
||||
|
||||
rk_log("try memalign of 512\n");
|
||||
rk_mpp_set_memalign(512);
|
||||
|
||||
tmp = rk_malloc(int, 100);
|
||||
if (tmp) {
|
||||
rk_log("malloc success\n");
|
||||
rk_free(tmp);
|
||||
} else {
|
||||
rk_log("malloc failed\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
30
osal/window/os_malloc.c
Normal file
30
osal/window/os_malloc.c
Normal 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.
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include "os_malloc.h"
|
||||
|
||||
int os_malloc(void **memptr, size_t alignment, size_t size)
|
||||
{
|
||||
*memptr = _aligned_malloc(size, alignment);
|
||||
return (*memptr)?(0):(-1);
|
||||
}
|
||||
|
||||
void os_free(void *ptr)
|
||||
{
|
||||
_aligned_free(ptr);
|
||||
}
|
||||
|
Reference in New Issue
Block a user