This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relate-to: I923216cf283e97eada4b600d5ebf8f5360c9e317 I3de75a1485c47fd54ea46da2f3079a6e5fae8084 I6e9296de9b22858f831ee503587fda51323ac902 Iead0efa96b95f026b6a5b20731c8069f688c9483 Ib4f04677cdc6951a0a9505efbd6ab47a7a79288b Ic88f9bae3137c439518901084d77285ace964d99 I6b6320e8a6444a43c6400527f2f982a39a3d9128 Ibaf2c7d0b9daad7248b2f226da164a7a301dc526 I4b820e50158e34a94d84195e4979ada6fefff190 I91c44d9e3e8af731175337bc80afa13a44222d6c I5c10141d76c1d2a1ce3efc656990a628dfd6a246 Change-Id: Ie30199897cd6fe64e2fd7a66126a3c87d25d9c1d Bug: https://jira.iotivity.org/browse/IOT-2453 Signed-off-by: Philippe Coval <philippe.coval@osg.samsung.com>
- Loading branch information
Showing
26 changed files
with
604 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/****************************************************************** | ||
* | ||
* Copyright 2018 Kistler Group All Rights Reserved. | ||
* | ||
* | ||
* | ||
* 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 OC_REFCOUNTER_H_ | ||
#define OC_REFCOUNTER_H_ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif // __cplusplus | ||
|
||
typedef struct oc_refcounter_t *oc_refcounter; | ||
|
||
/** | ||
* Desctructor of a data of a refcounter and it is called when a counter of thet refcounter reach 0. | ||
*/ | ||
typedef void (*oc_refcounter_dtor_data_func)(void* data); | ||
|
||
/** | ||
* Creates a new refcounter of a data. Ownership of the data is transferred to the refcounter. | ||
* For deallocates the data use oc_refcounter_dec, otherwise it can cause crash of the application. | ||
* | ||
* @param data - Data which will be owned by refcounter | ||
* @param dtor - Destructor of a data | ||
* | ||
* @return Poiner to newly created refcounter, NULL on allocation failure. | ||
*/ | ||
oc_refcounter oc_refcounter_create(void* data, oc_refcounter_dtor_data_func dtor); | ||
|
||
/** | ||
* Increments atommically reference count of a refcounter | ||
* | ||
* @param ref - A pointer to the refcounter | ||
* | ||
* @return returns the ref | ||
*/ | ||
oc_refcounter oc_refcounter_inc(oc_refcounter ref); | ||
|
||
/** | ||
* Decrements atommically a reference count of a refcounter. If the count of the refcounter | ||
* reach 0, it's call destructor of a data and the refcounter. | ||
* | ||
* @param ref - A pointer to the refcounter | ||
* | ||
* @return | ||
* on count == 0, a null pointer is returned | ||
* otherwise returns the refcounter | ||
*/ | ||
oc_refcounter oc_refcounter_dec(oc_refcounter ref); | ||
|
||
/** | ||
* Gets a current reference count of the refcount | ||
* | ||
* @param ref - A pointer to the refcounter | ||
* | ||
* @return returns a current reference count of the block | ||
*/ | ||
int32_t oc_refcounter_get_count(oc_refcounter ref); | ||
|
||
/** | ||
* Gets a data of the refcount | ||
* | ||
* @param ref - A pointer to the refcounter | ||
* | ||
* @return returns a data of the refcounter | ||
*/ | ||
void* oc_refcounter_get_data(oc_refcounter ref); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif // __cplusplus | ||
#endif // OC_REFCOUNTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/****************************************************************** | ||
* | ||
* Copyright 2018 Kistler Group All Rights Reserved. | ||
* | ||
* | ||
* | ||
* 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 "oc_refcounter.h" | ||
|
||
#include "oic_malloc.h" | ||
#include "ocatomic.h" | ||
#include "iotivity_debug.h" | ||
|
||
// Enable extra debug logging for malloc. Comment out to disable | ||
#ifdef ENABLE_REFCOUNTER_DEBUG | ||
#include "experimental/logger.h" | ||
#define TAG "OC_REFCOUNT" | ||
#endif | ||
|
||
typedef struct oc_refcounter_t | ||
{ | ||
/* count of the references*/ | ||
int32_t count; | ||
/* data owned by refcount */ | ||
void* data; | ||
/* data destructor*/ | ||
oc_refcounter_dtor_data_func dtor; | ||
} oc_refcounter_t; | ||
|
||
oc_refcounter oc_refcounter_create(void* data, oc_refcounter_dtor_data_func dtor) | ||
{ | ||
oc_refcounter ref = (oc_refcounter)OICMalloc(sizeof(*ref)); | ||
if (ref == NULL) | ||
{ | ||
return ref; | ||
} | ||
ref->count = 1; | ||
#ifdef ENABLE_REFCOUNTER_DEBUG | ||
OIC_LOG_V(INFO, TAG, "oc_refcounter_create: ref=%p count=%d", ref, 1); | ||
#endif | ||
ref->data = data; | ||
ref->dtor = dtor; | ||
|
||
return ref; | ||
} | ||
|
||
oc_refcounter oc_refcounter_inc(oc_refcounter ref) | ||
{ | ||
if (ref == NULL) | ||
{ | ||
return NULL; | ||
} | ||
OC_VERIFY(ref->count > 0); | ||
int32_t count = oc_atomic_increment(&ref->count); | ||
#ifdef ENABLE_REFCOUNTER_DEBUG | ||
OIC_LOG_V(INFO, TAG, "oc_refcounter_inc: ref=%p count=%d", ref, count); | ||
#else | ||
(void) count; | ||
#endif | ||
return ref; | ||
} | ||
|
||
oc_refcounter oc_refcounter_dec(oc_refcounter ref) | ||
{ | ||
if (ref == NULL) | ||
{ | ||
return NULL; | ||
} | ||
OC_VERIFY(ref->count > 0); | ||
int32_t count = oc_atomic_decrement(&ref->count); | ||
#ifdef ENABLE_REFCOUNTER_DEBUG | ||
OIC_LOG_V(INFO, TAG, "oc_refcounter_dec: ref=%p count=%d", ref, count); | ||
#endif | ||
if (count == 0) | ||
{ | ||
if (ref->dtor) | ||
{ | ||
ref->dtor(ref->data); | ||
} | ||
OICFree(ref); | ||
return NULL; | ||
} | ||
return ref; | ||
} | ||
|
||
int32_t oc_refcounter_get_count(oc_refcounter ref) | ||
{ | ||
if (ref == NULL) | ||
{ | ||
return -1; | ||
} | ||
OC_VERIFY(ref->count > 0); | ||
return ref->count; | ||
} | ||
|
||
void* oc_refcounter_get_data(oc_refcounter ref) | ||
{ | ||
if (ref == NULL) | ||
{ | ||
return NULL; | ||
} | ||
OC_VERIFY(ref->count > 0); | ||
return ref->data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#****************************************************************** | ||
# | ||
# Copyright 2018 Kistler Group All Rights Reserved. | ||
# | ||
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | ||
# | ||
# 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. | ||
# | ||
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | ||
|
||
import os | ||
import os.path | ||
from tools.scons.RunTest import run_test | ||
|
||
Import('test_env') | ||
|
||
# SConscript file for Local PKI google tests | ||
refcountertest_env = test_env.Clone() | ||
target_os = refcountertest_env.get('TARGET_OS') | ||
|
||
###################################################################### | ||
# Build flags | ||
###################################################################### | ||
refcountertest_env.PrependUnique(CPPPATH=['../include']) | ||
|
||
refcountertest_env.AppendUnique(LIBPATH=[ | ||
os.path.join(refcountertest_env.get('BUILD_DIR'), 'resource', 'c_common') | ||
]) | ||
refcountertest_env.PrependUnique(LIBS=['c_common']) | ||
|
||
if refcountertest_env.get('LOGGING'): | ||
refcountertest_env.AppendUnique(CPPDEFINES=['TB_LOG']) | ||
# | ||
###################################################################### | ||
# Source files and Targets | ||
###################################################################### | ||
refcountertests = refcountertest_env.Program('refcountertests', | ||
['linux/oc_refcounter_tests.cpp']) | ||
|
||
Alias("test", [refcountertests]) | ||
|
||
refcountertest_env.AppendTarget('test') | ||
if refcountertest_env.get('TEST') == '1': | ||
if target_os in ['linux', 'windows']: | ||
run_test(refcountertest_env, 'resource_ccommon_refcounter_test.memcheck', | ||
'resource/c_common/oc_refcounter/test/refcountertests') |
Oops, something went wrong.