Skip to content

Commit c3cad88

Browse files
Finally show the juniors what placement new should look like.
Shut up compiler warnings about matching operator delete. Make TAR archive work.
1 parent 08d1eb1 commit c3cad88

File tree

7 files changed

+84
-116
lines changed

7 files changed

+84
-116
lines changed

include/nbl/system/CAPKResourcesArchive.h

Lines changed: 0 additions & 85 deletions
This file was deleted.

include/nbl/system/CApplicationAndroid.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _NBL_SYSTEM_C_APPLICATION_FRAMEWORK_ANDROID_H_INCLUDED_
22
#define _NBL_SYSTEM_C_APPLICATION_FRAMEWORK_ANDROID_H_INCLUDED_
3+
34
#ifdef _NBL_PLATFORM_ANDROID_
5+
46
#include "nbl/core/declarations.h"
57
#include "nbl/system/CStdoutLoggerAndroid.h"
68
#include "nbl/system/IApplicationFramework.h"
@@ -135,4 +137,5 @@ namespace nbl::system
135137
}
136138

137139
#endif
140+
138141
#endif

include/nbl/system/CFileArchive.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,31 @@ class CInnerArchiveFile : public CFileView<T>
2020
{
2121
std::atomic_flag* alive;
2222
public:
23-
CInnerArchiveFile(CFileView<T>* arch, std::atomic_flag* _flag) : CFileView<T>(std::move(*arch)), alive(_flag)
23+
template<typename... Args>
24+
CInnerArchiveFile(std::atomic_flag* _flag, Args&&... args) : CFileView<T>(std::forward<Args>(args)...), alive(_flag)
2425
{
2526
}
2627
~CInnerArchiveFile() = default;
2728

2829
static void* operator new(size_t size) noexcept
2930
{
3031
assert(false);
32+
exit(-0x45);
3133
return nullptr;
3234
}
3335
static void* operator new[](size_t size) noexcept
3436
{
3537
assert(false);
38+
exit(-0x45);
3639
return nullptr;
3740
}
38-
static void* operator new(size_t size, void* ptr, std::atomic_flag* alive)
41+
static void* operator new(size_t size, void* ptr, std::atomic_flag* alive) noexcept
3942
{
4043
alive->test_and_set();
41-
return ::operator new(size, ptr);
44+
return ::operator new(size,ptr);
4245
}
46+
47+
//
4348
static void operator delete(void* ptr) noexcept
4449
{
4550
static_cast<CInnerArchiveFile*>(ptr)->alive->clear();
@@ -48,6 +53,13 @@ class CInnerArchiveFile : public CFileView<T>
4853
static void operator delete[](void* ptr) noexcept
4954
{
5055
assert(false);
56+
exit(-0x45);
57+
}
58+
59+
// make compiler shut up about initizaliation throwing exceptions
60+
static void operator delete(void* dummy, void* ptr, std::atomic_flag* alive) noexcept
61+
{
62+
::operator delete(ptr);
5163
}
5264
};
5365

@@ -66,7 +78,7 @@ class CFileArchive : public IFileArchive
6678
const auto* item = getItemFromPath(pathRelativeToArchive);
6779
if (!item)
6880
return nullptr;
69-
/*
81+
7082
switch (item->allocatorType)
7183
{
7284
case EAT_NULL:
@@ -81,7 +93,6 @@ class CFileArchive : public IFileArchive
8193
default: // directory or something
8294
break;
8395
}
84-
*/
8596
return nullptr;
8697
}
8798

@@ -104,28 +115,36 @@ class CFileArchive : public IFileArchive
104115
_NBL_ALIGNED_FREE(m_filesBuffer);
105116
_NBL_ALIGNED_FREE(m_fileFlags);
106117
}
107-
/*
118+
108119
template<class Allocator>
109-
core::smart_refctd_ptr<CInnerArchiveFile<Allocator>> getFile_impl(const IFileArchive::SListEntry* item)
120+
inline core::smart_refctd_ptr<CInnerArchiveFile<Allocator>> getFile_impl(const IFileArchive::SListEntry* item)
110121
{
111122
auto* file = reinterpret_cast<CInnerArchiveFile<Allocator>*>(m_filesBuffer+item->ID*SIZEOF_INNER_ARCHIVE_FILE);
112123
// NOTE: Intentionally calling grab() on maybe-not-existing object!
113124
const auto oldRefcount = file->grab();
114125

115126
if (oldRefcount==0) // need to construct (previous refcount was 0)
116127
{
128+
const auto fileBuffer = getFileBuffer(item);
117129
// Might have barged inbetween a refctr drop and finish of a destructor + delete,
118130
// need to wait for the "alive" flag to become `false` which tells us `operator delete` has finished.
119131
m_fileFlags[item->ID].wait(true);
120132
// coast is clear, do placement new
121133
new (file, &m_fileFlags[item->ID]) CInnerArchiveFile<Allocator>(
122-
static_cast<CFileView<Allocator>*>(readFile_impl(params).get()),
123-
&m_fileFlags[item->ID]
134+
m_fileFlags+item->ID,
135+
getDefaultAbsolutePath()/item->pathRelativeToArchive,
136+
IFile::ECF_READ, // TODO: stay like this until we allow write access to archived files
137+
std::get<void*>(fileBuffer),
138+
std::get<size_t>(fileBuffer),
139+
Allocator() // no archive uses stateful allocators yet
124140
);
125141
}
142+
// don't grab because we've already grabbed
126143
return core::smart_refctd_ptr<CInnerArchiveFile<Allocator>>(file,core::dont_grab);
127144
}
128-
*/
145+
146+
// this function will return a buffer that needs to be deallocated with an allocator matching `item->allocatorType`
147+
virtual std::pair<void*,size_t> getFileBuffer(const IFileArchive::SListEntry* item) = 0;
129148

130149
std::atomic_flag* m_fileFlags = nullptr;
131150
std::byte* m_filesBuffer = nullptr;

include/nbl/system/CFileView.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class IFileView : public IFile
1717
}
1818

1919
protected:
20-
IFileView(IFileView&& other) : IFile(path(other.getFileName()),other.getFlags()), m_buffer(other.m_buffer), m_size(other.m_size)
21-
{
22-
other.m_buffer = nullptr;
23-
}
2420
IFileView(path&& _name, const core::bitflag<E_CREATE_FLAGS> _flags, void* buffer, size_t fileSize) :
2521
IFile(std::move(_name),_flags), m_buffer(buffer), m_size(fileSize) {}
2622

@@ -60,7 +56,6 @@ class CFileView : public IFileView
6056
}
6157

6258
protected:
63-
CFileView(CFileView<allocator_t>&& other) : IFileView(std::move(other)), allocator(std::move(other.allocator)) {}
6459
~CFileView()
6560
{
6661
if (m_buffer)
@@ -81,9 +76,11 @@ class CFileView<CNullAllocator> : public IFileView
8176
public:
8277
CFileView(path&& _name, const core::bitflag<E_CREATE_FLAGS> _flags, void* buffer, const size_t fileSize) :
8378
IFileView(std::move(_name),_flags,buffer,fileSize) {}
79+
// the `_allocator` parameter is useless and unused but its necessary to have a uniform constructor signature across all specializations (saves us headaches in `CFileArchive`)
80+
CFileView(path&& _name, const core::bitflag<E_CREATE_FLAGS> _flags, void* buffer, const size_t fileSize, CNullAllocator&& _allocator) :
81+
CFileView(std::move(_name),_flags,buffer,fileSize) {}
8482

8583
protected:
86-
CFileView(CFileView<CNullAllocator>&& other) : IFileView(std::move(other)) {}
8784
~CFileView() = default;
8885
};
8986

src/nbl/system/CAPKResourcesArchive.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef _NBL_SYSTEM_C_APK_RESOURCES_ARCHIVE_LOADER_H_INCLUDED_
2+
#define _NBL_SYSTEM_C_APK_RESOURCES_ARCHIVE_LOADER_H_INCLUDED_
3+
4+
5+
#include "nbl/system/IFileArchive.h"
6+
7+
8+
#ifdef _NBL_PLATFORM_ANDROID_
9+
struct AAssetManager;
10+
struct JNIEnv;
11+
struct ANativeActivity;
12+
13+
namespace nbl::system
14+
{
15+
16+
class CAPKResourcesArchive final : public CFileArchive
17+
{
18+
public:
19+
static core::smart_refctd_ptr<CAPKResourcesArchive> create();
20+
21+
protected:
22+
CAPKResourcesArchive(
23+
path&& _path, system::logger_opt_smart_ptr&& logger,
24+
AAssetManager* _mgr, ANativeActivity* act, JNIEnv* jni
25+
) : CFileArchive(std::move(_path),std::move(logger),std::move(_items)),
26+
m_mgr(_mgr), m_activity(act), m_jniEnv(jni)
27+
{}
28+
29+
std::pair<void*, size_t> getFileBuffer(const IFileArchive::SListEntry* item) override;
30+
31+
32+
AAssetManager* m_mgr;
33+
JNIEnv* m_jniEnv;
34+
ANativeActivity* m_activity;
35+
};
36+
37+
}
38+
#endif
39+
40+
#endif

src/nbl/system/CArchiveLoaderTar.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,11 @@ struct STarHeader
4141
using namespace nbl;
4242
using namespace nbl::system;
4343

44-
/*
45-
core::smart_refctd_ptr<IFile> CArchiveLoaderTar::CArchive::readFile_impl(const SListEntry* item)
44+
std::pair<void*,size_t> CArchiveLoaderTar::CArchive::getFileBuffer(const IFileArchive::SListEntry* item)
4645
{
47-
uint8_t* buff = reinterpret_cast<uint8_t*>(m_file->getMappedPointer())+item->offset;
48-
auto a = core::make_smart_refctd_ptr<CFileView<CNullAllocator>>(
49-
core::smart_refctd_ptr<ISystem>(m_system),
50-
params.absolutePath,
51-
IFile::ECF_READ_WRITE, // TODO: this file cannot grow, unsure whether we should allow write access!
52-
buff,
53-
found->size);
54-
return a;
46+
assert(item->allocatorType==EAT_NULL);
47+
return {reinterpret_cast<uint8_t*>(m_file->getMappedPointer())+item->offset,item->size};
5548
}
56-
*/
5749

5850
core::smart_refctd_ptr<IFileArchive> CArchiveLoaderTar::createArchive_impl(core::smart_refctd_ptr<system::IFile>&& file, const std::string_view& password) const
5951
{

src/nbl/system/CArchiveLoaderTar.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,26 @@ namespace nbl::system
1111
class CArchiveLoaderTar final : public IArchiveLoader
1212
{
1313
public:
14-
class CArchive : public CFileArchive
14+
class CArchive final : public CFileArchive
1515
{
1616
public:
1717
CArchive(core::smart_refctd_ptr<IFile>&& _file, system::logger_opt_smart_ptr&& logger, core::vector<SListEntry>&& _items) :
1818
CFileArchive(path(_file->getFileName()),std::move(logger),std::move(_items)), m_file(std::move(_file)) {}
1919

20-
//
2120
protected:
21+
std::pair<void*,size_t> getFileBuffer(const IFileArchive::SListEntry* item) override;
22+
2223
core::smart_refctd_ptr<IFile> m_file;
2324
};
2425

2526
CArchiveLoaderTar(system::logger_opt_smart_ptr&& logger) : IArchiveLoader(std::move(logger)) {}
26-
virtual bool isALoadableFileFormat(IFile* file) const override
27+
28+
inline bool isALoadableFileFormat(IFile* file) const override
2729
{
2830
return !!createArchive_impl(core::smart_refctd_ptr<IFile>(file),"");
2931
}
3032

31-
const char** getAssociatedFileExtensions() const override
33+
inline const char** getAssociatedFileExtensions() const override
3234
{
3335
static const char* ext[]{ "tar", nullptr };
3436
return ext;

0 commit comments

Comments
 (0)