Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file memory_buffer_alloc.h
  3.  *
  4.  * \brief Buffer-based memory allocator
  5.  */
  6. /*
  7.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8.  *  SPDX-License-Identifier: GPL-2.0
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License along
  21.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  22.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23.  *
  24.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26. #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H
  27. #define MBEDTLS_MEMORY_BUFFER_ALLOC_H
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #include <stddef.h>
  36.  
  37. /**
  38.  * \name SECTION: Module settings
  39.  *
  40.  * The configuration options you can set for this module are in this section.
  41.  * Either change them in config.h or define them on the compiler command line.
  42.  * \{
  43.  */
  44.  
  45. #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE)
  46. #define MBEDTLS_MEMORY_ALIGN_MULTIPLE       4 /**< Align on multiples of this value */
  47. #endif
  48.  
  49. /* \} name SECTION: Module settings */
  50.  
  51. #define MBEDTLS_MEMORY_VERIFY_NONE         0
  52. #define MBEDTLS_MEMORY_VERIFY_ALLOC        (1 << 0)
  53. #define MBEDTLS_MEMORY_VERIFY_FREE         (1 << 1)
  54. #define MBEDTLS_MEMORY_VERIFY_ALWAYS       (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE)
  55.  
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59.  
  60. /**
  61.  * \brief   Initialize use of stack-based memory allocator.
  62.  *          The stack-based allocator does memory management inside the
  63.  *          presented buffer and does not call calloc() and free().
  64.  *          It sets the global mbedtls_calloc() and mbedtls_free() pointers
  65.  *          to its own functions.
  66.  *          (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if
  67.  *           MBEDTLS_THREADING_C is defined)
  68.  *
  69.  * \note    This code is not optimized and provides a straight-forward
  70.  *          implementation of a stack-based memory allocator.
  71.  *
  72.  * \param buf   buffer to use as heap
  73.  * \param len   size of the buffer
  74.  */
  75. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len );
  76.  
  77. /**
  78.  * \brief   Free the mutex for thread-safety and clear remaining memory
  79.  */
  80. void mbedtls_memory_buffer_alloc_free( void );
  81.  
  82. /**
  83.  * \brief   Determine when the allocator should automatically verify the state
  84.  *          of the entire chain of headers / meta-data.
  85.  *          (Default: MBEDTLS_MEMORY_VERIFY_NONE)
  86.  *
  87.  * \param verify    One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC,
  88.  *                  MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS
  89.  */
  90. void mbedtls_memory_buffer_set_verify( int verify );
  91.  
  92. #if defined(MBEDTLS_MEMORY_DEBUG)
  93. /**
  94.  * \brief   Print out the status of the allocated memory (primarily for use
  95.  *          after a program should have de-allocated all memory)
  96.  *          Prints out a list of 'still allocated' blocks and their stack
  97.  *          trace if MBEDTLS_MEMORY_BACKTRACE is defined.
  98.  */
  99. void mbedtls_memory_buffer_alloc_status( void );
  100.  
  101. /**
  102.  * \brief   Get the peak heap usage so far
  103.  *
  104.  * \param max_used      Peak number of bytes in use or committed. This
  105.  *                      includes bytes in allocated blocks too small to split
  106.  *                      into smaller blocks but larger than the requested size.
  107.  * \param max_blocks    Peak number of blocks in use, including free and used
  108.  */
  109. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
  110.  
  111. /**
  112.  * \brief   Reset peak statistics
  113.  */
  114. void mbedtls_memory_buffer_alloc_max_reset( void );
  115.  
  116. /**
  117.  * \brief   Get the current heap usage
  118.  *
  119.  * \param cur_used      Current number of bytes in use or committed. This
  120.  *                      includes bytes in allocated blocks too small to split
  121.  *                      into smaller blocks but larger than the requested size.
  122.  * \param cur_blocks    Current number of blocks in use, including free and used
  123.  */
  124. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
  125. #endif /* MBEDTLS_MEMORY_DEBUG */
  126.  
  127. /**
  128.  * \brief   Verifies that all headers in the memory buffer are correct
  129.  *          and contain sane values. Helps debug buffer-overflow errors.
  130.  *
  131.  *          Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined.
  132.  *          Prints out full header information if MBEDTLS_MEMORY_DEBUG
  133.  *          is defined. (Includes stack trace information for each block if
  134.  *          MBEDTLS_MEMORY_BACKTRACE is defined as well).
  135.  *
  136.  * \return             0 if verified, 1 otherwise
  137.  */
  138. int mbedtls_memory_buffer_alloc_verify( void );
  139.  
  140. #if defined(MBEDTLS_SELF_TEST)
  141. /**
  142.  * \brief          Checkup routine
  143.  *
  144.  * \return         0 if successful, or 1 if a test failed
  145.  */
  146. int mbedtls_memory_buffer_alloc_self_test( int verbose );
  147. #endif
  148.  
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152.  
  153. #endif /* memory_buffer_alloc.h */
  154.