jeudi 24 septembre 2020

Error while adding custom allocator in unordered map in c++

I am trying to add a unordered_map in OpenJDK code for experimental purpose. So instead of using global new and delete operators, I tried to pass an allocator to the unordered_map.

Here is my hpp file with defination of structure inheriting the std::allocator. I hope I got that right.

#include "memory/allocation.hpp"
#include <unordered_map>
#include <string>
#include <string.h>
#include <iostream>
template< class T >
struct MangedAllocator : std::allocator<T> {
    typedef typename std::allocator<T>::pointer pointer;
    typedef typename std::allocator<T>::size_type size_type;


    pointer allocate(size_type size,std::allocator<void>::const_pointer = 0) {
        void * p = (void *)AllocateHeap(size, mtInternal);
        if(p == 0) {
            throw std::bad_alloc();
        }
        return static_cast<pointer>(p);
    }

    void deallocate(pointer p, size_type) {
        FreeHeap((void*)p);
    }
} ;

using namespace std;
class StackTraceMap : CHeapObj<mtInternal>{
 private:

  static std::unordered_map<std::string,int,std::hash<std::string>,std::equal_to<std::string>,MangedAllocator<std::pair<const std::string, int>>> _frames;
  static std::unordered_map<int,std::string,std::hash<int>,std::equal_to<int>,MangedAllocator<std::pair<const int, std::string>>> _frames_reversed;
  static int _counter;
 public:
  static int addFrame(char* fr_name){
    if(_frames.find(fr_name)==_frames.end()){
        //not found
        _frames[fr_name]=_counter;
        _frames_reversed[_counter]=fr_name;
        ++_counter;
        return _counter-1;
    }
    else{
        return _frames[fr_name];

    }
  }
  static void printFrame(int n){
    cout<<_frames_reversed[n]<<"\n";
  }
  static void printMap(){
      cout<<"hello there\n";
      for (auto i : _frames)
        cout << i.first << "   " << i.second
             << endl;
      cout<<"done\n";
  }

};

And here is the cpp file with definitions of variables stated in hpp file.

#include "code/stackTraceMap.hpp"
using namespace std;
int StackTraceMap::_counter=1;
static std::unordered_map<std::string,int,std::hash<std::string>,std::equal_to<std::string>,MangedAllocator<std::pair<const std::string, int>>> _frames;
static std::unordered_map<int,std::string,std::hash<int>,std::equal_to<int>,MangedAllocator<std::pair<const int, std::string>>> _frames_reversed;

I am getting the following error while building:

/usr/bin/ld: /home/manavjeet/BTP/jdk14/build/linux-x86_64-server-slowdebug/hotspot/variant-server/libjvm/objs/thread.o: in function `StackTraceMap::addFrame(char*)':
/home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:37: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:37: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:39: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:40: undefined reference to `StackTraceMap::_frames_reversed[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:45: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/build/linux-x86_64-server-slowdebug/hotspot/variant-server/libjvm/objs/thread.o: in function `StackTraceMap::printFrame(int)':
/home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:50: undefined reference to `StackTraceMap::_frames_reversed[abi:cxx11]'
collect2: error: ld returned 1 exit status
* For target hotspot_variant-server_libjvm_objs_BUILD_LIBJVM_link:
/usr/bin/ld: /home/manavjeet/BTP/jdk14/build/linux-x86_64-server-slowdebug/hotspot/variant-server/libjvm/objs/thread.o: in function `StackTraceMap::addFrame(char*)':
/home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:37: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:37: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:39: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:40: undefined reference to `StackTraceMap::_frames_reversed[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:45: undefined reference to `StackTraceMap::_frames[abi:cxx11]'
/usr/bin/ld: /home/manavjeet/BTP/jdk14/build/linux-x86_64-server-slowdebug/hotspot/variant-server/libjvm/objs/thread.o: in function `StackTraceMap::printFrame(int)':
/home/manavjeet/BTP/jdk14/src/hotspot/share/code/stackTraceMap.hpp:50: undefined reference to `StackTraceMap::_frames_reversed[abi:cxx11]'
collect2: error: ld returned 1 exit status

When I try to use it using default allocator it works fine obviously after removing checks in openJDK makefile and source code. But I want it to work on normal CHeap of openjdk

Can someone explain what am I doing wrong? Thankyou.

Aucun commentaire:

Enregistrer un commentaire