Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clang-format to normalise c code #156

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BasedOnStyle: LLVM
UseTab: Never
# UseTab: Always
# IndentWidth: 4
BreakBeforeBraces: Attach
# put a space before the opening parenthesis in control flow constructs
BreakBeforeBinaryOperators: NonAssignment
AllowShortFunctionsOnASingleLine: None
IndentFunctionDeclarationAfterType: true
PointerAlignment: Left
# do not alter lines with trailing comments
ColumnLimit: 0
# do not indent public, protected, private
IndentAccessModifiers: false
88 changes: 44 additions & 44 deletions cc/CatchCvExceptionWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,63 @@
// TODO remove this one
namespace FF {

class SimpleWorkerBase : public ISyncWorker, public IAsyncWorker {
public:
virtual bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool unwrapOptionalArgsFromOpts(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
class SimpleWorkerBase : public ISyncWorker, public IAsyncWorker {
public:
virtual bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool unwrapOptionalArgsFromOpts(Nan::NAN_METHOD_ARGS_TYPE info) = 0;
virtual bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) = 0;

std::string execute() {
return "";
}
std::string execute() {
return "";
}

v8::Local<v8::Value> getReturnValue() {
return Nan::Undefined();
}
v8::Local<v8::Value> getReturnValue() {
return Nan::Undefined();
}

v8::Local<v8::Value> getReturnValue(Nan::NAN_METHOD_ARGS_TYPE info) {
return getReturnValue();
}
v8::Local<v8::Value> getReturnValue(Nan::NAN_METHOD_ARGS_TYPE info) {
return getReturnValue();
}

bool applyUnwrappers(Nan::NAN_METHOD_ARGS_TYPE info) {
return unwrapRequiredArgs(info)
|| (!hasOptArgsObject(info) && unwrapOptionalArgs(info))
|| (hasOptArgsObject(info) && unwrapOptionalArgsFromOpts(info));
}
};
bool applyUnwrappers(Nan::NAN_METHOD_ARGS_TYPE info) {
return unwrapRequiredArgs(info)
|| (!hasOptArgsObject(info) && unwrapOptionalArgs(info))
|| (hasOptArgsObject(info) && unwrapOptionalArgsFromOpts(info));
}
};

class SimpleWorker : public SimpleWorkerBase {
bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}
class SimpleWorker : public SimpleWorkerBase {
bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}

bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}
bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}

bool unwrapOptionalArgsFromOpts(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}
bool unwrapOptionalArgsFromOpts(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}

bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}
};
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return false;
}
};

}
} // namespace FF

struct CatchCvExceptionWorker : public FF::SimpleWorker {
public:
std::string execute() {
try {
return executeCatchCvExceptionWorker();
} catch (std::exception &e) {
return std::string(e.what());
}
}
std::string execute() {
try {
return executeCatchCvExceptionWorker();
} catch (std::exception& e) {
return std::string(e.what());
}
}

virtual std::string executeCatchCvExceptionWorker() = 0;
virtual std::string executeCatchCvExceptionWorker() = 0;
};

#endif
165 changes: 78 additions & 87 deletions cc/CustomMatAllocator.cc
Original file line number Diff line number Diff line change
@@ -1,124 +1,115 @@
#include "CustomMatAllocator.h"
//#include <iostream>
// #include <iostream>

#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING

cv::UMatData* CustomMatAllocator::allocate(
int dims, const int* sizes, int type, void* data0, size_t* step,
int dims, const int* sizes, int type, void* data0, size_t* step,
#if CV_VERSION_GREATER_EQUAL(4, 0, 0)
cv::AccessFlag flags,
cv::AccessFlag flags,
#else
int flags,
int flags,
#endif
cv::UMatUsageFlags usageFlags
) const
{
cv::UMatData* u = stdAllocator->allocate(dims, sizes, type, data0, step, flags, usageFlags);

if (NULL != u){
u->prevAllocator = u->currAllocator = this;
if( !(u->flags & cv::UMatData::USER_ALLOCATED) ){
// make mem change atomic
try {
variables->MemTotalChangeMutex.lock();
variables->TotalMem += u->size;
variables->CountMemAllocs ++;
variables->MemTotalChangeMutex.unlock();
this->FixupJSMem();
} catch (...){
printf("CustomMatAllocator::allocate - exception adjusting memory\n");
}
}
cv::UMatUsageFlags usageFlags) const {
cv::UMatData* u = stdAllocator->allocate(dims, sizes, type, data0, step, flags, usageFlags);

if (NULL != u) {
u->prevAllocator = u->currAllocator = this;
if (!(u->flags & cv::UMatData::USER_ALLOCATED)) {
// make mem change atomic
try {
variables->MemTotalChangeMutex.lock();
variables->TotalMem += u->size;
variables->CountMemAllocs++;
variables->MemTotalChangeMutex.unlock();
this->FixupJSMem();
} catch (...) {
printf("CustomMatAllocator::allocate - exception adjusting memory\n");
}
}
return u;
}
return u;
}

bool CustomMatAllocator::allocate(
cv::UMatData* u,
cv::UMatData* u,
#if CV_VERSION_GREATER_EQUAL(4, 0, 0)
cv::AccessFlag accessFlags,
cv::AccessFlag accessFlags,
#else
int accessFlags,
int accessFlags,
#endif
cv::UMatUsageFlags usageFlags
) const
{
// this does not seem to change memory allocation?
return stdAllocator->allocate(u, accessFlags, usageFlags);
cv::UMatUsageFlags usageFlags) const {
// this does not seem to change memory allocation?
return stdAllocator->allocate(u, accessFlags, usageFlags);
}


void CustomMatAllocator::deallocate(cv::UMatData* u) const
{
if (NULL != u){
if( !(u->flags & cv::UMatData::USER_ALLOCATED) ){
// make mem change atomic
variables->MemTotalChangeMutex.lock();
variables->TotalMem -= u->size;
variables->CountMemDeAllocs ++;
variables->MemTotalChangeMutex.unlock();
}
void CustomMatAllocator::deallocate(cv::UMatData* u) const {
if (NULL != u) {
if (!(u->flags & cv::UMatData::USER_ALLOCATED)) {
// make mem change atomic
variables->MemTotalChangeMutex.lock();
variables->TotalMem -= u->size;
variables->CountMemDeAllocs++;
variables->MemTotalChangeMutex.unlock();
}
stdAllocator->deallocate(u);
this->FixupJSMem();
}
stdAllocator->deallocate(u);
this->FixupJSMem();
}


// method to read the total mem, but mutex protected.
int64_t CustomMatAllocator::readtotalmem(){
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->TotalMem;
variables->MemTotalChangeMutex.unlock();
return Total;
int64_t CustomMatAllocator::readtotalmem() {
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->TotalMem;
variables->MemTotalChangeMutex.unlock();
return Total;
}

int64_t CustomMatAllocator::readmeminformed(){
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->TotalJSMem;
variables->MemTotalChangeMutex.unlock();
return Total;
int64_t CustomMatAllocator::readmeminformed() {
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->TotalJSMem;
variables->MemTotalChangeMutex.unlock();
return Total;
}

int64_t CustomMatAllocator::readnumallocated(){
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->CountMemAllocs;
variables->MemTotalChangeMutex.unlock();
return Total;
int64_t CustomMatAllocator::readnumallocated() {
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->CountMemAllocs;
variables->MemTotalChangeMutex.unlock();
return Total;
}

int64_t CustomMatAllocator::readnumdeallocated(){
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->CountMemDeAllocs;
variables->MemTotalChangeMutex.unlock();
return Total;
int64_t CustomMatAllocator::readnumdeallocated() {
int64_t Total;
variables->MemTotalChangeMutex.lock();
Total = variables->CountMemDeAllocs;
variables->MemTotalChangeMutex.unlock();
return Total;
}


void CustomMatAllocator::FixupJSMem() const {
// we can only do this IF we are on the main thread.
std::thread::id this_id = std::this_thread::get_id();
// we can only do this IF we are on the main thread.
std::thread::id this_id = std::this_thread::get_id();

if (variables->main_thread_id == this_id){
//std::cout << "thead is main " << this_id << "\n";
variables->MemTotalChangeMutex.lock();
int64_t adjust = variables->TotalMem - variables->TotalJSMem;
variables->TotalJSMem += adjust;
variables->MemTotalChangeMutex.unlock();
if (variables->main_thread_id == this_id) {
// std::cout << "thead is main " << this_id << "\n";
variables->MemTotalChangeMutex.lock();
int64_t adjust = variables->TotalMem - variables->TotalJSMem;
variables->TotalJSMem += adjust;
variables->MemTotalChangeMutex.unlock();

if (adjust){
//printf("will call Nan ajust by %d\n", (int)adjust);
Nan::AdjustExternalMemory(adjust);
//printf("done ajust by %d\n", (int)adjust);
}
} else {
//std::cout << "thead not main " << this_id << "\n";
if (adjust) {
// printf("will call Nan ajust by %d\n", (int)adjust);
Nan::AdjustExternalMemory(adjust);
// printf("done ajust by %d\n", (int)adjust);
}
} else {
// std::cout << "thead not main " << this_id << "\n";
}
}

// end only valid for 3.1.0+
#endif

Loading
Loading