Ignore:
Timestamp:
Feb 25, 2015, 3:04:36 PM (4 years ago)
Author:
nmedfort
Message:

More memory leak fixes. All known leaks accounted for.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/re_name.h

    r4438 r4516  
    2222        return false;
    2323    }
     24    using length_t = std::string::size_type;
    2425    enum class Type {
    2526        Byte
     
    2829        , Unknown
    2930    };
    30     const std::string & getNamespace() const;
    31     const std::string & getName() const;
    32     void setName(std::string);
     31    std::string getNamespace() const;
     32    std::string getName() const;
     33    void setName(const std::string &);
    3334    Type getType() const;
    3435    RE *getDefinition() const;
     
    4243    virtual ~Name() {}
    4344protected:
    44     friend Name * makeName(const std::string, RE *);   
    45     friend Name * makeByteName(const std::string, RE *);
    46     friend Name * makeName(const std::string, const Type);
    47     friend Name * makeName(const std::string, const std::string, const Type);   
    48     void* operator new (std::size_t size) noexcept {
    49         return mAllocator.allocate(size);
    50     }
    51     Name(const std::string && nameSpace, const std::string && name, const Type type, RE * defn)
     45    friend Name * makeName(const std::string &, RE *);
     46    friend Name * makeByteName(const std::string &, RE *);
     47    friend Name * makeName(const std::string &, const Type);
     48    friend Name * makeName(const std::string &, const std::string &, const Type);
     49    Name(const char * nameSpace, const length_t namespaceLength, const char * name, const length_t nameLength, const Type type, RE * defn)
    5250    : RE(ClassTypeId::Name)
    53     , mNamespace(std::move(nameSpace))
    54     , mName(std::move(name))
     51    , mNamespaceLength(namespaceLength)
     52    , mNamespace(replicateString(nameSpace, namespaceLength))
     53    , mNameLength(nameLength)
     54    , mName(replicateString(name, nameLength))
    5555    , mType(type)
    5656    , mDefiningRE(defn)
     
    5959
    6060    }
     61    inline const char * replicateString(const char * string, const length_t length) {
     62        if (string) {
     63            char * allocated = reinterpret_cast<char*>(mAllocator.allocate(length));
     64            std::memcpy(allocated, string, length);
     65            string = allocated;
     66        }
     67        return string;
     68    }
    6169
    6270private:
    63     std::string         mNamespace;
    64     std::string         mName;
     71    length_t            mNamespaceLength;
     72    const char *        mNamespace;
     73    length_t            mNameLength;
     74    const char *        mName;
    6575    const Type          mType;
    6676    RE *                mDefiningRE;
     
    6878};
    6979
    70 inline const std::string & Name::getNamespace() const {
    71     return mNamespace;
     80inline std::string Name::getNamespace() const {
     81    return std::string(mNamespace, mNamespaceLength);
    7282}
    7383
    74     inline const std::string & Name::getName() const {
    75         return mName;
    76     }
    77    
    78     inline void Name::setName(std::string n) {
    79         mName = n;
    80     }
     84inline std::string Name::getName() const {
     85    return std::string(mName, mNameLength);
     86}
     87
     88inline void Name::setName(const std::string & n) {
     89    mNameLength = n.length();
     90    mName = replicateString(n.c_str(), n.length());
     91}
    8192   
    8293inline Name::Type Name::getType() const {
     
    92103}
    93104
    94 inline Name * makeName(const std::string name, const Name::Type type = Name::Type::Unicode) {
    95     return new Name("", std::move(name), type, nullptr);
     105inline Name * makeName(const std::string & name, const Name::Type type = Name::Type::Unicode) {
     106    return new Name(nullptr, 0, name.c_str(), name.length(), type, nullptr);
    96107}
    97108
    98 inline Name * makeName(const std::string property, const std::string value, const Name::Type type = Name::Type::Unicode) {
    99     return new Name(std::move(property), std::move(value), type, nullptr);
     109inline Name * makeName(const std::string & property, const std::string & value, const Name::Type type = Name::Type::Unicode) {
     110    return new Name(property.c_str(), property.length(), value.c_str(), value.length(), type, nullptr);
    100111}
    101112
    102 inline Name * makeName(const std::string name, RE * cc) {
     113inline Name * makeName(const std::string & name, RE * cc) {
    103114    if (isa<Name>(cc)) {
    104115        return cast<Name>(cc);
     
    106117    else if (isa<CC>(cc)) {
    107118        Name::Type ccType = cast<CC>(cc)->max_codepoint() <= 0x7F ? Name::Type::Byte : Name::Type::Unicode;
    108         return new Name("", std::move(name), ccType, cc);
     119        return new Name(nullptr, 0, name.c_str(), name.length(), ccType, cc);
    109120    }
    110     else return new Name("", std::move(name), Name::Type::Unknown, cc);
     121    else return new Name(nullptr, 0, name.c_str(), name.length(), Name::Type::Unknown, cc);
    111122}
    112123
    113 inline Name * makeByteName(const std::string name, RE * cc) {
     124inline Name * makeByteName(const std::string & name, RE * cc) {
    114125    if (isa<Name>(cc)) {
    115126        return cast<Name>(cc);
    116127    }
    117128    else {
    118         return new Name("", std::move(name), Name::Type::Byte, cc);
     129        return new Name(nullptr, 0, name.c_str(), name.length(), Name::Type::Byte, cc);
    119130    }
    120131}
Note: See TracChangeset for help on using the changeset viewer.