/**
 * @file tkl_memory.c
 * @brief this file was auto-generated by tuyaos v&v tools, developer can add implements between BEGIN and END
 *
 * @warning: changes between user 'BEGIN' and 'END' will be keeped when run tuyaos v&v tools
 *           changes in other place will be overwrited and lost
 *
 * @copyright Copyright 2020-2021 Tuya Inc. All Rights Reserved.
 *
 */

// --- BEGIN: user defines and implements ---
#include "tkl_memory.h"
#include "tuya_error_code.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
// --- END: user defines and implements ---

/**
 * @brief Alloc memory of system
 *
 * @param[in] size: memory size
 *
 * @note This API is used to alloc memory of system.
 *
 * @return the memory address malloced
 */
void *tkl_system_malloc(size_t size)
{
    // --- BEGIN: user implements ---
    return malloc(size);
    // --- END: user implements ---
}

/**
 * @brief Free memory of system
 *
 * @param[in] ptr: memory point
 *
 * @note This API is used to free memory of system.
 *
 * @return void
 */
void tkl_system_free(void *ptr)
{
    // --- BEGIN: user implements ---
    return free(ptr);
    // --- END: user implements ---
}

/**
 * @brief Allocate and clear the memory
 *
 * @param[in]       nitems      the numbers of memory block
 * @param[in]       size        the size of the memory block
 *
 * @return the memory address calloced
 */
void *tkl_system_calloc(size_t nitems, size_t size)
{
    // --- BEGIN: user implements ---
    return calloc(nitems, size);
    // --- END: user implements ---
}

/**
 * @brief Re-allocate the memory
 *
 * @param[in]       nitems      source memory address
 * @param[in]       size        the size after re-allocate
 *
 * @return void
 */
void *tkl_system_realloc(void *ptr, size_t size)
{
    // --- BEGIN: user implements ---
    return realloc(ptr, size);
    // --- END: user implements ---
}

/**
 * @brief Get system free heap size
 *
 * @param none
 *
 * @return heap size
 */
int tkl_system_get_free_heap_size(void)
{
    // --- BEGIN: user implements ---
    struct sysinfo info;
    sysinfo(&info);

    return info.freeram;
    // --- END: user implements ---
}
