Skip to content
New issue

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

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

Already on GitHub? # to your account

implement simple array buffer allocator #169

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ include $(CLEAR_VARS)
LOCAL_CPPFLAGS += -std=c++11
LOCAL_MODULE := NativeScript
LOCAL_SRC_FILES := com_tns_Platform.cpp com_tns_JsDebugger.cpp com_tns_AssetExtractor.cpp com_tns_NativeScriptActity.cpp \
JEnv.cpp DirectBuffer.cpp \
JEnv.cpp DirectBuffer.cpp SimpleAllocator.cpp \
JsDebugger.cpp \
NativeScriptRuntime.cpp MetadataNode.cpp MetadataTreeNode.cpp MetadataReader.cpp \
MethodCache.cpp JavaObjectArrayCache.cpp \
Expand Down
38 changes: 38 additions & 0 deletions src/jni/SimpleAllocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "SimpleAllocator.h"
#include <cstdlib>


using namespace tns;


SimpleAllocator::SimpleAllocator()
{
}



SimpleAllocator::~SimpleAllocator()
{
}

void* SimpleAllocator::Allocate(size_t length)
{
void *data = calloc(length, 1);
return data;
}

void* SimpleAllocator::AllocateUninitialized(size_t length)
{
void *data = malloc(length);
return data;

}

void SimpleAllocator::Free(void* data, size_t length)
{
free(data);
}




24 changes: 24 additions & 0 deletions src/jni/SimpleAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SIMPLEALLOCATOR_H_
#define SIMPLEALLOCATOR_H_

#include "v8.h"

namespace tns
{
class SimpleAllocator : public v8::ArrayBuffer::Allocator
{
public:
SimpleAllocator();

~SimpleAllocator() override;

void* Allocate(size_t length) override;

void* AllocateUninitialized(size_t length) override;

void Free(void* data, size_t length) override;
};
}


#endif /* SIMPLEALLOCATOR_H_ */
4 changes: 4 additions & 0 deletions src/jni/com_tns_Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "NativeScriptAssert.h"
#include "JsDebugger.h"
#include "SimpleProfiler.h"
#include "SimpleAllocator.h"
#include "File.h"
#include <sstream>
#include <android/log.h>
Expand All @@ -39,6 +40,8 @@ Context::Scope *context_scope = nullptr;
bool tns::LogEnabled = true;
Isolate *g_isolate = nullptr;
std::string Constants::APP_ROOT_FOLDER_PATH = "";
SimpleAllocator g_allocator;


ObjectManager *g_objectManager = nullptr;

Expand Down Expand Up @@ -165,6 +168,7 @@ extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj
Platform* platform = v8::platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
V8::SetArrayBufferAllocator(&g_allocator);

g_isolate = Isolate::New();
auto isolate = g_isolate;
Expand Down