/*
 * @file hv_vos_Memory.h
 * @brief Header file of I2C driver.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) Use ()
 *
 * @endverbatim
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-03-01
 */



#ifndef _HV_VOS_MEMORY_H
#define _HV_VOS_MEMORY_H

#include "hv_vos_Types.h"

VOS_EXTERN_C_BEGIN

/** @defgroup VOS_MEMORY VOS_MEMORY
 *  VOS memory related functions
 *  @{
 */


/** @brief malloc memory
 *  @param size required memory size
 *  @return allocated memory pointer, return NULL if failed
*/
void *Hv_Vos_Malloc(UINT32 size);

/** @brief malloc memory and clear it
 *  @param nmemb memory count
 *  @param size required memory size
 *  @return allocated memory pointer, return NULL if failed
*/
void *Hv_Vos_Calloc(UINT32 nmemb, UINT32 size);

/** @brief change the size of allacated memory
 *  @param p allocated pointer, same as \link Hv_Vos_MALLOC \endlink if p is NULL
 *  @param size required memory size
 *  @return allocated memory pointer, return NULL if failed
*/
void *Hv_Vos_Realloc(void *p, UINT32 size);

/** @brief free allacated memory
 *  @param p allocated pointer
*/
void Hv_Vos_Free(void *p);

/** @brief copy memory area
 *  @param dest destination memory
 *  @param src source memory
 *  @param n copying size
 *  @return return pointer to dest
 */
void *Hv_Vos_Memcpy(void *dest, const void *src, UINT32 n);
void *Hv_Vos_VolatileMemcpy(void *dest, const volatile void *src, UINT32 n);

/** @brief fill memory with a constant byte
 *  @param s memory area to clear
 *  @param c constant byte
 *  @param n number of bytes
 *  @return pointer to s
 */
void *Hv_Vos_Memset(void *s, int c, UINT32 n);

char *Hv_Vos_Strdup(const char *s);

UINT32 Hv_Vos_MemoryRemaining();


#define HV_MALLOC Hv_Vos_Malloc /* !< short form of Hv_Vos_MALLOC */
#define HV_CALLOC Hv_Vos_Calloc /* !< short form of Hv_Vos_Calloc */
#define HV_MEMCPY Hv_Vos_Memcpy /* !< short form of Hv_Vos_Memcpy */
#define HV_MEMSET Hv_Vos_Memset /* !< short form of Hv_Vos_Memset */
#define HV_FREE   Hv_Vos_Free   /* !< short form of Hv_Vos_Free */
#define HV_STRDUP Hv_Vos_Strdup

#define HV_FREE_EX(ptr, sz)   HV_FREE(ptr)   /* !< Hv_Vos_Free with size parameter */

/* malloc stuct and clear it **/
#define HV_MALLOC_OBJECT(type) ((type*)Hv_Vos_Calloc(1, sizeof(type)))

/* malloc multiple struct and clear them */
#define HV_MALLOC_OBJECT_ARRAY(type, count) ((type*)Hv_Vos_Calloc(count, sizeof(type)))

/* free struct pointer and set as NULL */
#define HV_FREE_OBJECT(obj) do { Hv_Vos_Free(obj); obj = NULL; } while (0)

/* free struct array pointer and set as NULL */
#define HV_FREE_OBJECT_ARRAY(obj) do { Hv_Vos_Free(obj); obj = NULL; } while (0)

/* malloc buffer */
#define HV_MALLOC_BUFFER(size) Hv_Vos_Calloc(1, size)

/* free buffer */
#define HV_FREE_BUFFER(p) do { Hv_Vos_Free(p); p = NULL; } while (0)

/* macro for memory compare */
#define HV_MEMCMP(a, b, n) memcmp(a, b, n)

/* memory clean as 0 */
#define HV_BZERO(a, n) HV_MEMSET(a, '\0', n)


/** @} */

VOS_EXTERN_C_END

#endif



