From 55a2a3e4f5f73269e139522ef7c83d196c233b73 Mon Sep 17 00:00:00 2001 From: Doug Hudson Date: Wed, 24 Sep 2014 17:36:57 -0400 Subject: [PATCH] 1. Create csdk/ocmalloc directory 2. Create ocmalloc.c and ocmalloc.h 3. Remove OCMalloc and OCFree functions from ocstack.c and ocstack.h 4. Modify stack source modules to include ocmalloc.h 5. Modify stack makefiles to use ocmalloc.c and ocmalloc.h 6. Add doxygen function comments to ocmalloc.h 7. Remove #include from within #ifdef __cplusplus block 8. Add comment to ocmalloc.h describing the intention of the module. Change-Id: I453d4ced530d7539f8b00b22b3f651327ea6a42c --- csdk/libcoap-4.1.1/makefile | 7 +- csdk/makefile | 4 + csdk/occoap/src/occoap.c | 1 + csdk/occoap/src/occoaphelper.c | 1 + csdk/ocmalloc/include/ocmalloc.h | 87 +++++++++++++++++++++ csdk/ocmalloc/src/ocmalloc.c | 68 +++++++++++++++++ csdk/ocmalloc/test/linux/README | 22 ++++++ csdk/ocmalloc/test/linux/makefile | 106 ++++++++++++++++++++++++++ csdk/ocmalloc/test/linux/unittest.cpp | 84 ++++++++++++++++++++ csdk/stack/include/internal/ocstackinternal.h | 4 - csdk/stack/src/occlientcb.c | 1 + csdk/stack/src/ocobserve.c | 1 + csdk/stack/src/ocstack.c | 11 +-- 13 files changed, 380 insertions(+), 17 deletions(-) create mode 100644 csdk/ocmalloc/include/ocmalloc.h create mode 100644 csdk/ocmalloc/src/ocmalloc.c create mode 100644 csdk/ocmalloc/test/linux/README create mode 100644 csdk/ocmalloc/test/linux/makefile create mode 100644 csdk/ocmalloc/test/linux/unittest.cpp diff --git a/csdk/libcoap-4.1.1/makefile b/csdk/libcoap-4.1.1/makefile index ac35199..2d0b938 100644 --- a/csdk/libcoap-4.1.1/makefile +++ b/csdk/libcoap-4.1.1/makefile @@ -36,7 +36,8 @@ OCSOCK_DIR = $(ROOT_DIR)/ocsocket LOGGER_DIR = $(ROOT_DIR)/logger RANDOM_DIR = $(ROOT_DIR)/ocrandom STACK_DIR = $(ROOT_DIR)/stack -INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(RANDOM_DIR)/include +OCMALLOC_DIR = $(ROOT_DIR)/ocmalloc +INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(RANDOM_DIR)/include -I$(OCMALLOC_DIR)/include # Note for Arduino: The CC flag is set to the C++ compiler since Arduino build # includes Time.h header file which has C++ style definitions. @@ -70,8 +71,8 @@ CC_FLAGS.debug := -O0 -g3 -Wall -ffunction-sections -fdata-sections -fno-excepti CC_FLAGS.release := -Os -Wall -ffunction-sections -fdata-sections -fno-exceptions SOURCES:= pdu.c net.c debug.c encode.c uri.c coap_list.c resource.c hashkey.c \ - str.c option.c async.c subscribe.c block.c logger.c ocrandom.c -VPATH := $(OCSOCK_DIR)/src:$(LOGGER_DIR)/src:$(RANDOM_DIR)/src + str.c option.c async.c subscribe.c block.c logger.c ocrandom.c ocmalloc.c +VPATH := $(OCSOCK_DIR)/src:$(LOGGER_DIR)/src:$(RANDOM_DIR)/src:$(OCMALLOC_DIR)/src ifeq (arduino, $(findstring arduino,$(PLATFORM))) ifeq ($(ARDUINOWIFI),1) SOURCES += ocsocket_arduino_wifi.c diff --git a/csdk/makefile b/csdk/makefile index 185121e..1565a20 100644 --- a/csdk/makefile +++ b/csdk/makefile @@ -67,11 +67,13 @@ OCSOCKET_DIR = $(ROOT_DIR)/ocsocket LCOAP_DIR = $(ROOT_DIR)/libcoap-4.1.1 OCCOAP_DIR = $(ROOT_DIR)/occoap OCTBSTACK_DIR = $(ROOT_DIR)/stack +OCMALLOC_DIR = $(ROOT_DIR)/ocmalloc UTILS_DIR = $(ROOT_DIR)/../../oic-utilities/tb CJSON_DIR = $(UTILS_DIR)/cJSON OCCOAP_SRC = $(OCCOAP_DIR)/src OCTBSTACK_SRC = $(OCTBSTACK_DIR)/src +OCMALLOC_SRC = $(OCMALLOC_DIR)/src CJSON_SRC = $(CJSON_DIR) OCLOGGER_INC = $(OCLOGGER_DIR)/include @@ -80,6 +82,7 @@ OCSOCKET_INC = $(OCSOCKET_DIR)/include LCOAP_INC = $(LCOAP_DIR) OCCOAP_INC = $(OCCOAP_DIR)/include OCTBSTACK_INC = $(OCTBSTACK_DIR)/include +OCMALLOC_INC = $(OCMALLOC_DIR)/include CJSON_INC = $(CJSON_DIR) INC_DIRS := -I$(OCLOGGER_INC) @@ -87,6 +90,7 @@ INC_DIRS += -I$(OCRANDOM_INC) INC_DIRS += -I$(OCSOCKET_INC) INC_DIRS += -I$(LCOAP_INC) INC_DIRS += -I$(OCCOAP_INC) +INC_DIRS += -I$(OCMALLOC_INC) INC_DIRS += -I$(OCTBSTACK_INC) INC_DIRS += -I$(OCTBSTACK_INC)/internal INC_DIRS += -I$(CJSON_INC) diff --git a/csdk/occoap/src/occoap.c b/csdk/occoap/src/occoap.c index fd531d9..b0c9144 100644 --- a/csdk/occoap/src/occoap.c +++ b/csdk/occoap/src/occoap.c @@ -27,6 +27,7 @@ #include "occlientcb.h" #include "ocobserve.h" #include "logger.h" +#include "ocmalloc.h" #include #ifndef WITH_ARDUINO diff --git a/csdk/occoap/src/occoaphelper.c b/csdk/occoap/src/occoaphelper.c index a173fe5..9e4d07e 100644 --- a/csdk/occoap/src/occoaphelper.c +++ b/csdk/occoap/src/occoaphelper.c @@ -26,6 +26,7 @@ #include "logger.h" #include "ocobserve.h" #include "coap_time.h" +#include "ocmalloc.h" //----------------------------------------------------------------------------- // Macros diff --git a/csdk/ocmalloc/include/ocmalloc.h b/csdk/ocmalloc/include/ocmalloc.h new file mode 100644 index 0000000..66291d0 --- /dev/null +++ b/csdk/ocmalloc/include/ocmalloc.h @@ -0,0 +1,87 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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 OCMALLOC_H_ +#define OCMALLOC_H_ + +// The purpose of this module is to allow custom dynamic memory allocation +// code to easily be added to the TB Stack by redefining the OCMalloc and +// OCFree functions. Examples of when this might be needed are on TB +// platforms that do not support dynamic allocation or if a memory pool +// needs to be added. +// +// Note that these functions are intended to be used ONLY within the TB +// stack and NOT by the application code. The application should be +// responsible for its own dynamic allocation. + +//----------------------------------------------------------------------------- +// Includes +//----------------------------------------------------------------------------- +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +//----------------------------------------------------------------------------- +// Defines +//----------------------------------------------------------------------------- + + +//----------------------------------------------------------------------------- +// Typedefs +//----------------------------------------------------------------------------- + + +//----------------------------------------------------------------------------- +// Function prototypes +//----------------------------------------------------------------------------- + +/** + * Allocates a block of size bytes, returning a pointer to the beginning of + * the allocated block. + * + * NOTE: This function is intended to be used internally by the TB Stack. + * It is not intended to be used by applications. + * + * @param size - Size of the memory block in bytes, where size > 0 + * + * @return + * on success, a pointer to the allocated memory block + * on failure, a null pointer is returned + */ +void *OCMalloc(size_t size); + +/** + * Deallocate a block of memory previously allocated by a call to OCMalloc + * + * NOTE: This function is intended to be used internally by the TB Stack. + * It is not intended to be used by applications. + * + * @param ptr - Pointer to block of memory previously allocated by OCMalloc. + * If ptr is a null pointer, the function does nothing. + */ +void OCFree(void *ptr); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif /* OCMALLOC_H_ */ diff --git a/csdk/ocmalloc/src/ocmalloc.c b/csdk/ocmalloc/src/ocmalloc.c new file mode 100644 index 0000000..4966927 --- /dev/null +++ b/csdk/ocmalloc/src/ocmalloc.c @@ -0,0 +1,68 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + +//----------------------------------------------------------------------------- +// Includes +//----------------------------------------------------------------------------- +#include +#include "ocmalloc.h" + +//----------------------------------------------------------------------------- +// Typedefs +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// Private variables +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// Macros +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// Internal API function +//----------------------------------------------------------------------------- + + +//----------------------------------------------------------------------------- +// Private internal function prototypes +//----------------------------------------------------------------------------- + + +//----------------------------------------------------------------------------- +// Public APIs +//----------------------------------------------------------------------------- + +void *OCMalloc(size_t size) +{ + if (0 == size) + { + return NULL; + } + + return malloc(size); +} + +void OCFree(void *ptr) +{ + free(ptr); +} + diff --git a/csdk/ocmalloc/test/linux/README b/csdk/ocmalloc/test/linux/README new file mode 100644 index 0000000..4b28837 --- /dev/null +++ b/csdk/ocmalloc/test/linux/README @@ -0,0 +1,22 @@ +# To build the ocmalloc google unit test for Linux: + +# First +cd /csdk +make deepclean + +make BUILD=release +# or +make BUILD=debug + +# Next +cd /csdk/ocmalloc/test/linux + +make BUILD=release +# or +make BUILD=debug + +# Run the test test + +/csdk/ocmalloc/test/linux/release/unittest +# or +/csdk/ocmalloc/test/linux/debug/unittest diff --git a/csdk/ocmalloc/test/linux/makefile b/csdk/ocmalloc/test/linux/makefile new file mode 100644 index 0000000..7a39d6c --- /dev/null +++ b/csdk/ocmalloc/test/linux/makefile @@ -0,0 +1,106 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +# override with `make BUILD=debug` +# default to release build +BUILD := release +PLATFORM := linux +CC := g++ + +ifeq ($(ROOT_DIR), ) +ROOT_DIR = $(PWD)/../../.. +endif + +# You must create the file "local.properties" on your local machine which contains any local paths, etc +# local.properties should NOT be committed to repo +include $(ROOT_DIR)/local.properties +# GTEST_DIR contains the path to Google Test libs and must be defined in local.properties +# Example: +# GTEST_DIR := /home/johndoe/utils/gtest-1.7.0 + +# NOTE: to run app, make sure that LD_LIBRARY_PATH env variable +# contains $(GTEST_DIR)/lib/.libs + +OUT_DIR := $(PWD)/$(BUILD) +OBJ_DIR := $(OUT_DIR)/obj + + +OUT_DIR := $(PWD) + +OCLOGGER_DIR = $(ROOT_DIR)/logger +OCMALLOC_DIR = $(ROOT_DIR)/ocmalloc +OCRANDOM_DIR = $(ROOT_DIR)/ocrandom +OCSOCKET_DIR = $(ROOT_DIR)/ocsocket +LCOAP_DIR = $(ROOT_DIR)/libcoap-4.1.1 +OCCOAP_DIR = $(ROOT_DIR)/occoap +OCTBSTACK_DIR = $(ROOT_DIR)/stack + +OCLOGGER_INC = $(OCLOGGER_DIR)/include +OCMALLOC_INC = $(OCMALLOC_DIR)/include +OCRANDOM_INC = $(OCRANDOM_DIR)/include +OCSOCKET_INC = $(OCSOCKET_DIR)/include +LCOAP_INC = $(LCOAP_DIR) +OCCOAP_INC = $(OCCOAP_DIR)/include +OCTBSTACK_INC = $(OCTBSTACK_DIR)/include + +INC_DIRS := -I$(OCLOGGER_INC) +INC_DIRS += -I$(OCMALLOC_INC) +INC_DIRS += -I$(OCRANDOM_INC) +INC_DIRS += -I$(OCSOCKET_INC) +INC_DIRS += -I$(LCOAP_INC) +INC_DIRS += -I$(OCCOAP_INC) +INC_DIRS += -I$(OCTBSTACK_INC) +INC_DIRS += -I$(OCTBSTACK_INC)/internal +INC_DIRS += -I$(GTEST_DIR)/include + +CC_FLAGS.debug := -g -O0 -g3 -Wall -ffunction-sections -fdata-sections -fno-exceptions \ + -std=c++0x -pedantic $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD) -DTB_LOG +CC_FLAGS.release := -Os -Wall -fdata-sections -Wl,--gc-sections -Wl,-s -fno-exceptions \ + -std=c++0x $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD) + +LDLIBS += -loctbstack -lgtest -lgtest_main -lpthread +CPPFLAGS += $(CC_FLAGS.$(BUILD)) $(LDLIBS) -L$(GTEST_DIR)/lib/.libs + +SOURCES := unittest.cpp + +OBJECTS:= $(patsubst %.c, $(OBJ_DIR)/%.o, $(SOURCES)) + +PROGRAMS := unittest + +all: prep_dirs $(OBJECTS) $(PROGRAMS) + +prep_dirs: + -mkdir -p $(OUT_DIR) + -mkdir -p $(OBJ_DIR) + +$(OBJ_DIR)/%.o: %.cpp + $(CC) -c $(CPPFLAGS) $< -o $@ + +unittest: $(OBJ_DIR)/unittest.o + $(CC) $^ $(CPPFLAGS) -o $(OUT_DIR)/$(BUILD)/$@ + +.PHONY: clean + +clean: legacy_clean + -rm -rf release + -rm -rf debug + +legacy_clean: + rm -f *.o $(PROGRAMS) diff --git a/csdk/ocmalloc/test/linux/unittest.cpp b/csdk/ocmalloc/test/linux/unittest.cpp new file mode 100644 index 0000000..c485b26 --- /dev/null +++ b/csdk/ocmalloc/test/linux/unittest.cpp @@ -0,0 +1,84 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + + +extern "C" { + #include "ocmalloc.h" +} + +#include "gtest/gtest.h" +#include +#include +#include +#include +#include +#include +#include + +//----------------------------------------------------------------------------- +// Includes +//----------------------------------------------------------------------------- +#include +#include + +#include +#include +using namespace std; + +//----------------------------------------------------------------------------- +// Private variables +//----------------------------------------------------------------------------- +static uint8_t *pBuffer; + +//----------------------------------------------------------------------------- +// Tests +//----------------------------------------------------------------------------- + +TEST(OCMalloc, MallocPass1) +{ + // Try to allocate a small buffer + pBuffer = (uint8_t *)OCMalloc(1); + EXPECT_TRUE(pBuffer); + OCFree(pBuffer); +} + +TEST(OCMalloc, MallocPass2) +{ + // Try to allocate a small buffer + pBuffer = (uint8_t *)OCMalloc(128); + EXPECT_TRUE(pBuffer); + OCFree(pBuffer); +} + +TEST(OCMalloc, MallocFail1) +{ + // Try to allocate a buffer of size 0 + pBuffer = (uint8_t *)OCMalloc(0); + EXPECT_TRUE(NULL == pBuffer); + OCFree(pBuffer); +} + +TEST(OCMalloc, MallocFail2) +{ + // Try to allocate a ridiculous amount of RAM + pBuffer = (uint8_t *)OCMalloc((size_t)0x7FFFFFFFFFFFFFFF); + EXPECT_TRUE(NULL == pBuffer); + OCFree(pBuffer); +} diff --git a/csdk/stack/include/internal/ocstackinternal.h b/csdk/stack/include/internal/ocstackinternal.h index b9fc8a3..35d804e 100644 --- a/csdk/stack/include/internal/ocstackinternal.h +++ b/csdk/stack/include/internal/ocstackinternal.h @@ -184,10 +184,6 @@ OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty, OCResourceProperty resourceProperties, uint8_t enable); #endif -// TODO: ultimately OCMalloc and OCFree should be defined in a different module -void OCFree(void *ptr); -void *OCMalloc(size_t size); - #ifdef __cplusplus } #endif // __cplusplus diff --git a/csdk/stack/src/occlientcb.c b/csdk/stack/src/occlientcb.c index 092d464..6ff58a8 100644 --- a/csdk/stack/src/occlientcb.c +++ b/csdk/stack/src/occlientcb.c @@ -23,6 +23,7 @@ #include "occoap.h" #include "utlist.h" #include "logger.h" +#include "ocmalloc.h" #include /// Module Name diff --git a/csdk/stack/src/ocobserve.c b/csdk/stack/src/ocobserve.c index 41b4bc0..294c8ed 100644 --- a/csdk/stack/src/ocobserve.c +++ b/csdk/stack/src/ocobserve.c @@ -28,6 +28,7 @@ #include "utlist.h" #include "debug.h" #include "ocrandom.h" +#include "ocmalloc.h" // Module Name #define MOD_NAME PCF("ocobserve") diff --git a/csdk/stack/src/ocstack.c b/csdk/stack/src/ocstack.c index 99111ef..3f50e3b 100644 --- a/csdk/stack/src/ocstack.c +++ b/csdk/stack/src/ocstack.c @@ -30,6 +30,7 @@ #include "ocrandom.h" #include "debug.h" #include "occoap.h" +#include "ocmalloc.h" //----------------------------------------------------------------------------- // Typedefs @@ -1838,14 +1839,4 @@ OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle, return pointer; } -// TODO: Remove these! Should be in an ocmalloc module -void *OCMalloc(size_t size) { - TODO ("This should be moved into an ocmalloc dir and implemented as a separate OC module"); - return malloc(size); -} - -void OCFree(void *ptr) { - TODO ("This should be moved into an ocmalloc dir and implemented as a separate OC module"); - free(ptr); -} -- 2.7.4