source: rtems/cpukit/libtest/t-test-rtems-heap.c @ cfcc2cbf

5
Last change on this file since cfcc2cbf was cfcc2cbf, checked in by Sebastian Huber <sebastian.huber@…>, on 01/31/19 at 13:45:31

Add RTEMS Test Framework

Update #3199.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2018 embedded brains GmbH
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <t.h>
29
30#include <rtems/score/heapimpl.h>
31#include <rtems/score/wkspace.h>
32#include <rtems/malloc.h>
33
34typedef struct {
35        Heap_Information_block heap_info;
36        Heap_Information_block workspace_info;
37} T_resource_heap_context;
38
39static T_resource_heap_context T_resource_heap_instance;
40
41static void
42T_get_heap_info(Heap_Control *heap, Heap_Information_block *info)
43{
44        _Heap_Get_information(heap, info);
45        memset(&info->Stats, 0, sizeof(info->Stats));
46}
47
48static void
49T_heap_run_initialize(void)
50{
51        T_resource_heap_context *ctx;
52
53        ctx = &T_resource_heap_instance;
54        T_get_heap_info(&_Workspace_Area, &ctx->workspace_info);
55
56        if (!rtems_configuration_get_unified_work_area()) {
57                T_get_heap_info(RTEMS_Malloc_Heap, &ctx->heap_info);
58        }
59}
60
61static void
62T_heap_case_end(void)
63{
64        T_resource_heap_context *ctx;
65        Heap_Information_block info;
66        bool ok;
67
68        ctx = &T_resource_heap_instance;
69
70        T_get_heap_info(&_Workspace_Area, &info);
71        ok = memcmp(&info, &ctx->workspace_info, sizeof(info)) == 0;
72
73        if (!ok) {
74                const char *where;
75
76                if (rtems_configuration_get_unified_work_area()) {
77                        where = "workspace or heap";
78                } else {
79                        where = "workspace";
80                }
81
82                T_check_true(ok, NULL, "memory leak in %s", where);
83                memcpy(&ctx->workspace_info, &info, sizeof(info));
84        }
85
86        if (!rtems_configuration_get_unified_work_area()) {
87                T_get_heap_info(RTEMS_Malloc_Heap, &info);
88                ok = memcmp(&info, &ctx->heap_info, sizeof(info)) == 0;
89
90                if (!ok) {
91                        T_check_true(ok, NULL, "memory leak in heap");
92                        memcpy(&ctx->heap_info, &info, sizeof(info));
93                }
94        }
95}
96
97void
98T_check_heap(T_event event, const char *name)
99{
100        (void)name;
101
102        switch (event) {
103        case T_EVENT_RUN_INITIALIZE:
104                T_heap_run_initialize();
105                break;
106        case T_EVENT_CASE_END:
107                T_heap_case_end();
108                break;
109        default:
110                break;
111        };
112}
Note: See TracBrowser for help on using the repository browser.