0

i'm building a simple code in wdk in "native subsystem" i have a library that overload new and delete operator here is the code:
header file

    #include<ntddk.h>

    #define HEAP_ZERO_MEMORY    0x00000008
    #define HEAP_GROWABLE       0x00000002

    typedef NTSTATUS
    (NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
        IN PVOID Base,
        IN OUT PVOID *CommitAddress,
        IN OUT PSIZE_T CommitSize
        );

    typedef struct _RTL_HEAP_PARAMETERS {
        ULONG Length;
        SIZE_T SegmentReserve;
        SIZE_T SegmentCommit;
        SIZE_T DeCommitFreeBlockThreshold;
        SIZE_T DeCommitTotalFreeThreshold;
        SIZE_T MaximumAllocationSize;
        SIZE_T VirtualMemoryThreshold;
        SIZE_T InitialCommit;
        SIZE_T InitialReserve;
        PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
        SIZE_T Reserved[ 2 ];
    } RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;


    void*  operator new(size_t sz);

    void   operator delete(void* p);

    void* mg_malloc(unsigned int size);
    int mg_free(void *buffer);
    void InitAPSupportLibrary();
    void FinishAPSupportLibrary();

    extern "C"
    {
    PVOID RtlAllocateHeap(
            __in      PVOID HeapHandle,
            __in_opt  ULONG Flags,
            __in      SIZE_T Size
            );
        BOOLEAN RtlFreeHeap(
            __in      PVOID HeapHandle,
            __in_opt  ULONG Flags,
            __in      PVOID HeapBase
            );

    PVOID

    RtlCreateHeap(


      IN ULONG                Flags,
      IN PVOID                Base ,
      IN ULONG                Reserve ,
      IN ULONG                Commit,
      IN BOOLEAN              Lock ,
      IN PRTL_HEAP_PARAMETERS RtlHeapParams  );




        PVOID RtlDestroyHeap(
            __in  PVOID HeapHandle
            );
    }

and the cpp file code:

 #include "main.h"

    PVOID mainHeapHandle = NULL;


    void InitAPSupportLibrary()
    {
        mainHeapHandle = RtlCreateHeap(HEAP_GROWABLE,NULL,0,0,NULL,0);
    }

    void FinishAPSupportLibrary()
    {
        if(mainHeapHandle) RtlDestroyHeap(mainHeapHandle);

    }


    void* mg_malloc(unsigned int size)
    {

        if(!mainHeapHandle) 
            InitAPSupportLibrary();

        void *buf  = RtlAllocateHeap(mainHeapHandle,HEAP_ZERO_MEMORY,size);
        return buf;

    }

    int mg_free(void *buffer)
    {
        if (!mainHeapHandle) return 0;
        return RtlFreeHeap(mainHeapHandle,0,buffer)?1:0;
    }

    void*  operator new(size_t sz)
    {
        void* p = (void*)mg_malloc(sz);
        return p;
    }
    void  operator delete(void* p)
    {
        mg_free(p);
    }

and the error are :
error C2373: 'operator new' : redefinition; different type modifiers
error C2373: 'operator delete' : redefinition; different type modifiers

thanks for your help

Edited by native99

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Banfa
0

You have your declarations of new and delete wrong, new is declared like this void* operator new (std::size_t size) throw (std::bad_alloc);, similarly delete also has throw statement you have missed out, check the documentation of these functions.

Additonally you have only provided you own implementation of a single version of new but new has all the following overloads (the first and fourth ones are particularly important).

void* operator new (std::size_t size) throw (std::bad_alloc);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new (std::size_t size, void* ptr) throw();
void* operator new[] (std::size_t size) throw (std::bad_alloc);
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new[] (std::size_t size, void* ptr) throw();

delete also has more overloads than the single one you have implemented.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.