Replacing libstdc ++. Dylib (4.0) OSX Global New and Remote Operators

I am trying to replace global new ones and delete operators with Xcode 3.2, GCC 4.2, libstdC ++ 4.0, the dynamic version.

I took the prototypes directly from the "new" header and implemented them. They are inserted below.

The project is .plugin, thus a dynamic lib. This plugin SHOULD delegate allocation to main applications of their own alloc / free algorithms, which are in the old SDK SDK.

All my own calls to new / delete along with the values ​​of std :: list and std :: map are correctly replaced, but not when std :: vector :: push_back should grow its buffer. In this case, my new operator is not called, but my operator is deleted. I know this because I write the token in the first four bytes of any buffer allocated by my new statement, and I check this token in deleting the statement. Below is the violation code.

extern "C++"
{
__attribute__((visibility("default"))) void* operator new(std::size_t) throw (std::bad_alloc);
__attribute__((visibility("default"))) void* operator new[](std::size_t) throw (std::bad_alloc);
__attribute__((visibility("default"))) void operator delete(void*) throw();
__attribute__((visibility("default"))) void operator delete[](void*) throw();
__attribute__((visibility("default"))) void* operator new(std::size_t, const std::nothrow_t&) throw();
__attribute__((visibility("default"))) void* operator new[](std::size_t, const std::nothrow_t&) throw();
__attribute__((visibility("default"))) void operator delete(void*, const std::nothrow_t&) throw();
__attribute__((visibility("default"))) void operator delete[](void*, const std::nothrow_t&) throw();

}

The following code will trigger an assertion when "yo" goes out of scope because the memory allocated for std :: vector was not assigned by my new operator.

   {
        std::vector<std::string> yo;
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
    }

The following code is fine, because std :: vector :: reserve calls my new operator:

   {
        std::vector<std::string> yo;
        yo.reserve(4);
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
        yo.push_back("yoyoma");
    }

GBD () std::vector:: push_back, ( _M_insert_aux). , , , new std::vector:: push_back.

, . - push_back.

libstd++. a, .

- std::vector < std::string > , ?

, Windows VS9.

+3
4

, , , : .

, , .

, libs :

  • ++ :
  • , :
  • : ( )

, XCode 3.2 Build .

, :

#pragma GCC visibility push(hidden)

extern "C++"
{
    void* operator new(std::size_t) throw (std::bad_alloc);
    void* operator new[](std::size_t) throw (std::bad_alloc);
    void operator delete(void*) throw();
    void operator delete[](void*) throw();
    void* operator new(std::size_t, const std::nothrow_t&) throw();
    void* operator new[](std::size_t, const std::nothrow_t&) throw();
    void operator delete(void*, const std::nothrow_t&) throw();
    void operator delete[](void*, const std::nothrow_t&) throw();
} // extern "C++"

#pragma GCC visibility pop

, ? Static + Hidden Symbols. , STL, .

, , . .dylib, , .

+1

, , , STL .

http://www.cplusplus.com/reference/std/memory/allocator/

, , GC-Hans-Bohen GC ++, / GC, STL, .

, , , malloc. - .

0

_M_insert_aux vector.tcc, GCC. , , , .

  pointer __new_start(this->_M_allocate(__len));

operator new, ( std::allocator), , new_allocator.h:91, .

0

, GCC std::vector . MSVC . .

0

Source: https://habr.com/ru/post/1759417/


All Articles