Ticket #208: memdiff.patch

File memdiff.patch, 1.4 KB (added by pramsey, 16 years ago)

Prototype implementation

  • capi/geos_c.cpp

     
    107107
    108108static GEOSMessageHandler NOTICE_MESSAGE;
    109109static GEOSMessageHandler ERROR_MESSAGE;
     110static GEOSMemoryAllocator GEOS_MALLOC;
     111static GEOSMemoryDeallocator GEOS_FREE;
    110112static int WKBOutputDims = 2;
    111113static int WKBByteOrder = getMachineByteOrder();
    112114
     115#include <exception> // for std::bad_alloc
     116#include <new>
     117#include <cstdlib> // for malloc() and free()
     118#include <cstdio> // for printf()
     119
     120// Visual C++ fix of operator new
     121
     122void* operator new (size_t size) throw (std::bad_alloc)
     123{
     124  void *p;
     125  if(GEOS_MALLOC) {
     126    p=GEOS_MALLOC(size);
     127    printf("geos ");
     128  }
     129  else {
     130    p=malloc(size);
     131    printf("std ");
     132  }
     133  if (p==0) { // did malloc succeed?
     134    throw std::bad_alloc(); // ANSI/ISO compliant behavior
     135  }
     136  printf("alloc!\n");
     137  return p;
     138}
     139
     140void operator delete (void *p) throw()
     141{
     142  if(GEOS_FREE) {
     143    GEOS_FREE(p);
     144  }
     145  else {
     146    free(p);
     147  }
     148}
     149
     150
     151
    113152extern "C" {
    114153
    115154void
    116155initGEOS (GEOSMessageHandler nf, GEOSMessageHandler ef)
     156
    117157{
    118158        NOTICE_MESSAGE = nf;
    119159        ERROR_MESSAGE = ef;
    120160}
    121161
    122162void
     163initGEOSMemory(GEOSMemoryAllocator ma, GEOSMemoryDeallocator da)
     164{
     165        GEOS_FREE = da;
     166        GEOS_MALLOC = ma;
     167}
     168
     169void
    123170finishGEOS ()
    124171{
    125172        // Nothing to do