blob: 153b335252012b8f8402ad05f87df0964847384a [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Tamas Ban581034a2017-12-19 19:54:37 +000020/*
21 Original code taken from mcuboot project at:
22 https://github.com/runtimeco/mcuboot
23 Modifications are Copyright (c) 2018 Arm Limited.
24 */
25
26#include <stdlib.h>
Tamas Banf70ef8c2017-12-19 15:35:09 +000027#include <string.h>
28
29#include "os/os_heap.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000030#include <mbedtls/platform.h>
31
32/* D(void *os_malloc(size_t size)) */
33void *os_calloc(size_t nelem, size_t size)
34{
35 /* Note that this doesn't check for overflow. Assume the
36 * calls only come from within the app. */
37 size_t total = nelem * size;
Tamas Ban581034a2017-12-19 19:54:37 +000038 void *buf = malloc(total);
39
Tamas Banf70ef8c2017-12-19 15:35:09 +000040 if (buf) {
41 memset(buf, 0, total);
42 }
43 return buf;
44}
45
46void os_free(void *ptr)
47{
Tamas Ban581034a2017-12-19 19:54:37 +000048 free(ptr);
Tamas Banf70ef8c2017-12-19 15:35:09 +000049}
50
51/*
52 * Initialize mbedtls to be able to use the local heap.
53 */
54void os_heap_init(void)
55{
56 mbedtls_platform_set_calloc_free(os_calloc, os_free);
57}