avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] new, delete support


From: Ned Konz
Subject: Re: [avr-libc-dev] new, delete support
Date: Fri, 02 Jun 2006 01:01:18 -0700
User-agent: Thunderbird 1.5.0.2 (Macintosh/20060308)

kashey wrote:
Ned Konz пишет:
kashey wrote:
kashey пишет:
Ned Konz пишет:
kashey wrote:
Hi all.
When be included support new and delete functions support in avr-libc?


What do you need? Assuming you don't need the whole libstdc++ library, this will probably do what you need for typical C++ programs.


extern "C" {
#include <stddef.h>
#include <stdlib.h>
}

void *operator
new(size_t sz) throw()
{
  return malloc(sz);
}

void operator
delete(void *p)
{
    free(p);
}

extern "C" __cxa_pure_virtual();

void __cxa_pure_virtual()
{
   abort();
}


And what about calling Constructor/Destructor ?

What do you mean? That's not the job of avr-libc.

Is there some code that you want to write that won't call the constructor and destructor, given the above?

I am using a recent version of avr-gcc (4.2.0, almost) and am using templates, constructors, destructors, and so on using more or less just the above and avr-libc.

Most of my objects are static, and so the constructors get called before main() (and the destructors never do), but if there were actually an exit from my code (nowhere to go, in ROM) the destructors would also get called.
It's solution for static objects. If you can describe what i have do if I'll use dynamic object.

As example:
class KMyClass
{
   private:
      int *Array;
      int Len;
   public:
      KMyClass(int Len)
      {
         Len = len;
          Array = new int [len];
       }
      ~KMyClass()
      {
         delete [] Array;
      }
}

...
int main()
{
   KMyClass *var = new KMyClass(10);
   ...
   return 0;
}



Something like this (I added set_new_handler()) should work (this is a modified version of the libstdc++ code, with all the exception-related stuff pulled out):


// avr-gcc -Os -g -Wa,-ahlds=newDelete.lst -fno-exceptions -c newDelete.cpp

extern "C++" {
namespace std
{
  typedef unsigned int size_t;
  typedef void (*new_handler)();
  new_handler set_new_handler(new_handler) throw();
}

void operator delete(void*) throw();
void operator delete[](void*) throw();
void* operator new(std::size_t) throw();
void* operator new[](std::size_t) throw();
void operator delete(void*) throw();
void operator delete[](void*) throw();
// Default placement versions of operator new.
inline void* operator new(std::size_t, void* __p) throw() { return __p; }
inline void* operator new[](std::size_t, void* __p) throw() { return __p; }
// Default placement versions of operator delete.
inline void  operator delete  (void*, void*) throw() { }
inline void  operator delete[](void*, void*) throw() { }
}

// --- implementation ---
//

using std::new_handler;
extern "C" void *malloc (std::size_t);
extern "C" void free(void *);
extern void abort() __attribute__((noreturn));
new_handler __new_handler;

void *
operator new (std::size_t sz) throw()
{
    void *p;
    if (sz == 0)
        sz = 1;
    p = (void*) malloc(sz);

    while (p == 0)
    {
        new_handler handler = __new_handler;
        if (! handler)
            return 0;
        handler();
        p = (void *) malloc(sz);
    }

    return p;
}

void*
operator new[] (std::size_t sz) throw()
{
  return ::operator new(sz);
}

void
operator delete (void *ptr) throw ()
{
  if (ptr)
    free (ptr);
}

void
operator delete[] (void *ptr) throw ()
{
  ::operator delete (ptr);
}

void __cxa_pure_virtual()
{
    abort();
}

class KMyClass
{
   private:
      int *Array;
      int Len;
   public:
      KMyClass(int len) : Len(len), Array(new int[len]) { }
      ~KMyClass() { delete [] Array; }
      int& operator[] (int i) { return Array[i]; }
};

KMyClass *someFunc(int i)
{
   KMyClass *var = new KMyClass(i);
   return var;
}

void someOtherFunc(KMyClass *kmc)
{
    delete kmc;
}



Thanks,
--
Ned Konz
address@hidden
http://bike-nomad.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]