<commit_before>/*
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "evoasm-alloc.h"
#include "evoasm.h"

#include <stdlib.h>
#include <errno.h>

EVOASM_DEF_LOG_TAG("alloc")

void *
evoasm_malloc(size_t size) {
  void *ptr = malloc(size);
  if(evoasm_unlikely(!ptr)) {
    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zu bytes via malloc failed: %s", size, strerror(errno));
    return NULL;
  }
  return ptr;
}

void *
evoasm_aligned_alloc(size_t align, size_t size) {
#if _POSIX_C_SOURCE >= 200112L
  void *ptr;
  int error_code = posix_memalign(&ptr, align, size);
  if(evoasm_unlikely(error_code)) {
#elif __STDC_VERSION__ >= 201112L
  void *ptr = aligned_alloc(align, size);
  if(evoasm_unlikely(!ptr)) {
    int error_code = errno;
#else
#error No aligned memory allocation function found
#endif

    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zu bytes aligned to %zu via aligned_alloc failed: %s", size, align, strerror(error_code));
    return NULL;
  }
  return ptr;
}

void *
evoasm_calloc(size_t n, size_t size) {
  void *ptr = calloc(n, size);

  if(evoasm_unlikely(!ptr)) {
    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zux%zu (%zu) bytes via calloc failed: %s", n, size, n * size, strerror(errno));
    return NULL;
  }
  return ptr;<commit_msg>Explicitly use posix_memalign on OSX<commit_after>/*
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "evoasm-alloc.h"
#include "evoasm.h"

#include <stdlib.h>
#include <errno.h>

EVOASM_DEF_LOG_TAG("alloc")

void *
evoasm_malloc(size_t size) {
  void *ptr = malloc(size);
  if(evoasm_unlikely(!ptr)) {
    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zu bytes via malloc failed: %s", size, strerror(errno));
    return NULL;
  }
  return ptr;
}

void *
evoasm_aligned_alloc(size_t align, size_t size) {
  void *ptr;

#if defined(__APPLE__)
  int error_code = posix_memalign(&ptr, align, size);
  if(evoasm_unlikely(error_code)) {
#else
#  if defined(_WIN32)
  ptr = _aligned_malloc(size, align);
#  else
  ptr = aligned_alloc(align, size);
#  endif
  if(evoasm_unlikely(!ptr)) {
    int error_code = errno;
#endif

    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zu bytes aligned to %zu via aligned_alloc failed: %s", size, align, strerror(error_code));
    return NULL;
  }
  return ptr;
}

void *
evoasm_calloc(size_t n, size_t size) {
  void *ptr = calloc(n, size);

  if(evoasm_unlikely(!ptr)) {
    evoasm_error(EVOASM_ERROR_TYPE_ALLOC, EVOASM_ERROR_CODE_NONE,
                 "Allocating %zux%zu (%zu) bytes via calloc failed: %s", n, size, n * size, strerror(errno));
    return NULL;
  }
  return ptr;
{'fraction_non_alphanumeric': 0.10173160173160173, 'fraction_numerical': 0.005772005772005772, 'mean_word_length': 3.986810551558753, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3993038', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1361, 'n_words': 444}