1/* 2 * Copyright 2017, Data61 3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO) 4 * ABN 41 687 119 230. 5 * 6 * This software may be distributed and modified according to the terms of 7 * the BSD 2-Clause license. Note that NO WARRANTY is provided. 8 * See "LICENSE_BSD2.txt" for details. 9 * 10 * @TAG(DATA61_BSD) 11 */ 12 13#include <platsupport/io.h> 14#include <stdlib.h> 15 16static int ps_stdlib_malloc(UNUSED void *cookie, size_t size, void **ptr) 17{ 18 assert(ptr != NULL); 19 *ptr = malloc(size); 20 if (*ptr == NULL) { 21 return ENOMEM; 22 } 23 return 0; 24} 25 26static int ps_stdlib_calloc(UNUSED void *cookie, size_t nmemb, size_t size, void **ptr) 27{ 28 assert(ptr != NULL); 29 *ptr = calloc(nmemb, size); 30 if (*ptr == NULL) { 31 return ENOMEM; 32 } 33 return 0; 34} 35 36static int ps_stdlib_free(UNUSED void *cookie, UNUSED size_t size, void *ptr) 37{ 38 free(ptr); 39 return 0; 40} 41 42int ps_new_stdlib_malloc_ops(ps_malloc_ops_t *ops) 43{ 44 ops->malloc = ps_stdlib_malloc; 45 ops->calloc = ps_stdlib_calloc; 46 ops->free = ps_stdlib_free; 47 ops->cookie = NULL; 48 return 0; 49} 50