diff --git a/core/host/src/hdf_device_object.c b/core/host/src/hdf_device_object.c index 1c77379c..c44d4ee3 100644 --- a/core/host/src/hdf_device_object.c +++ b/core/host/src/hdf_device_object.c @@ -157,7 +157,7 @@ void HdfPmSetMode(struct HdfDeviceObject *deviceObject, uint32_t mode) bool HdfDeviceSetClass(struct HdfDeviceObject *deviceObject, DeviceClass deviceClass) { - if ((deviceObject == NULL) || (deviceClass >= DEVICE_CLASS_MAX) || (deviceClass >= DEVICE_CLASS_MAX)) { + if ((deviceObject == NULL) || (deviceClass >= DEVICE_CLASS_MAX)) { return false; } deviceObject->deviceClass = deviceClass; diff --git a/tools/hc-gen/src/ast.cpp b/tools/hc-gen/src/ast.cpp index 1caec38d..00e99ac9 100644 --- a/tools/hc-gen/src/ast.cpp +++ b/tools/hc-gen/src/ast.cpp @@ -884,11 +884,11 @@ std::shared_ptr AstObjectFactory::Build(std::shared_ptr ob { switch (object->Type()) { case PARSEROP_CONFNODE: - return std::shared_ptr(new ConfigNode(*ConfigNode::CastFrom(object))); + return std::make_shared(*ConfigNode::CastFrom(object)); case PARSEROP_CONFTERM: - return std::shared_ptr(new ConfigTerm(*ConfigTerm::CastFrom(object))); + return std::make_shared(*ConfigTerm::CastFrom(object)); case PARSEROP_ARRAY: - return std::shared_ptr(new ConfigArray(*static_cast(object.get()))); + return std::make_shared(*ConfigArray::CastFrom(object)); default: return std::make_shared(*object); } diff --git a/tools/hc-gen/src/parser.cpp b/tools/hc-gen/src/parser.cpp index df6ff84b..099888f7 100644 --- a/tools/hc-gen/src/parser.cpp +++ b/tools/hc-gen/src/parser.cpp @@ -88,7 +88,7 @@ std::shared_ptr Parser::ParseOne(const std::string &src, std::list oneAst = std::make_shared(rootNode); + auto oneAst = std::make_shared(rootNode); oneAst->Dump(*lexer_.GetSourceName()); return oneAst; @@ -145,7 +145,7 @@ std::shared_ptr Parser::ParseNode(Token &name, bool bracesStart) } } - auto node = std::shared_ptr(new ConfigNode(name, NODE_NOREF, "")); + auto node = std::make_shared(name, NODE_NOREF, ""); std::shared_ptr child; while (lexer_.Lex(current_) && current_ != '}') { switch (current_.type) { @@ -171,7 +171,7 @@ std::shared_ptr Parser::ParseNode(Token &name, bool bracesStart) Logger().Error() << lexer_ << "syntax error, node miss '}'"; return nullptr; } - return std::shared_ptr(node); + return node; } std::shared_ptr Parser::ParseTerm(Token &name) @@ -180,10 +180,7 @@ std::shared_ptr Parser::ParseTerm(Token &name) Logger().Error() << lexer_ << "syntax error, miss value of config term"; return nullptr; } - auto term = std::shared_ptr(new (std::nothrow) ConfigTerm(name, nullptr)); - if (term == nullptr) { - return nullptr; - } + auto term = std::make_shared(name, nullptr); switch (current_.type) { case STRING: term->AddChild(std::make_shared("", PARSEROP_STRING, current_.strval, current_)); @@ -220,7 +217,7 @@ std::shared_ptr Parser::ParseTerm(Token &name) return nullptr; } - return std::shared_ptr(term); + return term; } std::shared_ptr Parser::ParseTemplate() @@ -362,7 +359,7 @@ std::shared_ptr Parser::ParseNodeInherit(Token &name) std::shared_ptr Parser::ParseArray() { - auto array = std::shared_ptr(new ConfigArray(current_)); + auto array = std::make_shared(current_); int32_t arrayType = 0; while (lexer_.Lex(current_) && current_ != ']') { @@ -400,7 +397,7 @@ std::shared_ptr Parser::ParseArray() return nullptr; } - return std::shared_ptr(array); + return array; } std::shared_ptr Parser::GetAst() diff --git a/tools/hdi-gen/ast/ast.cpp b/tools/hdi-gen/ast/ast.cpp index 36e934e9..00a976ba 100755 --- a/tools/hdi-gen/ast/ast.cpp +++ b/tools/hdi-gen/ast/ast.cpp @@ -38,8 +38,7 @@ void AST::SetIdlFile(const String& idlFile) #else int index = idlFilePath_.LastIndexOf('/'); #endif - int end = (idlFilePath_.LastIndexOf(".idl") == -1) ? - (idlFilePath_.LastIndexOf(".idl")) : (idlFilePath_.LastIndexOf(".idl")); + int end = idlFilePath_.LastIndexOf(".idl"); name_ = idlFilePath_.Substring((index == -1) ? 0 : (index + 1), end); } diff --git a/tools/hdi-gen/parser/file_detail.cpp b/tools/hdi-gen/parser/file_detail.cpp index 70c499bd..2b836c5e 100755 --- a/tools/hdi-gen/parser/file_detail.cpp +++ b/tools/hdi-gen/parser/file_detail.cpp @@ -20,8 +20,7 @@ void FileDetail::SetFilePath(const String& filePath) #else int index = idlFilePath_.LastIndexOf('/'); #endif - int end = ((idlFilePath_.LastIndexOf(".idl") == -1) ? - idlFilePath_.LastIndexOf(".idl") : idlFilePath_.LastIndexOf(".idl")); + int end = idlFilePath_.LastIndexOf(".idl"); idlName_ = idlFilePath_.Substring((index == -1) ? 0 : (index + 1), end); } diff --git a/tools/leagecy/hc-gen/src/hcs_middle.c b/tools/leagecy/hc-gen/src/hcs_middle.c index b281764d..aa239b88 100644 --- a/tools/leagecy/hc-gen/src/hcs_middle.c +++ b/tools/leagecy/hc-gen/src/hcs_middle.c @@ -25,7 +25,8 @@ static ParserObject *HcsLookupAstObject(const ParserObject *current, const char HCS_ERROR("oom"); return NULL; } - char *nodeName = strtok(splitPath, "."); + char *buf = NULL; + char *nodeName = strtok_s(splitPath, ".", &buf); /* path must start with "root" */ if (nodeName == NULL || strcmp(nodeName, "root") != 0) { @@ -34,14 +35,14 @@ static ParserObject *HcsLookupAstObject(const ParserObject *current, const char } /* skip root in path */ - nodeName = strtok(NULL, "."); + nodeName = strtok_s(NULL, ".", &buf); ParserObject *object = HcsGetParserRoot(); while (nodeName != NULL) { object = HcsAstLookupObjectInChildren(object, nodeName); if (object == NULL) { break; } - nodeName = strtok(NULL, "."); + nodeName = strtok_s(NULL, ".", &buf); } HcsMemFree(splitPath); diff --git a/utils/include/hdf_object_alloc.h b/utils/include/hdf_object_alloc.h deleted file mode 100644 index 6c17f661..00000000 --- a/utils/include/hdf_object_alloc.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2021-2021 Huawei Device Co., Ltd. - * - * HDF is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * See the LICENSE file in the root of this repository for complete details. - */ - -#ifndef OBJECT_ALLOC_H -#define OBJECT_ALLOC_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct HdfObjectChunkConfig { - uint32_t chunkSize; - uint32_t chunkCount; -}; - -struct HdfObjectPoolConfig { - char *buffer; - uint32_t bufferSize; - uint32_t numChunks; - const struct HdfObjectChunkConfig *chunks; -}; - -void *HdfObjectAllocAlloc(size_t size); - -void HdfObjectAllocFree(void *object); - -const struct HdfObjectPoolConfig *ObjectAllocGetConfig(void); -void HdfObjectAllocInit(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* BLOCK_BUFFER_H */ - diff --git a/utils/src/hdf_object_alloc.c b/utils/src/hdf_object_alloc.c deleted file mode 100644 index b84f549f..00000000 --- a/utils/src/hdf_object_alloc.c +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. - * - * HDF is dual licensed: you can use it either under the terms of - * the GPL, or the BSD license, at your option. - * See the LICENSE file in the root of this repository for complete details. - */ - -#include "hdf_object_alloc.h" -#include "hdf_slist.h" -#include "osal_mutex.h" - -struct HdfChunkLink { - uint32_t buffSize; - uint8_t *buffer; -}; - -struct HdfObjectNode { - struct HdfSListNode entry; - uint32_t chunkCount; - uint32_t freeCount; - uint32_t chunkSize; - struct HdfChunkLink **chunkStack; -}; - -struct HdfObjectAlloc { - struct HdfSList nodes; - struct OsalMutex mutex; - bool isConstructed; -}; -static const unsigned int ALIGN_MASK = 3; -#define ALIGN4(x) (uint32_t)(((uintptr_t)(x) + ALIGN_MASK) & (~ALIGN_MASK)) - -#define OBJECT_NODE_SIZE sizeof(struct ObjectNode) -#define OBJECT_CHUNK_COOKIE_SIZE (sizeof(struct ChunkLink) + sizeof(void *)) - -void HdfObjectAllocConstruct(struct HdfObjectAlloc *alloc) -{ - HdfSListInit(&alloc->nodes); - OsalMutexInit(&alloc->mutex); - alloc->isConstructed = true; -} -struct HdfObjectAlloc *HdfObjectAllocGetInstance(void) -{ - static struct HdfObjectAlloc instance = { 0 }; - - if (!instance.isConstructed) { - HdfObjectAllocConstruct(&instance); - } - - return &instance; -} - -struct HdfObjectNode *HdfObjectAllocFindSuitableChunk( - struct HdfObjectAlloc *alloc, size_t size) -{ - struct HdfSListIterator it; - struct HdfObjectNode *bestFitNode = NULL; - struct HdfObjectNode *objectNode = NULL; - HdfSListIteratorInit(&it, &alloc->nodes); - - while (HdfSListIteratorHasNext(&it)) { - objectNode = (struct HdfObjectNode *)HdfSListIteratorNext(&it); - if (size == objectNode->chunkSize) { - bestFitNode = objectNode; - break; - } else if (size < objectNode->chunkSize) { - bestFitNode = objectNode; - } - } - - return bestFitNode; -} - -static void HdfObjectAllocPushObjectNode( - struct HdfObjectAlloc *alloc, struct HdfObjectNode *node) -{ - struct HdfSListIterator it; - struct HdfObjectNode *objectNode = NULL; - HdfSListIteratorInit(&it, &alloc->nodes); - - while (HdfSListIteratorHasNext(&it)) { - objectNode = (struct HdfObjectNode *)HdfSListIteratorNext(&it); - if (node->chunkSize >= objectNode->chunkSize) { - break; - } - } - - HdfSListIteratorInsert(&it, &node->entry); -} - -static void HdfObjectAllocPreloadChunk( - void *chunkBuf, uint32_t buffSize, uint32_t chunkSize) -{ - struct HdfObjectAlloc *allocator = HdfObjectAllocGetInstance(); - - if (buffSize > OBJECT_NODE_SIZE) { - uint32_t idx; - struct ChunkLink *chunkLink; - struct ObjectNode *node; - uint32_t alignedSize = ALIGN4(chunkSize); - uint32_t alignedBufSize = ALIGN4(buffSize); - uint32_t blockSize = alignedSize + sizeof(struct ChunkLink); - uint8_t *alignedBuff = (uint8_t *)(uintptr_t)ALIGN4(chunkBuf); - node = (struct ObjectNode *)(alignedBuff + alignedBufSize - OBJECT_NODE_SIZE); - node->freeCount = 0; - node->chunkSize = alignedSize; - node->chunkCount = ((uint8_t *)node - alignedBuff) / (blockSize + sizeof(void *)); - node->chunkStack = (struct ChunkLink **)(alignedBuff + node->chunkCount * blockSize); - - for (idx = 0; idx < node->chunkCount; idx++) { - chunkLink = (struct ChunkLink *)&alignedBuff[idx * blockSize]; - chunkLink->buffSize = node->chunkSize; - node->chunkStack[node->freeCount++] = chunkLink; - chunkLink->buffer = (uint8_t *)(chunkLink + 1); - } - - HdfObjectAllocPushObjectNode(allocator, node); - } -} - -void HdfObjectAllocLoadConfigs(const struct HdfObjectPoolConfig *configs) -{ - uint32_t idx; - char *chunkBuffBegin = configs->buffer; - char *chunkBuffEnd = configs->buffer + configs->bufferSize; - - for (idx = 0; (idx < configs->numChunks) && (chunkBuffBegin < chunkBuffEnd); idx++) { - const struct ObjectChunkConfig *chunkConfig = &configs->chunks[idx]; - size_t chunkBufSize = OBJECT_NODE_SIZE + \ - (OBJECT_CHUNK_COOKIE_SIZE + chunkConfig->chunkSize) * chunkConfig->chunkCount; - - if (chunkBuffBegin + chunkBufSize <= chunkBuffEnd) { - HdfObjectAllocPreloadChunk(chunkBuffBegin, chunkBufSize, chunkConfig->chunkSize); - } - - chunkBuffBegin += chunkBufSize; - } -} - -void HdfObjectAllocInit(void) -{ - const struct HdfObjectPoolConfig *config = HdfObjectAllocGetConfig(); - - if (config != NULL) { - HdfObjectAllocLoadConfigs(config); - } -} - -void *HdfObjectAllocAlloc(size_t size) -{ - struct HdfChunkLink *chunkLink = NULL; - struct HdfObjectNode *objectNode = NULL; - struct HdfObjectAlloc *allocator = HdfObjectAllocGetInstance(); - OsalMutexLock(&allocator->mutex); - objectNode = HdfObjectAllocFindSuitableChunk(allocator, size); - if ((objectNode != NULL) && (objectNode->freeCount == 0)) { - goto finished; - } - - if (objectNode->freeCount > objectNode->chunkCount) { - if (objectNode->freeCount > objectNode->chunkCount) { - } - } - - chunkLink = (struct ChunkLink *)objectNode->chunkStack[--objectNode->freeCount]; -finished: - OsalMutexUnlock(&allocator->mutex); - return chunkLink ? chunkLink->buffer : NULL; -} - -void HdfObjectAllocFree(void *object) -{ - struct HdfChunkLink *chunkLink = container_of(void, object, struct ChunkLink, buffer); - struct HdfObjectNode *objectNode = NULL; - struct HdfObjectAlloc *allocator = HdfObjectAllocGetInstance(); - OsalMutexLock(&allocator->mutex); - objectNode = HdfObjectAllocFindSuitableChunk(allocator, chunkLink->buffSize); - if (objectNode != NULL) { - objectNode->chunkStack[objectNode->freeCount++] = chunkLink; - - if (objectNode->freeCount > objectNode->chunkCount) { - HDF_LOGE("exception: count,free:%d,total %d", objectNode->freeCount, objectNode->chunkCount); - } - } - - OsalMutexUnlock(&allocator->mutex); -} -