• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *	http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <malloc.h>
16 #include <cstdio>
17 #include <cstdlib>
18 
19 #include "testmalloc.h"
20 
21 namespace testmalloc {
allocateInt(void)22 int allocateInt(void) {
23     printf("Trying to allocate %d\n", static_cast<int>(sizeof(int)));
24 
25     int *mem = static_cast<int *>(malloc(sizeof(int)));
26     if (mem == nullptr) {
27         printf("Could not allocate memory\n");
28         return 1;
29     } else {
30         printf("Memory allocated\n");
31         free(mem);
32         printf("Memory freed\n");
33         return 0;
34     }
35 }
36 
allocateChar(void)37 int allocateChar(void) {
38     printf("Trying to allocate %d\n", static_cast<int>(sizeof(char)));
39 
40     char *mem = static_cast<char *>(malloc(sizeof(char)));
41     if (mem == nullptr) {
42         printf("Could not allocate memory\n");
43         return 1;
44     } else {
45         printf("Memory allocated\n");
46         free(mem);
47         printf("Memory freed\n");
48         return 0;
49     }
50 }
51 
allocateDouble(void)52 int allocateDouble(void) {
53     printf("Trying to allocate %d\n", static_cast<int>(sizeof(double)));
54 
55     double *mem = static_cast<double *>(malloc(sizeof(double)));
56     if (mem == nullptr) {
57         printf("Could not allocate memory\n");
58         return 1;
59     } else {
60         printf("Memory allocated\n");
61         free(mem);
62         printf("Memory freed\n");
63         return 0;
64     }
65 }
66 
67 } // namespace testmalloc
68