<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed 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.
 */

#define NDEBUG

#include <ac_message_pool.h>

#include <ac_assert.h>
#include <ac_cache_line.h>
#include <ac_debug_printf.h>
#include <ac_intmath.h>
#include <ac_memmgr.h>

/**
 * Allocate count elements each at least size long and each aligned
 * on the alignment value. The value in pRaw is the unaligned pointer
 * and is passed to ac_free. The value in pAligned is the address of
 * the first element.
 *
 * @param count is the number of elements to create
 * @param size is the minimum size of each element created
 * @param alignment is a value to align on, such as AC_MAX_CACHE_LINE_LEN
 * @param pRaw is the address to return the raw pointer passed to ac_free
 * @param pAligned is the address to return the aligned pointer of the frist element
 * @param pElemSize is length of each element which will be a multipel of alignment
 */
AcStatus ac_calloc_align(AcU32 count, AcU32 size, AcU32 alignment,
    void** pRaw, void** pAligned, AcU32* pElemSize) {
  ac_debug_printf("ac_calloc_align:+count=%u size=%u alignment=%u\n"
                  "                 pRaw=%p pAligned=%p pElemSize=%p\n",
                  count, size, alignment, pRaw, pAligned, pElemSize);
  AcStatus status;
  void* raw = AC_NULL;
  void* aligned = AC_NULL;
  AcU32 elem_size = 0;
  AcUptr mask = 0;

  if (count == 0 | size == 0 | AC_COUNT_ONE_BITS(alignment) != 1
      | pRaw == AC_NULL | pAligned == AC_NULL | pElemSize == AC_NULL) {
    ac_debug_printf("ac_calloc_align: BAD_PARAM\n");
    status = AC_STATUS_BAD_PARAM;
    goto done;
  }

  mask = ~((AcUptr)alignment - 1);
  elem_size = (size + alignment - 1) & mask;
  AcU64 allocation_size = (elem_size * count) + alignment - 1;
  raw = ac_calloc(1, allocation_size);
  if (raw == AC_NULL) {
    ac_debug_printf("ac_calloc_align: OUT_OF_MEMORY\n");
    status = AC_STATUS_OUT_OF_MEMORY;
    goto done;
  }
  aligned = (void*)((((AcUptr)raw) + alignment - 1) & mask);

  status = AC_STATUS_OK;

done:
  if (status != AC_STATUS_OK) {
    ac_free(raw);
  } else {
    *pRaw = raw;
    *pAligned = aligned;
    *pElemSize = elem_size;
  }

  ac_debug_printf("ac_calloc_align:-raw=%p aligned=%p elem_size=%u status=%u\n",
      raw, aligned, elem_size, status);<commit_msg>Add parens around sub expression to suppress warnings.<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * Licensed 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.
 */

#define NDEBUG

#include <ac_message_pool.h>

#include <ac_assert.h>
#include <ac_cache_line.h>
#include <ac_debug_printf.h>
#include <ac_intmath.h>
#include <ac_memmgr.h>

/**
 * Allocate count elements each at least size long and each aligned
 * on the alignment value. The value in pRaw is the unaligned pointer
 * and is passed to ac_free. The value in pAligned is the address of
 * the first element.
 *
 * @param count is the number of elements to create
 * @param size is the minimum size of each element created
 * @param alignment is a value to align on, such as AC_MAX_CACHE_LINE_LEN
 * @param pRaw is the address to return the raw pointer passed to ac_free
 * @param pAligned is the address to return the aligned pointer of the frist element
 * @param pElemSize is length of each element which will be a multipel of alignment
 */
AcStatus ac_calloc_align(AcU32 count, AcU32 size, AcU32 alignment,
    void** pRaw, void** pAligned, AcU32* pElemSize) {
  ac_debug_printf("ac_calloc_align:+count=%u size=%u alignment=%u\n"
                  "                 pRaw=%p pAligned=%p pElemSize=%p\n",
                  count, size, alignment, pRaw, pAligned, pElemSize);
  AcStatus status;
  void* raw = AC_NULL;
  void* aligned = AC_NULL;
  AcU32 elem_size = 0;
  AcUptr mask = 0;

  if ((count == 0) | (size == 0) | (AC_COUNT_ONE_BITS(alignment) != 1)
      | (pRaw == AC_NULL) | (pAligned == AC_NULL) | (pElemSize == AC_NULL)) {
    ac_debug_printf("ac_calloc_align: BAD_PARAM\n");
    status = AC_STATUS_BAD_PARAM;
    goto done;
  }

  mask = ~((AcUptr)alignment - 1);
  elem_size = (size + alignment - 1) & mask;
  AcU64 allocation_size = (elem_size * count) + alignment - 1;
  raw = ac_calloc(1, allocation_size);
  if (raw == AC_NULL) {
    ac_debug_printf("ac_calloc_align: OUT_OF_MEMORY\n");
    status = AC_STATUS_OUT_OF_MEMORY;
    goto done;
  }
  aligned = (void*)((((AcUptr)raw) + alignment - 1) & mask);

  status = AC_STATUS_OK;

done:
  if (status != AC_STATUS_OK) {
    ac_free(raw);
  } else {
    *pRaw = raw;
    *pAligned = aligned;
    *pElemSize = elem_size;
  }

  ac_debug_printf("ac_calloc_align:-raw=%p aligned=%p elem_size=%u status=%u\n",
      raw, aligned, elem_size, status);
{'fraction_non_alphanumeric': 0.10247846721743716, 'fraction_numerical': 0.010546669010370891, 'mean_word_length': 3.6072874493927127, 'pattern_counts': {'":': 0, '<': 17, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6191375', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1844, 'n_words': 640}