Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:21

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSTestFrameworkImpl
0007  *
0008  * @brief This source file contains the implementation of
0009  *   T_check_heap().
0010  */
0011 
0012 /*
0013  * Copyright (C) 2018 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #include <rtems/test.h>
0038 
0039 #include <rtems/score/heapimpl.h>
0040 #include <rtems/score/wkspace.h>
0041 #include <rtems/malloc.h>
0042 
0043 typedef struct {
0044     Heap_Information_block heap_info;
0045     Heap_Information_block workspace_info;
0046 } T_resource_heap_context;
0047 
0048 static T_resource_heap_context T_resource_heap_instance;
0049 
0050 static void
0051 T_get_heap_info(Heap_Control *heap, Heap_Information_block *info)
0052 {
0053     _Heap_Get_information(heap, info);
0054     memset(&info->Stats, 0, sizeof(info->Stats));
0055 }
0056 
0057 static void
0058 T_heap_run_initialize(void)
0059 {
0060     T_resource_heap_context *ctx;
0061 
0062     ctx = &T_resource_heap_instance;
0063     T_get_heap_info(&_Workspace_Area, &ctx->workspace_info);
0064 
0065     if (!rtems_configuration_get_unified_work_area()) {
0066         T_get_heap_info(RTEMS_Malloc_Heap, &ctx->heap_info);
0067     }
0068 }
0069 
0070 static void
0071 T_heap_case_end(void)
0072 {
0073     T_resource_heap_context *ctx;
0074     Heap_Information_block info;
0075     bool ok;
0076 
0077     ctx = &T_resource_heap_instance;
0078 
0079     T_get_heap_info(&_Workspace_Area, &info);
0080     ok = memcmp(&info, &ctx->workspace_info, sizeof(info)) == 0;
0081 
0082     if (!ok) {
0083         const char *where;
0084 
0085         if (rtems_configuration_get_unified_work_area()) {
0086             where = "workspace or heap";
0087         } else {
0088             where = "workspace";
0089         }
0090 
0091         T_check(&T_special, ok, "memory leak in %s", where);
0092         memcpy(&ctx->workspace_info, &info, sizeof(info));
0093     }
0094 
0095     if (!rtems_configuration_get_unified_work_area()) {
0096         T_get_heap_info(RTEMS_Malloc_Heap, &info);
0097         ok = memcmp(&info, &ctx->heap_info, sizeof(info)) == 0;
0098 
0099         if (!ok) {
0100             T_check(&T_special, ok, "memory leak in heap");
0101             memcpy(&ctx->heap_info, &info, sizeof(info));
0102         }
0103     }
0104 }
0105 
0106 void
0107 T_check_heap(T_event event, const char *name)
0108 {
0109     (void)name;
0110 
0111     switch (event) {
0112     case T_EVENT_RUN_INITIALIZE_EARLY:
0113         T_heap_run_initialize();
0114         break;
0115     case T_EVENT_CASE_END:
0116         T_heap_case_end();
0117         break;
0118     default:
0119         break;
0120     };
0121 }