Changeset 6efc831 in rtems


Ignore:
Timestamp:
Nov 9, 2018, 11:11:11 AM (6 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
9a029ce
Parents:
c1f3c2b8
git-author:
Sebastian Huber <sebastian.huber@…> (11/09/18 11:11:11)
git-committer:
Sebastian Huber <sebastian.huber@…> (11/12/18 12:26:22)
Message:

Add rtems_malloc() and rtems_calloc()

Close #3583.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpukit/include/rtems/malloc.h

    rc1f3c2b8 r6efc831  
    143143
    144144/**
     145 * @brief Allocates a memory area of the specified size from the heap.
     146 *
     147 * This function is almost identical to malloc(). The only exception is that
     148 * errno is not set in case of a memory allocation failure.
     149 *
     150 * @param[in] size The memory area size in bytes.
     151 *
     152 * @retval NULL The memory allocation failed or @a size is zero.
     153 * @retval otherwise The begin address of the allocated memory area.
     154 */
     155void *rtems_malloc(size_t size)
     156  RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_WARN_UNUSED_RESULT;
     157
     158/**
     159 * @brief Allocates a memory area for the specified count of elements from the
     160 * heap.
     161 *
     162 * The allocated memory area is fully filled with zero bits.
     163 *
     164 * This function is almost identical to calloc(). The only exception is that
     165 * errno is not set in case of a memory allocation failure.
     166 *
     167 * @param[in] nelem The count of elements.
     168 * @param[in] elsize The size of each elements.
     169 *
     170 * @retval NULL The memory allocation failed or @a nelem is zero or @a elsize
     171 *   is zero.
     172 * @retval otherwise The begin address of the allocated memory area.
     173 */
     174void *rtems_calloc(size_t nelem, size_t elsize)
     175  RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE_2(1, 2) RTEMS_WARN_UNUSED_RESULT;
     176
     177/**
    145178 * @brief Extends the memory available for the heap using the memory area
    146179 * starting at @a area_begin of size @a area_size bytes.
  • cpukit/libcsupport/src/malloc_deferred.c

    rc1f3c2b8 r6efc831  
    2424#ifdef RTEMS_NEWLIB
    2525#include <stdlib.h>
     26#include <string.h>
    2627
    2728#include "malloc_p.h"
     
    137138  rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context );
    138139}
     140
     141void *rtems_malloc( size_t size )
     142{
     143  if ( size == 0 ) {
     144    return NULL;
     145  }
     146
     147  return rtems_heap_allocate_aligned_with_boundary( size, 0, 0 );
     148}
     149
     150void *rtems_calloc( size_t nelem, size_t elsize )
     151{
     152  size_t  length;
     153  void   *p;
     154
     155  length = nelem * elsize;
     156  p = rtems_malloc( length );
     157  RTEMS_OBFUSCATE_VARIABLE( p );
     158  if ( RTEMS_PREDICT_FALSE( p == NULL ) ) {
     159    return p;
     160  }
     161
     162  return memset( p, 0, length );
     163}
    139164#endif
  • testsuites/libtests/malloctest/init.c

    rc1f3c2b8 r6efc831  
    11491149}
    11501150
     1151static void test_rtems_malloc(void)
     1152{
     1153  void *p;
     1154
     1155  p = rtems_malloc(0);
     1156  rtems_test_assert(p == NULL);
     1157
     1158  errno = 0;
     1159  p = rtems_malloc(SIZE_MAX / 2);
     1160  rtems_test_assert(p == NULL);
     1161  rtems_test_assert(errno == 0);
     1162
     1163  p = rtems_malloc(1);
     1164  rtems_test_assert(p != NULL);
     1165
     1166  free(p);
     1167}
     1168
     1169static void test_rtems_calloc(void)
     1170{
     1171  void *p;
     1172  int *i;
     1173
     1174  p = rtems_calloc(0, 0);
     1175  rtems_test_assert(p == NULL);
     1176
     1177  p = rtems_calloc(0, 1);
     1178  rtems_test_assert(p == NULL);
     1179
     1180  p = rtems_calloc(1, 0);
     1181  rtems_test_assert(p == NULL);
     1182
     1183  errno = 0;
     1184  p = rtems_calloc(1, SIZE_MAX / 2);
     1185  rtems_test_assert(p == NULL);
     1186  rtems_test_assert(errno == 0);
     1187
     1188  errno = 0;
     1189  p = rtems_calloc(SIZE_MAX / 2, 1);
     1190  rtems_test_assert(p == NULL);
     1191  rtems_test_assert(errno == 0);
     1192
     1193  i = rtems_calloc(1, sizeof(*i));
     1194  rtems_test_assert(i != NULL);
     1195  rtems_test_assert(*i == 0);
     1196
     1197  free(i);
     1198}
     1199
    11511200static void test_heap_size_with_overhead(void)
    11521201{
     
    12971346  test_protected_heap_info();
    12981347  test_rtems_heap_allocate_aligned_with_boundary();
     1348  test_rtems_malloc();
     1349  test_rtems_calloc();
    12991350  test_greedy_allocate();
    13001351
Note: See TracChangeset for help on using the changeset viewer.