Beispiel #1
0
void* TRI_AllocateZ (TRI_memory_zone_t* zone, uint64_t n, bool set, char const* file, int line) {
#else
void* TRI_Allocate (TRI_memory_zone_t* zone, uint64_t n, bool set) {
#endif
  char* m;

#ifdef TRI_ENABLE_MAINTAINER_MODE
  CheckSize(n, file, line);

  m = MALLOC_WRAPPER(zone, (size_t) n + sizeof(uintptr_t));
#else
  m = MALLOC_WRAPPER(zone, (size_t) n);
#endif

  if (m == NULL) {
    if (zone->_failable) {
      TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY);
      return NULL;
    }

    if (CoreReserve == NULL) {
      fprintf(stderr,
              "FATAL: failed to allocate %llu bytes for memory zone %d" ZONE_DEBUG_LOCATION ", giving up!\n",
              (unsigned long long) n,
              (int) zone->_zid
              ZONE_DEBUG_PARAMS);
      TRI_EXIT_FUNCTION(EXIT_FAILURE, NULL);
    }

    free(CoreReserve);
    CoreReserve = NULL;

    fprintf(stderr,
            "failed to allocate %llu bytes for memory zone %d" ZONE_DEBUG_LOCATION ", retrying!\n",
            (unsigned long long) n,
            (int) zone->_zid
            ZONE_DEBUG_PARAMS);

#ifdef TRI_ENABLE_MAINTAINER_MODE
    return TRI_AllocateZ(zone, n, set, file, line);
#else
    return TRI_Allocate(zone, n, set);
#endif
  }

#ifdef TRI_ENABLE_MAINTAINER_MODE
  else if (set) {
    memset(m, 0, (size_t) n + sizeof(uintptr_t));
  }
  else {
    // prefill with 0xA5 (magic value, same as Valgrind will use)
    memset(m, 0xA5, (size_t) n + sizeof(uintptr_t));
  }
#else
  else if (set) {
Beispiel #2
0
void* TRI_AllocateZ(TRI_memory_zone_t* zone, uint64_t n, bool set,
                    char const* file, int line) {
#else
void* TRI_Allocate(TRI_memory_zone_t* zone, uint64_t n, bool set) {
#endif
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
  CheckSize(n, file, line);
#endif
  char* m = static_cast<char*>(MALLOC_WRAPPER(zone, (size_t)n));

  if (m == nullptr) {
    if (zone->_failable) {
      TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY);
      return nullptr;
    }

    if (CoreReserve == nullptr) {
      fprintf(stderr,
              "FATAL: failed to allocate %llu bytes for core mem zone "
              ZONE_DEBUG_LOCATION ", giving up!\n",
              (unsigned long long)n ZONE_DEBUG_PARAMS);
      TRI_EXIT_FUNCTION(EXIT_FAILURE, nullptr);
    }

    free(CoreReserve);
    CoreReserve = nullptr;

    fprintf(
        stderr,
        "failed to allocate %llu bytes for core mem zone" ZONE_DEBUG_LOCATION
        ", retrying!\n",
        (unsigned long long)n ZONE_DEBUG_PARAMS);

#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
    return TRI_AllocateZ(zone, n, set, file, line);
#else
    return TRI_Allocate(zone, n, set);
#endif
  }

  if (set) {
    memset(m, 0, (size_t)n);
  }
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
  else {
    // prefill with 0xA5 (magic value, same as Valgrind will use)
    memset(m, 0xA5, (size_t)n);
  }
#endif

  return m;
}
Beispiel #3
0
void* TRI_ReallocateZ (TRI_memory_zone_t* zone, void* m, uint64_t n, char const* file, int line) {
#else
void* TRI_Reallocate (TRI_memory_zone_t* zone, void* m, uint64_t n) {
#endif

  if (m == nullptr) {
#ifdef TRI_ENABLE_MAINTAINER_MODE
    return TRI_AllocateZ(zone, n, false, file, line);
#else
    return TRI_Allocate(zone, n, false);
#endif
  }

  char* p = (char*) m;

#ifdef TRI_ENABLE_MAINTAINER_MODE
  CheckSize(n, file, line);
#endif

  p = static_cast<char*>(REALLOC_WRAPPER(zone, p, (size_t) n));

  if (p == nullptr) {
    if (zone->_failable) {
      TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY);
      return nullptr;
    }

    if (CoreReserve == nullptr) {
      fprintf(stderr,
              "FATAL: failed to re-allocate %llu bytes for memory zone %d" ZONE_DEBUG_LOCATION ", giving up!\n",
              (unsigned long long) n,
              zone->_zid
              ZONE_DEBUG_PARAMS);
      TRI_EXIT_FUNCTION(EXIT_FAILURE, nullptr);
    }

    free(CoreReserve);
    CoreReserve = nullptr;

    fprintf(stderr,
            "failed to re-allocate %llu bytes for memory zone %d" ZONE_DEBUG_LOCATION ", retrying!\n",
            (unsigned long long) n,
            (int) zone->_zid
            ZONE_DEBUG_PARAMS);

#ifdef TRI_ENABLE_MAINTAINER_MODE
    return TRI_ReallocateZ(zone, m, n, file, line);
#else
    return TRI_Reallocate(zone, m, n);
#endif
  }

  return p;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief basic memory management for deallocate
////////////////////////////////////////////////////////////////////////////////

#ifdef TRI_ENABLE_MAINTAINER_MODE
void TRI_FreeZ (TRI_memory_zone_t* zone, void* m, char const* file, int line) {
#else
void TRI_Free (TRI_memory_zone_t* zone, void* m) {
#endif

  char* p = (char*) m;

#ifdef TRI_ENABLE_MAINTAINER_MODE
  if (p == nullptr) {
    fprintf(stderr,
            "freeing nil ptr " ZONE_DEBUG_LOCATION
            ZONE_DEBUG_PARAMS);
    // crash intentionally
    TRI_ASSERT(false);
  }
#endif

  free(p);
}