From 9f9156a7520a8fb62cdb9db4a7fc63434abbb17b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 9 Jan 2015 13:57:21 +0000 Subject: [PATCH] Track String memory allocations --- src/array_view.hh | 3 ++- src/completion.hh | 4 ++-- src/file.cc | 23 +++++++++++----------- src/file.hh | 9 ++++----- src/memory.hh | 7 +++++++ src/ncurses_ui.cc | 2 +- src/option_types.hh | 16 +++++++-------- src/string.cc | 12 ++++++------ src/string.hh | 48 +++++++++++++++++++++++++-------------------- src/unit_tests.cc | 4 ++-- 10 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/array_view.hh b/src/array_view.hh index 6a807c2c..1c9edf49 100644 --- a/src/array_view.hh +++ b/src/array_view.hh @@ -31,7 +31,8 @@ public: ArrayView(const Iterator& begin, const Iterator& end) : m_pointer(&(*begin)), m_size(end - begin) {} - ArrayView(const std::vector& v) + template + ArrayView(const std::vector& v) : m_pointer(&v[0]), m_size(v.size()) {} ArrayView(const std::initializer_list& v) diff --git a/src/completion.hh b/src/completion.hh index e978d0ac..50ce6607 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -1,18 +1,18 @@ #ifndef completion_hh_INCLUDED #define completion_hh_INCLUDED -#include #include #include "units.hh" #include "string.hh" +#include "vector.hh" namespace Kakoune { class Context; -using CandidateList = std::vector; +using CandidateList = Vector; struct Completions { diff --git a/src/file.cc b/src/file.cc index 96026300..3996ac24 100644 --- a/src/file.cc +++ b/src/file.cc @@ -4,7 +4,6 @@ #include "buffer.hh" #include "buffer_manager.hh" #include "buffer_utils.hh" -#include "completion.hh" #include "debug.hh" #include "unicode.hh" #include "regex.hh" @@ -260,7 +259,7 @@ String find_file(StringView filename, ArrayView paths) } template -std::vector list_files(StringView prefix, StringView dirname, +Vector list_files(StringView prefix, StringView dirname, Filter filter) { kak_assert(dirname.empty() or dirname.back() == '/'); @@ -270,8 +269,8 @@ std::vector list_files(StringView prefix, StringView dirname, auto closeDir = on_scope_end([=]{ closedir(dir); }); - std::vector result; - std::vector subseq_result; + Vector result; + Vector subseq_result; while (dirent* entry = readdir(dir)) { if (not filter(*entry)) @@ -301,9 +300,9 @@ std::vector list_files(StringView prefix, StringView dirname, return result.empty() ? subseq_result : result; } -std::vector complete_filename(StringView prefix, - const Regex& ignored_regex, - ByteCount cursor_pos) +CandidateList complete_filename(StringView prefix, + const Regex& ignored_regex, + ByteCount cursor_pos) { String real_prefix = parse_filename(prefix.substr(0, cursor_pos)); String dirname; @@ -329,14 +328,14 @@ std::vector complete_filename(StringView prefix, return not check_ignored_regex or not regex_match(entry.d_name, ignored_regex); }; - std::vector res = list_files(fileprefix, dirname, filter); + Vector res = list_files(fileprefix, dirname, filter); for (auto& file : res) file = dirname + file; std::sort(res.begin(), res.end()); return res; } -std::vector complete_command(StringView prefix, ByteCount cursor_pos) +Vector complete_command(StringView prefix, ByteCount cursor_pos) { String real_prefix = parse_filename(prefix.substr(0, cursor_pos)); @@ -361,7 +360,7 @@ std::vector complete_command(StringView prefix, ByteCount cursor_pos) | (st.st_mode & S_IXOTH); return S_ISDIR(st.st_mode) or (S_ISREG(st.st_mode) and executable); }; - std::vector res = list_files(fileprefix, dirname, filter); + Vector res = list_files(fileprefix, dirname, filter); for (auto& file : res) file = dirname + file; std::sort(res.begin(), res.end()); @@ -373,11 +372,11 @@ std::vector complete_command(StringView prefix, ByteCount cursor_pos) struct CommandCache { TimeSpec mtime = {}; - std::vector commands; + Vector commands; }; static UnorderedMap command_cache; - std::vector res; + Vector res; for (auto dir : split(getenv("PATH"), ':')) { String dirname = dir; diff --git a/src/file.hh b/src/file.hh index a8d2dc5c..f9817e2d 100644 --- a/src/file.hh +++ b/src/file.hh @@ -1,6 +1,7 @@ #ifndef file_hh_INCLUDED #define file_hh_INCLUDED +#include "completion.hh" #include "exception.hh" #include "regex.hh" @@ -46,12 +47,10 @@ String find_file(StringView filename, ArrayView paths); time_t get_fs_timestamp(StringView filename); -std::vector complete_filename(StringView prefix, - const Regex& ignore_regex, - ByteCount cursor_pos = -1); +CandidateList complete_filename(StringView prefix, const Regex& ignore_regex, + ByteCount cursor_pos = -1); -std::vector complete_command(StringView prefix, - ByteCount cursor_pos = -1); +CandidateList complete_command(StringView prefix, ByteCount cursor_pos = -1); } #endif // file_hh_INCLUDED diff --git a/src/memory.hh b/src/memory.hh index 36b5c31c..af932b24 100644 --- a/src/memory.hh +++ b/src/memory.hh @@ -31,6 +31,13 @@ template struct Allocator { using value_type = T; + // TODO: remove that once we have a c++11 compliant stdlib + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; Allocator() = default; template diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index c69e101d..48ca0991 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -707,7 +707,7 @@ static String make_info_box(StringView title, StringView message, assistant_size = { (int)assistant.size(), assistant[0].char_length() }; const CharCount max_bubble_width = max_width - assistant_size.column - 6; - std::vector lines = wrap_lines(message, max_bubble_width); + Vector lines = wrap_lines(message, max_bubble_width); CharCount bubble_width = title.char_length() + 2; for (auto& line : lines) diff --git a/src/option_types.hh b/src/option_types.hh index f1eb156c..eb63ddc8 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -34,8 +34,8 @@ inline void option_from_string(StringView str, bool& opt) constexpr Codepoint list_separator = ':'; -template -String option_to_string(const std::vector& opt) +template +String option_to_string(const std::vector& opt) { String res; for (size_t i = 0; i < opt.size(); ++i) @@ -47,11 +47,11 @@ String option_to_string(const std::vector& opt) return res; } -template -void option_from_string(StringView str, std::vector& opt) +template +void option_from_string(StringView str, std::vector& opt) { opt.clear(); - std::vector elems = split(str, list_separator, '\\'); + Vector elems = split(str, list_separator, '\\'); for (auto& elem: elems) { T opt_elem; @@ -60,8 +60,8 @@ void option_from_string(StringView str, std::vector& opt) } } -template -bool option_add(std::vector& opt, const std::vector& vec) +template +bool option_add(std::vector& opt, const std::vector& vec) { std::copy(vec.begin(), vec.end(), back_inserter(opt)); return not vec.empty(); @@ -88,7 +88,7 @@ void option_from_string(StringView str, UnorderedMap& opt) opt.clear(); for (auto& elem : split(str, list_separator, '\\')) { - std::vector pair_str = split(elem, '=', '\\'); + Vector pair_str = split(elem, '=', '\\'); if (pair_str.size() != 2) throw runtime_error("map option expects key=value"); std::pair pair; diff --git a/src/string.cc b/src/string.cc index ef115b2b..2bea1cf4 100644 --- a/src/string.cc +++ b/src/string.cc @@ -17,9 +17,9 @@ bool operator<(StringView lhs, StringView rhs) return cmp < 0; } -std::vector split(StringView str, char separator, char escape) +Vector split(StringView str, char separator, char escape) { - std::vector res; + Vector res; auto it = str.begin(); while (it != str.end()) { @@ -48,9 +48,9 @@ std::vector split(StringView str, char separator, char escape) return res; } -std::vector split(StringView str, char separator) +Vector split(StringView str, char separator) { - std::vector res; + Vector res; auto beg = str.begin(); for (auto it = beg; it != str.end(); ++it) { @@ -138,14 +138,14 @@ String expand_tabs(StringView line, CharCount tabstop, CharCount col) return res; } -std::vector wrap_lines(StringView text, CharCount max_width) +Vector wrap_lines(StringView text, CharCount max_width) { using Utf8It = utf8::iterator; Utf8It word_begin{text.begin()}; Utf8It word_end{word_begin}; Utf8It end{text.end()}; CharCount col = 0; - std::vector lines; + Vector lines; Utf8It line_begin = text.begin(); Utf8It line_end = line_begin; while (word_begin != end) diff --git a/src/string.hh b/src/string.hh index 8cbd7a68..18e9ae4e 100644 --- a/src/string.hh +++ b/src/string.hh @@ -4,50 +4,55 @@ #include "units.hh" #include "utf8.hh" #include "hash.hh" +#include "vector.hh" #include #include #include -#include namespace Kakoune { class StringView; -class String : public std::string +using StringBase = std::basic_string, + Allocator>; + +class String : public StringBase { public: String() {} - String(const char* content) : std::string(content) {} - String(std::string content) : std::string(std::move(content)) {} - explicit String(char content, CharCount count = 1) : std::string((size_t)(int)count, content) {} + String(const char* content) : StringBase(content) {} + String(StringBase content) : StringBase(std::move(content)) {} + template + String(const std::basic_string& content) : StringBase(content.begin(), content.end()) {} + explicit String(char content, CharCount count = 1) : StringBase((size_t)(int)count, content) {} explicit String(Codepoint cp, CharCount count = 1) { while (count-- > 0) utf8::dump(back_inserter(*this), cp); } template - String(Iterator begin, Iterator end) : std::string(begin, end) {} + String(Iterator begin, Iterator end) : StringBase(begin, end) {} - std::string& stdstr() { return *this; } - const std::string& stdstr() const { return *this; } + StringBase& stdstr() { return *this; } + const StringBase& stdstr() const { return *this; } [[gnu::always_inline]] - char operator[](ByteCount pos) const { return std::string::operator[]((int)pos); } + char operator[](ByteCount pos) const { return StringBase::operator[]((int)pos); } [[gnu::always_inline]] - char& operator[](ByteCount pos) { return std::string::operator[]((int)pos); } + char& operator[](ByteCount pos) { return StringBase::operator[]((int)pos); } Codepoint operator[](CharCount pos) { return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); } [[gnu::always_inline]] - ByteCount length() const { return ByteCount{(int)std::string::length()}; } + ByteCount length() const { return ByteCount{(int)StringBase::length()}; } CharCount char_length() const { return utf8::distance(begin(), end()); } ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); } CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); } - String& operator+=(const String& other) { std::string::operator+=(other); return *this; } - String& operator+=(const char* other) { std::string::operator+=(other); return *this; } - String& operator+=(char other) { std::string::operator+=(other); return *this; } + String& operator+=(const String& other) { StringBase::operator+=(other); return *this; } + String& operator+=(const char* other) { StringBase::operator+=(other); return *this; } + String& operator+=(char other) { StringBase::operator+=(other); return *this; } String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; } StringView substr(ByteCount pos, ByteCount length = INT_MAX) const; @@ -62,7 +67,8 @@ public: : m_data{data}, m_length{length} {} StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {} constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {} - StringView(const std::string& str) : m_data{str.data()}, m_length{(int)str.length()} {} + template + StringView(const std::basic_string& str) : m_data{str.data()}, m_length{(int)str.length()} {} StringView(const char& c) : m_data(&c), m_length(1) {} friend bool operator==(StringView lhs, StringView rhs); @@ -112,12 +118,12 @@ public: if (*end == '\0') unowned = begin; else - owned = std::string(begin, end); + owned = StringBase(begin, end); } operator const char*() const { return unowned ? unowned : owned.c_str(); } private: - std::string owned; + StringBase owned; const char* unowned = nullptr; }; @@ -229,8 +235,8 @@ inline String operator+(const String& lhs, const char* rhs) return lhs + StringView{rhs}; } -std::vector split(StringView str, char separator, char escape); -std::vector split(StringView str, char separator); +Vector split(StringView str, char separator, char escape); +Vector split(StringView str, char separator); String escape(StringView str, StringView characters, char escape); String unescape(StringView str, StringView characters, char escape); @@ -255,7 +261,7 @@ inline String operator"" _str(const char* str, size_t) inline String codepoint_to_str(Codepoint cp) { - std::string str; + StringBase str; utf8::dump(back_inserter(str), cp); return String(str); } @@ -279,7 +285,7 @@ bool subsequence_match(StringView str, StringView subseq); String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0); -std::vector wrap_lines(StringView text, CharCount max_width); +Vector wrap_lines(StringView text, CharCount max_width); inline size_t hash_value(const Kakoune::String& str) { diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 1e4ab51a..845b9a3e 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -106,14 +106,14 @@ void test_string() { kak_assert(String("youpi ") + "matin" == "youpi matin"); - std::vector splited = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':', '\\'); + Vector splited = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':', '\\'); kak_assert(splited[0] == "youpi"); kak_assert(splited[1] == "matin"); kak_assert(splited[2] == ""); kak_assert(splited[3] == "tchou:kanaky"); kak_assert(splited[4] == "hihi:"); - std::vector splitedview = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':'); + Vector splitedview = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':'); kak_assert(splitedview[0] == "youpi"); kak_assert(splitedview[1] == "matin"); kak_assert(splitedview[2] == "");