Skip to content

Commit

Permalink
Extended the memstorage to accepts buffers and cstrings. Prep work fo…
Browse files Browse the repository at this point in the history
…r the generic fetch_component
  • Loading branch information
karurochari committed Nov 30, 2024
1 parent f16cd27 commit df11bd4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/cache/commons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum class resource_t{
BUFFER, //uint8_t[]
XML_TREE, //pugi::xml_document
SCRIPT, //struct script_t
DYNAMIC_LIB, //?
PATH, //a string used as path reference, basically like buffer. Used to save info about path resolution.
OTHER_RESOURCE, //?
};

Expand Down
4 changes: 4 additions & 0 deletions include/cache/memory-storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

//Its content is not exposed via any sort of API to embedded scripts.

#include <span>
#include <utils/strings.hpp>
#include <ui-frame.hpp>
#include <cstddef>
Expand Down Expand Up @@ -69,7 +70,10 @@ class mem_storage_t{
inline entry_it end() {return entries.end();}
static inline mem_key_t empty_key = {"",0,resource_t::NONE};
public:
//Add a memory entry from various sources. Interpreting it is a task for the consumer later on, based on the resource type tag.

entry_it fetch_from_buffer(const mem_key_t& path, std::span<uint8_t const> str);
entry_it fetch_from_cstring(const mem_key_t& path, std::string_view str);
entry_it fetch_from_fs(const mem_key_t& path);
# ifdef HAS_CURL
entry_it fetch_from_http(const mem_key_t& path);
Expand Down
2 changes: 1 addition & 1 deletion include/cbindings/vs.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ enum vs_frame_access_t{
};
typedef enum vs_frame_access_t vs_frame_access_t;

enum struct vs_component_t{
enum vs_component_t{
VS_COMPONENT_NONE,
VS_COMPONENT_XML,
VS_COMPONENT_WASM,
Expand Down
11 changes: 10 additions & 1 deletion include/fetcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ enum struct component_t{
component_t component_t_i(const char* t);
constexpr const char* component_t_s(component_t t);

std::tuple<resolve_path::reason_t::t, scoped_rpath_t, component_t> fetch_component();
/*Fully fetches a component by:
- Looking for the best match which exists
- Save the path resolution to cache
- Load it based on its type:
- XML load and store as xml document (compile it if needed, and store the template as its own xml document?)
- WASM load in memory as binary (buffer) or a script once WASM scripts are supported?.
- LIB skip, the library will be loaded on demand
- CNATIVE compile and save it as script. Technically it is not, but I am not going to compile it over and over
*/
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component_t> fetch_component();

/**
* @brief Fetch resource using its path and some contextual information (or get it if already on cache)
Expand Down
29 changes: 26 additions & 3 deletions src/cache/memory-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cache/memory-storage.hpp>
#include <iostream>
#include <memory>
#include <new>
#include <sys/types.h>
#include <unordered_map>

Expand All @@ -16,6 +17,27 @@ namespace vs{
namespace cache{


mem_storage_t::entry_it mem_storage_t::fetch_from_buffer(const mem_key_t& path, std::span<uint8_t const> str){
uint8_t* buffer = new uint8_t[str.size()];
memcpy(buffer,str.data(),str.size());
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,str.size()}, +[](buffer_t* p){delete[] p->data;delete p;});
auto it = entries.emplace(path, w);

if(it.second==true)return it.first;
else return entries.end();
}

mem_storage_t::entry_it mem_storage_t::fetch_from_cstring(const mem_key_t& path, std::string_view str){
uint8_t* buffer = new uint8_t[str.size()+1];
memcpy(buffer,str.data(),str.size());
buffer[str.size()]=0;
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,str.size()+1}, +[](buffer_t* p){delete[] p->data;delete p;});
auto it = entries.emplace(path, w);

if(it.second==true)return it.first;
else return entries.end();
}

mem_storage_t::entry_it mem_storage_t::fetch_from_fs(const mem_key_t& path){
uint8_t* buffer = nullptr;
size_t fsize = 0;
Expand All @@ -36,7 +58,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_fs(const mem_key_t& path){
else return entries.end();
}
//Special destructor that must also destroy the buffer itself once done.
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,fsize}, +[](buffer_t* p){delete[] p->data;delete p;});
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,fsize+1}, +[](buffer_t* p){delete[] p->data;delete p;});
auto it = entries.emplace(path, w);
if(it.second==true)return it.first;
else return entries.end();
Expand Down Expand Up @@ -73,6 +95,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){
return entries.end();
}
else{
//TODO: Check if I need NULL termination and explcit fsize+1;
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,fsize}, +[](buffer_t* p){free((void*) p->data);delete p;});
auto it = entries.emplace(path, w);
curl_easy_cleanup(curl);
Expand Down Expand Up @@ -118,6 +141,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){
return entries.end();
}
else{
//TODO: Check if I need NULL termination and explcit fsize+1;
auto w =std::shared_ptr<buffer_t>(new buffer_t{buffer,fsize}, +[](buffer_t* p){free((void*)p->data);delete p;});
auto it = entries.emplace(path, w);
curl_easy_cleanup(curl);
Expand All @@ -136,7 +160,7 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){
#endif

mem_storage_t::entry_it mem_storage_t::fetch_from_res_storage(const mem_key_t& path){
//I need to have SQLITE integrate first.
//TODO: I need to have SQLITE integrate first.
return entries.end();
}

Expand All @@ -145,7 +169,6 @@ mem_storage_t::entry_it mem_storage_t::fetch_from_http(const mem_key_t& path){
auto it = entries.emplace(k,src);
if(it.second==true)return it.first;
else return entries.end();
;
}

void mem_storage_t::drop(const mem_key_t& key){
Expand Down
2 changes: 1 addition & 1 deletion src/fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ constexpr const char* component_t_s(component_t t){
else return nullptr;
}

std::tuple<resolve_path::reason_t::t, scoped_rpath_t, component_t> fetch_component(){
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component_t> fetch_component(){
//TODO:
}

Expand Down

0 comments on commit df11bd4

Please sign in to comment.