Example #1
0
static void* FailRealloc(TRI_memory_zone_t* zone, void* old, size_t n) {
  // we can fail, so let's check whether we should fail intentionally...
  if (zone->_failable && ShouldFail(n)) {
    // intentionally return NULL
    errno = ENOMEM;
    return nullptr;
  }

  return BuiltInRealloc(old, n);
}
Example #2
0
void TRI_SystemFreeZ (void* p, char const* file, int line) {
#else
void TRI_SystemFree (void* p) {
#endif

#ifdef TRI_ENABLE_MAINTAINER_MODE
  if (p == nullptr) {
    fprintf(stderr,
            "freeing nil ptr in %s:%d\n",
            file,
            line);
  }
#endif
  free(p);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief wrapper for realloc
///
/// this wrapper is used together with libev, as the builtin libev allocator
/// causes problems with Valgrind:
/// - http://lists.schmorp.de/pipermail/libev/2012q2/001917.html
/// - http://lists.gnu.org/archive/html/bug-gnulib/2011-03/msg00243.html
////////////////////////////////////////////////////////////////////////////////

void* TRI_WrappedReallocate (void* ptr, long size) {
  if (ptr == nullptr && size == 0) {
    return nullptr;
  }

  return BuiltInRealloc(ptr, (size_t) size);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief initialize memory subsystem
////////////////////////////////////////////////////////////////////////////////

void TRI_InitializeMemory () {
  if (CoreInitialized == 0) {
    static size_t const ReserveSize = 1024 * 1024 * 10;

    TriCoreMemZone._zid      = 0;
    TriCoreMemZone._failed   = false;
    TriCoreMemZone._failable = false;

    TriUnknownMemZone._zid      = 1;
    TriUnknownMemZone._failed   = false;
    TriUnknownMemZone._failable = true;

#ifdef TRI_ENABLE_FAILURE_TESTS 
    InitFailMalloc(); 
#endif

    CoreReserve = BuiltInMalloc(ReserveSize);

    if (CoreReserve == nullptr) {
      fprintf(stderr,
              "FATAL: cannot allocate initial core reserve of size %llu, giving up!\n",
              (unsigned long long) ReserveSize);
    }
    else {
      CoreInitialized = 1;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief shutdown memory subsystem
////////////////////////////////////////////////////////////////////////////////

void TRI_ShutdownMemory () {
  if (CoreInitialized == 1) {
    free(CoreReserve);
    CoreInitialized = 0;
  }
}