<commit_before>/** @file

  Memory allocation routines for libts

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
#include "libts.h"

#include <assert.h>
#if defined(linux)
// XXX: SHouldn't that be part of CPPFLAGS?
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#endif
#include <stdlib.h>
#include <string.h>

void *
ats_malloc(size_t size)
{
  void *ptr = NULL;

  /*
   * There's some nasty code in libts that expects
   * a MALLOC of a zero-sized item to work properly. Rather
   * than allocate any space, we simply return a NULL to make
   * certain they die quickly & don't trash things.
   */

  // Useful for tracing bad mallocs
  // ink_stack_trace_dump();
  if (likely(size > 0)) {
#if TS_HAS_JEMALLOC
    if (unlikely((ptr = JEMALLOC_P(malloc)(size)) == NULL)) {
#else
    if (unlikely((ptr = malloc(size)) == NULL)) {
#endif
      xdump();
      ink_fatal(1, "ats_malloc: couldn't allocate %zu bytes", size);
    }
  }
  return ptr;
}                               /* End ats_malloc */

void *
ats_calloc(size_t nelem, size_t elsize)
{
#if TS_HAS_JEMALLOC
  void *ptr = JEMALLOC_P(calloc)(nelem, elsize);
#else
  void *ptr = calloc(nelem, elsize);
#endif
  if (unlikely(ptr == NULL)) {
    xdump();
    ink_fatal(1, "ats_calloc: couldn't allocate %zu %zu byte elements", nelem, elsize);
  }
  return ptr;
}                               /* End ats_calloc */

void *
ats_realloc(void *ptr, size_t size)
{
#if TS_HAS_JEMALLOC
  void *newptr = JEMALLOC_P(realloc)(ptr, size);
#else
  void *newptr = realloc(ptr, size);
#endif
  if (unlikely(newptr == NULL)) {
    xdump();
    ink_fatal(1, "ats_realloc: couldn't reallocate %zu bytes", size);
  }
  return newptr;
}                               /* End ats_realloc */

// TODO: For Win32 platforms, we need to figure out what to do with memalign.
// The older code had ifdef's around such calls, turning them into ats_malloc().
void *
ats_memalign(size_t alignment, size_t size)
{
  void *ptr;

#if TS_HAS_POSIX_MEMALIGN || TS_HAS_JEMALLOC
  if (alignment <= 8)
    return ats_malloc(size);

#if defined(openbsd)
  if (alignment > PAGE_SIZE)
      alignment = PAGE_SIZE;
#endif

#if TS_HAS_JEMALLOC
  int retcode = JEMALLOC_P(posix_memalign)(&ptr, alignment, size);
#else
  int retcode = posix_memalign(&ptr, alignment, size);
#endif

  if (unlikely(retcode)) {
    if (retcode == EINVAL) {
      ink_fatal(1, "ats_memalign: couldn't allocate %d bytes at alignment %d - invalid alignment parameter",
                (int)size, (int)alignment);
    } else if (retcode == ENOMEM) {
      ink_fatal(1, "ats_memalign: couldn't allocate %d bytes at alignment %d - insufficient memory",
                (int)size, (int)alignment);
    } else {
      ink_fatal(1, "ats_memalign: couldn't allocate %d bytes at alignment %d - unknown error %d",
                (int)size, (int)alignment, retcode);
    }
  }
#else
  ptr = memalign(alignment, size);
  if (unlikely(ptr == NULL)) {
    ink_fatal(1, "ats_memalign: couldn't allocate %d bytes at alignment %d", (int) size, (int) alignment);
  }
#endif
  return ptr;
}                               /* End ats_memalign */

void
ats_free(void *ptr)
{<commit_msg>Format size_t with %zu<commit_after>/** @file

  Memory allocation routines for libts

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
#include "libts.h"

#include <assert.h>
#if defined(linux)
// XXX: SHouldn't that be part of CPPFLAGS?
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#endif
#include <stdlib.h>
#include <string.h>

void *
ats_malloc(size_t size)
{
  void *ptr = NULL;

  /*
   * There's some nasty code in libts that expects
   * a MALLOC of a zero-sized item to work properly. Rather
   * than allocate any space, we simply return a NULL to make
   * certain they die quickly & don't trash things.
   */

  // Useful for tracing bad mallocs
  // ink_stack_trace_dump();
  if (likely(size > 0)) {
#if TS_HAS_JEMALLOC
    if (unlikely((ptr = JEMALLOC_P(malloc)(size)) == NULL)) {
#else
    if (unlikely((ptr = malloc(size)) == NULL)) {
#endif
      xdump();
      ink_fatal(1, "ats_malloc: couldn't allocate %zu bytes", size);
    }
  }
  return ptr;
}                               /* End ats_malloc */

void *
ats_calloc(size_t nelem, size_t elsize)
{
#if TS_HAS_JEMALLOC
  void *ptr = JEMALLOC_P(calloc)(nelem, elsize);
#else
  void *ptr = calloc(nelem, elsize);
#endif
  if (unlikely(ptr == NULL)) {
    xdump();
    ink_fatal(1, "ats_calloc: couldn't allocate %zu %zu byte elements", nelem, elsize);
  }
  return ptr;
}                               /* End ats_calloc */

void *
ats_realloc(void *ptr, size_t size)
{
#if TS_HAS_JEMALLOC
  void *newptr = JEMALLOC_P(realloc)(ptr, size);
#else
  void *newptr = realloc(ptr, size);
#endif
  if (unlikely(newptr == NULL)) {
    xdump();
    ink_fatal(1, "ats_realloc: couldn't reallocate %zu bytes", size);
  }
  return newptr;
}                               /* End ats_realloc */

// TODO: For Win32 platforms, we need to figure out what to do with memalign.
// The older code had ifdef's around such calls, turning them into ats_malloc().
void *
ats_memalign(size_t alignment, size_t size)
{
  void *ptr;

#if TS_HAS_POSIX_MEMALIGN || TS_HAS_JEMALLOC
  if (alignment <= 8)
    return ats_malloc(size);

#if defined(openbsd)
  if (alignment > PAGE_SIZE)
      alignment = PAGE_SIZE;
#endif

#if TS_HAS_JEMALLOC
  int retcode = JEMALLOC_P(posix_memalign)(&ptr, alignment, size);
#else
  int retcode = posix_memalign(&ptr, alignment, size);
#endif

  if (unlikely(retcode)) {
    if (retcode == EINVAL) {
      ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - invalid alignment parameter",
                size, alignment);
    } else if (retcode == ENOMEM) {
      ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - insufficient memory",
                size, alignment);
    } else {
      ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu - unknown error %d",
                size, alignment, retcode);
    }
  }
#else
  ptr = memalign(alignment, size);
  if (unlikely(ptr == NULL)) {
    ink_fatal(1, "ats_memalign: couldn't allocate %zu bytes at alignment %zu",  size,  alignment);
  }
#endif
  return ptr;
}                               /* End ats_memalign */

void
ats_free(void *ptr)
{
{'fraction_non_alphanumeric': 0.10598137992602984, 'fraction_numerical': 0.00459125111592909, 'mean_word_length': 3.171276595744681, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6470643', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2500, 'n_words': 863}