Przeglądaj źródła

Pass Allocator around instead of using default_allocator() directly

Daniele Bartolini 10 lat temu
rodzic
commit
cf8924e4a5

+ 7 - 6
src/core/filesystem/apk_filesystem.cpp

@@ -14,8 +14,9 @@
 
 namespace crown
 {
-ApkFilesystem::ApkFilesystem(AAssetManager* asset_manager)
-	: _asset_manager(asset_manager)
+ApkFilesystem::ApkFilesystem(Allocator& a, AAssetManager* asset_manager)
+	: _allocator(&a)
+	, _asset_manager(asset_manager)
 {
 }
 
@@ -23,14 +24,14 @@ File* ApkFilesystem::open(const char* path, FileOpenMode::Enum mode)
 {
 	CE_ASSERT_NOT_NULL(path);
 	CE_ASSERT(mode == FileOpenMode::READ, "Cannot open for writing in Android assets folder");
-	ApkFile* file = CE_NEW(default_allocator(), ApkFile)(_asset_manager);
+	ApkFile* file = CE_NEW(*_allocator, ApkFile)(_asset_manager);
 	file->open(path, mode);
 	return file;
 }
 
 void ApkFilesystem::close(File& file)
 {
-	CE_DELETE(default_allocator(), &file);
+	CE_DELETE(*_allocator, &file);
 }
 
 bool ApkFilesystem::exists(const char* path)
@@ -83,8 +84,8 @@ void ApkFilesystem::list_files(const char* path, Vector<DynamicString>& files)
 	const char* filename = NULL;
 	while ((filename = AAssetDir_getNextFileName(root_dir)) != NULL)
 	{
-		DynamicString name(default_allocator());
-		name = filename;
+		TempAllocator512 ta;
+		DynamicString name(filename, ta);
 		vector::push_back(files, name);
 	}
 

+ 2 - 1
src/core/filesystem/apk_filesystem.h

@@ -19,11 +19,12 @@ namespace crown
 /// The assets folder is read-only and all the paths are relative.
 class ApkFilesystem : public Filesystem
 {
+	Allocator* _allocator;
 	AAssetManager* _asset_manager;
 
 public:
 
-	ApkFilesystem(AAssetManager* asset_manager);
+	ApkFilesystem(Allocator& a, AAssetManager* asset_manager);
 
 	/// @copydoc Filesystem::open()
 	File* open(const char* path, FileOpenMode::Enum mode);

+ 8 - 6
src/core/filesystem/disk_filesystem.cpp

@@ -12,16 +12,18 @@
 
 namespace crown
 {
-DiskFilesystem::DiskFilesystem()
-	: _prefix(default_allocator())
+DiskFilesystem::DiskFilesystem(Allocator& a)
+	: _allocator(&a)
+	, _prefix(a)
 {
 	char buf[512];
 	os::getcwd(buf, sizeof(buf));
 	_prefix = buf;
 }
 
-DiskFilesystem::DiskFilesystem(const char* prefix)
-	: _prefix(prefix)
+DiskFilesystem::DiskFilesystem(Allocator& a, const char* prefix)
+	: _allocator(&a)
+	, _prefix(prefix, a)
 {
 }
 
@@ -33,14 +35,14 @@ File* DiskFilesystem::open(const char* path, FileOpenMode::Enum mode)
 	DynamicString abs_path(alloc);
 	get_absolute_path(path, abs_path);
 
-	DiskFile* file = CE_NEW(default_allocator(), DiskFile)();
+	DiskFile* file = CE_NEW(*_allocator, DiskFile)();
 	file->open(abs_path.c_str(), mode);
 	return file;
 }
 
 void DiskFilesystem::close(File& file)
 {
-	CE_DELETE(default_allocator(), &file);
+	CE_DELETE(*_allocator, &file);
 }
 
 bool DiskFilesystem::exists(const char* path)

+ 3 - 2
src/core/filesystem/disk_filesystem.h

@@ -19,18 +19,19 @@ namespace crown
 /// @ingroup Filesystem
 class DiskFilesystem : public Filesystem
 {
+	Allocator* _allocator;
 	DynamicString _prefix;
 
 public:
 
 	/// Sets the root path to the current working directory of
 	/// the engine executable.
-	DiskFilesystem();
+	DiskFilesystem(Allocator& a);
 
 	/// Sets the root path to the given @a prefix.
 	/// @note
 	/// The @a prefix must be absolute.
-	DiskFilesystem(const char* prefix);
+	DiskFilesystem(Allocator& a, const char* prefix);
 
 	/// @copydoc Filesystem::open()
 	File* open(const char* path, FileOpenMode::Enum mode);

+ 2 - 2
src/device.cpp

@@ -161,9 +161,9 @@ void Device::init()
 	profiler_globals::init();
 
 #if CROWN_PLATFORM_ANDROID
-	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(const_cast<AAssetManager*>((AAssetManager*)_device_options.asset_manager()));
+	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options.asset_manager()));
 #else
-	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
+	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(default_allocator(), _device_options.bundle_dir());
 #endif // CROWN_PLATFORM_ANDROID
 
 	_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);

+ 3 - 4
src/resource/bundle_compiler.cpp

@@ -33,8 +33,8 @@
 namespace crown
 {
 BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
-	: _source_fs(source_dir)
-	, _bundle_fs(bundle_dir)
+	: _source_fs(default_allocator(), source_dir)
+	, _bundle_fs(default_allocator(), bundle_dir)
 	, _compilers(default_allocator())
 	, _files(default_allocator())
 {
@@ -70,8 +70,7 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 	register_resource_compiler(SPRITE_ANIMATION_TYPE, SPRITE_ANIMATION_VERSION, sar::compile);
 	register_resource_compiler(CONFIG_TYPE,           CONFIG_VERSION,           cor::compile);
 
-	DiskFilesystem temp;
-	temp.create_directory(bundle_dir);
+	_bundle_fs.create_directory(bundle_dir);
 
 	scan_source_dir("");