<commit_before>/* Copyright 2014 Samsung Electronics Co., Ltd.
 *
 * 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.
 */

/** \addtogroup mem Memory allocation
 * @{
 *
 * \addtogroup heap Heap
 * @{
 */

/**
 * Heap allocator interface
 */
#ifndef JERRY_MEM_HEAP_H
#define JERRY_MEM_HEAP_H

#include "globals.h"

/**
 * Type of allocation (argument of mem_Alloc)
 *
 * @see mem_heap_alloc_block
 */
typedef enum
{
  MEM_HEAP_ALLOC_SHORT_TERM, /**< allocated region will be freed soon */
  MEM_HEAP_ALLOC_LONG_TERM /**< allocated region most likely will not be freed soon */
} mem_heap_alloc_term_t;

extern void mem_heap_init (uint8_t *heap_start, size_t heap_size);
extern void mem_heap_finalize (void);
extern void* mem_heap_alloc_block (size_t size_in_bytes, mem_heap_alloc_term_t alloc_term);
extern bool mem_heap_try_resize_block (void *ptr, size_t size_in_bytes);
extern void mem_heap_free_block (void *ptr);
extern size_t __attribute_pure__ mem_heap_recommend_allocation_size (size_t minimum_allocation_size);
extern void mem_heap_print (bool dump_block_headers, bool dump_block_data, bool dump_stats);

#ifdef MEM_STATS
/**
 * Heap memory usage statistics
 */
typedef struct
{
  size_t size; /**< size */
  size_t blocks; /**< blocks count */

  size_t allocated_chunks; /**< currently allocated chunks */
  size_t peak_allocated_chunks; /**< peak allocated chunks */
  size_t global_peak_allocated_chunks; /**< non-resettable peak allocated chunks */

  size_t allocated_blocks; /**< currently allocated blocks */
  size_t peak_allocated_blocks; /**< peak allocated blocks */
  size_t global_peak_allocated_blocks; /**< non-resettable peak allocated blocks */

  size_t allocated_bytes; /**< currently allocated bytes */
  size_t peak_allocated_bytes; /**< peak allocated bytes */
  size_t global_peak_allocated_bytes; /**< non-resettable peak allocated bytes */

  size_t waste_bytes; /**< bytes waste due to blocks filled partially
                           and due to block headers */
  size_t peak_waste_bytes; /**< peak bytes waste */
  size_t global_peak_waste_bytes; /**< non-resettable peak bytes waste */
} mem_heap_stats_t;

extern void mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p);
extern void mem_heap_stats_reset_peak (void);
#endif /* MEM_STATS */

/**
 * @}
 * @}
 */

#endif /* !JERRY_MEM_HEAP_H */<commit_msg>Introducing MEM_DEFINE_LOCAL_ARRAY and MEM_FINALIZE_LOCAL_ARRAY to replace on-stack allocation of dynamic sized arrays.<commit_after>/* Copyright 2014 Samsung Electronics Co., Ltd.
 *
 * 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.
 */

/** \addtogroup mem Memory allocation
 * @{
 *
 * \addtogroup heap Heap
 * @{
 */

/**
 * Heap allocator interface
 */
#ifndef JERRY_MEM_HEAP_H
#define JERRY_MEM_HEAP_H

#include "globals.h"

/**
 * Type of allocation (argument of mem_Alloc)
 *
 * @see mem_heap_alloc_block
 */
typedef enum
{
  MEM_HEAP_ALLOC_SHORT_TERM, /**< allocated region will be freed soon */
  MEM_HEAP_ALLOC_LONG_TERM /**< allocated region most likely will not be freed soon */
} mem_heap_alloc_term_t;

extern void mem_heap_init (uint8_t *heap_start, size_t heap_size);
extern void mem_heap_finalize (void);
extern void* mem_heap_alloc_block (size_t size_in_bytes, mem_heap_alloc_term_t alloc_term);
extern bool mem_heap_try_resize_block (void *ptr, size_t size_in_bytes);
extern void mem_heap_free_block (void *ptr);
extern size_t __attribute_pure__ mem_heap_recommend_allocation_size (size_t minimum_allocation_size);
extern void mem_heap_print (bool dump_block_headers, bool dump_block_data, bool dump_stats);

#ifdef MEM_STATS
/**
 * Heap memory usage statistics
 */
typedef struct
{
  size_t size; /**< size */
  size_t blocks; /**< blocks count */

  size_t allocated_chunks; /**< currently allocated chunks */
  size_t peak_allocated_chunks; /**< peak allocated chunks */
  size_t global_peak_allocated_chunks; /**< non-resettable peak allocated chunks */

  size_t allocated_blocks; /**< currently allocated blocks */
  size_t peak_allocated_blocks; /**< peak allocated blocks */
  size_t global_peak_allocated_blocks; /**< non-resettable peak allocated blocks */

  size_t allocated_bytes; /**< currently allocated bytes */
  size_t peak_allocated_bytes; /**< peak allocated bytes */
  size_t global_peak_allocated_bytes; /**< non-resettable peak allocated bytes */

  size_t waste_bytes; /**< bytes waste due to blocks filled partially
                           and due to block headers */
  size_t peak_waste_bytes; /**< peak bytes waste */
  size_t global_peak_waste_bytes; /**< non-resettable peak bytes waste */
} mem_heap_stats_t;

extern void mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p);
extern void mem_heap_stats_reset_peak (void);
#endif /* MEM_STATS */

/**
 * Define a local array variable and allocate memory for the array on the heap.
 *
 * If requested number of elements is zero, assign NULL to the variable.
 *
 * Warning:
 *         if there is not enough memory on the heap, shutdown engine with ERR_OUT_OF_MEMORY.
 */
#define MEM_DEFINE_LOCAL_ARRAY(var_name, number, type) \
{ \
  type *var_name = ((number > 0) \
                    ? mem_heap_alloc_block ((number) * sizeof (type), MEM_HEAP_ALLOC_SHORT_TERM) \
                    : NULL); \
  \
  if (var_name == NULL) \
  { \
    jerry_exit (ERR_OUT_OF_MEMORY); \
  } \

/**
 * Free the previously defined local array variable, freeing corresponding block on the heap,
 * if it was allocated (i.e. if the array's size was non-zero).
 */
#define MEM_FINALIZE_LOCAL_ARRAY(var_name) \
  if (var_name != NULL) \
  { \
    mem_heap_free_block (var_name); \
  } \
}

/**
 * @}
 * @}
 */

#endif /* !JERRY_MEM_HEAP_H */
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.0028451632225217132, 'mean_word_length': 4.106269113149847, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 4, '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': '1333182', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2039, 'n_words': 653}